|
| 1 | +package sqlancer.questdb; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.sql.Statement; |
| 7 | +import java.util.Properties; |
| 8 | + |
| 9 | +import com.google.auto.service.AutoService; |
| 10 | + |
| 11 | +import sqlancer.AbstractAction; |
| 12 | +import sqlancer.DatabaseProvider; |
| 13 | +import sqlancer.SQLConnection; |
| 14 | +import sqlancer.SQLGlobalState; |
| 15 | +import sqlancer.SQLProviderAdapter; |
| 16 | +import sqlancer.common.query.SQLQueryAdapter; |
| 17 | +import sqlancer.common.query.SQLQueryProvider; |
| 18 | +import sqlancer.questdb.QuestDBProvider.QuestDBGlobalState; |
| 19 | +import sqlancer.questdb.gen.QuestDBDeleteGenerator; |
| 20 | +import sqlancer.questdb.gen.QuestDBIndexGenerator; |
| 21 | +import sqlancer.questdb.gen.QuestDBInsertGenerator; |
| 22 | +import sqlancer.questdb.gen.QuestDBTableGenerator; |
| 23 | + |
| 24 | +@AutoService(DatabaseProvider.class) |
| 25 | +public class QuestDBProvider extends SQLProviderAdapter<QuestDBGlobalState, QuestDBOptions> { |
| 26 | + public QuestDBProvider() { |
| 27 | + super(QuestDBGlobalState.class, QuestDBOptions.class); |
| 28 | + } |
| 29 | + |
| 30 | + public enum Action implements AbstractAction<QuestDBGlobalState> { |
| 31 | + INSERT(QuestDBInsertGenerator::getQuery), // |
| 32 | + CREATE_INDEX(QuestDBIndexGenerator::getQuery), // |
| 33 | + DELETE(QuestDBDeleteGenerator::generate); // |
| 34 | + // TODO: maybe implement these later |
| 35 | + // UPDATE(QuestDBUpdateGenerator::getQuery), // |
| 36 | + // CREATE_VIEW(QuestDBViewGenerator::generate), // |
| 37 | + |
| 38 | + private final SQLQueryProvider<QuestDBGlobalState> sqlQueryProvider; |
| 39 | + |
| 40 | + Action(SQLQueryProvider<QuestDBGlobalState> sqlQueryProvider) { |
| 41 | + this.sqlQueryProvider = sqlQueryProvider; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public SQLQueryAdapter getQuery(QuestDBGlobalState state) throws Exception { |
| 46 | + return sqlQueryProvider.getQuery(state); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static class QuestDBGlobalState extends SQLGlobalState<QuestDBOptions, QuestDBSchema> { |
| 51 | + |
| 52 | + @Override |
| 53 | + protected QuestDBSchema readSchema() throws SQLException { |
| 54 | + return QuestDBSchema.fromConnection(getConnection(), getDatabaseName()); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void generateDatabase(QuestDBGlobalState globalState) throws Exception { |
| 61 | + // TODO: should follow duckdb or cockrachdb? what's the difference between generate and create db? |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public SQLConnection createDatabase(QuestDBGlobalState globalState) throws Exception { |
| 66 | + String host = globalState.getOptions().getHost(); |
| 67 | + int port = globalState.getOptions().getPort(); |
| 68 | + if (host == null) { |
| 69 | + host = QuestDBOptions.DEFAULT_HOST; |
| 70 | + } |
| 71 | + if (port == sqlancer.MainOptions.NO_SET_PORT) { |
| 72 | + port = QuestDBOptions.DEFAULT_PORT; |
| 73 | + } |
| 74 | + // TODO(anxing): maybe not hardcode here... |
| 75 | + String databaseName = "qdb"; |
| 76 | + String tableName = "test"; |
| 77 | + String url = String.format("jdbc:postgresql://%s:%d/%s", host, port, databaseName); |
| 78 | + // use QuestDB default username & password for Postgres JDBC |
| 79 | + Properties properties = new Properties(); |
| 80 | + properties.setProperty("user", globalState.getDbmsSpecificOptions().getUserName()); |
| 81 | + properties.setProperty("password", globalState.getDbmsSpecificOptions().getPassword()); |
| 82 | + properties.setProperty("sslmode", "disable"); |
| 83 | + |
| 84 | + Connection con = DriverManager.getConnection(url, properties); |
| 85 | + // QuestDB cannot create or drop `DATABASE`, can only create or drop `TABLE` |
| 86 | + globalState.getState().logStatement("DROP TABLE IF EXISTS " + tableName + " CASCADE"); |
| 87 | + SQLQueryAdapter createTableCommand = new QuestDBTableGenerator().getQuery(globalState); |
| 88 | + globalState.getState().logStatement(createTableCommand); |
| 89 | + |
| 90 | + try (Statement s = con.createStatement()) { |
| 91 | + s.execute("DROP TABLE IF EXISTS " + tableName); |
| 92 | + } |
| 93 | + try (Statement s = con.createStatement()) { |
| 94 | + s.execute(createTableCommand.getQueryString()); |
| 95 | + } |
| 96 | + con.close(); |
| 97 | + con = DriverManager.getConnection(url, properties); |
| 98 | + return new SQLConnection(con); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String getDBMSName() { |
| 103 | + return "questdb"; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments