Search in sources :

Example 61 with Sql

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

the class CustomizedSqlsTest method test_query_without_entity.

@Test
public void test_query_without_entity() {
    pojos.initPet();
    dao.insert(Pet.create(4));
    Sql sql = Sqls.create("SELECT * FROM t_pet $condition");
    sql.setCondition(Cnd.where("name", "like", "pet_%").asc("name"));
    sql.setCallback(new SqlCallback() {

        public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
            List<Pet> pets = new ArrayList<Pet>(4);
            while (rs.next()) pets.add(dao.getObject(Pet.class, rs, null));
            return pets;
        }
    });
    dao.execute(sql);
    List<Pet> pets = sql.getList(Pet.class);
    assertEquals(4, pets.size());
    assertEquals("pet_00", pets.get(0).getName());
    assertEquals("pet_01", pets.get(1).getName());
    assertEquals("pet_02", pets.get(2).getName());
    assertEquals("pet_03", pets.get(3).getName());
}
Also used : SqlCallback(org.nutz.dao.sql.SqlCallback) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Pet(org.nutz.dao.test.meta.Pet) NutSql(org.nutz.dao.impl.sql.NutSql) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Example 62 with Sql

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

the class CustomizedSqlsTest method test_statice_null_field.

@Test
public void test_statice_null_field() {
    pojos.init();
    Sql sql = Sqls.create("INSERT INTO dao_country (name,detail) VALUES(@name,@detail)");
    sql.params().set("name", "ABC").set("detail", "haha");
    dao.execute(sql);
    assertEquals(1, dao.count("dao_country"));
    sql = Sqls.create("UPDATE dao_country SET detail=@detail WHERE name=@name");
    sql.params().set("name", "ABC").set("detail", null);
    dao.execute(sql);
    Country c = dao.fetch(Country.class, "ABC");
    assertNull(c.getDetail());
}
Also used : Country(org.nutz.dao.test.meta.Country) NutSql(org.nutz.dao.impl.sql.NutSql) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Example 63 with Sql

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

the class CustomizedSqlsTest method test_query_by_limit.

@Test
public void test_query_by_limit() {
    // For mysql only
    if (dao.meta().isMySql()) {
        pojos.initPet();
        dao.insert(Pet.create(8));
        assertEquals(8, dao.count(Pet.class));
        Sql sql = Sqls.queryEntity("SELECT * FROM t_pet $condition LIMIT @off,@size ");
        sql.setEntity(dao.getEntity(Pet.class));
        sql.params().set("off", 2).set("size", 2);
        sql.setCondition(Cnd.orderBy().asc("name"));
        dao.execute(sql);
        List<Pet> pets = sql.getList(Pet.class);
        assertEquals(2, pets.size());
        assertEquals("pet_02", pets.get(0).getName());
        assertEquals("pet_03", pets.get(1).getName());
    } else {
        Nutzs.notSupport(dao.meta());
    }
}
Also used : Pet(org.nutz.dao.test.meta.Pet) NutSql(org.nutz.dao.impl.sql.NutSql) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Example 64 with Sql

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

the class CustomizedSqlsTest method test_issue_1176.

@Test
public void test_issue_1176() {
    dao.create(Issue1176.class, true);
    Sql s = Sqls.create("insert into t_issue_1176 (colA,colB) values (@colA,@colB)");
    s.params().set("colA", "222222");
    s.params().set("colB", null);
    s.addBatch();
    s.params().set("colA", "1111111");
    s.params().set("colB", "测试1111");
    s.addBatch();
    dao.execute(s);
    Issue1176 re = dao.fetch(Issue1176.class, Cnd.where("colA", "=", "1111111"));
    assertNotNull(re);
    assertEquals("测试1111", re.getColB());
}
Also used : Issue1176(org.nutz.dao.test.meta.issue1176.Issue1176) NutSql(org.nutz.dao.impl.sql.NutSql) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Example 65 with Sql

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

the class CustomizedSqlsTest method test_in.

@Test
public void test_in() {
    Sqls.setSqlBorning(NutSql.class);
    dao.clear(Pet.class);
    dao.insert(Pet.create(4));
    List<Pet> pets = dao.query(Pet.class, null, dao.createPager(1, 2));
    Sql sql = Sqls.create("select * from t_pet where id in (@ids)");
    sql.setEntity(dao.getEntity(Pet.class));
    sql.setCallback(Sqls.callback.entities());
    sql.params().set("ids", new int[] { pets.get(0).getId(), pets.get(1).getId() });
    dao.execute(sql);
    List<Pet> npets = sql.getList(Pet.class);
    assertEquals(2, npets.size());
    assertEquals(npets.get(0).getId(), pets.get(0).getId());
    assertEquals(npets.get(1).getId(), pets.get(1).getId());
    Sqls.setSqlBorning(NutSql.class);
}
Also used : Pet(org.nutz.dao.test.meta.Pet) NutSql(org.nutz.dao.impl.sql.NutSql) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Aggregations

Sql (org.nutz.dao.sql.Sql)69 Test (org.junit.Test)33 MappingField (org.nutz.dao.entity.MappingField)14 ArrayList (java.util.ArrayList)12 NutSql (org.nutz.dao.impl.sql.NutSql)9 Pet (org.nutz.dao.test.meta.Pet)9 ResultSet (java.sql.ResultSet)8 SQLException (java.sql.SQLException)8 FileSqlManager (org.nutz.dao.impl.FileSqlManager)8 Connection (java.sql.Connection)7 SqlManager (org.nutz.dao.SqlManager)7 SqlCallback (org.nutz.dao.sql.SqlCallback)5 Record (org.nutz.dao.entity.Record)4 PojoSql (org.nutz.dao.test.meta.issue1074.PojoSql)4 Dao (org.nutz.dao.Dao)3 DaoException (org.nutz.dao.DaoException)3 PreparedStatement (java.sql.PreparedStatement)2 HashMap (java.util.HashMap)2 List (java.util.List)2 LinkField (org.nutz.dao.entity.LinkField)2