Skip to content

Commit

Permalink
修复SqlExecutor.execute执行ORACLE insert into select报ORA-00933问题
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed May 26, 2023
1 parent 5c510bf commit 791ba35
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* 【cron 】 修复SystemTimer无法结束进程问题(issue#3090@Github)
* 【core 】 修复BeanUtil.copyToList复制Long等类型错误问题(issue#3091@Github)
* 【poi 】 修复MapRowHandler结果Map无序问题(issue#I71SE8@Github)
* 【db 】 修复SqlExecutor.execute执行ORACLE insert into select报ORA-00933问题(issue#I778U7@Github)

-------------------------------------------------------------------------------------------------------------
# 5.8.18 (2023-04-27)
Expand Down
17 changes: 16 additions & 1 deletion hutool-db/src/main/java/cn/hutool/db/StatementUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ public static PreparedStatement prepareStatement(Connection conn, String sql, Co
* @since 3.2.3
*/
public static PreparedStatement prepareStatement(Connection conn, String sql, Object... params) throws SQLException {
return prepareStatement(GlobalDbConfig.returnGeneratedKey, conn, sql, params);
}

/**
* 创建{@link PreparedStatement}
*
* @param returnGeneratedKey 当为insert语句时,是否返回主键
* @param conn 数据库连接
* @param sql SQL语句,使用"?"做为占位符
* @param params "?"对应参数列表
* @return {@link PreparedStatement}
* @throws SQLException SQL异常
* @since 5.8.19
*/
public static PreparedStatement prepareStatement(boolean returnGeneratedKey, Connection conn, String sql, Object... params) throws SQLException {
Assert.notBlank(sql, "Sql String must be not blank!");
sql = sql.trim();

Expand All @@ -136,7 +151,7 @@ public static PreparedStatement prepareStatement(Connection conn, String sql, Ob

SqlLog.INSTANCE.log(sql, ArrayUtil.isEmpty(params) ? null : params);
PreparedStatement ps;
if (GlobalDbConfig.returnGeneratedKey && StrUtil.startWithIgnoreCase(sql, "insert")) {
if (returnGeneratedKey && StrUtil.startWithIgnoreCase(sql, "insert")) {
// 插入默认返回主键
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
} else {
Expand Down
6 changes: 3 additions & 3 deletions hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static int execute(Connection conn, String sql, Map<String, Object> param
public static int execute(Connection conn, String sql, Object... params) throws SQLException {
PreparedStatement ps = null;
try {
ps = StatementUtil.prepareStatement(conn, sql, params);
ps = StatementUtil.prepareStatement(false, conn, sql, params);
return ps.executeUpdate();
} finally {
DbUtil.close(ps);
Expand Down Expand Up @@ -128,7 +128,7 @@ public static Long executeForGeneratedKey(Connection conn, String sql, Object...
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()) {
Expand Down Expand Up @@ -271,7 +271,7 @@ public static <T> T query(Connection conn, SqlBuilder sqlBuilder, RsHandler<T> r
public static <T> T query(Connection conn, String sql, RsHandler<T> rsh, Object... params) throws SQLException {
PreparedStatement ps = null;
try {
ps = StatementUtil.prepareStatement(conn, sql, params);
ps = StatementUtil.prepareStatement(false, conn, sql, params);
return executeQuery(ps, rsh);
} finally {
DbUtil.close(ps);
Expand Down

0 comments on commit 791ba35

Please sign in to comment.