Skip to content

Commit 7c8934a

Browse files
committed
small cleanup of unused methods
1 parent 5a9bccf commit 7c8934a

File tree

9 files changed

+139
-232
lines changed

9 files changed

+139
-232
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
import java.util.List;
5454
import java.util.Map;
5555
import java.util.UUID;
56-
import java.util.stream.IntStream;
56+
import java.util.regex.Matcher;
57+
import java.util.regex.Pattern;
5758

5859
public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper {
5960
private static final Logger LOG = LoggerFactory.getLogger(PreparedStatementImpl.class);
@@ -376,7 +377,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
376377
if (parsedPreparedStatement.isHasResultSet()) {
377378
try {
378379
// Replace '?' with NULL to make SQL valid for DESCRIBE
379-
String sql = JdbcUtils.replaceQuestionMarks(originalSql, JdbcUtils.NULL);
380+
String sql = replaceQuestionMarks(originalSql, NULL_LITERAL);
380381
TableSchema tSchema = connection.getClient().getTableSchemaFromQuery(sql);
381382
resultSetMetaData = new ResultSetMetaDataImpl(tSchema.getColumns(),
382383
connection.getSchema(), connection.getCatalog(),
@@ -398,6 +399,28 @@ public ResultSetMetaData getMetaData() throws SQLException {
398399
return resultSetMetaData;
399400
}
400401

402+
public static final String NULL_LITERAL = "NULL";
403+
404+
private static final Pattern REPLACE_Q_MARK_PATTERN = Pattern.compile("(\"[^\"]*\"|`[^`]*`|'[^']*')|(\\?)");
405+
406+
public static String replaceQuestionMarks(String sql, String replacement) {
407+
Matcher matcher = REPLACE_Q_MARK_PATTERN.matcher(sql);
408+
409+
StringBuilder result = new StringBuilder();
410+
411+
while (matcher.find()) {
412+
if (matcher.group(1) != null) {
413+
// Quoted string — keep as-is
414+
matcher.appendReplacement(result, Matcher.quoteReplacement(matcher.group(1)));
415+
} else if (matcher.group(2) != null) {
416+
// Question mark outside quotes — replace it
417+
matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
418+
}
419+
}
420+
matcher.appendTail(result);
421+
return result.toString();
422+
}
423+
401424
@Override
402425
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
403426
checkClosed();

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

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@
1717
import java.time.OffsetDateTime;
1818
import java.time.ZonedDateTime;
1919
import java.time.temporal.TemporalAccessor;
20-
import java.util.ArrayList;
2120
import java.util.Collections;
2221
import java.util.HashMap;
23-
import java.util.List;
2422
import java.util.Map;
2523
import java.util.TreeMap;
26-
import java.util.regex.Matcher;
27-
import java.util.regex.Pattern;
2824

2925
public class JdbcUtils {
3026
//Define a map to store the mapping between ClickHouse data types and SQL data types
@@ -134,87 +130,6 @@ public static Class<?> convertToJavaClass(ClickHouseDataType clickhouseType) {
134130
return DATA_TYPE_CLASS_MAP.get(clickhouseType);
135131
}
136132

137-
public static List<String> tokenizeSQL(String sql) {
138-
List<String> tokens = new ArrayList<>();
139-
140-
// Remove SQL comments
141-
String withoutComments = sql
142-
.replaceAll("--.*?$", "") // Remove single-line comments
143-
.replaceAll("/\\*.*?\\*/", ""); // Remove multi-line comments
144-
145-
StringBuilder currentToken = new StringBuilder();
146-
boolean insideQuotes = false;
147-
148-
for (int i = 0; i < withoutComments.length(); i++) {
149-
char c = withoutComments.charAt(i);
150-
151-
if (c == '"') {
152-
insideQuotes = !insideQuotes; // Toggle the insideQuotes flag
153-
currentToken.append(c); // Include the quote in the token
154-
} else if (Character.isWhitespace(c) && !insideQuotes) {
155-
if (currentToken.length() > 0) {
156-
tokens.add(currentToken.toString());
157-
currentToken.setLength(0); // Clear the current token
158-
}
159-
} else {
160-
currentToken.append(c);
161-
}
162-
}
163-
164-
// Add the last token if it exists
165-
if (currentToken.length() > 0) {
166-
tokens.add(currentToken.toString());
167-
}
168-
169-
return tokens;
170-
}
171-
172-
public static boolean isBlank(String str) {
173-
return str == null || str.isEmpty() || str.trim().isEmpty();
174-
}
175-
176-
public static boolean containsIgnoresCase(List<String> list, String str) {
177-
if (list == null || list.isEmpty() || isBlank(str)) {
178-
return false;
179-
}
180-
181-
for (String s : list) {
182-
if (s.equalsIgnoreCase(str)) {
183-
return true;
184-
}
185-
}
186-
187-
return false;
188-
}
189-
190-
public static int indexOfIgnoresCase(List<String> list, String str) {
191-
if (list == null || list.isEmpty() || isBlank(str)) {
192-
return -1;
193-
}
194-
195-
for (int i = 0; i < list.size(); i++) {
196-
if (list.get(i).equalsIgnoreCase(str)) {
197-
return i;
198-
}
199-
}
200-
201-
return -1;
202-
}
203-
204-
public static String generateSqlTypeSizes(String columnName) {
205-
StringBuilder sql = new StringBuilder("multiIf(");
206-
sql.append("character_octet_length IS NOT NULL, character_octet_length, ");
207-
for (ClickHouseDataType type : ClickHouseDataType.values()) {
208-
if (type.getByteLength() > 0) {
209-
sql.append(columnName).append(" == '").append(type.name()).append("', ").append(type.getByteLength()).append(", ");
210-
}
211-
}
212-
sql.append("numeric_precision IS NOT NULL, numeric_precision, ");
213-
sql.append("0)");
214-
return sql.toString();
215-
}
216-
217-
218133
public static Object convert(Object value, Class<?> type) throws SQLException {
219134
if (value == null || type == null) {
220135
return value;
@@ -271,48 +186,4 @@ public static Object convert(Object value, Class<?> type) throws SQLException {
271186

272187
throw new SQLException("Unsupported conversion from " + value.getClass().getName() + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION);
273188
}
274-
275-
public static String escapeQuotes(String str) {
276-
if (str == null || str.isEmpty()) {
277-
return str;
278-
}
279-
return str
280-
.replace("'", "\\'")
281-
.replace("\"", "\\\"");
282-
}
283-
284-
private static Pattern UNQUOTE_TABLE_NAME = Pattern.compile(
285-
"^[\\\"`]?(.+?)[\\\"`]?$"
286-
);
287-
288-
public static String unquoteIdentifier(String str) {
289-
Matcher matcher = UNQUOTE_TABLE_NAME.matcher(str.trim());
290-
if (matcher.find()) {
291-
return matcher.group(1);
292-
} else {
293-
return str;
294-
}
295-
}
296-
297-
public static final String NULL = "NULL";
298-
299-
private static final Pattern REPLACE_Q_MARK_PATTERN = Pattern.compile("(\"[^\"]*\"|`[^`]*`|'[^']*')|(\\?)");
300-
301-
public static String replaceQuestionMarks(String sql, String replacement) {
302-
Matcher matcher = REPLACE_Q_MARK_PATTERN.matcher(sql);
303-
304-
StringBuilder result = new StringBuilder();
305-
306-
while (matcher.find()) {
307-
if (matcher.group(1) != null) {
308-
// Quoted string — keep as-is
309-
matcher.appendReplacement(result, Matcher.quoteReplacement(matcher.group(1)));
310-
} else if (matcher.group(2) != null) {
311-
// Question mark outside quotes — replace it
312-
matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
313-
}
314-
}
315-
matcher.appendTail(result);
316-
return result.toString();
317-
}
318189
}

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

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

3+
import com.clickhouse.jdbc.PreparedStatementImpl;
34
import org.antlr.v4.runtime.tree.ErrorNode;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -11,7 +12,6 @@
1112

1213
/**
1314
* Parser listener that collects information for prepared statement.
14-
*
1515
*/
1616
public class ParsedPreparedStatement extends ClickHouseParserBaseListener {
1717
private static final Logger LOG = LoggerFactory.getLogger(ParsedPreparedStatement.class);
@@ -68,10 +68,6 @@ public boolean isInsertWithSelect() {
6868
return insertWithSelect;
6969
}
7070

71-
public void setArgCount(int argCount) {
72-
this.argCount = argCount;
73-
}
74-
7571
public int getArgCount() {
7672
return argCount;
7773
}
@@ -128,12 +124,19 @@ public void setUseFunction(boolean useFunction) {
128124
this.useFunction = useFunction;
129125
}
130126

127+
public boolean isHasErrors() {
128+
return hasErrors;
129+
}
130+
131+
public void setHasErrors(boolean hasErrors) {
132+
this.hasErrors = hasErrors;
133+
}
134+
131135
@Override
132136
public void enterQueryStmt(ClickHouseParser.QueryStmtContext ctx) {
133137
ClickHouseParser.QueryContext qCtx = ctx.query();
134138
if (qCtx != null) {
135-
if (qCtx.selectStmt() != null || qCtx.selectUnionStmt() != null || qCtx.showStmt() != null
136-
|| qCtx.describeStmt() != null) {
139+
if (qCtx.selectStmt() != null || qCtx.selectUnionStmt() != null || qCtx.showStmt() != null || qCtx.describeStmt() != null) {
137140
setHasResultSet(true);
138141
}
139142
}
@@ -142,7 +145,7 @@ public void enterQueryStmt(ClickHouseParser.QueryStmtContext ctx) {
142145
@Override
143146
public void enterUseStmt(ClickHouseParser.UseStmtContext ctx) {
144147
if (ctx.databaseIdentifier() != null) {
145-
setUseDatabase(JdbcUtils.unquoteIdentifier(ctx.databaseIdentifier().getText()));
148+
setUseDatabase(SqlParser.unquoteIdentifier(ctx.databaseIdentifier().getText()));
146149
}
147150
}
148151

@@ -153,7 +156,7 @@ public void enterSetRoleStmt(ClickHouseParser.SetRoleStmtContext ctx) {
153156
} else {
154157
List<String> roles = new ArrayList<>();
155158
for (ClickHouseParser.IdentifierContext id : ctx.setRolesList().identifier()) {
156-
roles.add(JdbcUtils.unquoteIdentifier(id.getText()));
159+
roles.add(SqlParser.unquoteIdentifier(id.getText()));
157160
}
158161
setRoles(roles);
159162
}
@@ -166,12 +169,12 @@ public void enterColumnExprParam(ClickHouseParser.ColumnExprParamContext ctx) {
166169

167170
@Override
168171
public void visitErrorNode(ErrorNode node) {
169-
hasErrors = true;
172+
setHasErrors(true);
170173
}
171174

172175
@Override
173176
public void enterInsertParameterFuncExpr(ClickHouseParser.InsertParameterFuncExprContext ctx) {
174-
useFunction = true;
177+
setUseFunction(true);
175178
}
176179

177180
@Override
@@ -190,15 +193,15 @@ private void appendParameter(int startIndex) {
190193
if (argCount > paramPositions.length) {
191194
paramPositions = Arrays.copyOf(paramPositions, paramPositions.length + 10);
192195
}
193-
paramPositions[argCount-1] = startIndex;
196+
paramPositions[argCount - 1] = startIndex;
194197
if (LOG.isTraceEnabled()) {
195198
LOG.trace("parameter position {}", startIndex);
196199
}
197200
}
198201

199202
@Override
200203
public void enterInsertStmt(ClickHouseParser.InsertStmtContext ctx) {
201-
ClickHouseParser.TableIdentifierContext tableId = ctx.tableIdentifier();
204+
ClickHouseParser.TableIdentifierContext tableId = ctx.tableIdentifier();
202205
if (tableId != null) {
203206
this.table = tableId.identifier().IDENTIFIER().getText();
204207
}

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

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

3+
import org.antlr.v4.runtime.tree.ErrorNode;
4+
35
import java.util.ArrayList;
46
import java.util.Collections;
57
import java.util.List;
@@ -16,6 +18,8 @@ public class ParsedStatement extends ClickHouseParserBaseListener {
1618

1719
private List<String> roles;
1820

21+
private boolean hasErrors;
22+
1923
public void setUseDatabase(String useDatabase) {
2024
this.useDatabase = useDatabase;
2125
}
@@ -56,6 +60,19 @@ public List<String> getRoles() {
5660
return roles;
5761
}
5862

63+
public boolean isHasErrors() {
64+
return hasErrors;
65+
}
66+
67+
public void setHasErrors(boolean hasErrors) {
68+
this.hasErrors = hasErrors;
69+
}
70+
71+
@Override
72+
public void visitErrorNode(ErrorNode node) {
73+
setHasErrors(true);
74+
}
75+
5976
@Override
6077
public void enterQueryStmt(ClickHouseParser.QueryStmtContext ctx) {
6178
ClickHouseParser.QueryContext qCtx = ctx.query();
@@ -70,7 +87,7 @@ public void enterQueryStmt(ClickHouseParser.QueryStmtContext ctx) {
7087
@Override
7188
public void enterUseStmt(ClickHouseParser.UseStmtContext ctx) {
7289
if (ctx.databaseIdentifier() != null) {
73-
setUseDatabase(JdbcUtils.unquoteIdentifier(ctx.databaseIdentifier().getText()));
90+
setUseDatabase(SqlParser.unquoteIdentifier(ctx.databaseIdentifier().getText()));
7491
}
7592
}
7693

@@ -81,7 +98,7 @@ public void enterSetRoleStmt(ClickHouseParser.SetRoleStmtContext ctx) {
8198
} else {
8299
List<String> roles = new ArrayList<>();
83100
for (ClickHouseParser.IdentifierContext id : ctx.setRolesList().identifier()) {
84-
roles.add(JdbcUtils.unquoteIdentifier(id.getText()));
101+
roles.add(SqlParser.unquoteIdentifier(id.getText()));
85102
}
86103
setRoles(roles);
87104
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.antlr.v4.runtime.CommonTokenStream;
66
import org.antlr.v4.runtime.tree.IterativeParseTreeWalker;
77

8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
10+
811
public class SqlParser {
912

1013
public ParsedStatement parsedStatement(String sql) {
@@ -29,4 +32,26 @@ public ParsedPreparedStatement parsePreparedStatement(String sql) {
2932
IterativeParseTreeWalker.DEFAULT.walk(parserListener, parseTree);
3033
return parserListener;
3134
}
35+
36+
private final static Pattern UNQUOTE_INDENTIFIER = Pattern.compile(
37+
"^[\\\"`]?(.+?)[\\\"`]?$"
38+
);
39+
40+
public static String unquoteIdentifier(String str) {
41+
Matcher matcher = UNQUOTE_INDENTIFIER.matcher(str.trim());
42+
if (matcher.find()) {
43+
return matcher.group(1);
44+
} else {
45+
return str;
46+
}
47+
}
48+
49+
public static String escapeQuotes(String str) {
50+
if (str == null || str.isEmpty()) {
51+
return str;
52+
}
53+
return str
54+
.replace("'", "\\'")
55+
.replace("\"", "\\\"");
56+
}
3257
}

0 commit comments

Comments
 (0)