Skip to content

Commit a0a0fa1

Browse files
committed
Server: AbstractSQLExecutor新增getValue方法支持子类扩展处理value;DemoSQLExecutor新增支持Blob和Clob类型数据的处理;
1 parent 30c4dda commit a0a0fa1

10 files changed

Lines changed: 97 additions & 35 deletions

File tree

APIJSON-Java-Server/APIJSONDemo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>apijson.demo.server</groupId>
77
<artifactId>apijson-demo</artifactId>
8-
<version>3.0.0</version>
8+
<version>3.3.0</version>
99
<packaging>jar</packaging>
1010

1111
<name>APIJSONDemo</name>

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoSQLExecutor.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
package apijson.demo.server;
1616

17+
import java.io.BufferedReader;
18+
import java.sql.Blob;
19+
import java.sql.Clob;
1720
import java.sql.Connection;
1821
import java.sql.DriverManager;
1922
import java.sql.PreparedStatement;
2023
import java.sql.ResultSet;
24+
import java.sql.ResultSetMetaData;
2125
import java.sql.SQLException;
2226
import java.util.Collection;
2327
import java.util.HashMap;
@@ -26,6 +30,8 @@
2630

2731
import javax.validation.constraints.NotNull;
2832

33+
import com.alibaba.fastjson.JSONObject;
34+
2935
import zuo.biao.apijson.Log;
3036
import zuo.biao.apijson.server.AbstractSQLExecutor;
3137
import zuo.biao.apijson.server.SQLConfig;
@@ -111,6 +117,28 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
111117
return statement;
112118
}
113119

120+
@Override
121+
protected Object getValue(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition,
122+
JSONObject table, int columnIndex, Map<String, JSONObject> childMap) throws Exception {
123+
Object value = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, childMap);
124+
125+
if (value instanceof Blob) { //FIXME 存的是 abcde,取出来直接就是 [97, 98, 99, 100, 101] 这种 byte[] 类型,没有经过以下处理,但最终序列化后又变成了字符串 YWJjZGU=
126+
value = new String(((Blob) value).getBytes(1, (int) ((Blob) value).length()), "UTF-8");
127+
}
128+
else if (value instanceof Clob) {
129+
130+
StringBuffer sb = new StringBuffer();
131+
BufferedReader br = new BufferedReader(((Clob) value).getCharacterStream());
132+
String s = br.readLine();
133+
while (s != null) {
134+
sb.append(s);
135+
s = br.readLine();
136+
}
137+
value = sb.toString();
138+
}
139+
140+
return value;
141+
}
114142

115143
/**关闭连接,释放资源
116144
*/

APIJSON-Java-Server/APIJSONDemo_oracle/apijson-demo.iml renamed to APIJSON-Java-Server/APIJSONDemo_oracle/apijson-demo-oracle.iml

File renamed without changes.
Binary file not shown.

APIJSON-Java-Server/APIJSONDemo_oracle/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>apijson.demo.server</groupId>
7-
<artifactId>apijson-demo</artifactId>
8-
<version>2.1.0-SNAPSHOT</version>
7+
<artifactId>apijson-demo-oracle</artifactId>
8+
<version>3.3.0</version>
99
<packaging>jar</packaging>
1010

11-
<name>APIJSONDemo</name>
11+
<name>APIJSONDemoOracle</name>
1212
<description>Demo project for APIJSON Server</description>
1313

1414
<parent>

