This commit is contained in:
SwallowGG
2023-09-22 14:15:16 +08:00
parent de447e0de3
commit d7e811f4b4
17 changed files with 275 additions and 45 deletions

View File

@ -2,25 +2,26 @@ package ai.chat2db.plugin.mysql;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import ai.chat2db.plugin.mysql.builder.MysqlSqlBuilder;
import ai.chat2db.plugin.mysql.type.MysqlCharsetEnum;
import ai.chat2db.plugin.mysql.type.MysqlCollationEnum;
import ai.chat2db.plugin.mysql.type.MysqlColumnTypeEnum;
import ai.chat2db.spi.MetaData;
import ai.chat2db.spi.SqlBuilder;
import ai.chat2db.spi.jdbc.DefaultMetaService;
import ai.chat2db.spi.model.Function;
import ai.chat2db.spi.model.Procedure;
import ai.chat2db.spi.model.Table;
import ai.chat2db.spi.model.Trigger;
import ai.chat2db.spi.model.*;
import ai.chat2db.spi.sql.SQLExecutor;
import jakarta.validation.constraints.NotEmpty;
public class MysqlMetaData extends DefaultMetaService implements MetaData {
@Override
public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName,
@NotEmpty String tableName) {
@NotEmpty String tableName) {
String sql = "SHOW CREATE TABLE " + format(databaseName) + "."
+ format(tableName);
+ format(tableName);
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
if (resultSet.next()) {
return resultSet.getString("Create Table");
@ -34,14 +35,14 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
}
private static String ROUTINES_SQL
=
"SELECT SPECIFIC_NAME, ROUTINE_COMMENT, ROUTINE_DEFINITION FROM information_schema.routines WHERE "
+ "routine_type = '%s' AND ROUTINE_SCHEMA ='%s' AND "
+ "routine_name = '%s';";
=
"SELECT SPECIFIC_NAME, ROUTINE_COMMENT, ROUTINE_DEFINITION FROM information_schema.routines WHERE "
+ "routine_type = '%s' AND ROUTINE_SCHEMA ='%s' AND "
+ "routine_name = '%s';";
@Override
public Function function(Connection connection, @NotEmpty String databaseName, String schemaName,
String functionName) {
String functionName) {
String sql = String.format(ROUTINES_SQL, "FUNCTION", databaseName, functionName);
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
@ -60,11 +61,11 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
}
private static String TRIGGER_SQL
= "SELECT TRIGGER_NAME,EVENT_MANIPULATION, ACTION_STATEMENT FROM INFORMATION_SCHEMA.TRIGGERS where "
+ "TRIGGER_SCHEMA = '%s' AND TRIGGER_NAME = '%s';";
= "SELECT TRIGGER_NAME,EVENT_MANIPULATION, ACTION_STATEMENT FROM INFORMATION_SCHEMA.TRIGGERS where "
+ "TRIGGER_SCHEMA = '%s' AND TRIGGER_NAME = '%s';";
private static String TRIGGER_SQL_LIST
= "SELECT TRIGGER_NAME FROM INFORMATION_SCHEMA.TRIGGERS where TRIGGER_SCHEMA = '%s';";
= "SELECT TRIGGER_NAME FROM INFORMATION_SCHEMA.TRIGGERS where TRIGGER_SCHEMA = '%s';";
@Override
public List<Trigger> triggers(Connection connection, String databaseName, String schemaName) {
@ -84,7 +85,7 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
@Override
public Trigger trigger(Connection connection, @NotEmpty String databaseName, String schemaName,
String triggerName) {
String triggerName) {
String sql = String.format(TRIGGER_SQL, databaseName, triggerName);
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
@ -101,7 +102,7 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
@Override
public Procedure procedure(Connection connection, @NotEmpty String databaseName, String schemaName,
String procedureName) {
String procedureName) {
String sql = String.format(ROUTINES_SQL, "PROCEDURE", databaseName, procedureName);
return SQLExecutor.getInstance().execute(connection, sql, resultSet -> {
Procedure procedure = new Procedure();
@ -118,8 +119,8 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
}
private static String VIEW_SQL
= "SELECT TABLE_SCHEMA AS DatabaseName, TABLE_NAME AS ViewName, VIEW_DEFINITION AS definition, CHECK_OPTION, "
+ "IS_UPDATABLE FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s';";
= "SELECT TABLE_SCHEMA AS DatabaseName, TABLE_NAME AS ViewName, VIEW_DEFINITION AS definition, CHECK_OPTION, "
+ "IS_UPDATABLE FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s';";
@Override
public Table view(Connection connection, String databaseName, String schemaName, String viewName) {
@ -135,8 +136,18 @@ public class MysqlMetaData extends DefaultMetaService implements MetaData {
return table;
});
}
@Override
public SqlBuilder getSqlBuilder() {
return new MysqlSqlBuilder();
}
@Override
public TableMeta getTableMeta(String databaseName, String schemaName, String tableName) {
return TableMeta.builder()
.columnTypes(MysqlColumnTypeEnum.getTypes())
.charsets(MysqlCharsetEnum.getCharsets())
.collations(MysqlCollationEnum.getCollations())
.build();
}
}

View File

@ -25,7 +25,7 @@ public class MysqlSqlBuilder implements SqlBuilder {
// append primary key and index
for (TableIndex tableIndex : table.getIndexList()) {
MysqlIndexTypeEnum mysqlIndexTypeEnum = MysqlIndexTypeEnum.getByType(tableIndex.getType());
script.append("\t").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
script.append("\t").append("ADD ").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
}
script = new StringBuilder(script.substring(0, script.length() - 2));

View File

@ -0,0 +1,67 @@
package ai.chat2db.plugin.mysql.type;
import ai.chat2db.spi.model.Charset;
import org.checkerframework.checker.units.qual.C;
import java.util.Arrays;
import java.util.List;
public enum MysqlCharsetEnum {
UTF8("utf8", "utf8_general_ci"),
BIG5("big5", "big5_chinese_ci"),
DEC8("dec8", "dec8_swedish_ci"),
CP850("cp850", "cp850_general_ci"),
HP8("hp8", "hp8_english_ci"),
KOI8R("koi8r", "koi8r_general_ci"),
LATIN1("latin1", "latin1_swedish_ci"),
LATIN2("latin2", "latin2_general_ci"),
SWE7("swe7", "swe7_swedish_ci"),
ASCII("ascii", "ascii_general_ci"),
UJIS("ujis", "ujis_japanese_ci"),
SJIS("sjis", "sjis_japanese_ci"),
HEBREW("hebrew", "hebrew_general_ci"),
TIS620("tis620", "tis620_thai_ci"),
EUCKR("euckr", "euckr_korean_ci"),
KOI8U("koi8u", "koi8u_general_ci"),
GB2312("gb2312", "gb2312_chinese_ci"),
GREEK("greek", "greek_general_ci"),
CP1250("cp1250", "cp1250_general_ci"),
GBK("gbk", "gbk_chinese_ci"),
LATIN5("latin5", "latin5_turkish_ci"),
ARMSCII8("armscii8", "armscii8_general_ci"),
UCS2("ucs2", "ucs2_general_ci"),
CP866("cp866", "cp866_general_ci"),
KEYBCS2("keybcs2", "keybcs2_general_ci"),
MACCE("macce", "macce_general_ci"),
MACROMAN("macroman", "macroman_general_ci"),
CP852("cp852", "cp852_general_ci"),
LATIN7("latin7", "latin7_general_ci"),
UTF8MB4("utf8mb4", "utf8mb4_general_ci"),
CP1251("cp1251", "cp1251_general_ci"),
UTF16("utf16", "utf16_general_ci"),
UTF16LE("utf16le", "utf16le_general_ci"),
CP1256("cp1256", "cp1256_general_ci"),
CP1257("cp1257", "cp1257_general_ci"),
UTF32("utf32", "utf32_general_ci"),
BINARY("binary", "binary"),
GEOSTD8("geostd8", "geostd8_general_ci"),
CP932("cp932", "cp932_japanese_ci"),
EUCJPMS("eucjpms", "eucjpms_japanese_ci"),
GB18030("gb18030", "gb18030_chinese_ci");
private Charset charset;
MysqlCharsetEnum(String charsetName, String defaultCollationName) {
this.charset = new Charset(charsetName, defaultCollationName);
}
public Charset getCharset() {
return charset;
}
public static List<Charset> getCharsets() {
return Arrays.stream(MysqlCharsetEnum.values()).map(MysqlCharsetEnum::getCharset).collect(java.util.stream.Collectors.toList());
}
}

View File

@ -0,0 +1,69 @@
package ai.chat2db.plugin.mysql.type;
import ai.chat2db.spi.model.Collation;
import java.util.Arrays;
import java.util.List;
public enum MysqlCollationEnum {
UTF8_GENERAL_CI("utf8_general_ci"),
UTF8MB4_GENERAL_CI("utf8mb4_general_ci"),
BIG5_CHINESE_CI("big5_chinese_ci"),
DEC8_SWEDISH_CI("dec8_swedish_ci"),
HP8_ENGLISH_CI("hp8_english_ci"),
KOI8R_GENERAL_CI("koi8r_general_ci"),
LATIN1_SWEDISH_CI("latin1_swedish_ci"),
LATIN2_GENERAL_CI("latin2_general_ci"),
SWE7_SWEDISH_CI("swe7_swedish_ci"),
ASCII_GENERAL_CI("ascii_general_ci"),
UJIS_JAPANESE_CI("ujis_japanese_ci"),
SJIS_JAPANESE_CI("sjis_japanese_ci"),
HEBREW_GENERAL_CI("hebrew_general_ci"),
TIS620_THAI_CI("tis620_thai_ci"),
EUCKR_KOREAN_CI("euckr_korean_ci"),
KOI8U_GENERAL_CI("koi8u_general_ci"),
GB2312_CHINESE_CI("gb2312_chinese_ci"),
GREEK_GENERAL_CI("greek_general_ci"),
CP1250_GENERAL_CI("cp1250_general_ci"),
GBK_CHINESE_CI("gbk_chinese_ci"),
LATIN5_TURKISH_CI("latin5_turkish_ci"),
ARMSCII8_GENERAL_CI("armscii8_general_ci"),
CP1250_CZECH_CS("cp1250_czech_cs"),
UCS2_GENERAL_CI("ucs2_general_ci"),
CP866_GENERAL_CI("cp866_general_ci"),
KEYBCS2_GENERAL_CI("keybcs2_general_ci"),
MACCE_GENERAL_CI("macce_general_ci"),
MACROMAN_GENERAL_CI("macroman_general_ci"),
CP852_GENERAL_CI("cp852_general_ci"),
LATIN7_GENERAL_CI("latin7_general_ci"),
CP1251_GENERAL_CI("cp1251_general_ci"),
UTF16_GENERAL_CI("utf16_general_ci"),
UTF16LE_GENERAL_CI("utf16le_general_ci"),
CP1256_GENERAL_CI("cp1256_general_ci"),
CP1257_GENERAL_CI("cp1257_general_ci"),
UTF32_GENERAL_CI("utf32_general_ci"),
BINARY("binary"),
GEOSTD8_GENERAL_CI("geostd8_general_ci"),
CP932_JAPANESE_CI("cp932_japanese_ci"),
EUCJPMS_JAPANESE_CI("eucjpms_japanese_ci"),
GB18030_CHINESE_CI("gb18030_chinese_ci"),
;
private Collation collation;
MysqlCollationEnum(String collationName) {
this.collation = new Collation(collationName);
}
public Collation getCollation() {
return collation;
}
public static List<Collation> getCollations() {
return Arrays.asList(MysqlCollationEnum.values()).stream().map(MysqlCollationEnum::getCollation).collect(java.util.stream.Collectors.toList());
}
}

View File

@ -8,6 +8,7 @@ import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public enum MysqlColumnTypeEnum implements ColumnBuilder {
@ -131,7 +132,7 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder {
static {
for (MysqlColumnTypeEnum value : MysqlColumnTypeEnum.values()) {
COLUMN_TYPE_MAP.put(value.getColumnType().getDataTypeName(), value);
COLUMN_TYPE_MAP.put(value.getColumnType().getTypeName(), value);
}
}
@ -238,7 +239,7 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder {
}
private String buildDataType(TableColumn column, MysqlColumnTypeEnum type) {
String columnType = type.columnType.getDataTypeName();
String columnType = type.columnType.getTypeName();
if (Arrays.asList(BINARY, VARBINARY, VARCHAR, CHAR).contains(type)) {
return StringUtils.join(columnType, "(", column.getColumnSize(), ")");
}
@ -298,5 +299,11 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder {
return StringUtils.join(dataTypeName, middle);
}
public static List<ColumnType> getTypes(){
return Arrays.stream(MysqlColumnTypeEnum.values()).map(columnTypeEnum ->
columnTypeEnum.getColumnType()
).toList();
}
}

View File

@ -91,11 +91,11 @@ public enum MysqlIndexTypeEnum {
if (EditStatus.DELETE.name().equals(tableIndex.getEditStatus())) {
return buildDropIndex(tableIndex);
}
if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) {
if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) {
return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex));
}
if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) {
return StringUtils.join("MODIFY ", buildIndexScript(tableIndex));
if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) {
return StringUtils.join("ADD ", buildIndexScript(tableIndex));
}
return "";
}