ソースを参照

Model.getGeneratedKey() 转移至 Dialect,便于用户自行控制

James 8 年 前
コミット
04f7b19395

+ 4 - 27
src/main/java/com/jfinal/plugin/activerecord/Model.java

@@ -20,7 +20,6 @@ import java.io.Serializable;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -428,14 +427,14 @@ public abstract class Model<M extends Model> implements Serializable {
 		int result = 0;
 		try {
 			conn = config.getConnection();
-			if (config.dialect.isOracle())
+			if (config.dialect.isOracle()) {
 				pst = conn.prepareStatement(sql.toString(), table.getPrimaryKey());
-			else
+			} else {
 				pst = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
-			
+			}
 			config.dialect.fillStatement(pst, paras);
 			result = pst.executeUpdate();
-			getGeneratedKey(pst, table, config);
+			config.dialect.getModelGeneratedKey(this, pst, table, config);
 			getModifyFlag().clear();
 			return result >= 1;
 		} catch (Exception e) {
@@ -446,28 +445,6 @@ public abstract class Model<M extends Model> implements Serializable {
 	}
 	
 	/**
-	 * Get id after save method.
-	 */
-	private void getGeneratedKey(PreparedStatement pst, Table table, Config config) throws SQLException {
-		String[] pKeys = table.getPrimaryKey();
-		ResultSet rs = pst.getGeneratedKeys();
-		for (String pKey : pKeys) {
-			if (get(pKey) == null || config.dialect.isOracle()) {
-				if (rs.next()) {
-					Class colType = table.getColumnType(pKey);
-					if (colType == Integer.class || colType == int.class)
-						set(pKey, rs.getInt(1));
-					else if (colType == Long.class || colType == long.class)
-						set(pKey, rs.getLong(1));
-					else
-						set(pKey, rs.getObject(1));		// It returns Long object for int colType
-				}
-			}
-		}
-		rs.close();
-	}
-	
-	/**
 	 * Delete model.
 	 */
 	public boolean delete() {

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

@@ -75,6 +75,29 @@ public abstract class Dialect {
 	public List<Record> buildRecordList(Config config, ResultSet rs) throws SQLException {
 		return RecordBuilder.me.build(config, rs);
 	}
+
+	/**
+	 * Get id after save method.
+	 */
+	public void getModelGeneratedKey(Model<?> model, PreparedStatement pst, Table table, Config config) throws SQLException {
+		String[] pKeys = table.getPrimaryKey();
+		ResultSet rs = pst.getGeneratedKeys();
+		for (String pKey : pKeys) {
+			if (model.get(pKey) == null || config.getDialect().isOracle()) {
+				if (rs.next()) {
+					Class<?> colType = table.getColumnType(pKey);
+					if (colType == Integer.class || colType == int.class) {
+						model.set(pKey, rs.getInt(1));
+					} else if (colType == Long.class || colType == long.class) {
+						model.set(pKey, rs.getLong(1));
+					} else {
+						model.set(pKey, rs.getObject(1));		// It returns Long object for int colType
+					}
+				}
+			}
+		}
+		rs.close();
+	}
 	
 	public boolean isOracle() {
 		return false;