Переглянути джерело

修复在调用`save`时,`Oracle`数据库可能出现空指针的BUG

mxd 4 роки тому
батько
коміт
b0cfa5ff64

+ 5 - 1
magic-api/src/main/java/org/ssssssss/magicapi/modules/SQLModule.java

@@ -367,7 +367,11 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
 	 */
 	@Comment("查询int值,适合单行单列int的结果")
 	public Integer selectInt(@Comment("`SQL`语句") String sql) {
-		BoundSql boundSql = new BoundSql(sql, this);
+		return selectInt(new BoundSql(sql, this));
+	}
+
+	@UnableCall
+	public Integer selectInt(BoundSql boundSql){
 		return boundSql.getCacheValue(this.sqlInterceptors, () -> dataSourceNode.getJdbcTemplate().query(boundSql.getSql(),new SingleRowResultSetExtractor<>(Integer.class), boundSql.getParameters()));
 	}
 

+ 6 - 7
magic-api/src/main/java/org/ssssssss/magicapi/modules/table/NamedTable.java

@@ -157,7 +157,7 @@ public class NamedTable {
 
 	@Comment("保存到表中,当主键有值时则修改,否则插入")
 	public Object save() {
-		return this.save(null,false);
+		return this.save(null, false);
 	}
 
 	@Comment("保存到表中,当主键有值时则修改,否则插入")
@@ -166,16 +166,15 @@ public class NamedTable {
 			throw new MagicAPIException("请设置主键");
 		}
 		String primaryValue = Objects.toString(this.columns.get(this.primary), "");
-		if(StringUtils.isBlank(primaryValue) && data != null){
+		if (StringUtils.isBlank(primaryValue) && data != null) {
 			primaryValue = Objects.toString(data.get(this.primary), "");
 		}
-		if(beforeQuery){
-			if(StringUtils.isNotBlank(primaryValue)){
+		if (beforeQuery) {
+			if (StringUtils.isNotBlank(primaryValue)) {
 				List<Object> params = new ArrayList<>();
 				params.add(primaryValue);
-				Map<String, Object> oneMap = sqlModule.selectOne(new BoundSql("select count(1) count from " + this.tableName + " where " + this.primary + " = ?", params, sqlModule));
-				Integer count = new Integer(oneMap.get("count").toString());
-				if(count == 0){
+				Integer count = sqlModule.selectInt(new BoundSql("select count(1) count from " + this.tableName + " where " + this.primary + " = ?", params, sqlModule));
+				if (count == 0) {
 					return insert(data);
 				}
 				return update(data);