|
|
@@ -17,11 +17,13 @@
|
|
|
package com.jfinal.plugin.activerecord.dialect;
|
|
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Map.Entry;
|
|
|
import java.util.Set;
|
|
|
+import com.jfinal.plugin.activerecord.Model;
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
import com.jfinal.plugin.activerecord.Table;
|
|
|
|
|
|
@@ -206,4 +208,48 @@ public class PostgreSqlDialect extends Dialect {
|
|
|
public void fillStatement(PreparedStatement pst, Object... paras) throws SQLException {
|
|
|
fillStatementHandleDateType(pst, paras);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解决 PostgreSql 获取自增主键时 rs.getObject(1) 总是返回第一个字段的值,而非返回了 id 值
|
|
|
+ * issue: https://www.oschina.net/question/2312705_2243354
|
|
|
+ *
|
|
|
+ * 将 rs.getXxx(1) 改成了 rs.getXxx(pKey)
|
|
|
+ */
|
|
|
+ public void getModelGeneratedKey(Model<?> model, PreparedStatement pst, Table table) throws SQLException {
|
|
|
+ String[] pKeys = table.getPrimaryKey();
|
|
|
+ ResultSet rs = pst.getGeneratedKeys();
|
|
|
+ for (String pKey : pKeys) {
|
|
|
+ if (model.get(pKey) == null || isOracle()) {
|
|
|
+ if (rs.next()) {
|
|
|
+ Class<?> colType = table.getColumnType(pKey);
|
|
|
+ if (colType == Integer.class || colType == int.class) {
|
|
|
+ model.set(pKey, rs.getInt(pKey));
|
|
|
+ } else if (colType == Long.class || colType == long.class) {
|
|
|
+ model.set(pKey, rs.getLong(pKey));
|
|
|
+ } else {
|
|
|
+ model.set(pKey, rs.getObject(pKey));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rs.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解决 PostgreSql 获取自增主键时 rs.getObject(1) 总是返回第一个字段的值,而非返回了 id 值
|
|
|
+ * issue: https://www.oschina.net/question/2312705_2243354
|
|
|
+ *
|
|
|
+ * 将 rs.getXxx(1) 改成了 rs.getXxx(pKey)
|
|
|
+ */
|
|
|
+ public void getRecordGeneratedKey(PreparedStatement pst, Record record, String[] pKeys) throws SQLException {
|
|
|
+ ResultSet rs = pst.getGeneratedKeys();
|
|
|
+ for (String pKey : pKeys) {
|
|
|
+ if (record.get(pKey) == null || isOracle()) {
|
|
|
+ if (rs.next()) {
|
|
|
+ record.set(pKey, rs.getObject(pKey));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rs.close();
|
|
|
+ }
|
|
|
}
|