|
| 1 | +package ai.chat2db.plugin.timeplus; |
| 2 | + |
| 3 | +import ai.chat2db.spi.DBManage; |
| 4 | +import ai.chat2db.spi.jdbc.DefaultDBManage; |
| 5 | +import ai.chat2db.spi.model.AsyncContext; |
| 6 | +import ai.chat2db.spi.sql.ConnectInfo; |
| 7 | +import ai.chat2db.spi.sql.SQLExecutor; |
| 8 | +import java.sql.*; |
| 9 | +import java.util.Objects; |
| 10 | +import org.apache.commons.lang3.StringUtils; |
| 11 | + |
| 12 | +public class TimeplusDBManage extends DefaultDBManage implements DBManage { |
| 13 | + |
| 14 | + @Override |
| 15 | + public void exportDatabase( |
| 16 | + Connection connection, |
| 17 | + String databaseName, |
| 18 | + String schemaName, |
| 19 | + AsyncContext asyncContext |
| 20 | + ) throws SQLException { |
| 21 | + exportTablesOrViewsOrDictionaries( |
| 22 | + connection, |
| 23 | + databaseName, |
| 24 | + schemaName, |
| 25 | + asyncContext |
| 26 | + ); |
| 27 | + exportFunctions(connection, asyncContext); |
| 28 | + } |
| 29 | + |
| 30 | + private void exportFunctions( |
| 31 | + Connection connection, |
| 32 | + AsyncContext asyncContext |
| 33 | + ) throws SQLException { |
| 34 | + String sql = |
| 35 | + "SELECT name,create_query from system.functions where origin='SQLUserDefined'"; |
| 36 | + try ( |
| 37 | + ResultSet resultSet = connection.createStatement().executeQuery(sql) |
| 38 | + ) { |
| 39 | + while (resultSet.next()) { |
| 40 | + StringBuilder sqlBuilder = new StringBuilder(); |
| 41 | + sqlBuilder |
| 42 | + .append("DROP FUNCTION IF EXISTS ") |
| 43 | + .append(resultSet.getString("name")) |
| 44 | + .append(";") |
| 45 | + .append("\n") |
| 46 | + .append(resultSet.getString("create_query")) |
| 47 | + .append(";") |
| 48 | + .append("\n"); |
| 49 | + asyncContext.write(sqlBuilder.toString()); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void exportTablesOrViewsOrDictionaries( |
| 55 | + Connection connection, |
| 56 | + String databaseName, |
| 57 | + String schemaName, |
| 58 | + AsyncContext asyncContext |
| 59 | + ) throws SQLException { |
| 60 | + String sql = String.format( |
| 61 | + "SELECT create_table_query, has_own_data,engine,name from system.`tables` WHERE `database`='%s'", |
| 62 | + databaseName |
| 63 | + ); |
| 64 | + try ( |
| 65 | + Statement statement = connection.createStatement(); |
| 66 | + ResultSet resultSet = statement.executeQuery(sql) |
| 67 | + ) { |
| 68 | + while (resultSet.next()) { |
| 69 | + String ddl = resultSet.getString("create_table_query"); |
| 70 | + boolean dataFlag = resultSet.getInt("has_own_data") == 1; |
| 71 | + String tableType = resultSet.getString("engine"); |
| 72 | + String tableOrViewName = resultSet.getString("name"); |
| 73 | + if (Objects.equals("View", tableType)) { |
| 74 | + StringBuilder sqlBuilder = new StringBuilder(); |
| 75 | + sqlBuilder |
| 76 | + .append("DROP VIEW IF EXISTS ") |
| 77 | + .append(databaseName) |
| 78 | + .append(".") |
| 79 | + .append(tableOrViewName) |
| 80 | + .append(";") |
| 81 | + .append("\n") |
| 82 | + .append(ddl) |
| 83 | + .append(";") |
| 84 | + .append("\n"); |
| 85 | + asyncContext.write(sqlBuilder.toString()); |
| 86 | + } else if (Objects.equals("Dictionary", tableType)) { |
| 87 | + StringBuilder sqlBuilder = new StringBuilder(); |
| 88 | + sqlBuilder |
| 89 | + .append("DROP DICTIONARY IF EXISTS ") |
| 90 | + .append(databaseName) |
| 91 | + .append(".") |
| 92 | + .append(tableOrViewName) |
| 93 | + .append(";") |
| 94 | + .append("\n") |
| 95 | + .append(ddl) |
| 96 | + .append(";") |
| 97 | + .append("\n"); |
| 98 | + asyncContext.write(sqlBuilder.toString()); |
| 99 | + } else { |
| 100 | + StringBuilder sqlBuilder = new StringBuilder(); |
| 101 | + sqlBuilder |
| 102 | + .append("DROP STREAM IF EXISTS ") |
| 103 | + .append(databaseName) |
| 104 | + .append(".") |
| 105 | + .append(tableOrViewName) |
| 106 | + .append(";") |
| 107 | + .append("\n") |
| 108 | + .append(ddl) |
| 109 | + .append(";") |
| 110 | + .append("\n"); |
| 111 | + asyncContext.write(sqlBuilder.toString()); |
| 112 | + if (asyncContext.isContainsData() && dataFlag) { |
| 113 | + exportTableData( |
| 114 | + connection, |
| 115 | + databaseName, |
| 116 | + schemaName, |
| 117 | + tableOrViewName, |
| 118 | + asyncContext |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public Connection getConnection(ConnectInfo connectInfo) { |
| 128 | + String url = setDatabaseInJdbcUrl(connectInfo); |
| 129 | + connectInfo.setUrl(url); |
| 130 | + |
| 131 | + return super.getConnection(connectInfo); |
| 132 | + } |
| 133 | + |
| 134 | + private String setDatabaseInJdbcUrl(ConnectInfo connectInfo) { |
| 135 | + String databaseName; |
| 136 | + String url = connectInfo.getUrl(); |
| 137 | + if ( |
| 138 | + StringUtils.isBlank( |
| 139 | + (databaseName = connectInfo.getDatabaseName()) |
| 140 | + ) && |
| 141 | + StringUtils.isBlank((databaseName = connectInfo.getSchemaName())) |
| 142 | + ) { |
| 143 | + return url; |
| 144 | + } |
| 145 | + |
| 146 | + String connectAddress = |
| 147 | + connectInfo.getHost() + ":" + connectInfo.getPort(); |
| 148 | + String[] addressSplit = url.split(connectAddress); |
| 149 | + String connectParams = addressSplit[1]; |
| 150 | + if (connectParams.startsWith("/")) { |
| 151 | + // Remove / from connection parameters |
| 152 | + connectParams = connectParams.substring(1); |
| 153 | + } |
| 154 | + // Add database name |
| 155 | + return ( |
| 156 | + addressSplit[0] + |
| 157 | + connectAddress + |
| 158 | + "/" + |
| 159 | + databaseName + |
| 160 | + connectParams |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void dropTable( |
| 166 | + Connection connection, |
| 167 | + String databaseName, |
| 168 | + String schemaName, |
| 169 | + String tableName |
| 170 | + ) { |
| 171 | + String sql = "DROP STREAM IF EXISTS " + databaseName + "." + tableName; |
| 172 | + SQLExecutor.getInstance().execute(connection, sql, resultSet -> null); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void copyTable( |
| 177 | + Connection connection, |
| 178 | + String databaseName, |
| 179 | + String schemaName, |
| 180 | + String tableName, |
| 181 | + String newTableName, |
| 182 | + boolean copyData |
| 183 | + ) throws SQLException { |
| 184 | + String sql = "CREATE STREAM " + newTableName + " AS " + tableName + ""; |
| 185 | + SQLExecutor.getInstance().execute(connection, sql, resultSet -> null); |
| 186 | + if (copyData) { |
| 187 | + sql = "INSERT INTO " + |
| 188 | + newTableName + |
| 189 | + " SELECT * FROM table(" + |
| 190 | + tableName + |
| 191 | + ")"; |
| 192 | + SQLExecutor.getInstance() |
| 193 | + .execute(connection, sql, resultSet -> null); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments