Skip to content

Commit f12bf4e

Browse files
committed
Rewrite query parser to detect every keyword
1 parent e40c1ed commit f12bf4e

File tree

11 files changed

+375
-154
lines changed

11 files changed

+375
-154
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import tech.ydb.jdbc.common.FixedResultSetFactory;
1919
import tech.ydb.jdbc.context.YdbValidator;
2020
import tech.ydb.jdbc.query.ExplainedQuery;
21-
import tech.ydb.jdbc.query.QueryExpression;
21+
import tech.ydb.jdbc.query.QueryStatement;
2222
import tech.ydb.jdbc.query.YdbQuery;
2323
import tech.ydb.jdbc.settings.YdbOperationProperties;
2424
import tech.ydb.table.query.Params;
@@ -168,7 +168,7 @@ protected boolean updateState(List<YdbResult> results) {
168168
protected List<YdbResult> executeSchemeQuery(YdbQuery query) throws SQLException {
169169
connection.executeSchemeQuery(query, validator);
170170

171-
int expressionsCount = query.getExpressions().isEmpty() ? 1 : query.getExpressions().size();
171+
int expressionsCount = query.getStatements().isEmpty() ? 1 : query.getStatements().size();
172172
List<YdbResult> results = new ArrayList<>();
173173
for (int i = 0; i < expressionsCount; i++) {
174174
results.add(NO_UPDATED);
@@ -200,17 +200,17 @@ protected List<YdbResult> executeDataQuery(YdbQuery query, Params params) throws
200200

201201
List<YdbResult> results = new ArrayList<>();
202202
int idx = 0;
203-
for (QueryExpression exp: query.getExpressions()) {
203+
for (QueryStatement exp: query.getStatements()) {
204204
if (exp.isDDL()) {
205205
results.add(NO_UPDATED);
206206
continue;
207207
}
208-
if (!exp.hasResults()) {
208+
if (exp.hasUpdateCount()) {
209209
results.add(HAS_UPDATED);
210210
continue;
211211
}
212212

213-
if (idx < resultSets.size()) {
213+
if (exp.hasResults() && idx < resultSets.size()) {
214214
ResultSetReader rs = resultSets.get(idx);
215215
if (failOnTruncatedResult && rs.isTruncated()) {
216216
String msg = String.format(YdbConst.RESULT_IS_TRUNCATED, idx, rs.getRowCount());

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
* @author Aleksandr Gorshenin
66
*/
77
public enum QueryCmd {
8+
UNKNOWN,
89
SELECT,
910
CREATE_ALTER_DROP,
1011
INSERT_UPSERT,
11-
UPDATE_REPLACE_REMOVE
12+
UPDATE_REPLACE_DELETE
1213
}

jdbc/src/main/java/tech/ydb/jdbc/query/QueryExpression.java renamed to jdbc/src/main/java/tech/ydb/jdbc/query/QueryStatement.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
*
88
* @author Aleksandr Gorshenin
99
*/
10-
public class QueryExpression {
10+
public class QueryStatement {
1111
private final QueryType queryType;
1212
private final QueryCmd command;
1313
private final List<String> paramNames = new ArrayList<>();
14+
private boolean hasReturinng = false;
1415

15-
public QueryExpression(QueryType type, QueryCmd command) {
16+
public QueryStatement(QueryType type, QueryCmd command) {
1617
this.queryType = type;
1718
this.command = command;
1819
}
@@ -29,8 +30,16 @@ public void addParamName(String name) {
2930
this.paramNames.add(name);
3031
}
3132

33+
public void setHasReturning(boolean hasReturning) {
34+
this.hasReturinng = hasReturning;
35+
}
36+
37+
public boolean hasUpdateCount() {
38+
return (command == QueryCmd.INSERT_UPSERT || command == QueryCmd.UPDATE_REPLACE_DELETE); // && !hasReturinng;
39+
}
40+
3241
public boolean hasResults() {
33-
return command == QueryCmd.SELECT;
42+
return command == QueryCmd.SELECT; // || hasReturinng;
3443
}
3544

3645
public boolean isDDL() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tech.ydb.jdbc.query;
22

33
public enum QueryType {
4+
UNKNOWN,
5+
46
// DDL
57
SCHEME_QUERY,
68

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

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.sql.SQLDataException;
66
import java.sql.SQLException;
7-
import java.sql.SQLFeatureNotSupportedException;
87
import java.util.ArrayList;
98
import java.util.List;
109
import java.util.Map;
@@ -19,16 +18,22 @@
1918
* @author Aleksandr Gorshenin
2019
*/
2120
public class YdbQuery {
22-
private final ParsedQuery query;
21+
private final String originSQL;
22+
private final String preparedYQL;
23+
private final List<QueryStatement> statements;
24+
2325
private final QueryType type;
2426
private final boolean isAutoDeclare;
2527
private final List<String> paramNames = new ArrayList<>();
2628

27-
YdbQuery(ParsedQuery query, QueryType type, boolean isAutoDeclare) {
28-
this.query = query;
29+
YdbQuery(String originSQL, String preparedYQL, List<QueryStatement> stats, QueryType type, boolean isAutoDeclare) {
30+
this.originSQL = originSQL;
31+
this.preparedYQL = preparedYQL;
32+
this.statements = stats;
2933
this.type = type;
3034
this.isAutoDeclare = isAutoDeclare;
31-
for (QueryExpression expression : query.getExpressions()) {
35+
36+
for (QueryStatement expression : stats) {
3237
paramNames.addAll(expression.getParamNames());
3338
}
3439
}
@@ -38,11 +43,11 @@ public QueryType getType() {
3843
}
3944

4045
public String getOriginSQL() {
41-
return query.getOriginSQL();
46+
return originSQL;
4247
}
4348

44-
public List<QueryExpression> getExpressions() {
45-
return query.getExpressions();
49+
public List<QueryStatement> getStatements() {
50+
return statements;
4651
}
4752

4853
public boolean hasFreeParams() {
@@ -55,15 +60,15 @@ public List<String> getFreeParams() {
5560

5661
public String withParams(Params params) throws SQLException {
5762
if (paramNames.isEmpty()) {
58-
return query.getPreparedYQL();
63+
return preparedYQL;
5964
}
6065

6166
if (params == null) {
6267
if (!paramNames.isEmpty() && isAutoDeclare) {
6368
// Comment in place where must be declare section
64-
return "-- DECLARE " + paramNames.size() + " PARAMETERS\n" + query.getPreparedYQL();
69+
return "-- DECLARE " + paramNames.size() + " PARAMETERS\n" + preparedYQL;
6570
}
66-
return query.getPreparedYQL();
71+
return preparedYQL;
6772
}
6873

6974
StringBuilder yql = new StringBuilder();
@@ -84,30 +89,19 @@ public String withParams(Params params) throws SQLException {
8489
}
8590
}
8691

87-
yql.append(query.getPreparedYQL());
92+
yql.append(preparedYQL);
8893
return yql.toString();
8994
}
9095

91-
public static YdbQuery parseQuery(String queryText, YdbQueryProperties opts) throws SQLException {
92-
ParsedQuery query = ParsedQuery.parse(queryText, opts);
96+
public static YdbQuery parseQuery(String query, YdbQueryProperties opts) throws SQLException {
97+
YdbQueryParser parser = new YdbQueryParser(opts.isDetectQueryType(), opts.isDetectJdbcParameters());
98+
String preparedYQL = parser.parseSQL(query);
99+
93100
QueryType type = opts.getForcedQueryType();
94-
if (type != null) {
95-
return new YdbQuery(query, type, opts.isDeclareJdbcParameters());
96-
}
97-
for (QueryExpression exp: query.getExpressions()) {
98-
if (type == null) {
99-
type = exp.getType();
100-
} else {
101-
if (type != exp.getType()) {
102-
throw new SQLFeatureNotSupportedException(
103-
YdbConst.MULTI_TYPES_IN_ONE_QUERY + type + ", " + exp.getType()
104-
);
105-
}
106-
}
107-
}
108101
if (type == null) {
109-
type = QueryType.DATA_QUERY;
102+
type = parser.detectQueryType();
110103
}
111-
return new YdbQuery(query, type, opts.isDeclareJdbcParameters());
104+
105+
return new YdbQuery(query, preparedYQL, parser.getStatements(), type, opts.isDeclareJdbcParameters());
112106
}
113107
}

0 commit comments

Comments
 (0)