Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,26 @@ public void connectDatabase(Connection connection, String database) {

@Override
public void copyTable(Connection connection, String databaseName, String schemaName, String tableName, String newTableName, boolean copyData) throws SQLException {
String sourceTable = buildFullTableName(databaseName, schemaName, tableName);
String targetTable = buildFullTableName(databaseName, schemaName, newTableName);
String sql;
String ddl = Chat2DBContext.getDbMetaData().tableDDL(connection,
new TableMetadataRequest(databaseName, schemaName, tableName));
String createDdl = ddl.replace("[" + tableName + "]", "[" + newTableName + "]");
log.info("copy table DDL: {}", createDdl);
DefaultSQLExecutor.getInstance().execute(connection, createDdl, resultSet -> null);

if (copyData) {
sql = String.format(SQL_COPY_TABLE_DATA, targetTable, sourceTable);
} else {
sql = String.format(SQL_COPY_TABLE_STRUCTURE, targetTable, sourceTable);
String sourceTable = buildFullTableName(databaseName, schemaName, tableName);
String targetTable = buildFullTableName(databaseName, schemaName, newTableName);
String identityOn = String.format(SQL_SET_IDENTITY_INSERT, targetTable, "ON");
String identityOff = String.format(SQL_SET_IDENTITY_INSERT, targetTable, "OFF");
String insertSql = String.format(SQL_COPY_TABLE_DATA, targetTable, sourceTable);
log.info("copy table data sql: {}", insertSql);
try {
DefaultSQLExecutor.getInstance().execute(connection, identityOn, resultSet -> null);
DefaultSQLExecutor.getInstance().execute(connection, insertSql, resultSet -> null);
} finally {
DefaultSQLExecutor.getInstance().execute(connection, identityOff, resultSet -> null);
}
}

log.info(" copy table sql : {}", sql);
DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> null);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public final class SqlServerDBManagerConstants {

public static final String SQL_DROP_TABLE_EXISTS = "DROP TABLE IF EXISTS ";
public static final String SQL_DROP_VIEW_EXISTS = "DROP VIEW IF EXISTS ";
public static final String SQL_COPY_TABLE_DATA = "SELECT * INTO %s FROM %s";
public static final String SQL_COPY_TABLE_STRUCTURE = "SELECT * INTO %s FROM %s WHERE 1=0";
public static final String SQL_COPY_TABLE_DATA = "INSERT INTO %s SELECT * FROM %s";
public static final String SQL_SET_IDENTITY_INSERT = "SET IDENTITY_INSERT %s %s";
public static final String SQL_DROP_TABLE = "DROP TABLE %s";
public static final String SQL_DROP_VIEW = "DROP VIEW %s";
public static final String SQL_USE_DATABASE = "use [%s];";
Expand Down