Search in sources :

Example 11 with ConnCallback

use of org.nutz.dao.ConnCallback in project nutz by nutzam.

the class ExtDaoInvocationHandler method insertBySpecialChain.

/**
     * 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用)
     *
     * @see org.nutz.dao.Chain#addSpecial(String, Object)
     */
@SuppressWarnings({ "rawtypes" })
public static void insertBySpecialChain(Dao dao, Entity en, String tableName, Chain chain) {
    if (en != null)
        tableName = en.getTableName();
    if (tableName == null)
        throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!");
    final StringBuilder sql = new StringBuilder("INSERT INTO ").append(tableName).append(" (");
    StringBuilder _value_places = new StringBuilder(" VALUES(");
    final List<Object> values = new ArrayList<Object>();
    final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>();
    Chain head = chain.head();
    while (head != null) {
        String colName = head.name();
        MappingField mf = null;
        if (en != null) {
            mf = en.getField(colName);
            if (mf != null)
                colName = mf.getColumnNameInSql();
        }
        sql.append(colName);
        if (head.special()) {
            _value_places.append(head.value());
        } else {
            if (en != null)
                mf = en.getField(head.name());
            _value_places.append("?");
            values.add(head.value());
            ValueAdaptor adaptor = Jdbcs.getAdaptorBy(head.value());
            if (mf != null && mf.getAdaptor() != null)
                adaptor = mf.getAdaptor();
            adaptors.add(adaptor);
        }
        head = head.next();
        if (head != null) {
            sql.append(", ");
            _value_places.append(", ");
        }
    }
    sql.append(")");
    _value_places.append(")");
    sql.append(_value_places);
    if (log.isDebugEnabled())
        log.debug(sql);
    dao.run(new ConnCallback() {

        public void invoke(Connection conn) throws Exception {
            PreparedStatement ps = conn.prepareStatement(sql.toString());
            try {
                for (int i = 0; i < values.size(); i++) adaptors.get(i).set(ps, values.get(i), i + 1);
                ps.execute();
            } finally {
                Daos.safeClose(ps);
            }
        }
    });
}
Also used : Chain(org.nutz.dao.Chain) ConnCallback(org.nutz.dao.ConnCallback) ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DaoException(org.nutz.dao.DaoException) MappingField(org.nutz.dao.entity.MappingField) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException)

Aggregations

Connection (java.sql.Connection)11 ConnCallback (org.nutz.dao.ConnCallback)11 SQLException (java.sql.SQLException)7 DaoException (org.nutz.dao.DaoException)5 PreparedStatement (java.sql.PreparedStatement)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 MappingField (org.nutz.dao.entity.MappingField)3 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 HashSet (java.util.HashSet)2 Chain (org.nutz.dao.Chain)2 JdbcExpert (org.nutz.dao.jdbc.JdbcExpert)2 ValueAdaptor (org.nutz.dao.jdbc.ValueAdaptor)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Set (java.util.Set)1 DataSource (javax.sql.DataSource)1 Dao (org.nutz.dao.Dao)1 DatabaseMeta (org.nutz.dao.DatabaseMeta)1