mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复SqlExecutor.execute执行ORACLE insert into select报ORA-00933问题
This commit is contained in:
parent
2eafa12b31
commit
443e821c2f
@ -40,10 +40,9 @@ public class StatementUtil {
|
|||||||
* @param conn 数据库连接
|
* @param conn 数据库连接
|
||||||
* @param sqlBuilder {@link SqlBuilder}包括SQL语句和参数
|
* @param sqlBuilder {@link SqlBuilder}包括SQL语句和参数
|
||||||
* @return {@link PreparedStatement}
|
* @return {@link PreparedStatement}
|
||||||
* @throws SQLException SQL异常
|
|
||||||
* @since 4.1.3
|
* @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());
|
return prepareStatement(conn, sqlBuilder.build(), sqlBuilder.getParamValueArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,10 +53,9 @@ public class StatementUtil {
|
|||||||
* @param sql SQL语句,使用"?"做为占位符
|
* @param sql SQL语句,使用"?"做为占位符
|
||||||
* @param params "?"对应参数列表
|
* @param params "?"对应参数列表
|
||||||
* @return {@link PreparedStatement}
|
* @return {@link PreparedStatement}
|
||||||
* @throws SQLException SQL异常
|
|
||||||
* @since 3.2.3
|
* @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]));
|
return prepareStatement(conn, sql, params.toArray(new Object[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,13 +66,27 @@ public class StatementUtil {
|
|||||||
* @param sql SQL语句,使用"?"做为占位符
|
* @param sql SQL语句,使用"?"做为占位符
|
||||||
* @param params "?"对应参数列表或者Map表示命名参数
|
* @param params "?"对应参数列表或者Map表示命名参数
|
||||||
* @return {@link PreparedStatement}
|
* @return {@link PreparedStatement}
|
||||||
* @throws SQLException SQL异常
|
|
||||||
* @since 3.2.3
|
* @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()
|
return StatementBuilder.of()
|
||||||
.setConnection(conn)
|
.setConnection(conn)
|
||||||
.setReturnGeneratedKey(GlobalDbConfig.returnGeneratedKey)
|
.setReturnGeneratedKey(returnGeneratedKey)
|
||||||
.setSqlLog(SqlLog.INSTANCE)
|
.setSqlLog(SqlLog.INSTANCE)
|
||||||
.setSql(sql)
|
.setSql(sql)
|
||||||
.setParams(params)
|
.setParams(params)
|
||||||
@ -88,10 +100,9 @@ public class StatementUtil {
|
|||||||
* @param sql SQL语句,使用"?"做为占位符
|
* @param sql SQL语句,使用"?"做为占位符
|
||||||
* @param paramsBatch "?"对应参数批次列表
|
* @param paramsBatch "?"对应参数批次列表
|
||||||
* @return {@link PreparedStatement}
|
* @return {@link PreparedStatement}
|
||||||
* @throws SQLException SQL异常
|
|
||||||
* @since 4.1.13
|
* @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));
|
return prepareStatementForBatch(conn, sql, new ArrayIter<>(paramsBatch));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,13 +113,13 @@ public class StatementUtil {
|
|||||||
* @param sql SQL语句,使用"?"做为占位符
|
* @param sql SQL语句,使用"?"做为占位符
|
||||||
* @param paramsBatch "?"对应参数批次列表
|
* @param paramsBatch "?"对应参数批次列表
|
||||||
* @return {@link PreparedStatement}
|
* @return {@link PreparedStatement}
|
||||||
* @throws SQLException SQL异常
|
|
||||||
* @since 4.1.13
|
* @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()
|
return StatementBuilder.of()
|
||||||
.setConnection(conn)
|
.setConnection(conn)
|
||||||
.setReturnGeneratedKey(GlobalDbConfig.returnGeneratedKey)
|
.setReturnGeneratedKey(false)
|
||||||
.setSqlLog(SqlLog.INSTANCE)
|
.setSqlLog(SqlLog.INSTANCE)
|
||||||
.setSql(sql)
|
.setSql(sql)
|
||||||
.buildForBatch(paramsBatch);
|
.buildForBatch(paramsBatch);
|
||||||
@ -122,13 +133,13 @@ public class StatementUtil {
|
|||||||
* @param fields 字段列表,用于获取对应值
|
* @param fields 字段列表,用于获取对应值
|
||||||
* @param entities "?"对应参数批次列表
|
* @param entities "?"对应参数批次列表
|
||||||
* @return {@link PreparedStatement}
|
* @return {@link PreparedStatement}
|
||||||
* @throws SQLException SQL异常
|
|
||||||
* @since 4.6.7
|
* @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()
|
return StatementBuilder.of()
|
||||||
.setConnection(conn)
|
.setConnection(conn)
|
||||||
.setReturnGeneratedKey(GlobalDbConfig.returnGeneratedKey)
|
.setReturnGeneratedKey(false)
|
||||||
.setSqlLog(SqlLog.INSTANCE)
|
.setSqlLog(SqlLog.INSTANCE)
|
||||||
.setSql(sql)
|
.setSql(sql)
|
||||||
.buildForBatch(fields, entities);
|
.buildForBatch(fields, entities);
|
||||||
@ -144,14 +155,13 @@ public class StatementUtil {
|
|||||||
* @throws SQLException SQL异常
|
* @throws SQLException SQL异常
|
||||||
* @since 4.1.13
|
* @since 4.1.13
|
||||||
*/
|
*/
|
||||||
public static CallableStatement prepareCall(final Connection conn, String sql, final Object... params) throws SQLException {
|
public static CallableStatement prepareCall(final Connection conn, final String sql, final Object... params) throws SQLException {
|
||||||
Assert.notBlank(sql, "Sql String must be not blank!");
|
return StatementBuilder.of()
|
||||||
|
.setConnection(conn)
|
||||||
sql = sql.trim();
|
.setSqlLog(SqlLog.INSTANCE)
|
||||||
SqlLog.INSTANCE.log(sql, params);
|
.setSql(sql)
|
||||||
final CallableStatement call = conn.prepareCall(sql);
|
.setParams(params)
|
||||||
fillArrayParam(call, params);
|
.buildForCall();
|
||||||
return call;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// region ----- getGeneratedKey
|
// region ----- getGeneratedKey
|
||||||
|
@ -61,7 +61,7 @@ public class SqlExecutor {
|
|||||||
public static int execute(final Connection conn, final String sql, final Object... params) throws DbRuntimeException {
|
public static int execute(final Connection conn, final String sql, final Object... params) throws DbRuntimeException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
try {
|
try {
|
||||||
ps = StatementUtil.prepareStatement(conn, sql, params);
|
ps = StatementUtil.prepareStatement(false, conn, sql, params);
|
||||||
return ps.executeUpdate();
|
return ps.executeUpdate();
|
||||||
} catch (final SQLException e) {
|
} catch (final SQLException e) {
|
||||||
throw new DbRuntimeException(e);
|
throw new DbRuntimeException(e);
|
||||||
@ -143,7 +143,7 @@ public class SqlExecutor {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
ps = StatementUtil.prepareStatement(conn, sql, params);
|
ps = StatementUtil.prepareStatement(true, conn, sql, params);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
rs = ps.getGeneratedKeys();
|
rs = ps.getGeneratedKeys();
|
||||||
if (rs != null && rs.next()) {
|
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 {
|
public static <T> T query(final Connection conn, final String sql, final RsHandler<T> rsh, final Object... params) throws DbRuntimeException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
try {
|
try {
|
||||||
ps = StatementUtil.prepareStatement(conn, sql, params);
|
ps = StatementUtil.prepareStatement(false, conn, sql, params);
|
||||||
return executeQuery(ps, rsh);
|
return executeQuery(ps, rsh);
|
||||||
} catch (final SQLException e) {
|
|
||||||
throw new DbRuntimeException(e);
|
|
||||||
} finally {
|
} finally {
|
||||||
IoUtil.closeQuietly(ps);
|
IoUtil.closeQuietly(ps);
|
||||||
}
|
}
|
||||||
|
@ -22,10 +22,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
|||||||
import org.dromara.hutool.db.DbRuntimeException;
|
import org.dromara.hutool.db.DbRuntimeException;
|
||||||
import org.dromara.hutool.db.Entity;
|
import org.dromara.hutool.db.Entity;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -55,6 +52,7 @@ public class StatementBuilder implements Builder<StatementWrapper> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置SQL日志
|
* 设置SQL日志
|
||||||
|
*
|
||||||
* @param sqlLog {@link SqlLog}
|
* @param sqlLog {@link SqlLog}
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
@ -98,6 +96,7 @@ public class StatementBuilder implements Builder<StatementWrapper> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置是否返回主键
|
* 设置是否返回主键
|
||||||
|
*
|
||||||
* @param returnGeneratedKey 是否返回主键
|
* @param returnGeneratedKey 是否返回主键
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
@ -168,6 +167,26 @@ public class StatementBuilder implements Builder<StatementWrapper> {
|
|||||||
return ps;
|
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}
|
* 构建{@link StatementWrapper}
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user