Skip to content

Commit ae07138

Browse files
authored
Added support of SCHEME & EXPLAIN queries to PreparedStatement (#63)
2 parents 91a0239 + a113328 commit ae07138

File tree

6 files changed

+23
-49
lines changed

6 files changed

+23
-49
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public final class YdbConst {
8686
public static final String DATABASE_UNAVAILABLE = "Database is unavailable: ";
8787
public static final String CANNOT_LOAD_DATA_FROM_IS = "Unable to load data from input stream: ";
8888
public static final String CANNOT_LOAD_DATA_FROM_READER = "Unable to load data from reader: ";
89-
public static final String UNSUPPORTED_QUERY_TYPE_IN_PS = "Query type in prepared statement not supported: ";
9089
public static final String STATEMENT_IS_NOT_A_BATCH = "Statement cannot be executed as batch statement: ";
9190
public static final String MULTI_TYPES_IN_ONE_QUERY = "Query cannot contain expressions with different types: ";
9291
public static final String SCAN_QUERY_INSIDE_TRANSACTION = "Scan query cannot be executed inside active "

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import tech.ydb.jdbc.YdbConst;
2727
import tech.ydb.jdbc.YdbPrepareMode;
2828
import tech.ydb.jdbc.exception.ExceptionFactory;
29+
import tech.ydb.jdbc.query.QueryType;
2930
import tech.ydb.jdbc.query.YdbPreparedQuery;
3031
import tech.ydb.jdbc.query.YdbQuery;
3132
import tech.ydb.jdbc.query.params.BatchedQuery;
@@ -345,6 +346,10 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
345346
}
346347
}
347348

