Search in sources :

Example 21 with Sql

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

the class TestInsertSql method testInsertByCondition.

/**
	 * 带Where条件子句,将若干个POJO所有属性值插入数据库 <code>
	 * 	class Pet{
	 * 		private Integer id;
	 * 		private String name;
	 * 		private int age;
	 * 		//此处省略setter和getter方法
	 * 	}
	 *  Pet pet = new Pet();
	 *  pet.setName("小白");
	 *  pet.setAge(4);
	 *  insertByCondition("xxx", pet);
	 * </code> 会执行sql:INSERT INTO $table values('小黑','3') WHERE xxx ;
	 * 
	 * POJO的类型
	 * 
	 * @param condition
	 *            Where条件
	 * @param ts
	 *            带有数据的POJO,可多个不同类型或同类型
	 * @return 如果插入成功,返回true,否则返回false
	 */
@Test
public void testInsertByCondition() {
    pet.setName("小黑");
    pet.setAge(3);
    pet.setType("dog");
    pet.setMaster(null);
    Sql sql = insert.create("xxx = 'ooo'")[0];
    Assert.assertEquals("INSERT INTO t_pet(name,age,cate) VALUES(?,?,?)  WHERE xxx = 'ooo' ;", 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)

Example 22 with Sql

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

the class TestUpdateSql method testUpdateByField.

/**
	 * 修改给定字段
	 * 
	 * @param t
	 *            给定的对象
	 * @param fields
	 *            给定的字段
	 * @return 返回布尔
	 */
@Test
public <T> void testUpdateByField() {
    pet.setName("xiaohuang");
    pet.setAge(3);
    pet.setPetId(12L);
    Master master = new Master();
    master.setId(5L);
    pet.setMaster(master);
    String[] fields = { "name", "age", "master" };
    Sql sql = update.update(fields)[0];
    Assert.assertEquals("UPDATE t_pet SET name = ? , age = ? , master_id = ?  WHERE id = ?  ;", sql.sql);
    Assert.assertEquals("xiaohuang", sql.args.get(0));
    Assert.assertEquals(3, sql.args.get(1));
    Assert.assertEquals(5l, sql.args.get(2));
    Assert.assertEquals(12l, sql.args.get(3));
}
Also used : Master(test.po.Master) Sql(org.eweb4j.orm.sql.Sql) Test(org.junit.Test)

Example 23 with Sql

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

the class DeleteDAOImpl method batchDelete.

public <T> Number[] batchDelete(T... ts) throws DAOException {
    Number[] ids = null;
    Connection con = null;
    if (ts == null || ts.length == 0)
        return ids;
    ids = new Number[ts.length];
    try {
        con = ds.getConnection();
        Sql[] sqls = SqlFactory.getDeleteSql(ts).delete();
        List<Object[]> argList = new ArrayList<Object[]>(ts.length);
        for (Sql sql : sqls) {
            argList.add(sql.args.toArray());
        }
        Object[][] args = new Object[argList.size()][];
        for (int i = 0; i < argList.size(); i++) {
            args[i] = argList.get(i);
        }
        ids = JdbcUtil.batchUpdateWithArgs(con, sqls[0].sql, args);
    } catch (Exception e) {
        throw new DAOException("", e);
    }
    return ids;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DAOException(org.eweb4j.orm.dao.DAOException) Sql(org.eweb4j.orm.sql.Sql)

Example 24 with Sql

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

the class DeleteDAOImpl method deleteByFieldIsValue.

public <T> Number deleteByFieldIsValue(Class<T> clazz, String[] fields, String[] values) throws DAOException {
    Number id = 0;
    if (clazz != null && fields != null && values != null && fields.length == values.length) {
        Connection con = null;
        try {
            con = ds.getConnection();
            Sql[] sqls = SqlFactory.getDeleteSql(new Object[] { clazz.newInstance() }).delete(fields, values);
            id = JdbcUtil.updateWithArgs(con, sqls[0].sql, sqls[0].args.toArray());
        } catch (Exception e) {
            throw new DAOException("", e);
        }
    }
    return id;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) DAOException(org.eweb4j.orm.dao.DAOException) Sql(org.eweb4j.orm.sql.Sql)

Example 25 with Sql

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

the class UpdateDAOImpl method updateByFieldIsValue.

public <T> Number[] updateByFieldIsValue(T[] ts, String[] fields, String[] values) throws DAOException {
    Number[] ids = null;
    if (ts != null && ts.length > 0 && fields != null && fields.length > 0) {
        Connection con = null;
        ids = new Number[ts.length];
        try {
            Sql[] sqls = SqlFactory.getUpdateSql(ts).update(fields, values);
            for (int i = 0; i < ts.length; i++) {
                con = ds.getConnection();
                ids[i] = JdbcUtil.updateWithArgs(con, sqls[i].sql, sqls[i].args.toArray());
            // 更新缓存
            }
        } catch (Exception e) {
            throw new DAOException("updateByFieldIsValue exception ", e);
        }
    }
    return ids;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) DAOException(org.eweb4j.orm.dao.DAOException) Sql(org.eweb4j.orm.sql.Sql)

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