APIJSON-Java-Server/APIJSONDemo_oracle/src/main/java/apijson/demo/server/APIJSONApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class APIJSONApplication {
1616

1717
public static void main(String[] args) throws Exception {
1818
SpringApplication.run(APIJSONApplication.class, args);
19+
1920
System.out.println("\n\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON >>>>>>>>>>>>>>>>>>>>>>>>\n");
2021
System.out.println("开始测试:远程函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
2122
System.out.println("\n完成测试:远程函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

APIJSON-Java-Server/APIJSONDemo_oracle/src/main/java/apijson/demo/server/DemoSQLExecutor.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import zuo.biao.apijson.server.SQLConfig;
77

88
import javax.validation.constraints.NotNull;
9+
10+
import com.alibaba.fastjson.JSONObject;
11+
import com.fasterxml.jackson.annotation.JsonFormat.Value;
12+
13+
import java.io.BufferedReader;
914
import java.sql.*;
1015
import java.util.Collection;
1116
import java.util.HashMap;
@@ -82,6 +87,29 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
8287
return statement;
8388
}
8489

90+
91+
@Override
92+
protected Object getValue(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition,
93+
JSONObject table, int columnIndex, Map<String, JSONObject> childMap) throws Exception {
94+
Object value = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, childMap);
95+
96+
if (value instanceof Blob) {
97+
value = new String(((Blob) value).getBytes(1, (int) ((Blob) value).length()), "UTF-8");
98+
}
99+
else if (value instanceof Clob) {
100+
101+
StringBuffer sb = new StringBuffer();
102+
BufferedReader br = new BufferedReader(((Clob) value).getCharacterStream());
103+
String s = br.readLine();
104+
while (s != null) {
105+
sb.append(s);
106+
s = br.readLine();
107+
}
108+
value = sb.toString();
109+
}
110+
111+
return value;
112+
}
85113

86114
/**关闭连接,释放资源
87115
*/

APIJSON-Java-Server/APIJSONLibrary/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>zuo.biao.apijson.server</groupId>
77
<artifactId>apijson-library</artifactId>
8-
<version>3.0.0</version>
8+
<version>3.3.0</version>
99
<packaging>jar</packaging>
1010

1111
<name>APIJSONLibrary</name>

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
* @author Lemon
6868
*/
6969
public abstract class AbstractSQLConfig implements SQLConfig {
70-
private static final String TAG = "SQLConfig";
70+
private static final String TAG = "AbstractSQLConfig";
7171

7272

7373
/**

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractSQLExecutor.java

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @author Lemon
3838
*/
3939
public abstract class AbstractSQLExecutor implements SQLExecutor {
40-
private static final String TAG = "SQLExecutor";
40+
private static final String TAG = "AbstractSQLExecutor";
4141

4242

4343
//访问一次后丢失,可能因为static导致内存共享,别的地方改变了内部对象的值
@@ -280,16 +280,16 @@ protected void executeAppJoin(SQLConfig config, Map<Integer, JSONObject> resultM
280280

281281
jc = j.getJoinConfig();
282282
cc = j.getCacheConfig(); //这里用config改了getSQL后再还原很麻烦,所以提前给一个config2更好
283-
283+
284284
//取出 "id@": "@/User/userId" 中所有 userId 的值
285285
List<Object> targetValueList = new ArrayList<>();
286286
JSONObject mainTable;
287287
Object targetValue;
288-
288+
289289
for (int i = 0; i < resultMap.size(); i++) {
290290
mainTable = resultMap.get(i);
291291
targetValue = mainTable == null ? null : mainTable.get(j.getTargetKey());
292-
292+
293293
if (targetValue != null && targetValueList.contains(targetValue) == false) {
294294
targetValueList.add(targetValue);
295295
}
@@ -301,21 +301,21 @@ protected void executeAppJoin(SQLConfig config, Map<Integer, JSONObject> resultM
301301
jc.putWhere(j.getKey() + "{}", targetValueList, false);
302302

303303
jc.setMain(true).setPreparedValueList(new ArrayList<>());
304-
304+
305305
boolean prepared = jc.isPrepared();
306306
final String sql = jc.getSQL(false);
307307
jc.setPrepared(prepared);
308308

309309
if (StringUtil.isEmpty(sql, true)) {
310310
throw new NullPointerException(TAG + ".executeAppJoin StringUtil.isEmpty(sql, true) >> return null;");
311311
}
312-
312+
313313
long startTime = System.currentTimeMillis();
314314
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
315315
+ "\n executeAppJoin startTime = " + startTime
316316
+ "\n sql = \n " + sql
317317
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
318-
318+
319319
//执行副表的批量查询 并 缓存到 childMap
320320
ResultSet rs = executeQuery(jc);
321321

@@ -341,7 +341,7 @@ protected void executeAppJoin(SQLConfig config, Map<Integer, JSONObject> resultM
341341

342342
Log.d(TAG, "\n executeAppJoin while (rs.next()) { resultMap.put( " + index + ", result); "
343343
+ "\n >>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n");
344-
344+
345345
//缓存到 childMap
346346
cc.putWhere(j.getKey(), result.get(j.getKey()), false);
347347
cacheSql = cc.getSQL(false);
@@ -351,12 +351,12 @@ protected void executeAppJoin(SQLConfig config, Map<Integer, JSONObject> resultM
351351
}
352352

353353
rs.close();
354-
354+
355355

356356
long endTime = System.currentTimeMillis();
357357
Log.d(TAG, "\n\n executeAppJoin endTime = " + endTime + "; duration = " + (endTime - startTime)
358358
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
359-
359+
360360
}
361361
}
362362

@@ -429,25 +429,7 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
429429
}
430430
}
431431

432-
Object value = rs.getObject(columnIndex);
433-
// Log.d(TAG, "name:" + rsmd.getColumnName(i));
434-
// Log.d(TAG, "lable:" + rsmd.getColumnLabel(i));
435-
// Log.d(TAG, "type:" + rsmd.getColumnType(i));
436-
// Log.d(TAG, "typeName:" + rsmd.getColumnTypeName(i));
437-
438-
// Log.i(TAG, "select while (rs.next()) { >> for (int i = 0; i < length; i++) {"
439-
// + "\n >>> value = " + value);
440-
441-
if (value != null) { //数据库查出来的null和empty值都有意义,去掉会导致 Moment:{ @column:"content" } 部分无结果及中断数组查询!
442-
if (value instanceof Timestamp) {
443-
value = ((Timestamp) value).toString();
444-
}
445-
else if (value instanceof String && isJSONType(rsmd, columnIndex)) { //json String
446-
value = JSON.parse((String) value);
447-
}
448-
}
449-
450-
finalTable.put(lable, value);
432+
finalTable.put(lable, getValue(config, rs, rsmd, tablePosition, table, columnIndex, childMap));
451433

452434
return table;
453435
}
@@ -469,6 +451,29 @@ protected Map<Integer, JSONObject> onPutTable(@NotNull SQLConfig config, @NotNul
469451
}
470452

471453

454+
protected Object getValue(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd
455+
, final int tablePosition, @NotNull JSONObject table, final int columnIndex, Map<String, JSONObject> childMap) throws Exception {
456+
457+
Object value = rs.getObject(columnIndex);
458+
// Log.d(TAG, "name:" + rsmd.getColumnName(i));
459+
// Log.d(TAG, "lable:" + rsmd.getColumnLabel(i));
460+
// Log.d(TAG, "type:" + rsmd.getColumnType(i));
461+
// Log.d(TAG, "typeName:" + rsmd.getColumnTypeName(i));
462+
463+
// Log.i(TAG, "select while (rs.next()) { >> for (int i = 0; i < length; i++) {"
464+
// + "\n >>> value = " + value);
465+
466+
//数据库查出来的null和empty值都有意义,去掉会导致 Moment:{ @column:"content" } 部分无结果及中断数组查询!
467+
if (value instanceof Timestamp) {
468+
value = ((Timestamp) value).toString();
469+
}
470+
else if (value instanceof String && isJSONType(rsmd, columnIndex)) { //json String
471+
value = JSON.parse((String) value);
472+
}
473+
474+
return value;
475+
}
476+
472477

473478
/**判断是否为JSON类型
474479
* @param rsmd

0 commit comments

Comments
 (0)