mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-29 02:32:33 +08:00
Support for custom drivers
This commit is contained in:
22
chat2db-server/chat2db-plugins/chat2db-sqlserver/pom.xml
Normal file
22
chat2db-server/chat2db-plugins/chat2db-sqlserver/pom.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>ai.chat2db</groupId>
|
||||
<artifactId>chat2db-plugins</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>chat2db-sqlserver</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ai.chat2db</groupId>
|
||||
<artifactId>chat2db-spi</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,52 @@
|
||||
package ai.chat2db.plugin.sqlserver;
|
||||
|
||||
import ai.chat2db.spi.DBManage;
|
||||
import ai.chat2db.spi.sql.SQLExecutor;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SqlServerDBManage implements DBManage {
|
||||
@Override
|
||||
public void connectDatabase(String database) {
|
||||
try {
|
||||
SQLExecutor.getInstance().execute("use " + database + ";");
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modifyDatabase(String databaseName, String newDatabaseName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createDatabase(String databaseName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropDatabase(String databaseName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createSchema(String databaseName, String schemaName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropSchema(String databaseName, String schemaName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modifySchema(String databaseName, String schemaName, String newSchemaName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropTable(String databaseName, String schemaName, String tableName) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package ai.chat2db.plugin.sqlserver;
|
||||
|
||||
import ai.chat2db.spi.MetaData;
|
||||
import ai.chat2db.spi.jdbc.DefaultMetaService;
|
||||
import ai.chat2db.spi.sql.SQLExecutor;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SqlServerMetaData extends DefaultMetaService implements MetaData {
|
||||
private String functionSQL
|
||||
= "CREATE FUNCTION tableSchema.ufn_GetCreateTableScript( @schema_name NVARCHAR(128), @table_name NVARCHAR"
|
||||
+ "(128)) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @CreateTableScript NVARCHAR(MAX); DECLARE @IndexScripts "
|
||||
+ "NVARCHAR(MAX) = ''; DECLARE @ColumnDescriptions NVARCHAR(MAX) = N''; SELECT @CreateTableScript = CONCAT( "
|
||||
+ "'CREATE TABLE [', s.name, '].[' , t.name, '] (', STUFF( ( SELECT ', [' + c.name + '] ' + tp.name + CASE "
|
||||
+ "WHEN tp.name IN ('varchar', 'nvarchar', 'char', 'nchar') THEN '(' + IIF(c.max_length = -1, 'MAX', CAST(c"
|
||||
+ ".max_length AS NVARCHAR(10))) + ')' WHEN tp.name IN ('decimal', 'numeric') THEN '(' + CAST(c.precision AS "
|
||||
+ "NVARCHAR(10)) + ', ' + CAST(c.scale AS NVARCHAR(10)) + ')' ELSE '' END + ' ' + CASE WHEN c.is_nullable = 1"
|
||||
+ " THEN 'NULL' ELSE 'NOT NULL' END FROM sys.columns c JOIN sys.types tp ON c.user_type_id = tp.user_type_id "
|
||||
+ "WHERE c.object_id = t.object_id FOR XML PATH(''), TYPE ).value('/', 'nvarchar(max)'), 1, 1, ''), ');' ) "
|
||||
+ "FROM sys.tables t JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.name = @table_name AND s.name = "
|
||||
+ "@schema_name; SELECT @IndexScripts = @IndexScripts + 'CREATE ' + CASE WHEN i.is_unique = 1 THEN 'UNIQUE ' "
|
||||
+ "ELSE '' END + i.type_desc + ' INDEX [' + i.name + '] ON [' + s.name + '].[' + t.name + '] (' + STUFF( ( "
|
||||
+ "SELECT ', [' + c.name + ']' + CASE WHEN ic.is_descending_key = 1 THEN ' DESC' ELSE ' ASC' END FROM sys"
|
||||
+ ".index_columns ic JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id WHERE ic"
|
||||
+ ".object_id = i.object_id AND ic.index_id = i.index_id ORDER BY ic.key_ordinal FOR XML PATH('') ), 1, 1, "
|
||||
+ "'') + ')' + CASE WHEN i.has_filter = 1 THEN ' WHERE ' + i.filter_definition ELSE '' END + ';' + CHAR(13) +"
|
||||
+ " CHAR(10) FROM sys.indexes i JOIN sys.tables t ON i.object_id = t.object_id JOIN sys.schemas s ON t"
|
||||
+ ".schema_id = s.schema_id WHERE i.type > 0 AND t.name = @table_name AND s.name "
|
||||
+ "= @schema_name; SELECT @ColumnDescriptions += 'EXEC sp_addextendedproperty @name=N''MS_Description'', "
|
||||
+ "@value=N''' + CAST(p.value AS NVARCHAR(MAX)) + ''', @level0type=N''SCHEMA'', @level0name=N''' + "
|
||||
+ "@schema_name + ''', @level1type=N''TABLE'', @level1name=N''' + @table_name + ''', @level2type=N''COLUMN'',"
|
||||
+ " @level2name=N''' + c.name + ''';' + CHAR(13) + CHAR(10) FROM sys.extended_properties p JOIN sys.columns c"
|
||||
+ " ON p.major_id = c.object_id AND p.minor_id = c.column_id JOIN sys.tables t ON c.object_id = t.object_id "
|
||||
+ "JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE p.class = 1 AND t.name = @table_name AND s.name = "
|
||||
+ "@schema_name; SET @CreateTableScript = @CreateTableScript + CHAR(13) + CHAR(10) + @IndexScripts + CHAR(13)"
|
||||
+ " + CHAR(10)+ @ColumnDescriptions+ CHAR(10); RETURN @CreateTableScript; END";
|
||||
|
||||
|
||||
@Override
|
||||
public String tableDDL(String databaseName, String schemaName, String tableName) {
|
||||
try {
|
||||
System.out.println(functionSQL);
|
||||
SQLExecutor.getInstance().executeSql(functionSQL.replace("tableSchema", schemaName), resultSet -> null);
|
||||
} catch (Exception e) {
|
||||
//log.error("创建函数失败", e);
|
||||
}
|
||||
|
||||
String ddlSql = "SELECT " + schemaName + ".ufn_GetCreateTableScript('" + schemaName + "', '" + tableName
|
||||
+ "') AS sql";
|
||||
return SQLExecutor.getInstance().executeSql(ddlSql, resultSet -> {
|
||||
try {
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getString("sql");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package ai.chat2db.plugin.sqlserver;
|
||||
|
||||
|
||||
import ai.chat2db.plugin.sqlserver.builder.SqlServerDBConfigBuilder;
|
||||
import ai.chat2db.spi.DBManage;
|
||||
import ai.chat2db.spi.MetaData;
|
||||
import ai.chat2db.spi.Plugin;
|
||||
import ai.chat2db.spi.config.DBConfig;
|
||||
|
||||
public class SqlServerPlugin implements Plugin {
|
||||
@Override
|
||||
public DBConfig getDBConfig() {
|
||||
return SqlServerDBConfigBuilder.buildDBConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetaData getMetaData() {
|
||||
return new SqlServerMetaData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DBManage getDBManage() {
|
||||
return new SqlServerDBManage();
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package ai.chat2db.plugin.sqlserver.builder;
|
||||
|
||||
import ai.chat2db.spi.config.DBConfig;
|
||||
import ai.chat2db.spi.config.DriverConfig;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class SqlServerDBConfigBuilder {
|
||||
|
||||
public static DBConfig buildDBConfig() {
|
||||
DBConfig dbConfig = new DBConfig();
|
||||
dbConfig.setName("SQLServer");
|
||||
dbConfig.setDbType("SQLSERVER");
|
||||
DriverConfig driverConfig = new DriverConfig();
|
||||
driverConfig.setJdbcDriver("mssql-jdbc-11.2.1.jre17.jar");
|
||||
driverConfig.setJdbcDriverClass("com.microsoft.sqlserver.jdbc.SQLServerDriver");
|
||||
driverConfig.setDownloadJdbcDriverUrls(Lists.newArrayList("https://oss-chat2db.alibaba.com/lib/mssql-jdbc-11.2.1.jre17.jar"));
|
||||
driverConfig.setName(driverConfig.getJdbcDriver() + ":" + driverConfig.getJdbcDriverClass());
|
||||
dbConfig.setDefaultDriverConfig(driverConfig);
|
||||
dbConfig.setDriverConfigList(Lists.newArrayList(driverConfig));
|
||||
return dbConfig;
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
ai.chat2db.plugin.sqlserver.SqlServerPlugin
|
Reference in New Issue
Block a user