mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-28 10:12:39 +08:00
batch export
This commit is contained in:
@ -2,6 +2,7 @@ package ai.chat2db.plugin.sqlite;
|
||||
|
||||
import ai.chat2db.spi.DBManage;
|
||||
import ai.chat2db.spi.jdbc.DefaultDBManage;
|
||||
import ai.chat2db.spi.model.AsyncContext;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
@ -10,31 +11,31 @@ import java.sql.SQLException;
|
||||
public class SqliteDBManage extends DefaultDBManage implements DBManage {
|
||||
|
||||
@Override
|
||||
public String exportDatabase(Connection connection, String databaseName, String schemaName, boolean containData) throws SQLException {
|
||||
StringBuilder sqlBuilder = new StringBuilder();
|
||||
exportTables(connection, databaseName, schemaName,sqlBuilder, containData);
|
||||
exportViews(connection, databaseName, sqlBuilder);
|
||||
exportTriggers(connection, sqlBuilder);
|
||||
return sqlBuilder.toString();
|
||||
public void exportDatabase(Connection connection, String databaseName, String schemaName, AsyncContext asyncContext) throws SQLException {
|
||||
exportTables(connection, databaseName, schemaName,asyncContext);
|
||||
exportViews(connection, databaseName, asyncContext);
|
||||
exportTriggers(connection, asyncContext);
|
||||
}
|
||||
|
||||
private void exportTables(Connection connection, String databaseName, String schemaName, StringBuilder sqlBuilder, boolean containData) throws SQLException {
|
||||
private void exportTables(Connection connection, String databaseName, String schemaName, AsyncContext asyncContext) throws SQLException {
|
||||
try (ResultSet resultSet = connection.getMetaData().getTables(databaseName, null, null, new String[]{"TABLE", "SYSTEM TABLE"})) {
|
||||
while (resultSet.next()) {
|
||||
exportTable(connection,schemaName, resultSet.getString("TABLE_NAME"), sqlBuilder, containData);
|
||||
exportTable(connection,schemaName, resultSet.getString("TABLE_NAME"), asyncContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void exportTable(Connection connection, String schemaName, String tableName, StringBuilder sqlBuilder, boolean containData) throws SQLException {
|
||||
private void exportTable(Connection connection, String schemaName, String tableName, AsyncContext asyncContext) throws SQLException {
|
||||
String sql = String.format("SELECT sql FROM sqlite_master WHERE type='table' AND name='%s'", tableName);
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
if (resultSet.next()) {
|
||||
StringBuilder sqlBuilder = new StringBuilder();
|
||||
sqlBuilder.append("DROP TABLE IF EXISTS ").append(format(tableName)).append(";").append("\n")
|
||||
.append(resultSet.getString("sql")).append(";").append("\n");
|
||||
if (containData) {
|
||||
exportTableData(connection,schemaName, tableName, sqlBuilder);
|
||||
asyncContext.write(sqlBuilder.toString());
|
||||
if (asyncContext.isContainsData()) {
|
||||
exportTableData(connection,schemaName, tableName, asyncContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -44,39 +45,43 @@ public class SqliteDBManage extends DefaultDBManage implements DBManage {
|
||||
return "\""+tableName+"\"";
|
||||
}
|
||||
|
||||
private void exportViews(Connection connection, String databaseName, StringBuilder sqlBuilder) throws SQLException {
|
||||
private void exportViews(Connection connection, String databaseName, AsyncContext asyncContext) throws SQLException {
|
||||
try (ResultSet resultSet = connection.getMetaData().getTables(databaseName, null, null, new String[]{"VIEW"})) {
|
||||
while (resultSet.next()) {
|
||||
exportView(connection, resultSet.getString("TABLE_NAME"), sqlBuilder);
|
||||
exportView(connection, resultSet.getString("TABLE_NAME"), asyncContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void exportView(Connection connection, String viewName, StringBuilder sqlBuilder) throws SQLException {
|
||||
private void exportView(Connection connection, String viewName, AsyncContext asyncContext) throws SQLException {
|
||||
String sql = String.format("SELECT * FROM sqlite_master WHERE type = 'view' and name='%s';", viewName);
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
if (resultSet.next()) {
|
||||
StringBuilder sqlBuilder = new StringBuilder();
|
||||
sqlBuilder.append("DROP VIEW IF EXISTS ").append(format(viewName)).append(";").append("\n")
|
||||
.append(resultSet.getString("sql")).append(";").append("\n");
|
||||
asyncContext.write(sqlBuilder.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void exportTriggers(Connection connection, StringBuilder sqlBuilder) throws SQLException {
|
||||
private void exportTriggers(Connection connection, AsyncContext asyncContext) throws SQLException {
|
||||
String sql = "SELECT * FROM sqlite_master WHERE type = 'trigger';";
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
while (resultSet.next()) {
|
||||
String triggerName = resultSet.getString("name");
|
||||
exportTrigger(connection, triggerName, sqlBuilder);
|
||||
exportTrigger(connection, triggerName, asyncContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void exportTrigger(Connection connection, String triggerName, StringBuilder sqlBuilder) throws SQLException {
|
||||
private void exportTrigger(Connection connection, String triggerName, AsyncContext asyncContext) throws SQLException {
|
||||
String sql = String.format("SELECT * FROM sqlite_master WHERE type = 'trigger' and name='%s';", triggerName);
|
||||
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
|
||||
if (resultSet.next()) {
|
||||
StringBuilder sqlBuilder = new StringBuilder();
|
||||
sqlBuilder.append(resultSet.getString("sql")).append("\n");
|
||||
asyncContext.write(sqlBuilder.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user