Skip to content

Commit 8d4bcfd

Browse files
committed
Fixed usePrefixPath for preparing of YQL statements
1 parent d6978bb commit 8d4bcfd

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public final class YdbConst {
9191
public static final String CANNOT_LOAD_DATA_FROM_IS = "Unable to load data from input stream: ";
9292
public static final String CANNOT_LOAD_DATA_FROM_READER = "Unable to load data from reader: ";
9393
public static final String STATEMENT_IS_NOT_A_BATCH = "Statement cannot be executed as batch statement: ";
94+
public static final String UNABLE_PREPARE_STATEMENT = "Cannot prepare statement: ";
9495
public static final String MULTI_TYPES_IN_ONE_QUERY = "Query cannot contain expressions with different types: ";
9596
public static final String SCAN_QUERY_INSIDE_TRANSACTION = "Scan query cannot be executed inside active "
9697
+ "transaction. This behavior may be changed by property scanQueryTxMode";

jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import tech.ydb.core.settings.BaseRequestSettings;
2525
import tech.ydb.jdbc.YdbConst;
2626
import tech.ydb.jdbc.YdbPrepareMode;
27+
import tech.ydb.jdbc.YdbTracer;
2728
import tech.ydb.jdbc.exception.ExceptionFactory;
2829
import tech.ydb.jdbc.query.QueryType;
2930
import tech.ydb.jdbc.query.YdbPreparedQuery;
@@ -45,6 +46,7 @@
4546
import tech.ydb.table.TableClient;
4647
import tech.ydb.table.description.TableDescription;
4748
import tech.ydb.table.impl.PooledTableClient;
49+
import tech.ydb.table.query.DataQuery;
4850
import tech.ydb.table.query.ExplainDataQueryResult;
4951
import tech.ydb.table.rpc.grpc.GrpcTableRpc;
5052
import tech.ydb.table.settings.DescribeTableSettings;
@@ -393,11 +395,20 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
393395
String tablePath = joined(getPrefixPath(), query.getYqlBatcher().getTableName());
394396
TableDescription description = tableDescribeCache.getIfPresent(tablePath);
395397
if (description == null) {
398+
YdbTracer tracer = config.isTxTracedEnabled() ? YdbTracer.current() : null;
399+
if (tracer != null) {
400+
tracer.trace("--> describe table");
401+
tracer.traceRequest(tablePath);
402+
}
396403
DescribeTableSettings settings = withDefaultTimeout(new DescribeTableSettings());
397404
Result<TableDescription> result = retryCtx.supplyResult(
398405
session -> session.describeTable(tablePath, settings)
399406
).join();
400407

408+
if (tracer != null) {
409+
tracer.trace("<-- " + result.getStatus());
410+
}
411+
401412
if (result.isSuccess()) {
402413
description = result.getValue();
403414
tableDescribeCache.put(query.getOriginQuery(), description);
@@ -429,12 +440,29 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
429440
try {
430441
Map<String, Type> types = queryParamsCache.getIfPresent(query.getOriginQuery());
431442
if (types == null) {
432-
String yql = query.getPreparedYql();
443+
String yql = prefixPragma + query.getPreparedYql();
444+
YdbTracer tracer = config.isTxTracedEnabled() ? YdbTracer.current() : null;
445+
if (tracer != null) {
446+
tracer.trace("--> prepare data query");
447+
tracer.traceRequest(yql);
448+
}
449+
433450
PrepareDataQuerySettings settings = withDefaultTimeout(new PrepareDataQuerySettings());
434-
types = retryCtx.supplyResult(session -> session.prepareDataQuery(yql, settings))
435-
.join()
436-
.getValue()
437-
.types();
451+
Result<DataQuery> result = retryCtx.supplyResult(
452+
session -> session.prepareDataQuery(yql, settings)
453+
).join();
454+
455+
if (tracer != null) {
456+
tracer.trace("<-- " + result.getStatus());
457+
}
458+
if (!result.isSuccess()) {
459+
if (tracer != null) {
460+
tracer.close();
461+
}
462+
throw new SQLException(YdbConst.UNABLE_PREPARE_STATEMENT + result.getStatus());
463+
}
464+
465+
types = result.getValue().types();
438466
queryParamsCache.put(query.getOriginQuery(), types);
439467
}
440468

jdbc/src/test/java/tech/ydb/jdbc/YdbDriverTablesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ public void defaultModeTest() throws SQLException {
102102
}
103103

104104
// read all
105-
try (Statement st = connection.createStatement()) {
105+
try (PreparedStatement ps = connection.prepareStatement(SELECT_ALL)) {
106106
int readed = 0;
107-
try (ResultSet rs = st.executeQuery(SELECT_ALL)) {
107+
try (ResultSet rs = ps.executeQuery()) {
108108
while (rs.next()) {
109109
readed++;
110110
Assertions.assertEquals(readed, rs.getInt("id"));

0 commit comments

Comments
 (0)