2013-10-27 3 views
2

와 나는 완전한 쿼리를 작성하는 몇 가지 조건을 확인해야합니다 :쿼리 동적 greenDao

QueryBuilder QB = getMyObjDao() queryBuilder를();

경우 (someCondition) 다른

qb.where(MyObjDao.Properties.Prop1.eq(someValue)); 


qb.whereOr (MyObjDao.Properties.Prop2.eq (someValue와) MyObjDao.Properties.Prop2.eq (someValue와));

경우 (someOtherCondition) 다른

qb.where(MyObjDao.Properties.Prop3.eq(someValue)); 

qb.whereOr(MyObjDao.Properties.Prop4.eq(someValue)); 

그래서 쿼리 빌더 조건을 연결 동적 쿼리 빌더를 만들 수 있습니다? 정도 아무것도 :

(A = '%'+ 조건 1 또는 = '%'+ 조건 1 + '%'또는 = 조건 1 + '%')와
| (B = '%'+ 조건 2 또는 b = '%'+ 조건 2 + '%'또는 b = 조건 2 + '%')와 ....

어떻게 greenDao에 해당합니다?

+0

이것은 완전히 새로운 질문이며 greendao와 관련이없는 것 같습니다. 또한 소스 코드가 명확하지 않습니다. 문제를 일으키는 명확한 소스 코드를 제공하고 소스 코드 란 무엇이며 로그 출력은 무엇이며 텍스트는 무엇인지 분명히하십시오. – AlexS

답변

5

QueryBuilder.and() 및 은 WhereCondition을 결합하는데 사용된다. WhereConditionQueryBuilder.where() (AND을 사용하는 조건을 결합 함) 또는 QueryBuilder.whereOr()을 사용해야합니다.


모든 검색어는 의미가 없습니다. 결과적으로 제가 여러분에게 줄 수있는 코드는 예상대로 작동하지 않을 수 있습니다. 여러분이 기대하는 바를 추측하고 있습니다. 예 : 취할 수 있습니다. qb.whereOr(MyObjDao.Properties.Prop2.eq(someValue),MyObjDao.Properties.Prop2.eq(someValue))

이것은 where (Prop2 = someValue OR Prop2 = someValue) 또는 where (Prop2 = 'someValue' OR Prop2 = 'someValue')으로 변환됩니다. 각각의 OR 및 두 번째 문은 사용되지 않습니다. 당신이 그것에 %와 문자열을 검색하지 않는 경우

또 다른 예는 (a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and (b = '%'+condition2 or b = '%'+condition2+'%' or b = condition2 + '%') and .... 이다, 이런 곳에 절을 가진 쿼리는 것도 거짓 결과를 반환하지 것입니다.


의 경우 : 동적 목록에없는 또 다른 접근 방식을뿐만 아니라 사용할 수 있습니다 물론

ArrayList<WhereCondition> and = new ArrayList<WhereCondition>(); 

if (condition1 != null) { 
    and.add(queryBuilder.or(
      YourDao.Properties.a.eq("%"+condition1), 
      YourDao.Properties.a.eq("%"+condition1+"%"), 
      YourDao.Properties.a.eq(condition1+"%"))); 
} 

if (condition2 != null) { 
    and.add(queryBuilder.or(
      YourDao.Properties.a.eq("%"+condition2), 
      YourDao.Properties.a.eq("%"+condition2+"%"), 
      YourDao.Properties.a.eq(condition2+"%"))); 
} 

... 

if (and.size() == 1) { 
    queryBuilder.where(and.get(0)); 
} else if (and.size() > 1) { 
    WhereCondition first = and.remove(0); 
    queryBuilder.where(first, and.toArray(new WhereCondition[and.size()])); 
} 

UPDATE :

(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and 
(b = '%'+condition2 or b = '%'+condition2+'%' or b = condition2 + '%') and .... 

이 같은 것을 사용할 수 있습니다 :

01 대한 23,516,

:

QueryBuilder qb = getMyObjDao().queryBuilder(); 

if (someCondition) 

qb.where(MyObjDao.Properties.Prop1.eq(someValue)); 

else 
qb.whereOr(MyObjDao.Properties.Prop2.eq(someValue),MyObjDao.Properties.Prop2.eq(someValue)); 

if (someOtherCondition) 

qb.where(MyObjDao.Properties.Prop3.eq(someValue)); 

else 

qb.whereOr(MyObjDao.Properties.Prop4.eq(someValue)); 

당신은이를 사용할 수 있습니다

QueryBuilder<MyObj> queryBuilder = getMyObjDao().queryBuilder(); 
WhereCondition where = null; 

if (someCondition1) { 
    WhereCondition cond = MyObjDao.Properties.Prop1.eq(someValue); 
    if (where == null) { 
     where = cond; 
    } else { 
     where = queryBuilder.and(where, cond); 
    } 
} else { 
    WhereCondition cond = queryBuilder.or(
      MyObjDao.Properties.Prop2.eq(someValue), 
      MyObjDao.Properties.Prop2.eq(someOtherValue)); 
    if (where == null) { 
     where = cond; 
    } else { 
     where = queryBuilder.and(where, cond); 
    } 
} 

if (someOtherCondition) { 
    WhereCondition cond = MyObjDao.Properties.Prop3.eq(someValue); 
    if (where == null) { 
     where = cond; 
    } else { 
     where = queryBuilder.and(where, cond); 
    } 
} else { 
    WhereCondition cond = MyObjDao.Properties.Prop4.eq(someValue); 
    if (where == null) { 
     where = cond; 
    } else { 
     where = queryBuilder.or(where, cond2); 
    } 
} 

List<YourObj> result = queryBuilder.where(where).list(); 

당신이 greendao 사용하여 런타임에 WhereConditions을 결합 할 수있는 방법 possiblities 많이있다시피. 그러나 당신이 정말로하고 싶은 것을 상세하게 예시하고 설명하지 않는 한 아무도 당신을 도울 수 없습니다. 그런데

: 당신은 하나의 WhereCondition을 사용하는 경우, 어떤 차이가되지 않습니다 where() 또는 whereOr()를 사용

  • .
  • 아마도 (a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') 대신 (a like '%'+condition1 or a like '%'+condition1+'%' or a like condition1 + '%')을 쿼리하고 싶을 것입니다.
  • 통지, 그 (a like '%'+condition1 or a like '%'+condition1+'%' or a like condition1 + '%')a like '%'+condition1 또는 a like condition1+'%'a like '%'+condition1+'%' 일치합니다 일치하는 모든 결과 이후 (a like '%'+condition1+'%') 동일합니다.
+0

다른 질문을하고 답을 받아 들여야합니다. 당신은 grrendao와 조건을 연결하는 방법을 요청했다 : 내가 말한 것처럼 QueryBuilder.or()와 QueryBuilder.and()를 사용한다. – AlexS

+0

감사합니다. @AlexS. 이 접근법은 실제로 작동하며 장황함에도 불구하고 질문에 답합니다. 요점은 {WhereCondition}을 구성하여 점진적으로 where 절을 작성해야한다는 것입니다. querybuilder.or, queybuilder.and 및 if 문 논리를 사용하여이 작업을 수행 할 수 있습니다. –