Search in sources :

Example 1 with SqlExpression

use of org.nutz.dao.util.cri.SqlExpression 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 2 with SqlExpression

use of org.nutz.dao.util.cri.SqlExpression in project nutz by nutzam.

the class CndTest method test_add_other_or_method_by_github_issuse_148.

@Test
public void test_add_other_or_method_by_github_issuse_148() {
    SqlExpression e1 = Cnd.exps("city", "=", "beijing").or("city", "=", "shanghai").or("city", "=", "guangzhou").or("city", "=", "shenzhen");
    SqlExpression e2 = Cnd.exps("age", ">", 18).and("age", "<", 30);
    String exp = "WHERE (ct='beijing' OR ct='shanghai' OR ct='guangzhou' OR ct='shenzhen') AND (age>18 AND age<30)";
    assertEquals(exp, Cnd.where(e1).and(e2).toSql(en).trim());
}
Also used : SqlExpression(org.nutz.dao.util.cri.SqlExpression) Test(org.junit.Test)

Example 3 with SqlExpression

use of org.nutz.dao.util.cri.SqlExpression in project nutz by nutzam.

the class CndTest method test_obj_read_write.

/**
 * 序列化测试
 */
@Test
public void test_obj_read_write() {
    SqlExpression e2 = Cnd.exps("f2", "=", 1);
    SqlExpression e3 = Cnd.exps("f3", "=", 1);
    Condition c = Cnd.where(e2).andNot(e3);
    byte[] buf = Lang.toBytes(c);
    c = Lang.fromBytes(buf, Cnd.class);
    assertEquals(" WHERE (f2=1) AND NOT (f3=1)", c.toString());
}
Also used : Condition(org.nutz.dao.Condition) Cnd(org.nutz.dao.Cnd) SqlExpression(org.nutz.dao.util.cri.SqlExpression) Test(org.junit.Test)

Example 4 with SqlExpression

use of org.nutz.dao.util.cri.SqlExpression in project nutz by nutzam.

the class CndTest method test_like_in.

@Test
public void test_like_in() {
    int[] ages = { 4, 7, 9 };
    SqlExpression e = Cnd.exps("age", ">", 35).and("id", "<", 47);
    SqlExpression e2 = Cnd.exps("name", "\tLIKE ", "%t%").and("age", "IN  \n\r", ages).or(e);
    Condition c = Cnd.where("id", "=", 37).and(e).or(e2).asc("age").desc("id");
    String exp = "WHERE wid=37 AND (age>35 AND wid<47) OR (wname LIKE '%t%' AND age IN (4,7,9) OR (age>35 AND wid<47)) ORDER BY age ASC, wid DESC";
    assertEquals(exp, c.toSql(en).trim());
}
Also used : Condition(org.nutz.dao.Condition) SqlExpression(org.nutz.dao.util.cri.SqlExpression) Test(org.junit.Test)

Example 5 with SqlExpression

use of org.nutz.dao.util.cri.SqlExpression in project nutz by nutzam.

the class LamdaCndTest method test_add_other_or_method_by_github_issuse_148.

@Test
public void test_add_other_or_method_by_github_issuse_148() {
    SqlExpression e1 = Cnd.exps(Worker::getCity, "=", "beijing").or(Worker::getCity, "=", "shanghai").or(Worker::getCity, "=", "guangzhou").or(Worker::getCity, "=", "shenzhen");
    SqlExpression e2 = Cnd.exps(Worker::getAge, ">", 18).and(Worker::getAge, "<", 30);
    String exp = "WHERE (ct='beijing' OR ct='shanghai' OR ct='guangzhou' OR ct='shenzhen') AND (age>18 AND age<30)";
    assertEquals(exp, Cnd.where(e1).and(e2).toSql(en).trim());
}
Also used : SqlExpression(org.nutz.dao.util.cri.SqlExpression) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 SqlExpression (org.nutz.dao.util.cri.SqlExpression)7 Condition (org.nutz.dao.Condition)4 Cnd (org.nutz.dao.Cnd)2