Skip to content

Commit 62552e4

Browse files
authored
Added parsing of table name (#117)
2 parents 5974aa1 + 0364ecf commit 62552e4

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ public String parseSQL() throws SQLException {
291291
case ',':
292292
batcher.readComma();
293293
break;
294+
case '.':
295+
batcher.readPoint();
296+
break;
294297
case '=':
295298
batcher.readEqual();
296299
break;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ private enum State {
3838
VALUES_CLOSE_PAREN, // Readed ')'
3939

4040
WHERE, // Readed WHERE keyword (only for UPDATE/DELETE)
41+
WHERE_TABLE, // Readed table name in WHERE clause as part of identifier
42+
WHERE_POINT, // Readed '.' after table name in WHERE clause
4143
WHERE_COLUMN, // Readed column name in WHERE clause
4244
WHERE_EQUAL, // Readed '=' in WHERE clause
4345
WHERE_VALUE, // Readed column value in WHERE clause (support only ?)
@@ -195,11 +197,24 @@ public void readComma() {
195197
state = State.ERROR;
196198
}
197199

200+
public void readPoint() {
201+
if (state == State.WHERE_TABLE) {
202+
state = State.WHERE_POINT;
203+
return;
204+
}
205+
state = State.ERROR;
206+
}
207+
198208
public void readEqual() {
199209
if (state == State.COLUMNS_NAME && cmd == Cmd.UPDATE) {
200210
state = State.COLUMNS_EQUAL;
201211
return;
202212
}
213+
if (state == State.WHERE_TABLE) { // special case with column name == table name
214+
keyColumns.add(tableName);
215+
state = State.WHERE_EQUAL;
216+
return;
217+
}
203218
if (state == State.WHERE_COLUMN && (cmd == Cmd.UPDATE || cmd == Cmd.DELETE)) {
204219
state = State.WHERE_EQUAL;
205220
return;
@@ -347,6 +362,17 @@ public void readIdentifier(char[] query, int start, int length) {
347362
}
348363

349364
if (state == State.WHERE || state == State.WHERE_AND) {
365+
String identifier = unquote(query, start, length);
366+
if (tableName.equals(identifier)) {
367+
state = State.WHERE_TABLE;
368+
} else {
369+
keyColumns.add(identifier);
370+
state = State.WHERE_COLUMN;
371+
}
372+
return;
373+
}
374+
375+
if (state == State.WHERE_POINT) {
350376
keyColumns.add(unquote(query, start, length));
351377
state = State.WHERE_COLUMN;
352378
return;

jdbc/src/test/java/tech/ydb/jdbc/query/YdbQueryParserTest.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ public void batchedReplaceOneColumnTest(String query) throws SQLException {
560560

561561
@ParameterizedTest(name = "[{index}] {0} is batched update query")
562562
@ValueSource(strings = {
563-
"Update table_name set c1 = ?, c2 = ?, c3 = ? where k1 = ? AND k2 = ?",
564-
"\n update `table_name` set\t`c1`=?,c2=?,c3=?\tWhere\nk1=? AND k2=?;;;",
563+
"Update table_name set c1 = ?, c2 = ?, c3 = ? where table_name .k1 = ? AND k2 = ?",
564+
"\n update `table_name` set\t`c1`=?,c2=?,c3=?\tWhere\nk1=? AND `table_name`. `k2`=?;;;",
565565
"/* comment */ upDaTe `table_name` set `c1` /* commect */ = ?, c2 = \n?, c3 = ? WHERE k1=? AND k2=?;;\n-- com",
566566
";;UPDATE/* comment */table_name set `c1`= ?, c2 = ?, c3 = ? WHERE k1\n=\t?--comment\nAND k2=?",
567567
})
@@ -588,8 +588,8 @@ public void batchedUpdateTest(String query) throws SQLException {
588588
@ValueSource(strings = {
589589
"Update one_column set c1 = ? where k1 = ?",
590590
"\n update `one_column` set\t`c1`=?\tWhere\nk1=?;;;",
591-
"/* comment */ upDaTe `one_column` set `c1` /* commect */ = ? WHERE k1=?;;\n-- com",
592-
";;UPDATE/* comment */one_column set `c1`= ? WHERE k1=--comment\n?",
591+
"/* comment */ upDaTe `one_column` set `c1` /* commect */ = ? WHERE `one_column`.k1=?;;\n-- com",
592+
";;UPDATE/* comment */one_column set `c1`= ? WHERE one_column.`k1`=--comment\n?",
593593
})
594594
public void batchedUpdateOneColumnTest(String query) throws SQLException {
595595
YdbQueryParser parser = new YdbQueryParser(types, query, props);
@@ -610,12 +610,38 @@ public void batchedUpdateOneColumnTest(String query) throws SQLException {
610610
Assertions.assertEquals(Arrays.asList("c1"), batch.getColumns());
611611
}
612612

613+
@ParameterizedTest(name = "[{index}] {0} is batched update query")
614+
@ValueSource(strings = {
615+
"Update test_table set column = ? where test_table = ?",
616+
"Update test_table set column = ? where test_table . test_table = ?",
617+
"Update test_table set column = ? where `test_table`.test_table = ?",
618+
"Update test_table set column = ? where test_table.`test_table` = ?",
619+
})
620+
public void batchedUpdateOneColumnWithTableName(String query) throws SQLException {
621+
YdbQueryParser parser = new YdbQueryParser(types, query, props);
622+
parser.parseSQL();
623+
624+
Assertions.assertEquals(1, parser.getStatements().size());
625+
Assertions.assertEquals(QueryType.DATA_QUERY, parser.getStatements().get(0).getType());
626+
Assertions.assertEquals(QueryCmd.UPDATE_REPLACE_DELETE, parser.getStatements().get(0).getCmd());
627+
628+
YqlBatcher batch = parser.getYqlBatcher();
629+
Assertions.assertTrue(batch.isValidBatch());
630+
Assertions.assertEquals(YqlBatcher.Cmd.UPDATE, batch.getCommand());
631+
632+
Assertions.assertEquals("test_table", batch.getTableName());
633+
Assertions.assertEquals(1, batch.getKeyColumns().size());
634+
Assertions.assertEquals(Arrays.asList("test_table"), batch.getKeyColumns());
635+
Assertions.assertEquals(1, batch.getColumns().size());
636+
Assertions.assertEquals(Arrays.asList("column"), batch.getColumns());
637+
}
638+
613639
@ParameterizedTest(name = "[{index}] {0} is batched delete query")
614640
@ValueSource(strings = {
615-
"Delete from table_name where k1 = ? AND k2 = ?",
641+
"Delete from table_name where k1 = ? AND `table_name`.k2 = ?",
616642
"\n delete fRom `table_name`\tWhere\nk1=? AND k2=?;;;",
617643
"/* comment */ deLete from `table_name` WHERE k1=? AND k2=?;;\n-- com",
618-
";;DELETE/* comment */FRom table_name WHERE k1\n=\t?--comment\nAND k2=?",
644+
";;DELETE/* comment */FRom table_name WHERE k1\n=\t?--comment\nAND table_name.`k2`=?",
619645
})
620646
public void batchedDeleteTest(String query) throws SQLException {
621647
YdbQueryParser parser = new YdbQueryParser(types, query, props);
@@ -674,6 +700,9 @@ public void batchedDeleteOneColumnTest(String query) throws SQLException {
674700
"upsert into table_name set c1 = ?, c2 = ?, c3 = ?",
675701
"upsert table_name (c1, c2, c3) values (?, ?, ?)",
676702
"upsert table_name set c1 = ?, c2 = ?, c3 = ?",
703+
"update table_name set c1 = ?, c2 = ?, c3 = ? where other.id=?",
704+
"update table_name set c1 = ?, c2 = ?, c3 = ? where table_name.name.id=?",
705+
"update table_name set c1 = ?, c2 = ?, c3 = ? where Table_name.id=?",
677706
"upsert into table_name (c1, , c3) values (?, ?)",
678707
"upsert into table_name (c1, c2) values (?,,?)",
679708
"upsert into table_name (c1, c2, c3) values (?, ?, ?,)",

0 commit comments

Comments
 (0)