java - Ormlite Where issue with parenthesis, building meta-layer query library -
i've read other posts , docs how use "where" clause "create" parenthesis statements.
my requirement simple:
... companyid=1 , (director=true or officer=true) ; i'm writing routine takes array of object, parsed ormlite call. typical call looks this:
.., "companyid", 1, q.and, q.bracket, "director", true, q.or, "officer", true, q.bracket) the intent speed simple queries. there no desire replace ormlite's querying tools. simple meta-layer on top.
everything works fine simple queries, since parameters processed sequentially , clause built incrementally.
for parenthesis postponing processing until bracket closed.
this having problem. example docs using shows this:
-- ormlite docs... where<account, string> = querybuilder.where(); where.or( where.and( where.eq(account.name_field_name, "foo"), where.eq(account.password_field_name, "_secret")), where.and( where.eq(account.name_field_name, "bar"), where.eq(account.password_field_name, "qwerty"))); produces following approximate sql: select * account ((name = 'foo' , password = '_secret') or (name = 'bar' , password = 'qwerty')) the key thing understand docs example, same instance used in nested and(...) call. precisely i'm doing i'm still getting "did forget , or or" message.
the code implementing "delayed" processing looks this:
@suppresswarnings("unchecked") private void processwhere(where<?, ?> where, q q, list<qvalue> list) { if (null == list || list.size() < 2) { system.err.println("invalid passed: " + list); return; } if (q.equals(q.and)) where.and(getcondition(where, list.get(0)), getcondition(where, list.get(1))); else where.or(getcondition(where, list.get(0)), getcondition(where, list.get(1))); } the "qvalue" item "holder" column, condition , value data.
the "getcondition" method follows:
@suppresswarnings("rawtypes") protected getcondition(where<?, ?> where, qvalue qv) { if (null != && null != qv) return getcondition(where, qv.gettype(), qv.getcolumn(), qv.getvalue(), qv.getvalue2()); else return null; } @suppresswarnings("rawtypes") protected getcondition(where<?, ?> where, q cond, string key, object val, object val2) { if (null == || null == cond || null == key || null == val) return null; selectarg arg = new selectarg(); arg.setvalue(val); try { switch (cond) { case notnull: where.isnotnull(key); break; case isnull: where.isnull(key); break; case equals: where.eq(key, arg); break; case notequal: where.ne(key, arg); break; case greaterthan: where.gt(key, arg); break; case lessthan: where.lt(key, arg); break; case like: arg.setvalue("%" + val + "%"); where.like(key, arg); break; case likestart: arg.setvalue("" + val + "%"); where.like(key, arg); break; case likeend: arg.setvalue("%" + val); where.like(key, arg); break; case between: if (null != val && null != val2) where.between(key, val, val2); break; default: where.eq(key, arg); break; } } catch (sqlexception e) { globalconfig.log(e, true); return null; } return where; } as far can tell, i'm using object correctly, still getting a: "did forget , or or?" message.
i've tried creating "new" clauses querybuilder:
where w1 = qb.where() ; //process w1 conditions... return where.query() ; which fails or generates incorrect sql in various combinations i've tried. suggestions on how and(...) , or(...) methods working appreciated.
btw once library working properly, i'll put open source or donate gray, or both.
thanks in advance. anthony
i faced same issue , solved this:
where.eq("companyid", 1); where.and(where, where.or(where.eq("director", true), where.eq("officer", true))); or
where.and(where.eq("companyid", 1), where.or(where.eq("director", true), where.eq("officer", true))); which in sql gives us:
((`companyid` = 1 , `director` = 1 ) or (`companyid` = 1 , `officer` = 1 )) it's not identical example clause where companyid=1 , (director=true or officer=true) has same meaning.
Comments
Post a Comment