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 @@ -19,6 +19,11 @@
<groupId>ai.chat2db</groupId>
<artifactId>chat2db-community-oracle</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<artifactId>chat2db-community-dm</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.chat2db.plugin.dm;

import ai.chat2db.plugin.dm.enums.type.DMIndexTypeEnum;
import ai.chat2db.plugin.dm.identifier.DMIdentifierProcessor;
import ai.chat2db.spi.IDbManager;
import ai.chat2db.spi.IDbMetaData;
import ai.chat2db.spi.DefaultDBManager;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class DMDBManager extends DefaultDBManager implements IDbManager {


private String format(String tableName) {
return "\"" + tableName + "\"";
return DMIdentifierProcessor.INSTANCE.quoteIdentifierAlways(tableName);
}


Expand All @@ -66,7 +67,7 @@ public void exportDatabase(Connection connection, String databaseName, String sc
}

private void exportTables(Connection connection, String databaseName, String schemaName, AsyncContext asyncContext) throws SQLException {
String sql = String.format(SQL_SELECT_TABLE_NAME_ALL_TABLES, schemaName);
String sql = String.format(SQL_SELECT_TABLE_NAME_ALL_TABLES, DMIdentifierProcessor.INSTANCE.escapeString(schemaName));
try (PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
Expand All @@ -77,7 +78,7 @@ private void exportTables(Connection connection, String databaseName, String sch

@Override
public void exportTable(Connection connection, String databaseName, String schemaName, String tableName, AsyncContext asyncContext) throws SQLException {
String tableDDLSql = String.format(tableDDL, tableName, schemaName);
String tableDDLSql = String.format(tableDDL, DMIdentifierProcessor.INSTANCE.escapeString(tableName), DMIdentifierProcessor.INSTANCE.escapeString(schemaName));
StringBuilder ddlBuilder = new StringBuilder();
DefaultSQLExecutor.getInstance().execute(connection, tableDDLSql, resultSet -> {
if (resultSet.next()) {
Expand All @@ -91,7 +92,7 @@ public void exportTable(Connection connection, String databaseName, String schem
String tableComment = tables.get(0).getComment();
if (StringUtils.isNotBlank(tableComment)) {
ddlBuilder.append(SQL_COMMENT_TABLE).append(format(schemaName)).append(".").append(format(tableName))
.append(" IS '").append(tableComment.replace("'", "''")).append("'").append(";").append("\n");
.append(" IS '").append(DMIdentifierProcessor.INSTANCE.escapeString(tableComment)).append("'").append(";").append("\n");
}
}
List<TableColumn> columns = metaData.columns(connection,
Expand All @@ -103,7 +104,7 @@ public void exportTable(Connection connection, String databaseName, String schem
if (StringUtils.isNotBlank(comment)) {
ddlBuilder.append(SQL_COMMENT_COLUMN).append(format(schemaName)).append(".").append(format(tableName))
.append(".").append(format(columnName)).append(" IS ")
.append("'").append(comment.replace("'", "''"))
.append("'").append(DMIdentifierProcessor.INSTANCE.escapeString(comment))
.append("';").append("\n");
}
}
Expand Down Expand Up @@ -135,7 +136,7 @@ public void exportTable(Connection connection, String databaseName, String schem
&& (CollectionUtils.isNotEmpty(uniqueConstraintIndexName) && !uniqueConstraintIndexName.contains(indexName))) {
String sql = "select DBMS_METADATA.GET_DDL('INDEX','%s') as INDEX_DDL";
try {
DefaultSQLExecutor.getInstance().execute(connection, String.format(sql, indexName), resultSet -> {
DefaultSQLExecutor.getInstance().execute(connection, String.format(sql, DMIdentifierProcessor.INSTANCE.escapeString(indexName)), resultSet -> {
if (resultSet.next()) {
ddlBuilder.append(resultSet.getString("INDEX_DDL")).append("\n");
}
Expand Down Expand Up @@ -167,7 +168,7 @@ private void exportViews(Connection connection, String schemaName, AsyncContext
}

private void exportView(Connection connection, String viewName, String schemaName, AsyncContext asyncContext) throws SQLException {
String sql = String.format(SQL_SELECT_DBMS_METADATA_GET_DDL, viewName, schemaName);
String sql = String.format(SQL_SELECT_DBMS_METADATA_GET_DDL, DMIdentifierProcessor.INSTANCE.escapeString(viewName), DMIdentifierProcessor.INSTANCE.escapeString(schemaName));
try (PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
StringBuilder sqlBuilder = new StringBuilder();
Expand All @@ -187,7 +188,7 @@ private void exportProcedures(Connection connection, String schemaName, AsyncCon
}

private void exportProcedure(Connection connection, String schemaName, String procedureName, AsyncContext asyncContext) throws SQLException {
String sql = String.format(ROUTINES_SQL, "PROC", schemaName, procedureName);
String sql = String.format(ROUTINES_SQL, "PROC", DMIdentifierProcessor.INSTANCE.escapeString(schemaName), DMIdentifierProcessor.INSTANCE.escapeString(procedureName));
try (PreparedStatement statement = connection.prepareStatement(sql); ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
StringBuilder sqlBuilder = new StringBuilder();
Expand All @@ -198,7 +199,7 @@ private void exportProcedure(Connection connection, String schemaName, String pr
}

private void exportTriggers(Connection connection, String schemaName, AsyncContext asyncContext) throws SQLException {
String sql = String.format(TRIGGER_SQL_LIST, schemaName);
String sql = String.format(TRIGGER_SQL_LIST, DMIdentifierProcessor.INSTANCE.escapeString(schemaName));
try (PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String triggerName = resultSet.getString("TRIGGER_NAME");
Expand All @@ -208,7 +209,7 @@ private void exportTriggers(Connection connection, String schemaName, AsyncConte
}

private void exportTrigger(Connection connection, String schemaName, String triggerName, AsyncContext asyncContext) throws SQLException {
String sql = String.format(TRIGGER_SQL, schemaName, triggerName);
String sql = String.format(TRIGGER_SQL, DMIdentifierProcessor.INSTANCE.escapeString(schemaName), DMIdentifierProcessor.INSTANCE.escapeString(triggerName));
try (PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
StringBuilder sqlBuilder = new StringBuilder();
Expand All @@ -226,14 +227,14 @@ public void connectDatabase(Connection connection, String database) {
}
String schemaName = connectInfo.getSchemaName();
try {
DefaultSQLExecutor.getInstance().execute(connection, String.format(SQL_SET_SCHEMA, schemaName));
DefaultSQLExecutor.getInstance().execute(connection, String.format(SQL_SET_SCHEMA, DMIdentifierProcessor.escapeIdentifier(schemaName)));
} catch (SQLException e) {
log.error("connectDatabase error", e);
}
}

@Override
public String dropTable(Connection connection, String databaseName, String schemaName, String tableName) {
return String.format(SQL_DROP_TABLE_EXISTS, tableName);
return String.format(SQL_DROP_TABLE_EXISTS, DMIdentifierProcessor.INSTANCE.quoteIdentifierAlways(tableName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,20 @@
@Slf4j
public class DMMetaData extends DefaultMetaService implements IDbMetaData {



private static final ISQLIdentifierProcessor DM_IDENTIFIER_PROCESSOR = new DMIdentifierProcessor();

@Override
public List<Schema> schemas(Connection connection, String databaseName) {
List<Schema> schemas = DefaultSQLExecutor.getInstance().schemas(connection, databaseName, null);
return SortUtils.sortSchema(schemas, SYSTEM_SCHEMAS);
}

private String format(String tableName) {
return "\"" + tableName + "\"";
return DMIdentifierProcessor.INSTANCE.quoteIdentifierAlways(tableName);
}

protected static String tableDDL = "SELECT dbms_metadata.get_ddl('TABLE', '%s','%s') as ddl FROM dual ;";

public String tableDDL(Connection connection, String databaseName, String schemaName, String tableName) {
String tableDDLSql = String.format(tableDDL, tableName, schemaName);
String tableDDLSql = String.format(tableDDL, getSQLIdentifierProcessor().escapeString(tableName), getSQLIdentifierProcessor().escapeString(schemaName));
StringBuilder ddlBuilder = new StringBuilder();
DefaultSQLExecutor.getInstance().execute(connection, tableDDLSql, resultSet -> {
if (resultSet.next()) {
Expand All @@ -69,7 +65,7 @@ public String tableDDL(Connection connection, String databaseName, String schema
String tableComment = tables.get(0).getComment();
if (StringUtils.isNotBlank(tableComment)) {
ddlBuilder.append(SQL_COMMENT_TABLE).append(format(schemaName)).append(".").append(format(tableName))
.append(" IS '").append(tableComment.replace("'", "''")).append("'").append(";").append("\n");
.append(" IS '").append(getSQLIdentifierProcessor().escapeString(tableComment)).append("'").append(";").append("\n");
}
}
List<TableColumn> columns = this.columns(connection, databaseName, schemaName, tableName);
Expand All @@ -80,7 +76,7 @@ public String tableDDL(Connection connection, String databaseName, String schema
if (StringUtils.isNotBlank(comment)) {
ddlBuilder.append(SQL_COMMENT_COLUMN).append(format(schemaName)).append(".").append(format(tableName))
.append(".").append(format(columnName)).append(" IS ")
.append("'").append(comment.replace("'", "''"))
.append("'").append(getSQLIdentifierProcessor().escapeString(comment))
.append("';").append("\n");
}
}
Expand Down Expand Up @@ -115,7 +111,7 @@ public String tableDDL(Connection connection, String databaseName, String schema
if (StringUtils.isNotBlank(indexName) && !isPrimaryKey && !isUniqueConstraint) {
String sql = "select DBMS_METADATA.GET_DDL('INDEX','%s') as INDEX_DDL";
try {
DefaultSQLExecutor.getInstance().execute(connection, String.format(sql, indexName), resultSet -> {
DefaultSQLExecutor.getInstance().execute(connection, String.format(sql, getSQLIdentifierProcessor().escapeString(indexName)), resultSet -> {
if (resultSet.next()) {
ddlBuilder.append(resultSet.getString("INDEX_DDL")).append("\n");
}
Expand Down Expand Up @@ -151,7 +147,7 @@ public List<TableColumn> columns(Connection connection, String databaseName, Str
public Function function(Connection connection, @NotEmpty String databaseName, String schemaName,
String functionName) {

String sql = String.format(ROUTINES_SQL, "PROC", schemaName, functionName);
String sql = String.format(ROUTINES_SQL, "PROC", getSQLIdentifierProcessor().escapeString(schemaName), getSQLIdentifierProcessor().escapeString(functionName));
return DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> {
StringBuilder sb = new StringBuilder();
while (resultSet.next()) {
Expand All @@ -171,7 +167,7 @@ public Function function(Connection connection, @NotEmpty String databaseName, S
@Override
public Procedure procedure(Connection connection, @NotEmpty String databaseName, String schemaName,
String procedureName) {
String sql = String.format(ROUTINES_SQL, "PROC", schemaName, procedureName);
String sql = String.format(ROUTINES_SQL, "PROC", getSQLIdentifierProcessor().escapeString(schemaName), getSQLIdentifierProcessor().escapeString(procedureName));
return DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> {
StringBuilder sb = new StringBuilder();
while (resultSet.next()) {
Expand All @@ -193,7 +189,7 @@ public Procedure procedure(Connection connection, @NotEmpty String databaseName,
@Override
public List<Trigger> triggers(Connection connection, String databaseName, String schemaName) {
List<Trigger> triggers = new ArrayList<>();
String sql = String.format(TRIGGER_SQL_LIST, schemaName);
String sql = String.format(TRIGGER_SQL_LIST, getSQLIdentifierProcessor().escapeString(schemaName));
return DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> {
while (resultSet.next()) {
Trigger trigger = new Trigger();
Expand All @@ -210,7 +206,7 @@ public List<Trigger> triggers(Connection connection, String databaseName, String
public Trigger trigger(Connection connection, @NotEmpty String databaseName, String schemaName,
String triggerName) {

String sql = String.format(TRIGGER_SQL, schemaName, triggerName);
String sql = String.format(TRIGGER_SQL, getSQLIdentifierProcessor().escapeString(schemaName), getSQLIdentifierProcessor().escapeString(triggerName));
return DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> {
Trigger trigger = new Trigger();
trigger.setDatabaseName(databaseName);
Expand All @@ -227,7 +223,7 @@ public Trigger trigger(Connection connection, @NotEmpty String databaseName, Str

@Override
public Table view(Connection connection, String databaseName, String schemaName, String viewName) {
String sql = String.format(VIEW_SQL, schemaName, viewName);
String sql = String.format(VIEW_SQL, getSQLIdentifierProcessor().escapeString(schemaName), getSQLIdentifierProcessor().escapeString(viewName));
return DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> {
Table table = new Table();
table.setDatabaseName(databaseName);
Expand All @@ -244,7 +240,7 @@ public Table view(Connection connection, String databaseName, String schemaName,

@Override
public List<TableIndex> indexes(Connection connection, String databaseName, String schemaName, String tableName) {
String sql = String.format(INDEX_SQL, schemaName, tableName);
String sql = String.format(INDEX_SQL, getSQLIdentifierProcessor().escapeString(schemaName), getSQLIdentifierProcessor().escapeString(tableName));
return DefaultSQLExecutor.getInstance().execute(connection, sql, resultSet -> {
LinkedHashMap<String, TableIndex> map = new LinkedHashMap();
while (resultSet.next()) {
Expand Down Expand Up @@ -319,12 +315,12 @@ public IValueProcessor getValueProcessor() {

@Override
public ISQLIdentifierProcessor getSQLIdentifierProcessor() {
return DM_IDENTIFIER_PROCESSOR;
return DMIdentifierProcessor.INSTANCE;
}

@Override
public String getMetaDataName(String... names) {
return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "\"" + name + "\"").collect(Collectors.joining("."));
return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(DMIdentifierProcessor.INSTANCE::quoteIdentifierAlways).collect(Collectors.joining("."));
}


Expand Down
Loading
Loading