ソースを参照

Merge branch 'master' into jfinal-java8

James 8 年 前
コミット
3d7426af2f

+ 19 - 26
src/main/java/com/jfinal/plugin/activerecord/DbPro.java

@@ -174,35 +174,39 @@ public class DbPro {
 	}
 	
 	public Integer queryInt(String sql, Object... paras) {
-		return (Integer)queryColumn(sql, paras);
+		Number n = queryNumber(sql, paras);
+		return n != null ? n.intValue() : null;
 	}
 	
 	public Integer queryInt(String sql) {
-		return (Integer)queryColumn(sql, NULL_PARA_ARRAY);
+		return queryInt(sql, NULL_PARA_ARRAY);
 	}
 	
 	public Long queryLong(String sql, Object... paras) {
-		return (Long)queryColumn(sql, paras);
+		Number n = queryNumber(sql, paras);
+		return n != null ? n.longValue() : null;
 	}
 	
 	public Long queryLong(String sql) {
-		return (Long)queryColumn(sql, NULL_PARA_ARRAY);
+		return queryLong(sql, NULL_PARA_ARRAY);
 	}
 	
 	public Double queryDouble(String sql, Object... paras) {
-		return (Double)queryColumn(sql, paras);
+		Number n = queryNumber(sql, paras);
+		return n != null ? n.doubleValue() : null;
 	}
 	
 	public Double queryDouble(String sql) {
-		return (Double)queryColumn(sql, NULL_PARA_ARRAY);
+		return queryDouble(sql, NULL_PARA_ARRAY);
 	}
 	
 	public Float queryFloat(String sql, Object... paras) {
-		return (Float)queryColumn(sql, paras);
+		Number n = queryNumber(sql, paras);
+		return n != null ? n.floatValue() : null;
 	}
 	
 	public Float queryFloat(String sql) {
-		return (Float)queryColumn(sql, NULL_PARA_ARRAY);
+		return queryFloat(sql, NULL_PARA_ARRAY);
 	}
 	
 	public java.math.BigDecimal queryBigDecimal(String sql, Object... paras) {
@@ -254,11 +258,12 @@ public class DbPro {
 	}
 	
 	public Short queryShort(String sql, Object... paras) {
-		return (Short)queryColumn(sql, paras);
+		Number n = queryNumber(sql, paras);
+		return n != null ? n.shortValue() : null;
 	}
 	
 	public Short queryShort(String sql) {
-		return (Short)queryColumn(sql, NULL_PARA_ARRAY);
+		return queryShort(sql, NULL_PARA_ARRAY);
 	}
 	
 	public Number queryNumber(String sql, Object... paras) {
@@ -580,31 +585,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:

+ 17 - 31
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;
@@ -203,14 +202,16 @@ public abstract class Model<M extends Model> implements Serializable {
 	 * Get attribute of mysql type: int, integer, tinyint(n) n > 1, smallint, mediumint
 	 */
 	public Integer getInt(String attr) {
-		return (Integer)attrs.get(attr);
+		Number n = (Number)attrs.get(attr);
+		return n != null ? n.intValue() : null;
 	}
 	
 	/**
 	 * Get attribute of mysql type: bigint, unsign int
 	 */
 	public Long getLong(String attr) {
-		return (Long)attrs.get(attr);
+		Number n = (Number)attrs.get(attr);
+		return n != null ? n.longValue() : null;
 	}
 	
 	/**
@@ -245,14 +246,21 @@ public abstract class Model<M extends Model> implements Serializable {
 	 * Get attribute of mysql type: real, double
 	 */
 	public Double getDouble(String attr) {
-		return (Double)attrs.get(attr);
+		Number n = (Number)attrs.get(attr);
+		return n != null ? n.doubleValue() : null;
 	}
 	
 	/**
 	 * Get attribute of mysql type: float
 	 */
 	public Float getFloat(String attr) {
-		return (Float)attrs.get(attr);
+		Number n = (Number)attrs.get(attr);
+		return n != null ? n.floatValue() : null;
+	}
+	
+	public Short getShort(String attr) {
+		Number n = (Number)attrs.get(attr);
+		return n != null ? n.shortValue() : null;
 	}
 	
 	/**
@@ -428,14 +436,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);
 			getModifyFlag().clear();
 			return result >= 1;
 		} catch (Exception e) {
@@ -446,28 +454,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() {

+ 13 - 4
src/main/java/com/jfinal/plugin/activerecord/Record.java

@@ -215,14 +215,16 @@ public class Record implements Serializable {
 	 * Get column of mysql type: int, integer, tinyint(n) n > 1, smallint, mediumint
 	 */
 	public Integer getInt(String column) {
-		return (Integer)getColumns().get(column);
+		Number n = getNumber(column);
+		return n != null ? n.intValue() : null;
 	}
 	
 	/**
 	 * Get column of mysql type: bigint
 	 */
 	public Long getLong(String column) {
-		return (Long)getColumns().get(column);
+		Number n = getNumber(column);
+		return n != null ? n.longValue() : null;
 	}
 	
 	/**
@@ -257,14 +259,21 @@ public class Record implements Serializable {
 	 * Get column of mysql type: real, double
 	 */
 	public Double getDouble(String column) {
-		return (Double)getColumns().get(column);
+		Number n = getNumber(column);
+		return n != null ? n.doubleValue() : null;
 	}
 	
 	/**
 	 * Get column of mysql type: float
 	 */
 	public Float getFloat(String column) {
-		return (Float)getColumns().get(column);
+		Number n = getNumber(column);
+		return n != null ? n.floatValue() : null;
+	}
+	
+	public Short getShort(String column) {
+		Number n = getNumber(column);
+		return n != null ? n.shortValue() : null;
 	}
 	
 	/**

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

@@ -76,6 +76,44 @@ public abstract class Dialect {
 		return RecordBuilder.me.build(config, rs);
 	}
 	
+	/**
+	 * Get id after save method.
+	 */
+	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(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();
+	}
+	
+	/**
+	 * 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;
 	}