|
| 1 | +package sqlancer.clickhouse; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.sql.Connection; |
| 5 | +import java.sql.DriverManager; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.sql.Statement; |
| 8 | + |
| 9 | +import sqlancer.AbstractAction; |
| 10 | +import sqlancer.DatabaseProvider; |
| 11 | +import sqlancer.GlobalState; |
| 12 | +import sqlancer.IgnoreMeException; |
| 13 | +import sqlancer.Main.QueryManager; |
| 14 | +import sqlancer.Main.StateLogger; |
| 15 | +import sqlancer.Query; |
| 16 | +import sqlancer.QueryAdapter; |
| 17 | +import sqlancer.QueryProvider; |
| 18 | +import sqlancer.Randomly; |
| 19 | +import sqlancer.StateToReproduce; |
| 20 | +import sqlancer.StatementExecutor; |
| 21 | +import sqlancer.clickhouse.ClickhouseProvider.ClickhouseGlobalState; |
| 22 | +import sqlancer.clickhouse.gen.ClickhouseInsertGenerator; |
| 23 | +import sqlancer.clickhouse.gen.ClickhouseTableGenerator; |
| 24 | + |
| 25 | +public class ClickhouseProvider implements DatabaseProvider<ClickhouseGlobalState, ClickhouseOptions> { |
| 26 | + |
| 27 | + public static enum Action implements AbstractAction<ClickhouseGlobalState> { |
| 28 | + |
| 29 | + INSERT(ClickhouseInsertGenerator::getQuery); |
| 30 | + |
| 31 | + private final QueryProvider<ClickhouseGlobalState> queryProvider; |
| 32 | + |
| 33 | + private Action(QueryProvider<ClickhouseGlobalState> queryProvider) { |
| 34 | + this.queryProvider = queryProvider; |
| 35 | + } |
| 36 | + |
| 37 | + public Query getQuery(ClickhouseGlobalState state) throws SQLException { |
| 38 | + return queryProvider.getQuery(state); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private static int mapActions(ClickhouseGlobalState globalState, Action a) { |
| 43 | + Randomly r = globalState.getRandomly(); |
| 44 | + switch (a) { |
| 45 | + case INSERT: |
| 46 | + return r.getInteger(0, globalState.getOptions().getMaxNumberInserts()); |
| 47 | + default: |
| 48 | + throw new AssertionError(a); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static class ClickhouseGlobalState extends GlobalState<ClickhouseOptions> { |
| 53 | + |
| 54 | + private ClickhouseSchema schema; |
| 55 | + |
| 56 | + public void setSchema(ClickhouseSchema schema) { |
| 57 | + this.schema = schema; |
| 58 | + } |
| 59 | + |
| 60 | + public ClickhouseSchema getSchema() { |
| 61 | + return schema; |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void generateAndTestDatabase(ClickhouseGlobalState globalState) throws SQLException { |
| 68 | + StateLogger logger = globalState.getLogger(); |
| 69 | + QueryManager manager = globalState.getManager(); |
| 70 | + globalState.setSchema( |
| 71 | + ClickhouseSchema.fromConnection(globalState.getConnection(), globalState.getDatabaseName())); |
| 72 | + for (int i = 0; i < Randomly.fromOptions(1); i++) { |
| 73 | + boolean success = false; |
| 74 | + do { |
| 75 | + Query qt = new ClickhouseTableGenerator().getQuery(globalState); |
| 76 | + success = manager.execute(qt); |
| 77 | + logger.writeCurrent(globalState.getState()); |
| 78 | + globalState.setSchema( |
| 79 | + ClickhouseSchema.fromConnection(globalState.getConnection(), globalState.getDatabaseName())); |
| 80 | + try { |
| 81 | + logger.getCurrentFileWriter().close(); |
| 82 | + } catch (IOException e) { |
| 83 | + // TODO Auto-generated catch block |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + logger.currentFileWriter = null; |
| 87 | + } while (!success); |
| 88 | + } |
| 89 | + |
| 90 | + StatementExecutor<ClickhouseGlobalState, Action> se = new StatementExecutor<ClickhouseGlobalState, Action>( |
| 91 | + globalState, globalState.getDatabaseName(), Action.values(), ClickhouseProvider::mapActions, (q) -> { |
| 92 | + if (q.couldAffectSchema()) { |
| 93 | + globalState.setSchema(ClickhouseSchema.fromConnection(globalState.getConnection(), |
| 94 | + globalState.getDatabaseName())); |
| 95 | + } |
| 96 | + if (globalState.getSchema().getDatabaseTables().isEmpty()) { |
| 97 | + throw new IgnoreMeException(); |
| 98 | + } |
| 99 | + }); |
| 100 | + se.executeStatements(); |
| 101 | + manager.incrementCreateDatabase(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Connection createDatabase(String databaseName, StateToReproduce state) throws SQLException { |
| 106 | + String url = "jdbc:clickhouse://localhost:8123/test"; |
| 107 | + Connection con = DriverManager.getConnection(url, "", "password"); |
| 108 | + state.statements.add(new QueryAdapter("USE test")); |
| 109 | + state.statements.add(new QueryAdapter("DROP DATABASE IF EXISTS " + databaseName + " CASCADE")); |
| 110 | + String createDatabaseCommand = "CREATE DATABASE " + databaseName; |
| 111 | + state.statements.add(new QueryAdapter(createDatabaseCommand)); |
| 112 | + state.statements.add(new QueryAdapter("USE " + databaseName)); |
| 113 | + try (Statement s = con.createStatement()) { |
| 114 | + s.execute("DROP DATABASE IF EXISTS " + databaseName); |
| 115 | + } |
| 116 | + try (Statement s = con.createStatement()) { |
| 117 | + s.execute(createDatabaseCommand); |
| 118 | + } |
| 119 | + con.close(); |
| 120 | + con = DriverManager.getConnection("jdbc:clickhouse://localhost:8123/" + databaseName, "", "password"); |
| 121 | + return con; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public String getDBMSName() { |
| 126 | + return "Clickhouse"; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public StateToReproduce getStateToReproduce(String databaseName) { |
| 131 | + return new StateToReproduce(databaseName); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public ClickhouseGlobalState generateGlobalState() { |
| 136 | + return new ClickhouseGlobalState(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public ClickhouseOptions getCommand() { |
| 141 | + return new ClickhouseOptions(); |
| 142 | + } |
| 143 | +} |
0 commit comments