8
8
9
9
import tech .ydb .jdbc .YdbConst ;
10
10
import tech .ydb .jdbc .query .params .JdbcPrm ;
11
+ import tech .ydb .jdbc .settings .YdbQueryProperties ;
11
12
12
13
13
14
/**
17
18
public class YdbQueryParser {
18
19
private final boolean isDetectQueryType ;
19
20
private final boolean isDetectJdbcParameters ;
21
+ private final boolean isForceJdbcParamters ;
20
22
private final boolean isConvertJdbcInToList ;
21
23
22
24
private final List <QueryStatement > statements = new ArrayList <>();
@@ -26,10 +28,11 @@ public class YdbQueryParser {
26
28
27
29
private int jdbcPrmIndex = 0 ;
28
30
29
- public YdbQueryParser (String origin , boolean isDetectQueryType , boolean isDetectParameters , boolean isConvertIn ) {
30
- this .isDetectQueryType = isDetectQueryType ;
31
- this .isDetectJdbcParameters = isDetectParameters ;
32
- this .isConvertJdbcInToList = isConvertIn ;
31
+ public YdbQueryParser (String origin , YdbQueryProperties props ) {
32
+ this .isDetectQueryType = props .isDetectQueryType ();
33
+ this .isDetectJdbcParameters = props .isDetectJdbcParameters ();
34
+ this .isForceJdbcParamters = props .isForceJdbcParameters ();
35
+ this .isConvertJdbcInToList = props .isReplaceJdbcInByYqlList ();
33
36
this .origin = origin ;
34
37
this .parsed = new StringBuilder (origin .length () + 10 );
35
38
}
@@ -45,7 +48,7 @@ public YqlBatcher getYqlBatcher() {
45
48
public QueryType detectQueryType () throws SQLException {
46
49
QueryType type = null ;
47
50
for (QueryStatement st : statements ) {
48
- if (st .getType () == QueryType .UNKNOWN ) {
51
+ if (st .getType () == QueryType .UNKNOWN || st . getType () == QueryType . DECLARE ) {
49
52
continue ;
50
53
}
51
54
@@ -218,6 +221,12 @@ public String parseSQL() throws SQLException {
218
221
batcher .readIdentifier (chars , keywordStart , keywordLength );
219
222
}
220
223
224
+ // starts with DECLARE
225
+ if (parseDeclareKeyword (chars , keywordStart , keywordLength )) {
226
+ statement = new QueryStatement (type , QueryType .DECLARE , QueryCmd .UNKNOWN );
227
+ batcher .readIdentifier (chars , keywordStart , keywordLength );
228
+ }
229
+
221
230
// starts with INSERT, UPSERT
222
231
if (parseInsertKeyword (chars , keywordStart , keywordLength )) {
223
232
statement = new QueryStatement (type , QueryType .DATA_QUERY , QueryCmd .INSERT_UPSERT );
@@ -251,9 +260,13 @@ public String parseSQL() throws SQLException {
251
260
}
252
261
253
262
statements .add (statement );
254
- detectJdbcArgs = statement .getType () != QueryType .SCHEME_QUERY
255
- && statement .getType () != QueryType .UNKNOWN
256
- && isDetectJdbcParameters ;
263
+
264
+ detectJdbcArgs = isDetectJdbcParameters ;
265
+ detectJdbcArgs = detectJdbcArgs && statement .getType () != QueryType .SCHEME_QUERY ;
266
+ detectJdbcArgs = detectJdbcArgs && statement .getType () != QueryType .DECLARE ;
267
+ if (!isForceJdbcParamters ) {
268
+ detectJdbcArgs = detectJdbcArgs && statement .getType () != QueryType .UNKNOWN ;
269
+ }
257
270
}
258
271
}
259
272
@@ -612,6 +625,20 @@ private static boolean parseSelectKeyword(char[] query, int offset, int length)
612
625
&& (query [offset + 5 ] | 32 ) == 't' ;
613
626
}
614
627
628
+ private static boolean parseDeclareKeyword (char [] query , int offset , int length ) {
629
+ if (length != 7 ) {
630
+ return false ;
631
+ }
632
+
633
+ return (query [offset ] | 32 ) == 'd'
634
+ && (query [offset + 1 ] | 32 ) == 'e'
635
+ && (query [offset + 2 ] | 32 ) == 'c'
636
+ && (query [offset + 3 ] | 32 ) == 'l'
637
+ && (query [offset + 4 ] | 32 ) == 'a'
638
+ && (query [offset + 5 ] | 32 ) == 'r'
639
+ && (query [offset + 6 ] | 32 ) == 'e' ;
640
+ }
641
+
615
642
private static boolean parseUpdateKeyword (char [] query , int offset , int length ) {
616
643
if (length != 6 ) {
617
644
return false ;
0 commit comments