use of org.nutz.dao.sql.Criteria in project nutz by nutzam.
the class NutDao method doLinkQuery.
private LinkVisitor doLinkQuery(final EntityOperator opt, final Condition _cnd, final Map<String, Condition> cnds) {
return new LinkVisitor() {
public void visit(final Object obj, final LinkField lnk) {
Pojo pojo = opt.maker().makeQuery(lnk.getLinkedEntity());
pojo.setOperatingObject(obj);
PItem[] _cndItems = Pojos.Items.cnd(lnk.createCondition(obj));
pojo.append(_cndItems);
Condition cnd = _cnd;
if (_cnd == null && cnds != null)
cnd = cnds.get(lnk.getLinkedField().getName());
if (cnd != null) {
if (cnd instanceof Criteria) {
Criteria cri = (Criteria) cnd;
SqlExpressionGroup seg = cri.where();
if (_cndItems.length > 0 && seg != null && !seg.isEmpty()) {
seg.setTop(false);
pojo.append(Pojos.Items.wrap(" AND "));
}
pojo.append(cri);
if (cri.getPager() != null) {
pojo.setPager(cri.getPager());
expert.formatQuery(pojo);
}
} else // 普通条件
{
pojo.append(new ConditionPItem(cnd));
}
}
pojo.setAfter(lnk.getCallback());
pojo.setEntity(lnk.getLinkedEntity());
_exec(pojo);
lnk.setValue(obj, pojo.getResult());
}
};
}
use of org.nutz.dao.sql.Criteria in project nutz by nutzam.
the class NutDao method _count.
private int _count(Entity<?> en, String tableName, Condition cnd) {
// 如果有条件的话
if (null != cnd) {
Pojo pojo = pojoMaker.makeFunc(tableName, "COUNT", "*");
pojo.setEntity(en);
// 高级条件接口,直接得到 WHERE 子句
if (cnd instanceof Criteria) {
if (cnd instanceof SimpleCriteria) {
String beforeWhere = ((SimpleCriteria) cnd).getBeforeWhere();
if (!Strings.isBlank(beforeWhere))
pojo.append(Pojos.Items.wrap(beforeWhere));
}
pojo.append(((Criteria) cnd).where());
// MySQL/PgSQL/SqlServer 与 Oracle/H2的结果会不一样,奇葩啊
GroupBy gb = ((Criteria) cnd).getGroupBy();
pojo.append(gb);
} else // 否则暴力获取 WHERE 子句
{
String str = Pojos.formatCondition(en, cnd);
if (!Strings.isBlank(str)) {
String[] ss = str.toUpperCase().split("ORDER BY");
pojo.append(Pojos.Items.wrap(str.substring(0, ss[0].length())));
}
}
// 设置回调,并执行 SQL
pojo.setAfter(_pojo_fetchInt);
_exec(pojo);
return pojo.getInt();
}
// 没有条件,直接生成表达式
return func(tableName, "COUNT", "*");
}
use of org.nutz.dao.sql.Criteria in project nutz by nutzam.
the class CndTest method test_in_by_criteria_string_array.
/**
* Criteria 接口测试String[]数组
*/
@Test
public void test_in_by_criteria_string_array() {
String[] ids = { "bj", "sh", "gz", "sz" };
Criteria cri = Cnd.cri();
cri.where().andInStrArray("nm", ids);
assertEquals(" WHERE nm IN ('bj','sh','gz','sz')", cri.toString());
}
use of org.nutz.dao.sql.Criteria in project nutz by nutzam.
the class LamdaCndTest method test_not_in_by_criteria_string_list.
/**
* Criteria 接口测试List<String>
*/
@Test
public void test_not_in_by_criteria_string_list() {
List<String> ids = new ArrayList<String>();
ids.add("bj");
ids.add("sh");
ids.add("gz");
ids.add("sz");
Criteria cri = Cnd.cri();
cri.where().andNotInStrList(Worker::getId, ids);
assertEquals(" WHERE id NOT IN ('bj','sh','gz','sz')", cri.toString());
}
use of org.nutz.dao.sql.Criteria in project nutz by nutzam.
the class CndTest method test_in_by_criteria_int_array.
/**
* Criteria 接口测试int[]数组
*/
@Test
public void test_in_by_criteria_int_array() {
int[] ids = { 1, 2, 3 };
Criteria cri = Cnd.cri();
cri.where().andInIntArray2("nm", ids);
assertEquals(" WHERE nm IN (1,2,3)", cri.toString());
}
Aggregations