|
| 1 | +package com.clickhouse.jdbc.internal.parser.javacc; |
| 2 | + |
| 3 | +import com.clickhouse.client.config.ClickHouseDefaults; |
| 4 | +import com.clickhouse.data.ClickHouseChecker; |
| 5 | +import com.clickhouse.data.ClickHouseFormat; |
| 6 | +import com.clickhouse.data.ClickHouseUtils; |
| 7 | + |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +public class JdbcParseHandler extends ParseHandler { |
| 14 | + private static final String SETTING_MUTATIONS_SYNC = "mutations_sync"; |
| 15 | + |
| 16 | + private static final JdbcParseHandler INSTANCE; |
| 17 | + |
| 18 | + static { |
| 19 | + INSTANCE = new JdbcParseHandler(true, true, true); |
| 20 | + }; |
| 21 | + |
| 22 | + public static JdbcParseHandler getInstance() { |
| 23 | + return INSTANCE; |
| 24 | + } |
| 25 | + |
| 26 | + private final boolean allowLocalFile; |
| 27 | + private final boolean allowLightWeightDelete; |
| 28 | + private final boolean allowLightWeightUpdate; |
| 29 | + |
| 30 | + private void addMutationSetting(String sql, StringBuilder builder, Map<String, Integer> positions, |
| 31 | + Map<String, String> settings, int index) { |
| 32 | + boolean hasSetting = settings != null && !settings.isEmpty(); |
| 33 | + String setting = hasSetting ? settings.get(SETTING_MUTATIONS_SYNC) : null; |
| 34 | + if (setting == null) { |
| 35 | + String keyword = "SETTINGS"; |
| 36 | + Integer settingsIndex = positions.get(keyword); |
| 37 | + |
| 38 | + if (settingsIndex == null) { |
| 39 | + builder.append(sql.substring(index)).append(" SETTINGS mutations_sync=1"); |
| 40 | + if (hasSetting) { |
| 41 | + builder.append(','); |
| 42 | + } |
| 43 | + } else { |
| 44 | + builder.append(sql.substring(index, settingsIndex)).append("SETTINGS mutations_sync=1,") |
| 45 | + .append(sql.substring(settingsIndex + keyword.length())); |
| 46 | + } |
| 47 | + } else { |
| 48 | + builder.append(sql.substring(index)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private ClickHouseSqlStatement handleDelete(String sql, StatementType stmtType, String cluster, String database, |
| 53 | + String table, String input, String compressAlgorithm, String compressLevel, String format, String file, |
| 54 | + List<Integer> parameters, Map<String, Integer> positions, Map<String, String> settings, |
| 55 | + Set<String> tempTables) { |
| 56 | + StringBuilder builder = new StringBuilder(); |
| 57 | + int index = positions.get("DELETE"); |
| 58 | + if (index > 0) { |
| 59 | + builder.append(sql.substring(0, index)); |
| 60 | + } |
| 61 | + index = positions.get("FROM"); |
| 62 | + Integer whereIdx = positions.get("WHERE"); |
| 63 | + if (whereIdx != null) { |
| 64 | + builder.append("ALTER TABLE "); |
| 65 | + if (!ClickHouseChecker.isNullOrEmpty(database)) { |
| 66 | + builder.append('`').append(database).append('`').append('.'); |
| 67 | + } |
| 68 | + builder.append('`').append(table).append('`').append(" DELETE "); |
| 69 | + addMutationSetting(sql, builder, positions, settings, whereIdx); |
| 70 | + } else { |
| 71 | + builder.append("TRUNCATE TABLE").append(sql.substring(index + 4)); |
| 72 | + } |
| 73 | + return new ClickHouseSqlStatement(builder.toString(), stmtType, cluster, database, table, input, |
| 74 | + compressAlgorithm, compressLevel, format, file, parameters, null, settings, null); |
| 75 | + } |
| 76 | + |
| 77 | + private ClickHouseSqlStatement handleUpdate(String sql, StatementType stmtType, String cluster, String database, |
| 78 | + String table, String input, String compressAlgorithm, String compressLevel, String format, String file, |
| 79 | + List<Integer> parameters, Map<String, Integer> positions, Map<String, String> settings, |
| 80 | + Set<String> tempTables) { |
| 81 | + StringBuilder builder = new StringBuilder(); |
| 82 | + int index = positions.get("UPDATE"); |
| 83 | + if (index > 0) { |
| 84 | + builder.append(sql.substring(0, index)); |
| 85 | + } |
| 86 | + builder.append("ALTER TABLE "); |
| 87 | + index = positions.get("SET"); |
| 88 | + if (!ClickHouseChecker.isNullOrEmpty(database)) { |
| 89 | + builder.append('`').append(database).append('`').append('.'); |
| 90 | + } |
| 91 | + builder.append('`').append(table).append('`').append(" UPDATE"); // .append(sql.substring(index + 3)); |
| 92 | + addMutationSetting(sql, builder, positions, settings, index + 3); |
| 93 | + return new ClickHouseSqlStatement(builder.toString(), stmtType, cluster, database, table, input, |
| 94 | + compressAlgorithm, compressLevel, format, file, parameters, null, settings, null); |
| 95 | + } |
| 96 | + |
| 97 | + private ClickHouseSqlStatement handleInFileForInsertQuery(String sql, StatementType stmtType, String cluster, |
| 98 | + String database, String table, String input, String compressAlgorithm, String compressLevel, String format, |
| 99 | + String file, List<Integer> parameters, Map<String, Integer> positions, Map<String, String> settings, |
| 100 | + Set<String> tempTables) { |
| 101 | + StringBuilder builder = new StringBuilder(sql.length()); |
| 102 | + builder.append(sql.substring(0, positions.get("FROM"))); |
| 103 | + Integer index = positions.get("SETTINGS"); |
| 104 | + if (index == null || index < 0) { |
| 105 | + index = positions.get("FORMAT"); |
| 106 | + } |
| 107 | + if (index != null && index > 0) { |
| 108 | + builder.append(sql.substring(index)); |
| 109 | + } else { |
| 110 | + ClickHouseFormat f = ClickHouseFormat.fromFileName(ClickHouseUtils.unescape(file)); |
| 111 | + if (f == null) { |
| 112 | + f = (ClickHouseFormat) ClickHouseDefaults.FORMAT.getDefaultValue(); |
| 113 | + } |
| 114 | + format = f.name(); |
| 115 | + builder.append("FORMAT ").append(format); |
| 116 | + } |
| 117 | + return new ClickHouseSqlStatement(builder.toString(), stmtType, cluster, database, table, input, |
| 118 | + compressAlgorithm, compressLevel, format, file, parameters, null, settings, null); |
| 119 | + } |
| 120 | + |
| 121 | + private ClickHouseSqlStatement handleOutFileForSelectQuery(String sql, StatementType stmtType, String cluster, |
| 122 | + String database, String table, String input, String compressAlgorithm, String compressLevel, String format, |
| 123 | + String file, List<Integer> parameters, Map<String, Integer> positions, Map<String, String> settings, |
| 124 | + Set<String> tempTables) { |
| 125 | + StringBuilder builder = new StringBuilder(sql.length()); |
| 126 | + builder.append(sql.substring(0, positions.get("INTO"))); |
| 127 | + Integer index = positions.get("FORMAT"); |
| 128 | + if (index != null && index > 0) { |
| 129 | + builder.append(sql.substring(index)); |
| 130 | + } |
| 131 | + return new ClickHouseSqlStatement(builder.toString(), stmtType, cluster, database, table, input, |
| 132 | + compressAlgorithm, compressLevel, format, file, parameters, null, settings, null); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public ClickHouseSqlStatement handleStatement(String sql, StatementType stmtType, String cluster, String database, |
| 137 | + String table, String input, String compressAlgorithm, String compressLevel, String format, String file, |
| 138 | + List<Integer> parameters, Map<String, Integer> positions, Map<String, String> settings, |
| 139 | + Set<String> tempTables) { |
| 140 | + boolean hasFile = allowLocalFile && !ClickHouseChecker.isNullOrEmpty(file) && file.charAt(0) == '\''; |
| 141 | + ClickHouseSqlStatement s = null; |
| 142 | + if (stmtType == StatementType.DELETE) { |
| 143 | + s = allowLightWeightDelete ? s |
| 144 | + : handleDelete(sql, stmtType, cluster, database, table, input, compressAlgorithm, compressLevel, |
| 145 | + format, file, parameters, positions, settings, tempTables); |
| 146 | + } else if (stmtType == StatementType.UPDATE) { |
| 147 | + s = allowLightWeightUpdate ? s |
| 148 | + : handleUpdate(sql, stmtType, cluster, database, table, input, compressAlgorithm, compressLevel, |
| 149 | + format, file, parameters, positions, settings, tempTables); |
| 150 | + } else if (stmtType == StatementType.INSERT && hasFile) { |
| 151 | + s = handleInFileForInsertQuery(sql, stmtType, cluster, database, table, input, compressAlgorithm, |
| 152 | + compressLevel, format, file, parameters, positions, settings, tempTables); |
| 153 | + } else if (stmtType == StatementType.SELECT && hasFile) { |
| 154 | + s = handleOutFileForSelectQuery(sql, stmtType, cluster, database, table, input, compressAlgorithm, |
| 155 | + compressLevel, format, file, parameters, positions, settings, tempTables); |
| 156 | + } |
| 157 | + return s; |
| 158 | + } |
| 159 | + |
| 160 | + private JdbcParseHandler(boolean allowLightWeightDelete, boolean allowLightWeightUpdate, boolean allowLocalFile) { |
| 161 | + this.allowLightWeightDelete = allowLightWeightDelete; |
| 162 | + this.allowLightWeightUpdate = allowLightWeightUpdate; |
| 163 | + this.allowLocalFile = allowLocalFile; |
| 164 | + } |
| 165 | +} |
0 commit comments