Skip to content

Commit d4d8613

Browse files
committed
reworking code of the statement impl
1 parent 0e09ff7 commit d4d8613

File tree

7 files changed

+37
-53
lines changed

7 files changed

+37
-53
lines changed

clickhouse-jdbc/src/test/java/com/clickhouse/client/api/sql/SQLUtilsTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.clickhouse.client.api.sql;
22

3+
import com.clickhouse.jdbc.internal.SqlParser;
34
import org.testng.annotations.DataProvider;
45
import org.testng.annotations.Test;
56

@@ -129,6 +130,14 @@ public void testIsSimpleIdentifier(String identifier, boolean expected) {
129130
public void testIsSimpleIdentifier_NullInput() {
130131
SQLUtils.isSimpleIdentifier(null);
131132
}
132-
133133

134+
@Test
135+
public void testUnquoteIdentifier() {
136+
String[] names = new String[]{"test", "`test name1`", "\"test name 2\""};
137+
String[] expected = new String[]{"test", "test name1", "test name 2"};
138+
139+
for (int i = 0; i < names.length; i++) {
140+
assertEquals(SQLUtils.unquoteIdentifier(names[i]), expected[i]);
141+
}
142+
}
134143
}

client-v2/src/main/java/com/clickhouse/client/api/sql/SQLUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.clickhouse.client.api.sql;
22

