use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class TestDeleteSql method testDeleteByField.
/**
* 删除记录,按给定字段
*
* @param columns
* @return
*/
@Test
public void testDeleteByField() {
Sql sql = delete.delete(new String[] { "age" })[0];
Assert.assertEquals("DELETE FROM t_pet WHERE age = ? ;", sql.sql);
Assert.assertEquals(30, sql.args.get(0));
}
use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class TestInsertSql method testInsertByFields.
/**
* 给定POJO属性值,插入数据库 <code>
* class Pet{
* private Integer id;
* private String name;
* private int age;
* //此处省略setter和getter方法
* }
* class Master{
* private Integer id;
* private String name;
* private String gender;
* }
*
* Pet pet = new Pet();
* pet.setName("小黄");
*
* Master master = new Master();
* master.setGender("女");
*
* Object[] objs = new Object[]{master, pet};
* String[] masterFields = new String[]{"gender"};
* String[] petFields = new String[]{"name"};
* String[][] fields = new String[][]{masterFields, petFields};
* insertByFields(objs,masterFields, petFields);
* </code>会执行sql:INSERT INTO $masterTable(gender) values('女');INSERT INTO
* $petTable(name) values('小黄') ;
*
* POJO的类型
*
* @param ts
* 带有数据的POJO,多个不同类型或同类型
* @param fields
* 按数组下标对应POJO的属性名
* @return 如果插入成功,返回true,否则返回false
*/
@Test
public void testInsertByFields() {
pet.setName("小黑");
Sql sql = insert.createByFields(new String[] { "name" })[0];
Assert.assertEquals("INSERT INTO t_pet(name) VALUES(?) ;", sql.sql);
Assert.assertEquals("小黑", sql.args.get(0));
}
use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class TestInsertSql method testMap.
@Test
public void testMap() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("table", "t_pet");
map.put("idColumn", "id");
map.put("columns", new String[] { "num", "name", "age", "cate" });
map.put("values", new Object[] { 123, "weiwei", 3, "dog" });
Pet pet = new Pet();
pet.setName("xxx");
pet.setNumber("4444");
pet.setAge(3);
pet.setType("cat");
final InsertSqlCreator<?> insert = SqlFactory.getInsertSql(map, pet);
Sql[] sql = insert.create();
Assert.assertEquals("INSERT INTO t_pet(num,name,age,cate) VALUES(?,?,?,?) ;", sql[0].sql);
Assert.assertEquals(123, sql[0].args.get(0));
Assert.assertEquals("weiwei", sql[0].args.get(1));
Assert.assertEquals(3, sql[0].args.get(2));
Assert.assertEquals("dog", sql[0].args.get(3));
Assert.assertEquals("INSERT INTO t_pet(num,name,age,cate) VALUES(?,?,?,?) ;", sql[1].sql);
Assert.assertEquals("4444", sql[1].args.get(0));
Assert.assertEquals("xxx", sql[1].args.get(1));
Assert.assertEquals(3, sql[1].args.get(2));
Assert.assertEquals("cat", sql[1].args.get(3));
}
use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class TestInsertSql method testInsertWithOneRel.
/**
*
* @throws Exception
*/
@Test
public void testInsertWithOneRel() throws Exception {
Master master = new Master();
master.setId(5L);
pet.setName("xiaohei");
pet.setAge(2);
pet.setMaster(master);
String[] fields = { "name", "age", "master" };
Sql sql = insert.createByFields(fields)[0];
Assert.assertEquals("INSERT INTO t_pet(name,age,master_id) VALUES(?,?,?) ;", sql.sql);
Assert.assertEquals("xiaohei", sql.args.get(0));
Assert.assertEquals(2, sql.args.get(1));
Assert.assertEquals(5l, sql.args.get(2));
}
use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.
the class TestUpdateSql method testUpdate.
/**
* 修改表记录所有不为null值的字段,通过主键值作为条件
*
* @param ts
* @return
*/
@Test
public void testUpdate() {
pet.setPetId(5L);
pet.setName("小黑");
pet.setType("dog");
pet.setAge(1111);
Master master = new Master();
master.setId(9L);
pet.setMaster(master);
Sql sql = update.update()[0];
Assert.assertEquals("UPDATE t_pet SET name = ? ,age = ? ,cate = ? ,master_id = ? WHERE id = ? ;", sql.sql);
Assert.assertEquals("小黑", sql.args.get(0));
Assert.assertEquals(1111, sql.args.get(1));
Assert.assertEquals("dog", sql.args.get(2));
Assert.assertEquals(9l, sql.args.get(3));
Assert.assertEquals(5l, sql.args.get(4));
}
Aggregations