Skip to content
Merged
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
@@ -1,6 +1,7 @@
package ai.chat2db.plugin.sqlserver;

import ai.chat2db.spi.IDbManager;
import ai.chat2db.plugin.sqlserver.identifier.SqlServerIdentifierProcessor;
import ai.chat2db.spi.DefaultDBManager;
import ai.chat2db.community.domain.api.model.async.AsyncContext;
import ai.chat2db.spi.sql.Chat2DBContext;
Expand Down Expand Up @@ -82,7 +83,8 @@ public void exportTable(Connection connection, String databaseName, String schem
String tableDDL = Chat2DBContext.getDbMetaData().tableDDL(connection,
new TableMetadataRequest(databaseName, schemaName, tableName));
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append(SQL_DROP_TABLE_EXISTS).append("[").append(tableName).append("]").append(";").append("\ngo\n")
sqlBuilder.append(SQL_DROP_TABLE_EXISTS)
.append(buildFullTableName(null, schemaName, tableName)).append(";").append("\ngo\n")
.append(tableDDL);
asyncContext.write(sqlBuilder.toString());
if (asyncContext.isContainsData()) {
Expand All @@ -103,7 +105,7 @@ private void exportViews(Connection connection, String databaseName, String sche
while (resultSet.next()) {
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append(SQL_DROP_VIEW_EXISTS)
.append("[").append(resultSet.getString("TABLE_NAME")).append("]")
.append(buildFullTableName(null, schemaName, resultSet.getString("TABLE_NAME")))
.append(";\n").append("go").append("\n")
.append(resultSet.getString("VIEW_DEFINITION")).append(";").append("\n")
.append("go").append("\n");
Expand All @@ -117,18 +119,24 @@ private void exportFunctions(Connection connection, String schemaName, AsyncCont
DefaultSQLExecutor.getInstance().preExecute(connection, ROUTINES_SQL, new String[]{"FN", schemaName}, resultSet -> {
while (resultSet.next()) {
String functionName = resultSet.getString("name");
exportFunction(connection, functionName, asyncContext);
exportFunction(connection, schemaName, functionName, asyncContext);
}
});

}

private void exportFunction(Connection connection, String functionName, AsyncContext asyncContext) {
String sql = String.format(ROUTINES_DDL_SQL, "'SQL_SCALAR_FUNCTION', 'SQL_TABLE_VALUED_FUNCTION'", functionName);
private void exportFunction(Connection connection, String schemaName, String functionName,
AsyncContext asyncContext) {
String sql = String.format(ROUTINES_DDL_SQL,
"'SQL_SCALAR_FUNCTION', 'SQL_TABLE_VALUED_FUNCTION'",
SqlServerIdentifierProcessor.INSTANCE.escapeString(functionName),
SqlServerIdentifierProcessor.INSTANCE.escapeString(schemaName));
DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> {
if (resultSet.next()) {
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append(String.format(DROP_FUNCTION_SQL, functionName, functionName));
String qualifiedName = buildFullTableName(null, schemaName, functionName);
sqlBuilder.append(String.format(DROP_FUNCTION_SQL,
SqlServerIdentifierProcessor.INSTANCE.escapeString(qualifiedName), qualifiedName));
sqlBuilder.append(resultSet.getString("definition"))
.append("\n").append("go").append("\n");
asyncContext.write(sqlBuilder.toString());
Expand All @@ -141,17 +149,22 @@ private void exportProcedures(Connection connection, String schemaName, AsyncCon
DefaultSQLExecutor.getInstance().preExecute(connection, ROUTINES_SQL, new String[]{"P", schemaName}, resultSet -> {
while (resultSet.next()) {
String procedureName = resultSet.getString("name");
exportProcedure(connection, procedureName, asyncContext);
exportProcedure(connection, schemaName, procedureName, asyncContext);
}
});
}

private void exportProcedure(Connection connection, String procedureName, AsyncContext asyncContext) throws SQLException {
String sql = String.format(ROUTINES_DDL_SQL, "'SQL_STORED_PROCEDURE'", procedureName);
private void exportProcedure(Connection connection, String schemaName, String procedureName,
AsyncContext asyncContext) throws SQLException {
String sql = String.format(ROUTINES_DDL_SQL, "'SQL_STORED_PROCEDURE'",
SqlServerIdentifierProcessor.INSTANCE.escapeString(procedureName),
SqlServerIdentifierProcessor.INSTANCE.escapeString(schemaName));
try (PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append(String.format(DROP_PROCEDURE_SQL, procedureName, procedureName));
String qualifiedName = buildFullTableName(null, schemaName, procedureName);
sqlBuilder.append(String.format(DROP_PROCEDURE_SQL,
SqlServerIdentifierProcessor.INSTANCE.escapeString(qualifiedName), qualifiedName));
sqlBuilder.append(resultSet.getString("definition")).append("\n").append("go").append("\n");
asyncContext.write(sqlBuilder.toString());

Expand All @@ -172,7 +185,9 @@ private void exportTriggers(Connection connection, String schemaName, AsyncConte
@Override
public void connectDatabase(Connection connection, String database) {
try {
DefaultSQLExecutor.getInstance().execute(connection, String.format(SQL_USE_DATABASE, database));
DefaultSQLExecutor.getInstance().execute(connection,
String.format(SQL_USE_DATABASE,
SqlServerIdentifierProcessor.INSTANCE.quoteIdentifierAlways(database)));
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -559,8 +574,7 @@ private static String quoteIdentifier(String identifier) {
if (StringUtils.isBlank(identifier)) {
return identifier;
}
String value = unquoteIdentifier(identifier);
return "[" + value.replace("]", "]]") + "]";
return SqlServerIdentifierProcessor.INSTANCE.quoteIdentifierAlways(unquoteIdentifier(identifier));
}

static String unquoteIdentifier(String identifier) {
Expand Down Expand Up @@ -589,6 +603,12 @@ public String dropTable(Connection connection, String databaseName, String schem
return String.format(SQL_DROP_TABLE, fullTableName);
}

@Override
public String truncateTable(Connection connection, String databaseName, String schemaName, String tableName) {
return String.format(SQL_TRUNCATE_TABLE,
buildFullTableName(databaseName, schemaName, tableName));
}

@Override
public void dropView(Connection connection, String databaseName, String schemaName, String viewName) {
String fullTableName = buildFullTableName(databaseName, schemaName, viewName);
Expand Down
Loading
Loading