修复SqlExecutor.execute执行ORACLE insert into select报ORA-00933问题

This commit is contained in:
Looly 2023-05-26 22:49:19 +08:00
parent 2eafa12b31
commit 443e821c2f
3 changed files with 59 additions and 32 deletions

View File

@ -40,10 +40,9 @@ public class StatementUtil {
* @param conn 数据库连接
* @param sqlBuilder {@link SqlBuilder}包括SQL语句和参数
* @return {@link PreparedStatement}
* @throws SQLException SQL异常
* @since 4.1.3
*/
public static PreparedStatement prepareStatement(final Connection conn, final SqlBuilder sqlBuilder) throws SQLException {
public static PreparedStatement prepareStatement(final Connection conn, final SqlBuilder sqlBuilder) {
return prepareStatement(conn, sqlBuilder.build(), sqlBuilder.getParamValueArray());
}
@ -54,10 +53,9 @@ public class StatementUtil {
* @param sql SQL语句使用"?"做为占位符
* @param params "?"对应参数列表
* @return {@link PreparedStatement}
* @throws SQLException SQL异常
* @since 3.2.3
*/
public static PreparedStatement prepareStatement(final Connection conn, final String sql, final Collection<Object> params) throws SQLException {
public static PreparedStatement prepareStatement(final Connection conn, final String sql, final Collection<Object> params) {
return prepareStatement(conn, sql, params.toArray(new Object[0]));
}
@ -68,13 +66,27 @@ public class StatementUtil {
* @param sql SQL语句使用"?"做为占位符
* @param params "?"对应参数列表或者Map表示命名参数
* @return {@link PreparedStatement}
* @throws SQLException SQL异常
* @since 3.2.3
*/
public static PreparedStatement prepareStatement(final Connection conn, final String sql, final Object... params) throws SQLException {
public static PreparedStatement prepareStatement(final Connection conn, final String sql, final Object... params) {
return prepareStatement(GlobalDbConfig.returnGeneratedKey, conn, sql, params);
}
/**
* 创建{@link PreparedStatement}
*
* @param returnGeneratedKey 当为insert语句时是否返回主键
* @param conn 数据库连接
* @param sql SQL语句使用"?"做为占位符
* @param params "?"对应参数列表或者Map表示命名参数
* @return {@link PreparedStatement}
* @since 5.8.19
*/
public static PreparedStatement prepareStatement(final boolean returnGeneratedKey,
final Connection conn, final String sql, final Object... params) {
return StatementBuilder.of()
.setConnection(conn)
.setReturnGeneratedKey(GlobalDbConfig.returnGeneratedKey)
.setReturnGeneratedKey(returnGeneratedKey)
.setSqlLog(SqlLog.INSTANCE)
.setSql(sql)
.setParams(params)
@ -88,10 +100,9 @@ public class StatementUtil {
* @param sql SQL语句使用"?"做为占位符
* @param paramsBatch "?"对应参数批次列表
* @return {@link PreparedStatement}
* @throws SQLException SQL异常
* @since 4.1.13
*/
public static PreparedStatement prepareStatementForBatch(final Connection conn, final String sql, final Object[]... paramsBatch) throws SQLException {
public static PreparedStatement prepareStatementForBatch(final Connection conn, final String sql, final Object[]... paramsBatch) {
return prepareStatementForBatch(conn, sql, new ArrayIter<>(paramsBatch));
}
@ -102,13 +113,13 @@ public class StatementUtil {
* @param sql SQL语句使用"?"做为占位符
* @param paramsBatch "?"对应参数批次列表
* @return {@link PreparedStatement}
* @throws SQLException SQL异常
* @since 4.1.13
*/
public static PreparedStatement prepareStatementForBatch(final Connection conn, final String sql, final Iterable<Object[]> paramsBatch) throws SQLException {
public static PreparedStatement prepareStatementForBatch(final Connection conn, final String sql,
final Iterable<Object[]> paramsBatch) {
return StatementBuilder.of()
.setConnection(conn)
.setReturnGeneratedKey(GlobalDbConfig.returnGeneratedKey)
.setReturnGeneratedKey(false)
.setSqlLog(SqlLog.INSTANCE)
.setSql(sql)
.buildForBatch(paramsBatch);
@ -122,13 +133,13 @@ public class StatementUtil {
* @param fields 字段列表用于获取对应值
* @param entities "?"对应参数批次列表
* @return {@link PreparedStatement}
* @throws SQLException SQL异常
* @since 4.6.7
*/
public static PreparedStatement prepareStatementForBatch(final Connection conn, final String sql, final Iterable<String> fields, final Entity... entities) throws SQLException {
public static PreparedStatement prepareStatementForBatch(final Connection conn, final String sql,
final Iterable<String> fields, final Entity... entities) {
return StatementBuilder.of()
.setConnection(conn)
.setReturnGeneratedKey(GlobalDbConfig.returnGeneratedKey)
.setReturnGeneratedKey(false)
.setSqlLog(SqlLog.INSTANCE)
.setSql(sql)
.buildForBatch(fields, entities);
@ -144,14 +155,13 @@ public class StatementUtil {
* @throws SQLException SQL异常
* @since 4.1.13
*/
public static CallableStatement prepareCall(final Connection conn, String sql, final Object... params) throws SQLException {
Assert.notBlank(sql, "Sql String must be not blank!");
sql = sql.trim();
SqlLog.INSTANCE.log(sql, params);
final CallableStatement call = conn.prepareCall(sql);
fillArrayParam(call, params);
return call;
public static CallableStatement prepareCall(final Connection conn, final String sql, final Object... params) throws SQLException {
return StatementBuilder.of()
.setConnection(conn)
.setSqlLog(SqlLog.INSTANCE)
.setSql(sql)
.setParams(params)
.buildForCall();
}
// region ----- getGeneratedKey

View File

@ -61,7 +61,7 @@ public class SqlExecutor {
public static int execute(final Connection conn, final String sql, final Object... params) throws DbRuntimeException {
PreparedStatement ps = null;
try {
ps = StatementUtil.prepareStatement(conn, sql, params);
ps = StatementUtil.prepareStatement(false, conn, sql, params);
return ps.executeUpdate();
} catch (final SQLException e) {
throw new DbRuntimeException(e);
@ -143,7 +143,7 @@ public class SqlExecutor {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = StatementUtil.prepareStatement(conn, sql, params);
ps = StatementUtil.prepareStatement(true, conn, sql, params);
ps.executeUpdate();
rs = ps.getGeneratedKeys();
if (rs != null && rs.next()) {
@ -275,10 +275,8 @@ public class SqlExecutor {
public static <T> T query(final Connection conn, final String sql, final RsHandler<T> rsh, final Object... params) throws DbRuntimeException {
PreparedStatement ps = null;
try {
ps = StatementUtil.prepareStatement(conn, sql, params);
ps = StatementUtil.prepareStatement(false, conn, sql, params);
return executeQuery(ps, rsh);
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
IoUtil.closeQuietly(ps);
}

View File

@ -22,10 +22,7 @@ import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.db.DbRuntimeException;
import org.dromara.hutool.db.Entity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
@ -55,6 +52,7 @@ public class StatementBuilder implements Builder<StatementWrapper> {
/**
* 设置SQL日志
*
* @param sqlLog {@link SqlLog}
* @return this
*/
@ -98,6 +96,7 @@ public class StatementBuilder implements Builder<StatementWrapper> {
/**
* 设置是否返回主键
*
* @param returnGeneratedKey 是否返回主键
* @return this
*/
@ -168,6 +167,26 @@ public class StatementBuilder implements Builder<StatementWrapper> {
return ps;
}
/**
* 创建存储过程或函数调用的{@link StatementWrapper}
*
* @return StatementWrapper
* @since 6.0.0
*/
public CallableStatement buildForCall() {
Assert.notBlank(sql, "Sql String must be not blank!");
sqlLog.log(sql, ArrayUtil.isEmpty(params) ? null : params);
try {
return (CallableStatement) StatementWrapper
.of(connection.prepareCall(sql))
.fillArrayParam(params)
.getRaw();
} catch (final SQLException e) {
throw new DbRuntimeException(e);
}
}
/**
* 构建{@link StatementWrapper}
*