Skip to content

Commit 4a6d7d3

Browse files
authored
Added option 'forceJdbcParameters' to detect ? in whole query text (#106)
2 parents ac9bed2 + f6452e9 commit 4a6d7d3

File tree

8 files changed

+125
-52
lines changed

8 files changed

+125
-52
lines changed

jdbc/src/main/java/tech/ydb/jdbc/query/QueryType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public enum QueryType {
44
UNKNOWN,
55

6+
DECLARE,
7+
68
// DDL
79
SCHEME_QUERY,
810

jdbc/src/main/java/tech/ydb/jdbc/query/YdbQuery.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ public List<QueryStatement> getStatements() {
5959
}
6060

6161
public static YdbQuery parseQuery(String query, YdbQueryProperties opts) throws SQLException {
62-
YdbQueryParser parser = new YdbQueryParser(
63-
query, opts.isDetectQueryType(), opts.isDetectJdbcParameters(), opts.isReplaceJdbcInByYqlList()
64-
);
62+
YdbQueryParser parser = new YdbQueryParser(query, opts);
6563
String preparedYQL = parser.parseSQL();
6664

6765
QueryType type = null;

jdbc/src/main/java/tech/ydb/jdbc/query/YdbQueryParser.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import tech.ydb.jdbc.YdbConst;
1010
import tech.ydb.jdbc.query.params.JdbcPrm;
11+
import tech.ydb.jdbc.settings.YdbQueryProperties;
1112

1213

1314
/**
@@ -17,6 +18,7 @@
1718
public 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;

jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public DriverPropertyInfo[] toPropertyInfo() throws SQLException {
228228
YdbQueryProperties.DISABLE_DETECT_SQL_OPERATIONS.toInfo(properties),
229229
YdbQueryProperties.DISABLE_JDBC_PARAMETERS.toInfo(properties),
230230
YdbQueryProperties.DISABLE_JDBC_PARAMETERS_DECLARE.toInfo(properties),
231+
YdbQueryProperties.FORCE_JDBC_PARAMETERS.toInfo(properties),
231232
YdbQueryProperties.REPLACE_JDBC_IN_BY_YQL_LIST.toInfo(properties),
232233

233234
YdbQueryProperties.REPLACE_INSERT_TO_UPSERT.toInfo(properties),

jdbc/src/main/java/tech/ydb/jdbc/settings/YdbQueryProperties.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class YdbQueryProperties {
2828
static final YdbProperty<Boolean> DISABLE_JDBC_PARAMETERS = YdbProperty.bool("disableJdbcParameters",
2929
"Disable auto detect JDBC standart parameters '?'", false);
3030

31+
static final YdbProperty<Boolean> FORCE_JDBC_PARAMETERS = YdbProperty.bool("forceJdbcParameters",
32+
"Enable auto detect JDBC standart parameters '?' for all statements except DDL and DECLARE", false);
33+
3134
static final YdbProperty<Boolean> REPLACE_JDBC_IN_BY_YQL_LIST = YdbProperty.bool("replaceJdbcInByYqlList",
3235
"Convert SQL operation IN (?, ?, ... ,?) to YQL operation IN $list", true);
3336

@@ -56,6 +59,7 @@ public class YdbQueryProperties {
5659
private final boolean isDetectJdbcParameters;
5760
private final boolean isReplaceJdbcInToYqlList;
5861
private final boolean isDeclareJdbcParameters;
62+
private final boolean isForceJdbcParameters;
5963

6064
private final boolean isPrepareDataQueries;
6165
private final boolean isDetectBatchQueries;
@@ -65,8 +69,10 @@ public class YdbQueryProperties {
6569
private final boolean isForceScanSelect;
6670

6771
public YdbQueryProperties(YdbConfig config) throws SQLException {
68-
Properties props = config.getProperties();
72+
this(config.getProperties());
73+
}
6974

75+
public YdbQueryProperties(Properties props) throws SQLException {
7076
boolean disableAutoPreparedBatches = DISABLE_AUTO_PREPARED_BATCHES.readValue(props).getValue();
7177
boolean disablePrepareDataQueries = DISABLE_PREPARE_DATAQUERY.readValue(props).getValue();
7278

@@ -79,11 +85,10 @@ public YdbQueryProperties(YdbConfig config) throws SQLException {
7985
boolean disableSqlOperationsDetect = DISABLE_DETECT_SQL_OPERATIONS.readValue(props).getValue();
8086

8187
this.isDetectQueryType = !disableSqlOperationsDetect;
82-
this.isDetectJdbcParameters = !disableSqlOperationsDetect && !disableJdbcParametersParse;
83-
this.isDeclareJdbcParameters = !disableSqlOperationsDetect && !disableJdbcParametersParse
84-
&& !disableJdbcParametersDeclare;
85-
this.isReplaceJdbcInToYqlList = !disableSqlOperationsDetect && !disableJdbcParametersParse
86-
&& replaceJdbcInByYqlList;
88+
this.isForceJdbcParameters = FORCE_JDBC_PARAMETERS.readValue(props).getValue();
89+
this.isDetectJdbcParameters = isForceJdbcParameters || (isDetectQueryType && !disableJdbcParametersParse);
90+
this.isDeclareJdbcParameters = isDetectJdbcParameters && !disableJdbcParametersDeclare;
91+
this.isReplaceJdbcInToYqlList = isDetectJdbcParameters && replaceJdbcInByYqlList;
8792

8893

8994
YdbValue<QueryType> forcedType = FORCE_QUERY_MODE.readValue(props);
@@ -140,4 +145,8 @@ public boolean isForceBulkUpsert() {
140145
public boolean isForceScanSelect() {
141146
return isForceScanSelect;
142147
}
148+
149+
public boolean isForceJdbcParameters() {
150+
return isForceJdbcParameters;
151+
}
143152
}

0 commit comments

Comments
 (0)