Skip to content

fix:解决数据源为Oracle时,@explain 报错问题;使用自增主键,代码中获取不到插入id问题 #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3979,7 +3979,7 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
}
return "DELETE FROM " + tablePath + config.getWhereString(true) + (config.isMySQL() ? config.getLimitString() : ""); // PostgreSQL 不允许 LIMIT
default:
String explain = (config.isExplain() ? (config.isSQLServer() || config.isOracle() ? "SET STATISTICS PROFILE ON " : "EXPLAIN ") : "");
String explain = config.isExplain() ? (config.isSQLServer() ? "SET STATISTICS PROFILE ON " : (config.isOracle() ? "EXPLAIN PLAN FOR " : "EXPLAIN ")) : "";
if (config.isTest() && RequestMethod.isGetMethod(config.getMethod(), true)) { // FIXME 为啥是 code 而不是 count ?
String q = config.getQuote(); // 生成 SELECT ( (24 >=0 AND 24 <3) ) AS `code` LIMIT 1 OFFSET 0
return explain + "SELECT " + config.getWhereString(false) + " AS " + q + JSONResponse.KEY_COUNT + q + config.getLimitString();
Expand Down
10 changes: 8 additions & 2 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,13 @@ public PreparedStatement getStatement(@NotNull SQLConfig config, String sql) thr

PreparedStatement statement; //创建Statement对象
if (config.getMethod() == RequestMethod.POST && config.getId() == null) { //自增id
statement = getConnection(config).prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
if (config.isOracle()) {
// 解决 oracle 使用自增主键 插入获取不到id问题
String[] generatedColumns = {config.getIdKey()};
statement = getConnection(config).prepareStatement(sql, generatedColumns);
} else {
statement = getConnection(config).prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
}
}
else if (RequestMethod.isGetMethod(config.getMethod(), true)) {
statement = getConnection(config).prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
Expand Down Expand Up @@ -1250,7 +1256,7 @@ public int executeUpdate(@NotNull SQLConfig config, String sql) throws Exception
if (config.getId() == null && config.getMethod() == RequestMethod.POST) { // 自增id
ResultSet rs = stt.getGeneratedKeys();
if (rs != null && rs.next()) {
config.setId(rs.getLong(1)); //返回插入的主键id FIXME Oracle 拿不到
config.setId(rs.getLong(1));
}
}

Expand Down