Search in sources :

Example 26 with PreparedStatementResultHandler

use of org.sagacity.sqltoy.callback.PreparedStatementResultHandler in project sagacity-sqltoy by chenrenfei.

the class SqlUtil method executeSql.

/**
 * @todo 执行Sql语句完成修改操作
 * @param typeHandler
 * @param executeSql
 * @param params
 * @param paramsType
 * @param conn
 * @param dbType
 * @param autoCommit
 * @param processWord
 * @return
 * @throws Exception
 */
public static Long executeSql(TypeHandler typeHandler, final String executeSql, final Object[] params, final Integer[] paramsType, final Connection conn, final Integer dbType, final Boolean autoCommit, boolean processWord) throws Exception {
    // 对sql进行关键词符号替换
    String realSql = processWord ? ReservedWordsUtil.convertSql(executeSql, dbType) : executeSql;
    SqlExecuteStat.showSql("execute sql=", realSql, params);
    boolean hasSetAutoCommit = false;
    Long updateCounts = null;
    if (autoCommit != null) {
        if (!autoCommit == conn.getAutoCommit()) {
            conn.setAutoCommit(autoCommit);
            hasSetAutoCommit = true;
        }
    }
    PreparedStatement pst = conn.prepareStatement(realSql);
    Object result = preparedStatementProcess(null, pst, null, new PreparedStatementResultHandler() {

        @Override
        public void execute(Object obj, PreparedStatement pst, ResultSet rs) throws SQLException, IOException {
            // sqlserver 存在timestamp不能赋值问题,通过对象完成的修改、插入忽视掉timestamp列
            if (dbType == DBType.SQLSERVER && paramsType != null) {
                setSqlServerParamsValue(typeHandler, conn, dbType, pst, params, paramsType, 0);
            } else {
                setParamsValue(typeHandler, conn, dbType, pst, params, paramsType, 0);
            }
            pst.executeUpdate();
            // 返回update的记录数量
            this.setResult(Long.valueOf(pst.getUpdateCount()));
        }
    });
    if (result != null) {
        updateCounts = (Long) result;
    }
    if (hasSetAutoCommit && autoCommit != null) {
        conn.setAutoCommit(!autoCommit);
    }
    return updateCounts;
}
Also used : SQLException(java.sql.SQLException) PreparedStatementResultHandler(org.sagacity.sqltoy.callback.PreparedStatementResultHandler) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Aggregations

PreparedStatement (java.sql.PreparedStatement)26 ResultSet (java.sql.ResultSet)26 PreparedStatementResultHandler (org.sagacity.sqltoy.callback.PreparedStatementResultHandler)26 ArrayList (java.util.ArrayList)13 List (java.util.List)13 SQLException (java.sql.SQLException)9 IOException (java.io.IOException)8 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)6 ColumnMeta (org.sagacity.sqltoy.model.ColumnMeta)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 QueryResult (org.sagacity.sqltoy.model.QueryResult)4 ReflectPropertyHandler (org.sagacity.sqltoy.callback.ReflectPropertyHandler)3 ReflectPropsHandler (org.sagacity.sqltoy.callback.ReflectPropsHandler)3 OneToManyModel (org.sagacity.sqltoy.config.model.OneToManyModel)3 TableMeta (org.sagacity.sqltoy.model.TableMeta)3 SqlWithAnalysis (org.sagacity.sqltoy.config.model.SqlWithAnalysis)2 TableCascadeModel (org.sagacity.sqltoy.config.model.TableCascadeModel)2 SavePKStrategy (org.sagacity.sqltoy.dialect.model.SavePKStrategy)2 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)2