1818import java .sql .Blob ;
1919import java .sql .Clob ;
2020import java .sql .Connection ;
21+ import java .sql .Date ;
2122import java .sql .DriverManager ;
2223import java .sql .PreparedStatement ;
2324import java .sql .ResultSet ;
2425import java .sql .ResultSetMetaData ;
2526import java .sql .SQLException ;
2627import java .sql .Savepoint ;
2728import java .sql .Statement ;
29+ import java .sql .Time ;
2830import java .sql .Timestamp ;
2931import java .util .ArrayList ;
3032import java .util .Collection ;
@@ -198,7 +200,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
198200 case HEAD :
199201 case HEADS :
200202 rs = executeQuery (config );
201-
203+
202204 executedSQLCount ++;
203205
204206 result = rs .next () ? AbstractParser .newSuccessResult ()
@@ -239,7 +241,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
239241 }
240242
241243 rs = executeQuery (config );
242-
244+
243245 if (config .isExplain () == false ) { //只有 SELECT 才能 EXPLAIN
244246 executedSQLCount ++;
245247 }
@@ -515,7 +517,7 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
515517 }
516518 }
517519
518- finalTable .put (lable , getValue (config , rs , rsmd , tablePosition , table , columnIndex , childMap ));
520+ finalTable .put (lable , getValue (config , rs , rsmd , tablePosition , table , columnIndex , lable , childMap ));
519521
520522 return table ;
521523 }
@@ -538,7 +540,7 @@ protected List<JSONObject> onPutTable(@NotNull SQLConfig config, @NotNull Result
538540
539541
540542 protected Object getValue (@ NotNull SQLConfig config , @ NotNull ResultSet rs , @ NotNull ResultSetMetaData rsmd
541- , final int tablePosition , @ NotNull JSONObject table , final int columnIndex , Map <String , JSONObject > childMap ) throws Exception {
543+ , final int tablePosition , @ NotNull JSONObject table , final int columnIndex , String lable , Map <String , JSONObject > childMap ) throws Exception {
542544
543545 Object value = rs .getObject (columnIndex );
544546 // Log.d(TAG, "name:" + rsmd.getColumnName(i));
@@ -549,18 +551,31 @@ protected Object getValue(@NotNull SQLConfig config, @NotNull ResultSet rs, @Not
549551 // Log.i(TAG, "select while (rs.next()) { >> for (int i = 0; i < length; i++) {"
550552 // + "\n >>> value = " + value);
551553
554+ boolean castToJson = false ;
555+
552556 //数据库查出来的null和empty值都有意义,去掉会导致 Moment:{ @column:"content" } 部分无结果及中断数组查询!
553- if (value instanceof Timestamp ) {
557+ if (value instanceof Boolean || value instanceof Number ) {
558+ //加快判断速度
559+ }
560+ else if (value instanceof Timestamp ) {
554561 value = ((Timestamp ) value ).toString ();
555562 }
556- else if (value instanceof String && isJSONType (rsmd , columnIndex )) { //json String
557- value = JSON .parse ((String ) value );
563+ else if (value instanceof Date ) {
564+ value = ((Date ) value ).toString ();
565+ }
566+ else if (value instanceof Time ) {
567+ value = ((Time ) value ).toString ();
568+ }
569+ else if (value instanceof String && isJSONType (config , rsmd , columnIndex , lable )) { //json String
570+ castToJson = true ;
558571 }
559572 else if (value instanceof Blob ) { //FIXME 存的是 abcde,取出来直接就是 [97, 98, 99, 100, 101] 这种 byte[] 类型,没有经过以下处理,但最终序列化后又变成了字符串 YWJjZGU=
573+ castToJson = true ;
560574 value = new String (((Blob ) value ).getBytes (1 , (int ) ((Blob ) value ).length ()), "UTF-8" );
561575 }
562- else if (value instanceof Clob ) {
563-
576+ else if (value instanceof Clob ) { //SQL Server TEXT 类型 居然走这个
577+ castToJson = true ;
578+
564579 StringBuffer sb = new StringBuffer ();
565580 BufferedReader br = new BufferedReader (((Clob ) value ).getCharacterStream ());
566581 String s = br .readLine ();
@@ -571,24 +586,44 @@ else if (value instanceof Clob) {
571586 value = sb .toString ();
572587 }
573588
589+ if (castToJson == false ) {
590+ List <String > json = config .getJson ();
591+ castToJson = json != null && json .contains (lable );
592+ }
593+ if (castToJson ) {
594+ try {
595+ value = JSON .parse ((String ) value );
596+ } catch (Exception e ) {
597+ Log .e (TAG , "getValue try { value = JSON.parse((String) value); } catch (Exception e) { \n " + e .getMessage ());
598+ }
599+ }
600+
574601 return value ;
575602 }
576603
577604
578605 /**判断是否为JSON类型
606+ * @param config
607+ * @param lable
579608 * @param rsmd
580609 * @param position
581610 * @return
582611 */
583612 @ Override
584- public boolean isJSONType (ResultSetMetaData rsmd , int position ) {
613+ public boolean isJSONType (@ NotNull SQLConfig config , ResultSetMetaData rsmd , int position , String lable ) {
585614 try {
615+ String column = rsmd .getColumnTypeName (position );
586616 //TODO CHAR和JSON类型的字段,getColumnType返回值都是1 ,如果不用CHAR,改用VARCHAR,则可以用上面这行来提高性能。
587617 //return rsmd.getColumnType(position) == 1;
588- return rsmd .getColumnTypeName (position ).toLowerCase ().contains ("json" );
618+
619+ if (column .toLowerCase ().contains ("json" )) {
620+ return true ;
621+ }
589622 } catch (SQLException e ) {
590623 e .printStackTrace ();
591624 }
625+ // List<String> json = config.getJson();
626+ // return json != null && json.contains(lable);
592627 return false ;
593628 }
594629
@@ -645,7 +680,7 @@ public Connection getConnection(@NotNull SQLConfig config) throws Exception {
645680 v = 1 ;
646681 Log .e (TAG , "getStatement try { String[] vs = config.getDBVersion().split([.]); ... >> } catch (Exception e) {\n " + e .getMessage ());
647682 }
648-
683+
649684 if (v >= 8 ) {
650685 connection = DriverManager .getConnection (config .getDBUri () + "?userSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&user="
651686 + config .getDBAccount () + "&password=" + config .getDBPassword ());
@@ -658,7 +693,7 @@ public Connection getConnection(@NotNull SQLConfig config) throws Exception {
658693 else { //PostgreSQL 不允许 cross-database
659694 connection = DriverManager .getConnection (config .getDBUri (), config .getDBAccount (), config .getDBPassword ());
660695 }
661-
696+
662697 connectionMap .put (config .getDatabase (), connection );
663698 }
664699
0 commit comments