3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
36
public class SQLUtils {
47
/**
58
* Escapes and quotes a string literal for use in SQL queries.
@@ -112,4 +115,17 @@ public static boolean isSimpleIdentifier(String identifier) {
112115
}
113116
return SIMPLE_IDENTIFIER_PATTERN.matcher(identifier).matches();
114117
}
118+
119+
private final static Pattern UNQUOTE_INDENTIFIER = Pattern.compile(
120+
"^[\\\"`]?(.+?)[\\\"`]?$"
121+
);
122+
123+
public static String unquoteIdentifier(String str) {
124+
Matcher matcher = UNQUOTE_INDENTIFIER.matcher(str.trim());
125+
if (matcher.find()) {
126+
return matcher.group(1);
127+
} else {
128+
return str;
129+
}
130+
}
115131
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ParsedPreparedStatement.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.clickhouse.jdbc.internal;
22

3+
import com.clickhouse.client.api.sql.SQLUtils;
34
import org.antlr.v4.runtime.tree.ErrorNode;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -144,7 +145,7 @@ public void enterQueryStmt(ClickHouseParser.QueryStmtContext ctx) {
144145
@Override
145146
public void enterUseStmt(ClickHouseParser.UseStmtContext ctx) {
146147
if (ctx.databaseIdentifier() != null) {
147-
setUseDatabase(SqlParser.unquoteIdentifier(ctx.databaseIdentifier().getText()));
148+
setUseDatabase(SQLUtils.unquoteIdentifier(ctx.databaseIdentifier().getText()));
148149
}
149150
}
150151

@@ -155,7 +156,7 @@ public void enterSetRoleStmt(ClickHouseParser.SetRoleStmtContext ctx) {
155156
} else {
156157
List<String> roles = new ArrayList<>();
157158
for (ClickHouseParser.IdentifierContext id : ctx.setRolesList().identifier()) {
158-
roles.add(SqlParser.unquoteIdentifier(id.getText()));
159+
roles.add(SQLUtils.unquoteIdentifier(id.getText()));
159160
}
160161
setRoles(roles);
161162
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ParsedStatement.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.clickhouse.jdbc.internal;
22

3+
import com.clickhouse.client.api.sql.SQLUtils;
34
import org.antlr.v4.runtime.tree.ErrorNode;
45

56
import java.util.ArrayList;
@@ -87,7 +88,7 @@ public void enterQueryStmt(ClickHouseParser.QueryStmtContext ctx) {
8788
@Override
8889
public void enterUseStmt(ClickHouseParser.UseStmtContext ctx) {
8990
if (ctx.databaseIdentifier() != null) {
90-
setUseDatabase(SqlParser.unquoteIdentifier(ctx.databaseIdentifier().getText()));
91+
setUseDatabase(SQLUtils.unquoteIdentifier(ctx.databaseIdentifier().getText()));
9192
}
9293
}
9394

@@ -98,7 +99,7 @@ public void enterSetRoleStmt(ClickHouseParser.SetRoleStmtContext ctx) {
9899
} else {
99100
List<String> roles = new ArrayList<>();
100101
for (ClickHouseParser.IdentifierContext id : ctx.setRolesList().identifier()) {
101-
roles.add(SqlParser.unquoteIdentifier(id.getText()));
102+
roles.add(SQLUtils.unquoteIdentifier(id.getText()));
102103
}
103104
setRoles(roles);
104105
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/SqlParser.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212

13-
import java.util.regex.Matcher;
14-
import java.util.regex.Pattern;
15-
1613
public class SqlParser {
1714

1815
private static final Logger LOG = LoggerFactory.getLogger(SqlParser.class);
@@ -41,28 +38,6 @@ public ParsedPreparedStatement parsePreparedStatement(String sql) {
4138
return parserListener;
4239
}
4340

44-
private final static Pattern UNQUOTE_INDENTIFIER = Pattern.compile(
45-
"^[\\\"`]?(.+?)[\\\"`]?$"
46-
);
47-
48-
public static String unquoteIdentifier(String str) {
49-
Matcher matcher = UNQUOTE_INDENTIFIER.matcher(str.trim());
50-
if (matcher.find()) {
51-
return matcher.group(1);
52-
} else {
53-
return str;
54-
}
55-
}
56-
57-
public static String escapeQuotes(String str) {
58-
if (str == null || str.isEmpty()) {
59-
return str;
60-
}
61-
return str
62-
.replace("'", "\\'")
63-
.replace("\"", "\\\"");
64-
}
65-
6641
private static class ParserErrorListener extends BaseErrorListener {
6742
@Override
6843
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.clickhouse.jdbc.metadata;
22

3+
import com.clickhouse.client.api.sql.SQLUtils;
34
import com.clickhouse.data.ClickHouseColumn;
45
import com.clickhouse.data.ClickHouseDataType;
56
import com.clickhouse.jdbc.ConnectionImpl;
67
import com.clickhouse.jdbc.Driver;
78
import com.clickhouse.jdbc.JdbcV2Wrapper;
89
import com.clickhouse.jdbc.ResultSetImpl;
10+
import com.clickhouse.jdbc.StatementImpl;
911
import com.clickhouse.jdbc.internal.ClientInfoProperties;
1012
import com.clickhouse.jdbc.internal.DriverProperties;
1113
import com.clickhouse.jdbc.internal.ExceptionUtils;
@@ -859,9 +861,9 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
859861
"'NO' as IS_AUTOINCREMENT, " +
860862
"'NO' as IS_GENERATEDCOLUMN " +
861863
" FROM system.columns" +
862-
" WHERE database LIKE '" + (schemaPattern == null ? "%" : SqlParser.escapeQuotes(schemaPattern)) + "'" +
863-
" AND table LIKE '" + (tableNamePattern == null ? "%" : SqlParser.escapeQuotes(tableNamePattern)) + "'" +
864-
" AND name LIKE '" + (columnNamePattern == null ? "%" : SqlParser.escapeQuotes(columnNamePattern)) + "'" +
864+
" WHERE database LIKE " + SQLUtils.enquoteLiteral(schemaPattern == null ? "%" : schemaPattern) +
865+
" AND table LIKE " + SQLUtils.enquoteLiteral(tableNamePattern == null ? "%" : tableNamePattern) +
866+
" AND name LIKE " + SQLUtils.enquoteLiteral(columnNamePattern == null ? "%" : columnNamePattern) +
865867
" ORDER BY TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION";
866868
try {
867869
return new MetadataResultSet((ResultSetImpl) connection.createStatement().executeQuery(sql))

jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/SqlParserTest.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,6 @@ public void testPreparedStatementInsertSQL() {
182182
assertEquals(parsed.getAssignValuesGroups(), 1);
183183
}
184184

185-
@Test
186-
public void testUnquoteIdentifier() {
187-
String[] names = new String[]{"test", "`test name1`", "\"test name 2\""};
188-
String[] expected = new String[]{"test", "test name1", "test name 2"};
189-
190-
for (int i = 0; i < names.length; i++) {
191-
assertEquals(SqlParser.unquoteIdentifier(names[i]), expected[i]);
192-
}
193-
}
194-
195-
@Test
196-
public void testEscapeQuotes() {
197-
String[] inStr = new String[]{"%valid_name%", "' OR 1=1 --", "\" OR 1=1 --"};
198-
String[] outStr = new String[]{"%valid_name%", "\\' OR 1=1 --", "\\\" OR 1=1 --"};
199-
200-
for (int i = 0; i < inStr.length; i++) {
201-
assertEquals(SqlParser.escapeQuotes(inStr[i]), outStr[i]);
202-
}
203-
}
204-
205185
@Test
206186
public void testStmtWithCasts() {
207187
String sql = "SELECT ?::integer, ?, '?::integer' FROM table WHERE v = ?::integer"; // CAST(?, INTEGER)

0 commit comments

Comments
 (0)