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());
}
};
}
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;
}
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());
}
}
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());
}
use of org.nutz.dao.sql.Criteria in project nutz by nutzam.
the class CndTest method test_in_by_criteria_long_list.
/**
* Criteria 接口测试List<Long>
*/
@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());
}
Aggregations