/
NikolayIvkin
/
sqlancer2
Обзор
Документация
Войти
/
NikolayIvkin
/
sqlancer2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/sqlancer/clickhouse/ClickHouseProvider.java
151 строка
6 KB
Ilya Yatsishin
Fix ClickHouse connection string
30 июн 2022, 16:32
30 июн 2022, 16:32
d4c43ea
Код
Авторство
О чём код?
package sqlancer.clickhouse; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.stream.Collectors; import com.google.auto.service.AutoService; import sqlancer.AbstractAction; import sqlancer.DatabaseProvider; import sqlancer.IgnoreMeException; import sqlancer.MainOptions; import sqlancer.Randomly; import sqlancer.SQLConnection; import sqlancer.SQLGlobalState; import sqlancer.SQLProviderAdapter; import sqlancer.StatementExecutor; import sqlancer.clickhouse.ClickHouseProvider.ClickHouseGlobalState; import sqlancer.clickhouse.gen.ClickHouseCommon; import sqlancer.clickhouse.gen.ClickHouseInsertGenerator; import sqlancer.clickhouse.gen.ClickHouseTableGenerator; import sqlancer.common.query.SQLQueryAdapter; import sqlancer.common.query.SQLQueryProvider; @AutoService(DatabaseProvider.class) public class ClickHouseProvider extends SQLProviderAdapter<ClickHouseGlobalState, ClickHouseOptions> { public ClickHouseProvider() { super(ClickHouseGlobalState.class, ClickHouseOptions.class); } public enum Action implements AbstractAction<ClickHouseGlobalState> { INSERT(ClickHouseInsertGenerator::getQuery); private final SQLQueryProvider<ClickHouseGlobalState> sqlQueryProvider; Action(SQLQueryProvider<ClickHouseGlobalState> sqlQueryProvider) { this.sqlQueryProvider = sqlQueryProvider; } @Override public SQLQueryAdapter getQuery(ClickHouseGlobalState state) throws Exception { return sqlQueryProvider.getQuery(state); } } private static int mapActions(ClickHouseGlobalState globalState, Action a) { Randomly r = globalState.getRandomly(); switch (a) { case INSERT: return r.getInteger(0, globalState.getOptions().getMaxNumberInserts()); default: throw new AssertionError(a); } } public static class ClickHouseGlobalState extends SQLGlobalState<ClickHouseOptions, ClickHouseSchema> { private ClickHouseOptions clickHouseOptions; public void setClickHouseOptions(ClickHouseOptions clickHouseOptions) { this.clickHouseOptions = clickHouseOptions; } public ClickHouseOptions getClickHouseOptions() { return this.clickHouseOptions; } public String getOracleName() { return String.join("_", this.clickHouseOptions.oracle.stream().map(o -> o.toString()).collect(Collectors.toList())); } @Override public String getDatabaseName() { return super.getDatabaseName() + this.getOracleName(); } @Override protected ClickHouseSchema readSchema() throws SQLException { return ClickHouseSchema.fromConnection(getConnection(), getDatabaseName()); } } @Override public void generateDatabase(ClickHouseGlobalState globalState) throws Exception { for (int i = 0; i < Randomly.fromOptions(1); i++) { boolean success; do { String tableName = ClickHouseCommon.createTableName(i); SQLQueryAdapter qt = ClickHouseTableGenerator.createTableStatement(tableName, globalState); success = globalState.executeStatement(qt); } while (!success); } StatementExecutor<ClickHouseGlobalState, Action> se = new StatementExecutor<>(globalState, Action.values(), ClickHouseProvider::mapActions, (q) -> { if (globalState.getSchema().getDatabaseTables().isEmpty()) { throw new IgnoreMeException(); } }); se.executeStatements(); } @Override public SQLConnection createDatabase(ClickHouseGlobalState globalState) throws SQLException { String host = globalState.getOptions().getHost(); int port = globalState.getOptions().getPort(); if (host == null) { host = ClickHouseOptions.DEFAULT_HOST; } if (port == MainOptions.NO_SET_PORT) { port = ClickHouseOptions.DEFAULT_PORT; } ClickHouseOptions clickHouseOptions = globalState.getDbmsSpecificOptions(); globalState.setClickHouseOptions(clickHouseOptions); String url = String.format("jdbc:clickhouse://%s:%d/%s", host, port, "default"); String databaseName = globalState.getDatabaseName(); Connection con = DriverManager.getConnection(url, globalState.getOptions().getUserName(), globalState.getOptions().getPassword()); String dropDatabaseCommand = "DROP DATABASE IF EXISTS " + databaseName; globalState.getState().logStatement(dropDatabaseCommand); String createDatabaseCommand = "CREATE DATABASE IF NOT EXISTS " + databaseName; globalState.getState().logStatement(createDatabaseCommand); try (Statement s = con.createStatement()) { s.execute(dropDatabaseCommand); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } try (Statement s = con.createStatement()) { s.execute(createDatabaseCommand); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } con.close(); con = DriverManager.getConnection(String.format("jdbc:clickhouse://%s:%d/%s", host, port, databaseName), globalState.getOptions().getUserName(), globalState.getOptions().getPassword()); return new SQLConnection(con); } @Override public String getDBMSName() { return "clickhouse"; } }