好的,我弄乱了一些代码,这实现了我想要的——但我非常欢迎评论来改进或提出更好的方法来实现预期的结果。
protected QueryBuilder<T, Long> getQuery(String text, boolean wide, boolean count) {
if (null == fields)
reflectKeyInfo();
QueryBuilder<T, Long> qb = null;
if (count)
qb = getDao().queryBuilder().setCountOf(true);
else
qb = getDao().queryBuilder();
if (null == qb)
return null;
Where<T, Long> where = qb.where();
boolean equals = text.contains("=") ;
boolean like = text.contains("~") ;
if (equals || like) {
String field = null ;
String value = null ;
try {
if (equals) {
field = Strings.left(text, text.indexOf("=")) ;
value = Strings.right(text, text.indexOf("=")) ;
if (Strings.isValid(field) && Strings.isValid(value))
where.eq(field, value) ;
}
else
if (like) {
field = Strings.left(text, text.indexOf("~")) ;
value = Strings.right(text, text.indexOf("~")) ;
if (Strings.isValid(field) && Strings.isValid(value))
where.like(field, "%" + value + "%") ;
}
}
catch (SQLException e) {
lastError = GlobalConfig.log(e, true) ;
Dialogs.alert("Query Failed",
"<br />Unable to extract field and value from " +
field + "=" + value + "<br />" + lastError) ;
}
}
else
for (String field : fields.keySet()) {
try {
if (fields.get(field).isAssignableFrom(String.class)) {
if (wide)
where.like(field, "%" + text + "%");
else
where.eq(field, text);
where.or();
}
}
catch (SQLException e) {
lastError = GlobalConfig.log(e, true);
}
} // for
return qb;
}