Search in sources :

Example 1 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[] ids = null;
    Connection con = null;
    if (clazz == null || clazz.length == 0)
        return ids;
    ids = new Number[clazz.length];
    try {
        con = ds.getConnection();
        Object[] ts = new Object[clazz.length];
        for (int i = 0; i < clazz.length; ++i) {
            ts[i] = clazz[i].newInstance();
        }
        Sql[] sqls = SqlFactory.getDeleteSql(ts).delete(fields, values);
        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 : Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DAOException(org.eweb4j.orm.dao.DAOException) Sql(org.eweb4j.orm.sql.Sql) DAOException(org.eweb4j.orm.dao.DAOException)

Example 2 with Sql

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

the class DeleteDAOImpl method deleteByFields.

public <T> Number deleteByFields(T t, String[] fields) throws DAOException {
    Number id = 0;
    if (t != null) {
        Connection con = null;
        try {
            con = ds.getConnection();
            Sql[] sqls = SqlFactory.getDeleteSql(new Object[] { t }).delete(fields);
            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 3 with Sql

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

the class InsertDAOImpl method batchInsert.

public <T> Number[] batchInsert(T[] ts, String[] fields, Object[] values) throws DAOException {
    Number[] ids = null;
    Connection con = null;
    if (ts == null || ts.length == 0)
        return ids;
    try {
        con = ds.getConnection();
        Sql[] sqls = SqlFactory.getInsertSql(ts).createByFieldsIsValues(fields, values);
        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 4 with Sql

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

the class InsertDAOImpl method insertByCondition.

public <T> Number[] insertByCondition(T[] ts, String condition) throws DAOException {
    Number[] ids = null;
    Connection con = null;
    if (ts == null || ts.length == 0)
        return ids;
    ids = new Number[ts.length];
    try {
        Sql[] sqls = SqlFactory.getInsertSql(ts).create(condition);
        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("", 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)

Example 5 with Sql

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

the class InsertDAOImpl method batchInsert.

public <T> Number[] batchInsert(T[] ts, String... fields) throws DAOException {
    Number[] ids = null;
    Connection con = null;
    if (ts == null || ts.length == 0)
        return ids;
    try {
        con = ds.getConnection();
        Sql[] sqls = null;
        if (fields != null && fields.length > 0)
            sqls = SqlFactory.getInsertSql(ts).createByFields(fields);
        else
            sqls = SqlFactory.getInsertSql(ts).create();
        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)

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