Browse Source

DbPro.getGeneratedKey() 重构至 Dialect 中

James 8 years ago
parent
commit
211d5a71a6

+ 4 - 16
src/main/java/com/jfinal/plugin/activerecord/DbPro.java

@@ -580,31 +580,19 @@ public class DbPro {
 		config.dialect.forDbSave(tableName, pKeys, record, sql, paras);
 		
 		PreparedStatement pst;
-		if (config.dialect.isOracle())
+		if (config.dialect.isOracle()) {
 			pst = conn.prepareStatement(sql.toString(), pKeys);
-		else
+		} else {
 			pst = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
-			
+		}
 		config.dialect.fillStatement(pst, paras);
 		int result = pst.executeUpdate();
-		getGeneratedKey(pst, record, pKeys);
+		config.dialect.getRecordGeneratedKey(pst, record, pKeys);
 		DbKit.close(pst);
 		return result >= 1;
 	}
 	
 	/**
-	 * Get id after save record.
-	 */
-	private void getGeneratedKey(PreparedStatement pst, Record record, String[] pKeys) throws SQLException {
-		ResultSet rs = pst.getGeneratedKeys();
-		for (String pKey : pKeys)
-			if (record.get(pKey) == null || config.dialect.isOracle())
-				if (rs.next())
-					record.set(pKey, rs.getObject(1));
-		rs.close();
-	}
-	
-	/**
 	 * Save record.
 	 * <pre>
 	 * Example:

+ 15 - 0
src/main/java/com/jfinal/plugin/activerecord/dialect/Dialect.java

@@ -99,6 +99,21 @@ public abstract class Dialect {
 		rs.close();
 	}
 	
+	/**
+	 * Get id after save record.
+	 */
+	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(1));
+				}
+			}
+		}
+		rs.close();
+	}
+	
 	public boolean isOracle() {
 		return false;
 	}