Search in sources :

Example 16 with Sql

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));
}
Also used : Master(test.po.Master) Sql(org.eweb4j.orm.sql.Sql) Test(org.junit.Test)

Example 17 with Sql

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));
}
Also used : Master(test.po.Master) Sql(org.eweb4j.orm.sql.Sql) Test(org.junit.Test)

Example 18 with Sql

use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.

the class TestUpdateSql 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("idValue", 5);
    map.put("columns", new String[] { "num", "name", "age", "type" });
    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");
    UpdateSqlCreator<?> update = SqlFactory.getUpdateSql(map, pet);
    Sql sql = update.update()[0];
    Assert.assertEquals("UPDATE t_pet SET num = ? ,name = ? ,age = ? ,type = ?  WHERE id = ?  ;", sql.sql);
    Assert.assertEquals(123, sql.args.get(0));
    Assert.assertEquals("weiwei", sql.args.get(1));
    Assert.assertEquals(3, sql.args.get(2));
    Assert.assertEquals("dog", sql.args.get(3));
    Assert.assertEquals(5, sql.args.get(4));
}
Also used : HashMap(java.util.HashMap) Pet(test.po.Pet) Sql(org.eweb4j.orm.sql.Sql) Test(org.junit.Test)

Example 19 with Sql

use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.

the class TestDeleteSql method testDeleteById.

/**
	 * 删除记录(按主键)
	 * 
	 * @param ts
	 * @return
	 */
@Test
public void testDeleteById() {
    pet.setPetId(10L);
    Sql sql = delete.delete()[0];
    Assert.assertEquals("DELETE FROM t_pet WHERE id = ?  ;", sql.sql);
    Assert.assertEquals(10l, sql.args.get(0));
}
Also used : Sql(org.eweb4j.orm.sql.Sql) Test(org.junit.Test)

Example 20 with Sql

use of org.eweb4j.orm.sql.Sql in project eweb4j-framework by laiweiwei.

the class TestInsertSql method testInsert.

/**
	 * 将若干个POJO的所有属性值插入数据库 例如: <code>
	 *  class Pet{
	 *  	private Integer id;
	 *  	private String name;
	 *  	private int age;
	 *      //此处省略setter和getter方法
	 *  }
	 * 	Pet pet = new Pet();
	 *  pet.setName("小黑");
	 *  pet.setAge(3);
	 *  insert(pet);
	 * </code> 会执行sql:INSERT INTO $table values('小黑','3');
	 * 
	 * POJO的类型
	 * 
	 * @param ts
	 *            带有数据的POJO,可多个不同类型或同类型
	 * @return 如果插入成功,返回true,否则返回false.
	 */
@Test
public <T> void testInsert() {
    pet.setName("小黑");
    pet.setAge(3);
    pet.setType("dog");
    Sql sql = insert.create()[0];
    Assert.assertEquals("INSERT INTO t_pet(name,age,cate) VALUES(?,?,?) ;", sql.sql);
    Assert.assertEquals("小黑", sql.args.get(0));
    Assert.assertEquals(3, sql.args.get(1));
    Assert.assertEquals("dog", sql.args.get(2));
}
Also used : Sql(org.eweb4j.orm.sql.Sql) Test(org.junit.Test)

Aggregations

Sql (org.eweb4j.orm.sql.Sql)28 Connection (java.sql.Connection)13 DAOException (org.eweb4j.orm.dao.DAOException)13 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)6 Master (test.po.Master)4 HashMap (java.util.HashMap)3 Pet (test.po.Pet)3