-
Notifications
You must be signed in to change notification settings - Fork 614
[jdbc-v2] Support multi-dot notation for database names #2663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
185c626
1f9c61f
5dd3bbb
5287098
823b35d
12f27b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| import com.clickhouse.jdbc.internal.parser.antlr4.ClickHouseParserBaseListener; | ||
| import com.clickhouse.jdbc.internal.parser.javacc.ClickHouseSqlParser; | ||
| import com.clickhouse.jdbc.internal.parser.javacc.ClickHouseSqlStatement; | ||
| import com.clickhouse.jdbc.internal.parser.javacc.ClickHouseSqlUtils; | ||
| import com.clickhouse.jdbc.internal.parser.javacc.JdbcParseHandler; | ||
| import com.clickhouse.jdbc.internal.parser.javacc.StatementType; | ||
| import org.antlr.v4.runtime.BaseErrorListener; | ||
|
|
@@ -248,6 +249,56 @@ public void enterColumnExprPrecedence3(ClickHouseParser.ColumnExprPrecedence3Con | |
| super.enterColumnExprPrecedence3(ctx); | ||
| } | ||
|
|
||
| private String unquoteTableIdentifier(String rawTableId) { | ||
|
||
| if (rawTableId == null || rawTableId.isEmpty()) { | ||
| return rawTableId; | ||
| } | ||
|
|
||
| // Parse respecting quoted identifiers - don't split dots inside quotes | ||
| StringBuilder result = new StringBuilder(); | ||
| boolean inQuote = false; | ||
| char quoteChar = 0; | ||
| StringBuilder currentPart = new StringBuilder(); | ||
|
|
||
| for (int i = 0; i < rawTableId.length(); i++) { | ||
| char ch = rawTableId.charAt(i); | ||
|
|
||
| if (!inQuote && (ch == '`' || ch == '"' || ch == '\'')) { | ||
| inQuote = true; | ||
| quoteChar = ch; | ||
| currentPart.append(ch); | ||
| } else if (inQuote && ch == quoteChar) { | ||
| // Check for escaped quote (doubled quote) | ||
| if (i + 1 < rawTableId.length() && rawTableId.charAt(i + 1) == quoteChar) { | ||
| currentPart.append(ch).append(ch); | ||
| i++; // Skip the next quote | ||
| } else { | ||
| inQuote = false; | ||
| currentPart.append(ch); | ||
| } | ||
| } else if (!inQuote && ch == '.') { | ||
devdavidkarlsson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Dot outside quotes - split here | ||
| if (result.length() > 0) { | ||
| result.append('.'); | ||
| } | ||
| result.append(ClickHouseSqlUtils.unescape(currentPart.toString())); | ||
| currentPart.setLength(0); | ||
| } else { | ||
| currentPart.append(ch); | ||
| } | ||
| } | ||
|
|
||
| // Append the last part | ||
| if (currentPart.length() > 0) { | ||
| if (result.length() > 0) { | ||
| result.append('.'); | ||
| } | ||
| result.append(ClickHouseSqlUtils.unescape(currentPart.toString())); | ||
| } | ||
|
|
||
| return result.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitErrorNode(ErrorNode node) { | ||
| parsedStatement.setHasErrors(true); | ||
|
|
@@ -268,15 +319,15 @@ public void enterAssignmentValuesList(ClickHouseParser.AssignmentValuesListConte | |
| @Override | ||
| public void enterTableExprIdentifier(ClickHouseParser.TableExprIdentifierContext ctx) { | ||
| if (ctx.tableIdentifier() != null) { | ||
| parsedStatement.setTable(SQLUtils.unquoteIdentifier(ctx.tableIdentifier().getText())); | ||
| parsedStatement.setTable(unquoteTableIdentifier(ctx.tableIdentifier().getText())); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void enterInsertStmt(ClickHouseParser.InsertStmtContext ctx) { | ||
| ClickHouseParser.TableIdentifierContext tableId = ctx.tableIdentifier(); | ||
| if (tableId != null) { | ||
| parsedStatement.setTable(SQLUtils.unquoteIdentifier(tableId.getText())); | ||
| parsedStatement.setTable(unquoteTableIdentifier(tableId.getText())); | ||
| } | ||
|
|
||
| ClickHouseParser.ColumnsClauseContext columns = ctx.columnsClause(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -903,18 +903,32 @@ void nestedIdentifier(): {} { | |
| (<ASTERISK> | anyIdentifier()) (LOOKAHEAD(2) <DOT> (<ASTERISK> | anyIdentifier()))* | ||
| } | ||
|
|
||
| void tableIdentifier(boolean record): { Token t; } { | ||
| void tableIdentifier(boolean record): { Token t; java.util.List<String> parts = new java.util.ArrayList<>(); } { | ||
| ( | ||
| (LOOKAHEAD(2) databaseIdentifier(record) <DOT>)? t = anyIdentifier() | ||
| t = anyIdentifier() { parts.add(ClickHouseSqlUtils.unescape(t.image)); } | ||
| ( | ||
| LOOKAHEAD(2) <DOT> t = anyIdentifier() { parts.add(ClickHouseSqlUtils.unescape(t.image)); } | ||
| )* | ||
| (LOOKAHEAD(2) | ||
| <LPAREN> { token_source.addCustomKeywordPosition(ClickHouseSqlStatement.KEYWORD_TABLE_COLUMNS_START, token); } | ||
| anyExprList() | ||
| <RPAREN> { token_source.addCustomKeywordPosition(ClickHouseSqlStatement.KEYWORD_TABLE_COLUMNS_END, token); } | ||
| )? | ||
| ) | ||
| { | ||
| if (record && t != null && token_source.table == null) { | ||
| token_source.table = ClickHouseSqlUtils.unescape(t.image); | ||
| if (record && token_source.table == null && parts.size() > 0) { | ||
|
||
| // Last part is always the table name | ||
| token_source.table = parts.get(parts.size() - 1); | ||
|
|
||
| // All parts before the last are the database (if any) | ||
| if (parts.size() > 1) { | ||
| StringBuilder db = new StringBuilder(); | ||
| for (int i = 0; i < parts.size() - 1; i++) { | ||
| if (i > 0) db.append('.'); | ||
| db.append(parts.get(i)); | ||
| } | ||
| token_source.database = db.toString(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.