Search in sources :

Example 1 with Cnd

use of org.nutz.dao.Cnd in project nutz by nutzam.

the class SimpleDaoTest method test_cnd_clone2.

@Test
public void test_cnd_clone2() {
    // 序列化的方式, 不要考究SQL条件的合理性
    dao.insert(Pet.create(30));
    Cnd cnd = Cnd.where("age", ">", 15).and(Cnd.exps("age", ">", 0).and("age", "<", 16));
    cnd.asc("age");
    int t = dao.count(Pet.class, cnd);
    assertNotNull(Lang.toBytes(cnd));
    Stopwatch sw = Stopwatch.begin();
    cnd.clone();
    sw.stop();
    System.out.println(sw);
    assertEquals(t, dao.count(Pet.class, cnd.clone()));
    // 仅拷贝where条件
    Cnd cndCloned = cnd.cloneWhere();
    assertEquals(t, dao.count(Pet.class, cndCloned));
    // 修改原来的cnd条件, 使其互相矛盾,结果肯定是0
    cnd.and("age", "<", 0);
    assertEquals(0, dao.count(Pet.class, cnd));
    // 克隆的cndCloned应该不受影响
    assertEquals(t, dao.count(Pet.class, cndCloned));
}
Also used : Cnd(org.nutz.dao.Cnd) Stopwatch(org.nutz.lang.Stopwatch) 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 2 with Cnd

use of org.nutz.dao.Cnd in project nutz by nutzam.

the class SimpleDaoTest method test_cnd_clone.

@Test
public void test_cnd_clone() throws IOException {
    try {
        Cnd cnd = Cnd.NEW().and("abc", "=", 123);
        Cnd.byCri(cnd.getCri());
        Cnd.byCri(cnd.getCri());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Cnd(org.nutz.dao.Cnd) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 3 with Cnd

use of org.nutz.dao.Cnd in project nutz by nutzam.

the class QueryTest method queryByJoin_with_cnd.

@Test
public void queryByJoin_with_cnd() {
    dao.create(Pet.class, true);
    dao.create(Master.class, true);
    Master master = new Master();
    master.setName("zozoh");
    Pet petA = new Pet();
    petA.setName("wendal");
    petA.setAge(31);
    Pet petB = new Pet();
    petB.setName("pangwu");
    petB.setAge(30);
    master.setPets(Arrays.asList(petA, petB));
    dao.insertWith(master, null);
    Cnd cnd = Cnd.NEW();
    cnd.asc("name");
    List<Master> list = dao.queryByJoin(Master.class, "pets", cnd);
    assertEquals(1, list.size());
    assertEquals(2, list.get(0).getPets().size());
}
Also used : Master(org.nutz.dao.test.meta.Master) Cnd(org.nutz.dao.Cnd) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 4 with Cnd

use of org.nutz.dao.Cnd in project nutz by nutzam.

the class CustomizedSqlsTest method test_issue_1281.

@Test
public void test_issue_1281() {
    SqlExpression sqle = Exps.inSql2("id", "select user_id from role where id in (%s)", Arrays.asList(1, 2, 3));
    Cnd cnd = Cnd.where(sqle);
    System.out.println(cnd.toString());
    StringBuilder sb = new StringBuilder();
    // 取出带问号的SQL
    sqle.joinSql(null, sb);
    System.out.println(sb);
    assertEquals("id IN (select user_id from role where id in (?,?,?))", sb.toString());
}
Also used : Cnd(org.nutz.dao.Cnd) SqlExpression(org.nutz.dao.util.cri.SqlExpression) Test(org.junit.Test)

Example 5 with Cnd

use of org.nutz.dao.Cnd in project nutz by nutzam.

the class UpdateTest method test_update_self_plus.

@Test
public void test_update_self_plus() {
    dao.create(Pet.class, true);
    Pet pet = Pet.create("Xy");
    pet.setAge(98);
    dao.insert(pet);
    pet = dao.fetch(Pet.class, (Cnd) null);
    dao.update(Pet.class, Chain.makeSpecial("age", "+1"), null);
    assertEquals(pet.getAge() + 1, dao.fetch(Pet.class, pet.getId()).getAge());
}
Also used : Cnd(org.nutz.dao.Cnd) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Aggregations

Cnd (org.nutz.dao.Cnd)9 Test (org.junit.Test)8 Pet (org.nutz.dao.test.meta.Pet)4 Issue1163Pet (org.nutz.dao.test.meta.issue1163.Issue1163Pet)2 AbcPet (org.nutz.dao.test.meta.nutzcn.AbcPet)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DaoException (org.nutz.dao.DaoException)1 Pager (org.nutz.dao.pager.Pager)1 Master (org.nutz.dao.test.meta.Master)1 IotObject (org.nutz.dao.test.meta.issueXXX.IotObject)1 SqlExpression (org.nutz.dao.util.cri.SqlExpression)1 Stopwatch (org.nutz.lang.Stopwatch)1