88
99import tech .ydb .jdbc .YdbConst ;
1010import tech .ydb .jdbc .query .params .JdbcPrm ;
11+ import tech .ydb .jdbc .settings .YdbQueryProperties ;
1112
1213
1314/**
1718public class YdbQueryParser {
1819 private final boolean isDetectQueryType ;
1920 private final boolean isDetectJdbcParameters ;
21+ private final boolean isForceJdbcParamters ;
2022 private final boolean isConvertJdbcInToList ;
2123
2224 private final List <QueryStatement > statements = new ArrayList <>();
@@ -26,10 +28,11 @@ public class YdbQueryParser {
2628
2729 private int jdbcPrmIndex = 0 ;
2830
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 ();
3336 this .origin = origin ;
3437 this .parsed = new StringBuilder (origin .length () + 10 );
3538 }
@@ -45,7 +48,7 @@ public YqlBatcher getYqlBatcher() {
4548 public QueryType detectQueryType () throws SQLException {
4649 QueryType type = null ;
4750 for (QueryStatement st : statements ) {
48- if (st .getType () == QueryType .UNKNOWN ) {
51+ if (st .getType () == QueryType .UNKNOWN || st . getType () == QueryType . DECLARE ) {
4952 continue ;
5053 }
5154
@@ -218,6 +221,12 @@ public String parseSQL() throws SQLException {
218221 batcher .readIdentifier (chars , keywordStart , keywordLength );
219222 }
220223
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+
221230 // starts with INSERT, UPSERT
222231 if (parseInsertKeyword (chars , keywordStart , keywordLength )) {
223232 statement = new QueryStatement (type , QueryType .DATA_QUERY , QueryCmd .INSERT_UPSERT );
@@ -251,9 +260,13 @@ public String parseSQL() throws SQLException {
251260 }
252261
253262 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+ }
257270 }
258271 }
259272
@@ -612,6 +625,20 @@ private static boolean parseSelectKeyword(char[] query, int offset, int length)
612625 && (query [offset + 5 ] | 32 ) == 't' ;
613626 }
614627
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+
615642 private static boolean parseUpdateKeyword (char [] query , int offset , int length ) {
616643 if (length != 6 ) {
617644 return false ;
0 commit comments