use of org.nutz.dao.sql.Sql in project nutz by nutzam.
the class SqlImplTest method test_sql_get_list.
@Test
public void test_sql_get_list() {
Sql sql = Sqls.create("SELECT version()");
ArrayList<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
list.add(new HashMap<Object, Object>());
list.add(new HashMap<Object, Object>());
sql.getContext().setResult(list);
// 传入 map结果会导致上面的isFrom 为false
List<?> re = sql.getList(Map.class);
assertTrue(re == list);
// 因为list中的实例是HashMap,因此能够正常返回
re = sql.getList(HashMap.class);
assertTrue(re == list);
}
use of org.nutz.dao.sql.Sql in project nutz by nutzam.
the class SimpleDaoExecTest method test_simple_h2_call.
// H2 支持简单的CALL语句
@Test
public void test_simple_h2_call() {
if (!dao.meta().isH2())
// Only test for h2 now
return;
dao.create(Pet.class, true);
dao.insert(Pet.create("wendal"));
dao.execute(Sqls.create("CALL SELECT MAX(ID) FROM t_PET"));
Sql sql = Sqls.fetchInt("CALL SELECT MAX(ID) FROM t_PET");
dao.execute(sql);
assertEquals(1, sql.getInt());
sql = Sqls.fetchInt("CALL 15*25");
dao.execute(sql);
assertEquals(15 * 25, sql.getInt());
}
use of org.nutz.dao.sql.Sql in project nutz by nutzam.
the class SimpleDaoExecTest method test_exec_out.
@Test
public void test_exec_out() {
if (!dao.meta().isMySql())
// Only test for mysql now
return;
dao.create(Pet.class, true);
dao.insert(Pet.create("wendal"));
Pet pet = dao.fetch(Pet.class, "wendal");
dao.execute(Sqls.create("DROP PROCEDURE IF EXISTS proc_pet_fetch"));
dao.execute(Sqls.create("CREATE PROCEDURE proc_pet_fetch(IN nm varchar(1024), OUT outId int)\nBEGIN\n\tselect id into outId from t_pet where name=nm;\nEND"));
// 像普通自定义SQL那样创建SQL对象.
Sql sql = Sqls.create("CALL proc_pet_fetch(@nm, @OUTid)");
sql.setEntity(dao.getEntity(Pet.class));
// 设置入参
sql.params().set("nm", "wendal");
// 设置出参类型,注意,必须加OUT开头
sql.params().set("OUTid", Types.INTEGER);
dao.execute(sql);
Record re = sql.getOutParams();
assertNotNull(re);
assertEquals(pet.getId(), re.get("id"));
}
use of org.nutz.dao.sql.Sql in project nutz by nutzam.
the class SimpleDaoExecTest method test_simple_mysql_exec2.
// Mysql支持存储过程
@Test
public void test_simple_mysql_exec2() {
if (!dao.meta().isMySql())
// Only test for mysql now
return;
dao.create(Pet.class, true);
dao.insert(Pet.create("wendal"));
dao.execute(Sqls.create("DROP PROCEDURE IF EXISTS proc_pet_fetch"));
dao.execute(Sqls.create("CREATE PROCEDURE proc_pet_fetch(IN nm varchar(1024))\nBEGIN\n\tSELECT * FROM t_pet where name=nm;\nEND"));
Sql sql = Sqls.fetchEntity("CALL proc_pet_fetch(@nm)");
sql.setEntity(dao.getEntity(Pet.class));
sql.params().set("nm", "wendal");
dao.execute(sql);
Pet pet = sql.getObject(Pet.class);
assertNotNull(pet);
assertEquals("wendal", pet.getName());
}
use of org.nutz.dao.sql.Sql in project nutz by nutzam.
the class NutDao method drop.
public boolean drop(String tableName) {
if (!exists(tableName))
return false;
Sql sql = Sqls.createf("DROP TABLE %s", tableName);
_exec(sql);
return true;
}
Aggregations