Skip to content

Commit 271ec8c

Browse files
authored
Merge pull request #22 from ydb-platform/additional_configs
Additional config for query modes
2 parents 76ea4e4 + d0a1d8d commit 271ec8c

18 files changed

+440
-120
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## 2.0.1 ##
2+
3+
* Added parameter `forceQueryMode` to use for specifying the type of query
4+
* Execution of scan or scheme query inside active transaction will raise exception
5+
6+
## 2.0.0 ##
7+
8+
* Auto detect of YDB query type
9+
* Support of batch queries
10+
* Support of standart positional parameters
11+
* Added the ability to set the YQL variable by alphabetic order
12+

jdbc/src/main/java/tech/ydb/jdbc/YdbConst.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public final class YdbConst {
9191
public static final String UNSUPPORTED_QUERY_TYPE_IN_PS = "Query type in prepared statement not supported: ";
9292
public static final String STATEMENT_IS_NOT_A_BATCH = "Statement cannot be executed as batch statement: ";
9393
public static final String MULTI_TYPES_IN_ONE_QUERY = "Query cannot contain expressions with different types: ";
94+
public static final String SCAN_QUERY_INSIDE_TRANSACTION = "Scan query cannot be executed inside active "
95+
+ "transaction. This behavior may be changed by property scanQueryTxMode";
96+
public static final String SCHEME_QUERY_INSIDE_TRANSACTION = "Scheme query cannot be executed inside active "
97+
+ "transaction. This behavior may be changed by property schemeQueryTxMode";
9498

9599
// Cast errors
96100

jdbc/src/main/java/tech/ydb/jdbc/impl/YdbConnectionImpl.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
import tech.ydb.jdbc.context.YdbContext;
3737
import tech.ydb.jdbc.context.YdbExecutor;
3838
import tech.ydb.jdbc.context.YdbTxState;
39+
import tech.ydb.jdbc.exception.YdbExecutionException;
3940
import tech.ydb.jdbc.query.QueryType;
4041
import tech.ydb.jdbc.query.YdbQuery;
42+
import tech.ydb.jdbc.settings.FakeTxMode;
43+
import tech.ydb.jdbc.settings.YdbOperationProperties;
4144
import tech.ydb.table.Session;
4245
import tech.ydb.table.query.DataQuery;
4346
import tech.ydb.table.query.DataQueryResult;
@@ -61,6 +64,8 @@ public class YdbConnectionImpl implements YdbConnection {
6164
private final YdbContext ctx;
6265
private final YdbExecutor executor;
6366
private final Supplier<YdbDatabaseMetaData> metaDataSupplier;
67+
private final FakeTxMode scanQueryTxMode;
68+
private final FakeTxMode schemeQueryTxMode;
6469

6570
private volatile YdbTxState state;
6671

@@ -69,10 +74,11 @@ public YdbConnectionImpl(YdbContext context) throws SQLException {
6974
this.metaDataSupplier = Suppliers.memoize(() -> new YdbDatabaseMetaDataImpl(this))::get;
7075
this.executor = new YdbExecutor(LOGGER);
7176

72-
int txLevel = ctx.getOperationProperties().getTransactionLevel();
73-
boolean txAutoCommit = ctx.getOperationProperties().isAutoCommit();
74-
this.state = YdbTxState.create(txLevel, txAutoCommit);
77+
YdbOperationProperties props = ctx.getOperationProperties();
78+
this.scanQueryTxMode = props.getScanQueryTxMode();
79+
this.schemeQueryTxMode = props.getSchemeQueryTxMode();
7580

81+
this.state = YdbTxState.create(props.getTransactionLevel(), props.isAutoCommit());
7682
this.ctx.register();
7783
}
7884

@@ -256,7 +262,17 @@ public void executeSchemeQuery(YdbQuery query, YdbExecutor executor) throws SQLE
256262
ensureOpened();
257263

258264
if (state.isInsideTransaction()) {
259-
commit();
265+
switch (schemeQueryTxMode) {
266+
case FAKE_TX:
267+
break;
268+
case SHADOW_COMMIT:
269+
commit();
270+
break;
271+
case ERROR:
272+
default:
273+
throw new YdbExecutionException(YdbConst.SCHEME_QUERY_INSIDE_TRANSACTION);
274+
275+
}
260276
}
261277

262278
// Scheme query does not affect transactions or result sets
@@ -292,6 +308,20 @@ public DataQueryResult executeDataQuery(YdbQuery query, YdbExecutor executor,
292308
public ResultSetReader executeScanQuery(YdbQuery query, YdbExecutor executor, Params params) throws SQLException {
293309
ensureOpened();
294310

311+
if (state.isInsideTransaction()) {
312+
switch (scanQueryTxMode) {
313+
case FAKE_TX:
314+
break;
315+
case SHADOW_COMMIT:
316+
commit();
317+
break;
318+
case ERROR:
319+
default:
320+
throw new YdbExecutionException(YdbConst.SCAN_QUERY_INSIDE_TRANSACTION);
321+
322+
}
323+
}
324+
295325
String yql = query.getYqlQuery(params);
296326
Collection<ResultSetReader> resultSets = new LinkedBlockingQueue<>();
297327
Duration scanQueryTimeout = ctx.getOperationProperties().getScanQueryTimeout();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public QueryType type() {
9191
}
9292

9393
public static YdbQuery from(YdbQueryOptions opts, String sql) throws SQLException {
94-
YdbQueryBuilder builder = new YdbQueryBuilder(sql);
94+
YdbQueryBuilder builder = new YdbQueryBuilder(sql, opts.getForcedQueryType());
9595
JdbcQueryLexer.buildQuery(builder, opts);
9696
return new YdbQuery(opts, builder);
9797
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public class YdbQueryBuilder {
1515
private final String origin;
1616
private final StringBuilder query;
1717
private final List<String> args = new ArrayList<>();
18+
private final QueryType forcedType;
1819

1920
private int argsCounter = 0;
2021
private QueryType currentType = null;
2122

22-
public YdbQueryBuilder(String origin) {
23+
public YdbQueryBuilder(String origin, QueryType forcedType) {
2324
this.origin = origin;
2425
this.query = new StringBuilder(origin.length() + 10);
26+
this.forcedType = forcedType;
2527
}
2628

2729
public String createNextArgName() {
@@ -36,6 +38,10 @@ public String createNextArgName() {
3638
}
3739

3840
public void setQueryType(QueryType type) throws YdbNonRetryableException {
41+
if (forcedType != null) {
42+
return;
43+
}
44+
3945
if (currentType != null && currentType != type) {
4046
String msg = YdbConst.MULTI_TYPES_IN_ONE_QUERY + currentType + ", " + type;
4147
throw new YdbNonRetryableException(msg, StatusCode.BAD_REQUEST);
@@ -44,7 +50,15 @@ public void setQueryType(QueryType type) throws YdbNonRetryableException {
4450
}
4551

4652
public QueryType getQueryType() {
47-
return currentType != null ? currentType : QueryType.DATA_QUERY;
53+
if (forcedType != null) {
54+
return forcedType;
55+
}
56+
57+
if (currentType != null) {
58+
return currentType;
59+
}
60+
61+
return QueryType.DATA_QUERY;
4862
}
4963

5064
public String getOriginSQL() {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ public class YdbQueryOptions {
2323
private final boolean isPrepareDataQueries;
2424
private final boolean isDetectBatchQueries;
2525

26+
private final QueryType forcedType;
27+
2628
@VisibleForTesting
2729
YdbQueryOptions(
2830
boolean enforceV1,
2931
boolean detectQueryType,
3032
boolean detectJbdcParams,
3133
boolean declareJdbcParams,
3234
boolean prepareDataQuery,
33-
boolean detectBatchQuery
35+
boolean detectBatchQuery,
36+
QueryType forcedType
3437
) {
3538
this.isEnforceSyntaxV1 = enforceV1;
3639

@@ -40,6 +43,8 @@ public class YdbQueryOptions {
4043

4144
this.isPrepareDataQueries = prepareDataQuery;
4245
this.isDetectBatchQueries = detectBatchQuery;
46+
47+
this.forcedType = forcedType;
4348
}
4449

4550
public boolean isEnforceSyntaxV1() {
@@ -66,6 +71,10 @@ public boolean isDetectBatchQueries() {
6671
return isDetectBatchQueries;
6772
}
6873

74+
public QueryType getForcedQueryType() {
75+
return forcedType;
76+
}
77+
6978
public static YdbQueryOptions createFrom(YdbOperationProperties props) {
7079
int level = props.getJdbcSupportLevel();
7180

@@ -111,13 +120,16 @@ public static YdbQueryOptions createFrom(YdbOperationProperties props) {
111120
declareJdbcParams = declareJdbcParams && detectJbdcParams;
112121
}
113122

123+
QueryType forcedQueryType = props.getForcedQueryType();
124+
114125
return new YdbQueryOptions(
115126
enforceV1,
116127
detectQueryType,
117128
detectJbdcParams,
118129
declareJdbcParams,
119130
prepareDataQuery,
120-
detectBatchQuery
131+
detectBatchQuery,
132+
forcedQueryType
121133
);
122134
}
123135
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package tech.ydb.jdbc.settings;
2+
3+
/**
4+
*
5+
* @author Aleksandr Gorshenin
6+
*/
7+
public enum FakeTxMode {
8+
ERROR,
9+
FAKE_TX,
10+
SHADOW_COMMIT,
11+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ static PropertyConverter<String> stringValue() {
1414
return value -> value;
1515
}
1616

17+
static <E extends Enum<E>> PropertyConverter<E> enumValue(Class<E> clazz) {
18+
return value -> {
19+
for (E v: clazz.getEnumConstants()) {
20+
if (value.equalsIgnoreCase(v.name())) {
21+
return v;
22+
}
23+
}
24+
return null;
25+
};
26+
}
27+
1728
static PropertyConverter<Duration> durationValue() {
1829
return value -> {
1930
String targetValue = "PT" + value.replace(" ", "").toUpperCase(Locale.ROOT);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package tech.ydb.jdbc.settings;
22

3+
import java.io.UnsupportedEncodingException;
34
import java.net.URI;
45
import java.net.URISyntaxException;
56
import java.net.URLEncoder;
6-
import java.nio.charset.StandardCharsets;
7-
import java.io.UnsupportedEncodingException;
87
import java.sql.SQLException;
98
import java.util.Collection;
109
import java.util.Collections;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Map;
55
import java.util.Objects;
66

7+
import tech.ydb.jdbc.query.QueryType;
8+
79
public class YdbOperationProperties {
810
public static final int MAX_ROWS = 1000; // TODO: how to figure out the max rows of current connection?
911

@@ -21,6 +23,10 @@ public class YdbOperationProperties {
2123

2224
private final int jdbcSupportLevel;
2325

26+
private final FakeTxMode scanQueryTxMode;
27+
private final FakeTxMode schemeQueryTxMode;
28+
private final QueryType forcedQueryType;
29+
2430
public YdbOperationProperties(Map<YdbOperationProperty<?>, ParsedProperty> params) {
2531
this.params = Objects.requireNonNull(params);
2632

@@ -36,6 +42,12 @@ public YdbOperationProperties(Map<YdbOperationProperty<?>, ParsedProperty> param
3642
this.cacheConnectionsInDriver = params.get(YdbOperationProperty.CACHE_CONNECTIONS_IN_DRIVER).getParsedValue();
3743

3844
this.jdbcSupportLevel = params.get(YdbOperationProperty.JDBC_SUPPORT_LEVEL).getParsedValue();
45+
46+
this.scanQueryTxMode = params.get(YdbOperationProperty.SCAN_QUERY_TX_MODE).getParsedValue();
47+
this.schemeQueryTxMode = params.get(YdbOperationProperty.SCHEME_QUERY_TX_MODE).getParsedValue();
48+
49+
ParsedProperty forcedType = params.get(YdbOperationProperty.FORCE_QUERY_MODE);
50+
this.forcedQueryType = forcedType != null ? forcedType.getParsedValue() : null;
3951
}
4052

4153
public Map<YdbOperationProperty<?>, ParsedProperty> getParams() {
@@ -58,6 +70,18 @@ public boolean isFailOnTruncatedResult() {
5870
return failOnTruncatedResult;
5971
}
6072

73+
public FakeTxMode getScanQueryTxMode() {
74+
return scanQueryTxMode;
75+
}
76+
77+
public FakeTxMode getSchemeQueryTxMode() {
78+
return schemeQueryTxMode;
79+
}
80+
81+
public QueryType getForcedQueryType() {
82+
return forcedQueryType;
83+
}
84+
6185
public Duration getSessionTimeout() {
6286
return sessionTimeout;
6387
}

0 commit comments

Comments
 (0)