Search in sources :

Example 11 with Criteria

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());
        }
    };
}
Also used : DoUpdateRelationLinkVisitor(org.nutz.dao.impl.link.DoUpdateRelationLinkVisitor) DoInsertRelationLinkVisitor(org.nutz.dao.impl.link.DoInsertRelationLinkVisitor) DoClearLinkVisitor(org.nutz.dao.impl.link.DoClearLinkVisitor) DoDeleteLinkVisitor(org.nutz.dao.impl.link.DoDeleteLinkVisitor) DoClearRelationByHostFieldLinkVisitor(org.nutz.dao.impl.link.DoClearRelationByHostFieldLinkVisitor) DoUpdateLinkVisitor(org.nutz.dao.impl.link.DoUpdateLinkVisitor) DoInsertLinkVisitor(org.nutz.dao.impl.link.DoInsertLinkVisitor) LinkVisitor(org.nutz.dao.entity.LinkVisitor) DoClearRelationByLinkedFieldLinkVisitor(org.nutz.dao.impl.link.DoClearRelationByLinkedFieldLinkVisitor) Condition(org.nutz.dao.Condition) Pojo(org.nutz.dao.sql.Pojo) SqlExpressionGroup(org.nutz.dao.util.cri.SqlExpressionGroup) ConditionPItem(org.nutz.dao.impl.sql.pojo.ConditionPItem) PItem(org.nutz.dao.sql.PItem) Criteria(org.nutz.dao.sql.Criteria) SimpleCriteria(org.nutz.dao.util.cri.SimpleCriteria) LinkField(org.nutz.dao.entity.LinkField) ConditionPItem(org.nutz.dao.impl.sql.pojo.ConditionPItem)

Example 12 with Criteria

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", "*");
}
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)

Example 13 with Criteria

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());
}
Also used : Criteria(org.nutz.dao.sql.Criteria) Test(org.junit.Test)

Example 14 with Criteria

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&lt;String&gt;
 */
@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());
}
Also used : ArrayList(java.util.ArrayList) Criteria(org.nutz.dao.sql.Criteria) Test(org.junit.Test)

Example 15 with Criteria

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());
}
Also used : Criteria(org.nutz.dao.sql.Criteria) Test(org.junit.Test)

Aggregations

Criteria (org.nutz.dao.sql.Criteria)19 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 Pojo (org.nutz.dao.sql.Pojo)4 SimpleCriteria (org.nutz.dao.util.cri.SimpleCriteria)4 ConditionPItem (org.nutz.dao.impl.sql.pojo.ConditionPItem)3 LinkField (org.nutz.dao.entity.LinkField)2 LinkVisitor (org.nutz.dao.entity.LinkVisitor)2 DoClearLinkVisitor (org.nutz.dao.impl.link.DoClearLinkVisitor)2 DoClearRelationByHostFieldLinkVisitor (org.nutz.dao.impl.link.DoClearRelationByHostFieldLinkVisitor)2 DoClearRelationByLinkedFieldLinkVisitor (org.nutz.dao.impl.link.DoClearRelationByLinkedFieldLinkVisitor)2 DoDeleteLinkVisitor (org.nutz.dao.impl.link.DoDeleteLinkVisitor)2 DoInsertLinkVisitor (org.nutz.dao.impl.link.DoInsertLinkVisitor)2 DoInsertRelationLinkVisitor (org.nutz.dao.impl.link.DoInsertRelationLinkVisitor)2 DoUpdateLinkVisitor (org.nutz.dao.impl.link.DoUpdateLinkVisitor)2 DoUpdateRelationLinkVisitor (org.nutz.dao.impl.link.DoUpdateRelationLinkVisitor)2 PItem (org.nutz.dao.sql.PItem)2 Pet (org.nutz.dao.test.meta.Pet)2 SqlExpressionGroup (org.nutz.dao.util.cri.SqlExpressionGroup)2 Condition (org.nutz.dao.Condition)1