Skip to content

Commit fca6ffd

Browse files
authored
Merge pull request #434 from ifooling/master
fix:解决数据源为Oracle时,@Explain 报错问题;使用自增主键,代码中获取不到插入id问题
2 parents 98f2826 + 8f50678 commit fca6ffd

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3979,7 +3979,7 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
39793979
}
39803980
return "DELETE FROM " + tablePath + config.getWhereString(true) + (config.isMySQL() ? config.getLimitString() : ""); // PostgreSQL 不允许 LIMIT
39813981
default:
3982-
String explain = (config.isExplain() ? (config.isSQLServer() || config.isOracle() ? "SET STATISTICS PROFILE ON " : "EXPLAIN ") : "");
3982+
String explain = config.isExplain() ? (config.isSQLServer() ? "SET STATISTICS PROFILE ON " : (config.isOracle() ? "EXPLAIN PLAN FOR " : "EXPLAIN ")) : "";
39833983
if (config.isTest() && RequestMethod.isGetMethod(config.getMethod(), true)) { // FIXME 为啥是 code 而不是 count ?
39843984
String q = config.getQuote(); // 生成 SELECT ( (24 >=0 AND 24 <3) ) AS `code` LIMIT 1 OFFSET 0
39853985
return explain + "SELECT " + config.getWhereString(false) + " AS " + q + JSONResponse.KEY_COUNT + q + config.getLimitString();

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,13 @@ public PreparedStatement getStatement(@NotNull SQLConfig config, String sql) thr
10681068

10691069
PreparedStatement statement; //创建Statement对象
10701070
if (config.getMethod() == RequestMethod.POST && config.getId() == null) { //自增id
1071-
statement = getConnection(config).prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
1071+
if (config.isOracle()) {
1072+
// 解决 oracle 使用自增主键 插入获取不到id问题
1073+
String[] generatedColumns = {config.getIdKey()};
1074+
statement = getConnection(config).prepareStatement(sql, generatedColumns);
1075+
} else {
1076+
statement = getConnection(config).prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
1077+
}
10721078
}
10731079
else if (RequestMethod.isGetMethod(config.getMethod(), true)) {
10741080
statement = getConnection(config).prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
@@ -1250,7 +1256,7 @@ public int executeUpdate(@NotNull SQLConfig config, String sql) throws Exception
12501256
if (config.getId() == null && config.getMethod() == RequestMethod.POST) { // 自增id
12511257
ResultSet rs = stt.getGeneratedKeys();
12521258
if (rs != null && rs.next()) {
1253-
config.setId(rs.getLong(1)); //返回插入的主键id FIXME Oracle 拿不到
1259+
config.setId(rs.getLong(1));
12541260
}
12551261
}
12561262

0 commit comments

Comments
 (0)