349+
if (query.getType() == QueryType.EXPLAIN_QUERY || query.getType() == QueryType.SCHEME_QUERY) {
350+
return new InMemoryQuery(query, queryOptions.isDeclareJdbcParameters());
351+
}
352+
348353
if (query.getYqlBatcher() != null && mode == YdbPrepareMode.AUTO) {
349354
Map<String, Type> types = queryParamsCache.getIfPresent(query.getOriginQuery());
350355
if (types == null) {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import tech.ydb.jdbc.context.YdbExecutor;
3232
import tech.ydb.jdbc.context.YdbValidator;
3333
import tech.ydb.jdbc.query.ExplainedQuery;
34-
import tech.ydb.jdbc.query.QueryType;
3534
import tech.ydb.jdbc.query.YdbPreparedQuery;
3635
import tech.ydb.jdbc.query.YdbQuery;
3736
import tech.ydb.jdbc.settings.FakeTxMode;
@@ -307,10 +306,6 @@ private YdbPreparedStatement prepareStatement(String sql, int resultSetType, Ydb
307306

308307
YdbQuery query = ctx.findOrParseYdbQuery(sql);
309308

310-
if (query.getType() != QueryType.DATA_QUERY && query.getType() != QueryType.SCAN_QUERY) {
311-
throw new SQLException(YdbConst.UNSUPPORTED_QUERY_TYPE_IN_PS + query.getType());
312-
}
313-
314309
YdbPreparedQuery params = ctx.findOrPrepareParams(query, mode);
315310
return new YdbPreparedStatementImpl(this, query, params, resultSetType);
316311
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ public boolean execute() throws SQLException {
132132
case SCAN_QUERY:
133133
newState = executeScanQuery(query, prepared.getQueryText(prms), prms);
134134
break;
135+
case SCHEME_QUERY:
136+
newState = executeSchemeQuery(query);
137+
break;
138+
case EXPLAIN_QUERY:
139+
newState = executeExplainQuery(query);
140+
break;
135141
default:
136142
throw new IllegalStateException("Internal error. Unsupported query type " + query.getType());
137143
}

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementImplTest.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ public class YdbPreparedStatementImplTest {
6363

6464
@BeforeAll
6565
public static void initTable() throws SQLException {
66-
try (Statement statement = jdbc.connection().createStatement();) {
66+
try (PreparedStatement ps = jdbc.connection().prepareStatement(TEST_TABLE.createTableSQL())) {
6767
// create test table
68-
statement.execute(TEST_TABLE.createTableSQL());
68+
ps.execute();
6969
}
7070
}
7171

7272
@AfterAll
7373
public static void dropTable() throws SQLException {
74-
try (Statement statement = jdbc.connection().createStatement();) {
75-
statement.execute(TEST_TABLE.dropTableSQL());
74+
try (PreparedStatement ps = jdbc.connection().prepareStatement(TEST_TABLE.dropTableSQL())) {
75+
ps.execute();
7676
}
7777
}
7878

@@ -82,8 +82,8 @@ public void afterEach() throws SQLException {
8282
return;
8383
}
8484

85-
try (Statement statement = jdbc.connection().createStatement()) {
86-
statement.execute(TEST_TABLE.deleteAllSQL());
85+
try (PreparedStatement ps = jdbc.connection().prepareStatement(TEST_TABLE.deleteAllSQL())) {
86+
ps.execute();
8787
}
8888

8989
jdbc.connection().close();
@@ -429,19 +429,6 @@ public void executeScanQueryAsUpdate() throws SQLException {
429429
}
430430
}
431431

432-
@Test
433-
public void executeUnsupportedModes() throws SQLException {
434-
ExceptionAssert.sqlException("Query type in prepared statement not supported: SCHEME_QUERY", () -> {
435-
String sql = "DROP TABLE test_table;";
436-
jdbc.connection().prepareStatement(sql);
437-
});
438-
439-
ExceptionAssert.sqlException("Query type in prepared statement not supported: EXPLAIN_QUERY", () -> {
440-
String sql = "EXPLAIN " + prepareSelectAll();
441-
jdbc.connection().prepareStatement(sql);
442-
});
443-
}
444-
445432
@ParameterizedTest(name = "with {0}")
446433
@EnumSource(value = SqlQueries.YqlQuery.class)
447434
public void executeExplainQueryExplicitly(SqlQueries.YqlQuery mode) throws SQLException {

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbQueryPreparedStatementImplTest.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ public class YdbQueryPreparedStatementImplTest {
6161

6262
@BeforeAll
6363
public static void initTable() throws SQLException {
64-
try (Statement statement = jdbc.connection().createStatement();) {
64+
try (PreparedStatement ps = jdbc.connection().prepareStatement(TEST_TABLE.createTableSQL())) {
6565
// create test table
66-
statement.execute(TEST_TABLE.createTableSQL());
66+
ps.execute();
6767
}
6868
}
6969

7070
@AfterAll
7171
public static void dropTable() throws SQLException {
72-
try (Statement statement = jdbc.connection().createStatement();) {
73-
statement.execute(TEST_TABLE.dropTableSQL());
72+
try (PreparedStatement ps = jdbc.connection().prepareStatement(TEST_TABLE.dropTableSQL())) {
73+
ps.execute();
7474
}
7575
}
7676

@@ -80,8 +80,8 @@ public void afterEach() throws SQLException {
8080
return;
8181
}
8282

83-
try (Statement statement = jdbc.connection().createStatement()) {
84-
statement.execute(TEST_TABLE.deleteAllSQL());
83+
try (PreparedStatement ps = jdbc.connection().prepareStatement(TEST_TABLE.deleteAllSQL())) {
84+
ps.execute();
8585
}
8686

8787
jdbc.connection().close();
@@ -94,11 +94,6 @@ private String upsertSql(String column, String type) {
9494
.replaceAll("#tableName", TEST_TABLE_NAME);
9595
}
9696

97-
private YdbPreparedStatement prepareUpsert(YdbPrepareMode mode,String column, String type)
98-
throws SQLException {
99-
return jdbc.connection().unwrap(YdbConnection.class).prepareStatement(upsertSql(column, type), mode);
100-
}
101-
10297
private PreparedStatement prepareSimpleSelect(String column) throws SQLException {
10398
String sql = SIMPLE_SELECT_SQL
10499
.replaceAll("#column", column)
@@ -375,19 +370,6 @@ public void executeScanQueryAsUpdate() throws SQLException {
375370
}
376371
}
377372

378-
@Test
379-
public void executeUnsupportedModes() throws SQLException {
380-
ExceptionAssert.sqlException("Query type in prepared statement not supported: SCHEME_QUERY", () -> {
381-
String sql = "DROP TABLE test_table;";
382-
jdbc.connection().prepareStatement(sql);
383-
});
384-
385-
ExceptionAssert.sqlException("Query type in prepared statement not supported: EXPLAIN_QUERY", () -> {
386-
String sql = "EXPLAIN " + prepareSelectAll();
387-
jdbc.connection().prepareStatement(sql);
388-
});
389-
}
390-
391373
@ParameterizedTest(name = "with {0}")
392374
@EnumSource(value = SqlQueries.YqlQuery.class)
393375
public void executeExplainQueryExplicitly(SqlQueries.YqlQuery mode) throws SQLException {

0 commit comments

Comments
 (0)