Skip to content

Commit af280db

Browse files
committed
存储过程:新增支持单独指定数据库名/模式名 schema
1 parent edd8570 commit af280db

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
193193

194194
FunctionBean fb = parseFunction(function, currentObject, false, containRaw);
195195

196-
JSONObject row = FUNCTION_MAP.get(fb.getMethod());
196+
JSONObject row = FUNCTION_MAP.get(fb.getMethod()); //FIXME fb.getSchema() + "." + fb.getMethod()
197197
if (row == null) {
198198
throw new UnsupportedOperationException("不允许调用远程函数 " + fb.getMethod() + " !");
199199
}
@@ -224,7 +224,8 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
224224

225225
try {
226226
return invoke(parser, fb.getMethod(), fb.getTypes(), fb.getValues(), row.getString("returnType"), currentObject, type);
227-
} catch (Exception e) {
227+
}
228+
catch (Exception e) {
228229
if (e instanceof NoSuchMethodException) {
229230
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 不在后端工程的DemoFunction内!"
230231
+ "\n请检查函数名和参数数量是否与已定义的函数一致!"
@@ -445,14 +446,28 @@ public static FunctionBean parseFunction(@NotNull String function, @NotNull JSON
445446
int start = function.indexOf("(");
446447
int end = function.lastIndexOf(")");
447448
String method = (start <= 0 || end != function.length() - 1) ? null : function.substring(0, start);
448-
if (StringUtil.isEmpty(method, true)) {
449-
throw new IllegalArgumentException("字符 " + function + " 不合法!函数的名称 function 不能为空,"
450-
+ "且必须为 function(key0,key1,...) 这种单函数格式!"
449+
450+
int dotInd = method == null ? -1 : method.indexOf(".");
451+
String schema = dotInd < 0 ? null : method.substring(0, dotInd);
452+
method = dotInd < 0 ? method : method.substring(dotInd + 1);
453+
454+
if (StringUtil.isName(method) == false) {
455+
throw new IllegalArgumentException("字符 " + method + " 不合法!函数的名称 function 不能为空且必须符合方法命名规范!"
456+
+ "总体必须为 function(key0,key1,...) 这种单函数格式!"
451457
+ "\nfunction必须符合 " + (isSQLFunction ? "SQL 函数/SQL 存储过程" : "Java 函数") + " 命名,key 是用于在 request 内取值的键!");
452458
}
459+
if (isSQLFunction != true && StringUtil.isNotEmpty(schema, true)) {
460+
throw new IllegalArgumentException("字符 " + schema + " 不合法!远程函数不允许指定类名!"
461+
+ "且必须为 function(key0,key1,...) 这种单函数格式!"
462+
+ "\nfunction必须符合 " + (isSQLFunction ? "SQL 函数/SQL 存储过程" : "Java 函数") + " 命名,key 是用于在 request 内取值的键!");
463+
}
464+
if (schema != null && StringUtil.isName(schema) == false) {
465+
throw new IllegalArgumentException("字符 " + schema + " 不合法!数据库名/模式名 不能为空且必须符合命名规范!"
466+
+ "且必须为 function(key0,key1,...) 这种单函数格式!"
467+
+ "\nfunction必须符合 " + (isSQLFunction ? "SQL 函数/SQL 存储过程" : "Java 函数") + " 命名,key 是用于在 request 内取值的键!");
468+
}
453469

454470
String[] keys = StringUtil.split(function.substring(start + 1, end));
455-
456471
int length = keys == null ? 0 : keys.length;
457472

458473
Class<?>[] types;
@@ -515,6 +530,7 @@ else if (v instanceof Collection) { // 泛型兼容? // JSONArray
515530

516531
FunctionBean fb = new FunctionBean();
517532
fb.setFunction(function);
533+
fb.setSchema(schema);
518534
fb.setMethod(method);
519535
fb.setKeys(keys);
520536
fb.setTypes(types);
@@ -551,33 +567,50 @@ public static <T> T getArgValue(@NotNull JSONObject currentObject, String keyOrV
551567
return null;
552568
}
553569

554-
if (keyOrValue.endsWith("`") && keyOrValue.lastIndexOf("`") == 0) {
570+
571+
if (keyOrValue.endsWith("`") && keyOrValue.substring(1).indexOf("`") == keyOrValue.length() - 2) {
555572
return (T) currentObject.get(keyOrValue.substring(1, keyOrValue.length() - 1));
556573
}
557574

558-
if (keyOrValue.endsWith("'") && keyOrValue.lastIndexOf("'") == 0) {
575+
if (keyOrValue.endsWith("'") && keyOrValue.substring(1).indexOf("'") == keyOrValue.length() - 2) {
559576
return (T) keyOrValue.substring(1, keyOrValue.length() - 1);
560577
}
561578

562-
if (StringUtil.isName(keyOrValue.startsWith("@") ? keyOrValue.substring(1) : keyOrValue)) {
563-
return (T) currentObject.get(keyOrValue);
564-
}
565-
566579
// 传参加上 @raw:"key()" 避免意外情况
567580
Object val = containRaw ? AbstractSQLConfig.RAW_MAP.get(keyOrValue) : null;
568581
if (val != null) {
569582
return (T) ("".equals(val) ? keyOrValue : val);
570583
}
571584

585+
if (StringUtil.isName(keyOrValue.startsWith("@") ? keyOrValue.substring(1) : keyOrValue)) {
586+
return (T) currentObject.get(keyOrValue);
587+
}
588+
589+
if ("true".equals(keyOrValue)) {
590+
return (T) Boolean.TRUE;
591+
}
592+
if ("false".equals(keyOrValue)) {
593+
return (T) Boolean.FALSE;
594+
}
595+
596+
// 性能更好,但居然非法格式也不报错
597+
//try {
598+
// val = Boolean.valueOf(keyOrValue); // JSON.parse(keyOrValue);
599+
// return (T) val;
600+
//}
601+
//catch (Throwable e) {
602+
// Log.d(TAG, "getArgValue try {\n" +
603+
// " val = Boolean.valueOf(keyOrValue);" +
604+
// "} catch (Throwable e) = " + e.getMessage());
605+
//}
606+
572607
try {
573-
val = JSON.parse(keyOrValue);
574-
if (apijson.JSON.isBooleanOrNumberOrString(val)) {
575-
return (T) val;
576-
}
608+
val = Double.valueOf(keyOrValue); // JSON.parse(keyOrValue);
609+
return (T) val;
577610
}
578611
catch (Throwable e) {
579612
Log.d(TAG, "getArgValue try {\n" +
580-
" Object val = JSON.parse(keyOrValue);" +
613+
" val = Double.valueOf(keyOrValue);" +
581614
"} catch (Throwable e) = " + e.getMessage());
582615
}
583616

@@ -586,6 +619,7 @@ public static <T> T getArgValue(@NotNull JSONObject currentObject, String keyOrV
586619

587620
public static class FunctionBean {
588621
private String function;
622+
private String schema;
589623
private String method;
590624
private String[] keys;
591625
private Class<?>[] types;
@@ -598,7 +632,14 @@ public void setFunction(String function) {
598632
this.function = function;
599633
}
600634

601-
public String getMethod() {
635+
public String getSchema() {
636+
return schema;
637+
}
638+
public void setSchema(String schema) {
639+
this.schema = schema;
640+
}
641+
642+
public String getMethod() {
602643
return method;
603644
}
604645
public void setMethod(String method) {
@@ -640,6 +681,8 @@ public String toFunctionCallString(boolean useValue) {
640681
* @return
641682
*/
642683
public String toFunctionCallString(boolean useValue, String quote) {
684+
//String sch = getSchema();
685+
//String s = (StringUtil.isEmpty(sch) ? "" : sch + ".") + getMethod() + "(";
643686
String s = getMethod() + "(";
644687

645688
Object[] args = useValue ? getValues() : getKeys();

APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, SQLC
105105
request.remove(KEY_DROP);
106106
}
107107

108-
String raw = request.getString(JSONRequest.KEY_RAW);
109-
String[] rks = StringUtil.split(raw);
110-
rawKeyList = rks == null || rks.length <= 0 ? null : Arrays.asList(rks);
111-
}
108+
if (isTable) {
109+
String raw = request.getString(JSONRequest.KEY_RAW);
110+
String[] rks = StringUtil.split(raw);
111+
rawKeyList = rks == null || rks.length <= 0 ? null : Arrays.asList(rks);
112+
}
113+
}
112114

113115
@Override
114116
public String getParentPath() {
@@ -818,6 +820,10 @@ public void parseFunction(String rawKey, String key, String value, String parent
818820
FunctionBean fb = AbstractFunctionParser.parseFunction(value, currentObject, true, containRaw);
819821

820822
SQLConfig config = newSQLConfig(true);
823+
String sch = fb.getSchema();
824+
if (StringUtil.isNotEmpty(sch, true)) {
825+
config.setSchema(sch);
826+
}
821827
config.setProcedure(fb.toFunctionCallString(true));
822828
result = parseResponse(config, true);
823829

0 commit comments

Comments
 (0)