Search in sources :

Example 1 with GroupBy

use of org.nutz.dao.sql.GroupBy 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", "*");
}
Also used : Pojo(org.nutz.dao.sql.Pojo) GroupBy(org.nutz.dao.sql.GroupBy) SimpleCriteria(org.nutz.dao.util.cri.SimpleCriteria) Criteria(org.nutz.dao.sql.Criteria) SimpleCriteria(org.nutz.dao.util.cri.SimpleCriteria)

Aggregations

Criteria (org.nutz.dao.sql.Criteria)1 GroupBy (org.nutz.dao.sql.GroupBy)1 Pojo (org.nutz.dao.sql.Pojo)1 SimpleCriteria (org.nutz.dao.util.cri.SimpleCriteria)1