Search in sources :

Example 1 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) {
    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);
            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) 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) LinkField(org.nutz.dao.entity.LinkField) ConditionPItem(org.nutz.dao.impl.sql.pojo.ConditionPItem)

Example 2 with Criteria

use of org.nutz.dao.sql.Criteria in project nutz by nutzam.

the class EntityOperator method addUpdateByPkAndCnd.

public Pojo addUpdateByPkAndCnd(final Entity<?> en, final Object obj, final Condition cnd) {
    if (null == en)
        return null;
    // 触发Pojo拦截器
    _fireEvent("prevUpdate", obj, en);
    Pojo pojo = dao.pojoMaker.makeUpdate(en, null);
    boolean pureCnd = en.getPkType() == PkType.UNKNOWN;
    if (!pureCnd) {
        pojo.append(Pojos.Items.cndAuto(en, Lang.first(obj)));
        pojo.append(new Static(" AND "));
    }
    if (cnd instanceof Criteria) {
        // 只取它的where条件
        pojo.append(((Criteria) cnd).where().setTop(pureCnd));
    } else {
        pojo.append(new ConditionPItem(cnd).setTop(pureCnd));
    }
    pojo.setOperatingObject(obj);
    pojoList.add(pojo);
    return pojo;
}
Also used : Pojo(org.nutz.dao.sql.Pojo) Criteria(org.nutz.dao.sql.Criteria) Static(org.nutz.dao.util.cri.Static) ConditionPItem(org.nutz.dao.impl.sql.pojo.ConditionPItem)

Example 3 with Criteria

use of org.nutz.dao.sql.Criteria in project nutz by nutzam.

the class SimpleDaoTest method test_escape_char.

/**
 * for issue #515 写给 mysql 一个特殊的例子
 */
@Test
public void test_escape_char() {
    if (dao.meta().isMySql()) {
        dao.insert(Pet.create("A").setNickName("AAA"));
        dao.insert(Pet.create("B").setNickName("B%B"));
        Criteria cri = Cnd.cri();
        cri.where().andLike("alias", "\\%");
        List<Pet> pets = dao.query(Pet.class, cri);
        assertEquals(1, pets.size());
        assertEquals("B", pets.get(0).getName());
    }
}
Also used : Criteria(org.nutz.dao.sql.Criteria) SimpleCriteria(org.nutz.dao.util.cri.SimpleCriteria) AbcPet(org.nutz.dao.test.meta.nutzcn.AbcPet) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 4 with Criteria

use of org.nutz.dao.sql.Criteria in project nutz by nutzam.

the class QueryTest method query_by_cri_equals_null.

/**
 * Github Issue #101
 */
@Test
public void query_by_cri_equals_null() {
    Criteria cri = Cnd.cri();
    cri.where().andEquals("name", null);
    List<Pet> pets = dao.query(Pet.class, cri, null);
    assertEquals(0, pets.size());
    cri = Cnd.cri();
    cri.where().andNotEquals("name", null);
    pets = dao.query(Pet.class, cri, null);
    assertEquals(8, pets.size());
}
Also used : Criteria(org.nutz.dao.sql.Criteria) SimpleCriteria(org.nutz.dao.util.cri.SimpleCriteria) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 5 with Criteria

use of org.nutz.dao.sql.Criteria in project nutz by nutzam.

the class CndTest method test_in_by_criteria_long_list.

/**
 *  Criteria 接口测试List&lt;Long&gt;
 */
@Test
public void test_in_by_criteria_long_list() {
    List<Long> ids = new ArrayList<Long>();
    ids.add(1L);
    ids.add(2L);
    ids.add(3L);
    Criteria cri = Cnd.cri();
    cri.where().andInList("nm", ids);
    assertEquals(" WHERE nm IN (1,2,3)", cri.toString());
}
Also used : ArrayList(java.util.ArrayList) 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