From 55ce1084d8fbfaa5ffceb3f1ccc9a53127d15ef3 Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Thu, 11 Jan 2024 18:33:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?OceanBase=E5=9F=BA=E7=A1=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/oceanbase/OceanBaseMetaData.java | 61 +++++++++++++++++++ .../tools/base/enums/DataSourceTypeEnum.java | 5 ++ .../api/controller/rdb/RdbDmlController.java | 6 +- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/OceanBaseMetaData.java b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/OceanBaseMetaData.java index 2c1020fa8..52b2f0aed 100644 --- a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/OceanBaseMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/OceanBaseMetaData.java @@ -2,8 +2,69 @@ import ai.chat2db.spi.MetaData; import ai.chat2db.spi.jdbc.DefaultMetaService; +import ai.chat2db.spi.model.Table; +import ai.chat2db.spi.model.TableColumn; +import ai.chat2db.spi.sql.SQLExecutor; +import org.apache.commons.lang3.StringUtils; + +import java.sql.Connection; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; public class OceanBaseMetaData extends DefaultMetaService implements MetaData { + @Override + public List tables(Connection connection, String databaseName, String schemaName, String tableName) { + String sql = "SHOW TABLE STATUS FROM "+ databaseName + " where name = '" + tableName + "';"; + return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { + List
tables = new ArrayList<>(); + while (resultSet.next()) { + Table table = new Table(); + table.setDatabaseName(databaseName); + table.setSchemaName(schemaName); + table.setName(resultSet.getString("NAME")); + table.setComment(resultSet.getString("COMMENT")); + tables.add(table); + } + return tables; + }); + } + + @Override + public List columns(Connection connection, String databaseName, String schemaName, String tableName) { + return null; + /*String sql = String.format(SELECT_TABLE_COLUMNS, databaseName, tableName); + List tableColumns = new ArrayList<>(); + return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { + while (resultSet.next()) { + TableColumn column = new TableColumn(); + column.setDatabaseName(databaseName); + column.setTableName(tableName); + column.setOldName(resultSet.getString("COLUMN_NAME")); + column.setName(resultSet.getString("COLUMN_NAME")); + //column.setColumnType(resultSet.getString("COLUMN_TYPE")); + column.setColumnType(resultSet.getString("DATA_TYPE").toUpperCase()); + //column.setDataType(resultSet.getInt("DATA_TYPE")); + column.setDefaultValue(resultSet.getString("COLUMN_DEFAULT")); + column.setAutoIncrement(resultSet.getString("EXTRA").contains("auto_increment")); + column.setComment(resultSet.getString("COLUMN_COMMENT")); + column.setPrimaryKey("PRI".equalsIgnoreCase(resultSet.getString("COLUMN_KEY"))); + column.setNullable("YES".equalsIgnoreCase(resultSet.getString("IS_NULLABLE")) ? 1 : 0); + column.setOrdinalPosition(resultSet.getInt("ORDINAL_POSITION")); + column.setDecimalDigits(resultSet.getInt("NUMERIC_SCALE")); + column.setCharSetName(resultSet.getString("CHARACTER_SET_NAME")); + column.setCollationName(resultSet.getString("COLLATION_NAME")); + setColumnSize(column, resultSet.getString("COLUMN_TYPE")); + tableColumns.add(column); + } + return tableColumns; + });*/ + } + @Override + public String getMetaDataName(String... names) { + return Arrays.stream(names).filter(name -> StringUtils.isNotBlank(name)).map(name -> "`" + name + "`").collect(Collectors.joining(".")); + } } diff --git a/chat2db-server/chat2db-server-tools/chat2db-server-tools-base/src/main/java/ai/chat2db/server/tools/base/enums/DataSourceTypeEnum.java b/chat2db-server/chat2db-server-tools/chat2db-server-tools-base/src/main/java/ai/chat2db/server/tools/base/enums/DataSourceTypeEnum.java index 7a9cbcc64..2a4050f83 100644 --- a/chat2db-server/chat2db-server-tools/chat2db-server-tools-base/src/main/java/ai/chat2db/server/tools/base/enums/DataSourceTypeEnum.java +++ b/chat2db-server/chat2db-server-tools/chat2db-server-tools-base/src/main/java/ai/chat2db/server/tools/base/enums/DataSourceTypeEnum.java @@ -26,6 +26,11 @@ public enum DataSourceTypeEnum implements BaseEnum { */ MONGODB("mongo数据库连接"), + /** + * oceanbase数据库连接 + */ + OCEANBASE("oceanbase数据库连接"), + ; final String description; diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java index 2197d84e1..c709b2575 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/rdb/RdbDmlController.java @@ -128,7 +128,11 @@ public ListResult executeTable(@RequestBody DmlTableRequest req } else { MetaData metaData = Chat2DBContext.getMetaData(); // 拼接`tableName`,避免关键字被占用问题 - param.setSql("select * from " + metaData.getMetaDataName(request.getTableName())); + if (DataSourceTypeEnum.OCEANBASE.getCode().equals(type)) { + param.setSql("select * from " + metaData.getMetaDataName(request.getDatabaseName(), request.getTableName())); + } else { + param.setSql("select * from " + metaData.getMetaDataName(request.getTableName())); + } } return dlTemplateService.execute(param) .map(rdbWebConverter::dto2vo); From 4791c8381178a5545c10389c1a57cf8509e1a75d Mon Sep 17 00:00:00 2001 From: tmlx1990 Date: Fri, 12 Jan 2024 14:20:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?OceanBase=E5=9F=BA=E7=A1=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/oceanbase/OceanBaseMetaData.java | 147 ++++- .../builder/OceanBaseSqlBuilder.java | 290 ++++---- .../oceanbase/type/OceanBaseCharsetEnum.java | 36 + .../type/OceanBaseCollationEnum.java | 44 ++ .../type/OceanBaseColumnTypeEnum.java | 622 ++++++++---------- .../type/OceanBaseDefaultValueEnum.java | 29 + .../type/OceanBaseIndexTypeEnum.java | 267 ++++---- 7 files changed, 811 insertions(+), 624 deletions(-) create mode 100644 chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseCharsetEnum.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseCollationEnum.java create mode 100644 chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseDefaultValueEnum.java diff --git a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/OceanBaseMetaData.java b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/OceanBaseMetaData.java index 52b2f0aed..e8975422e 100644 --- a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/OceanBaseMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/OceanBaseMetaData.java @@ -1,24 +1,31 @@ package ai.chat2db.plugin.oceanbase; +import ai.chat2db.plugin.oceanbase.builder.OceanBaseSqlBuilder; +import ai.chat2db.plugin.oceanbase.type.*; import ai.chat2db.spi.MetaData; +import ai.chat2db.spi.SqlBuilder; import ai.chat2db.spi.jdbc.DefaultMetaService; -import ai.chat2db.spi.model.Table; -import ai.chat2db.spi.model.TableColumn; +import ai.chat2db.spi.model.*; import ai.chat2db.spi.sql.SQLExecutor; +import jakarta.validation.constraints.NotEmpty; import org.apache.commons.lang3.StringUtils; import java.sql.Connection; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; import java.util.stream.Collectors; public class OceanBaseMetaData extends DefaultMetaService implements MetaData { + @Override + public SqlBuilder getSqlBuilder() { + return new OceanBaseSqlBuilder(); + } @Override public List
tables(Connection connection, String databaseName, String schemaName, String tableName) { - String sql = "SHOW TABLE STATUS FROM "+ databaseName + " where name = '" + tableName + "';"; + String sql = "SHOW TABLE STATUS FROM "+ format(databaseName) + " where name = '" + tableName + "';"; return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { List
tables = new ArrayList<>(); while (resultSet.next()) { @@ -33,10 +40,10 @@ public List
tables(Connection connection, String databaseName, String sch }); } + private static String SELECT_TABLE_COLUMNS = "SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s' order by ORDINAL_POSITION"; @Override public List columns(Connection connection, String databaseName, String schemaName, String tableName) { - return null; - /*String sql = String.format(SELECT_TABLE_COLUMNS, databaseName, tableName); + String sql = String.format(SELECT_TABLE_COLUMNS, databaseName, tableName); List tableColumns = new ArrayList<>(); return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { while (resultSet.next()) { @@ -61,10 +68,130 @@ public List columns(Connection connection, String databaseName, Str tableColumns.add(column); } return tableColumns; - });*/ + }); + } + + private void setColumnSize(TableColumn column, String columnType) { + try { + if (columnType.contains("(")) { + String size = columnType.substring(columnType.indexOf("(") + 1, columnType.indexOf(")")); + if ("SET".equalsIgnoreCase(column.getColumnType()) || "ENUM".equalsIgnoreCase(column.getColumnType())) { + column.setValue(size); + } else { + if (size.contains(",")) { + String[] sizes = size.split(","); + if (StringUtils.isNotBlank(sizes[0])) { + column.setColumnSize(Integer.parseInt(sizes[0])); + } + if (StringUtils.isNotBlank(sizes[1])) { + column.setDecimalDigits(Integer.parseInt(sizes[1])); + } + } else { + column.setColumnSize(Integer.parseInt(size)); + } + } + } + } catch (Exception e) { + } + } + + @Override + public TableMeta getTableMeta(String databaseName, String schemaName, String tableName) { + return TableMeta.builder() + .columnTypes(OceanBaseColumnTypeEnum.getTypes()) + .charsets(OceanBaseCharsetEnum.getCharsets()) + .collations(OceanBaseCollationEnum.getCollations()) + .indexTypes(OceanBaseIndexTypeEnum.getIndexTypes()) + .defaultValues(OceanBaseDefaultValueEnum.getDefaultValues()) + .build(); + } + @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(name -> format(name)).collect(Collectors.joining(".")); + } + + + @Override + public String tableDDL(Connection connection, @NotEmpty String databaseName, String schemaName, + @NotEmpty String tableName) { + String sql = "SHOW CREATE TABLE " + format(databaseName) + "." + + format(tableName); + return SQLExecutor.getInstance().execute(connection, sql, resultSet -> { + if (resultSet.next()) { + return resultSet.getString("Create Table"); + } + return null; + }); } + + @Override + public List indexes(Connection connection, String databaseName, String schemaName, String tableName) { + StringBuilder queryBuf = new StringBuilder("SHOW INDEX FROM "); + queryBuf.append(format(tableName)); + queryBuf.append("."); + queryBuf.append(format(databaseName)); + return SQLExecutor.getInstance().execute(connection, queryBuf.toString(), resultSet -> { + LinkedHashMap map = new LinkedHashMap(); + while (resultSet.next()) { + String keyName = resultSet.getString("Key_name"); + TableIndex tableIndex = map.get(keyName); + if (tableIndex != null) { + List columnList = tableIndex.getColumnList(); + columnList.add(getTableIndexColumn(resultSet)); + columnList = columnList.stream().sorted(Comparator.comparing(TableIndexColumn::getOrdinalPosition)) + .collect(Collectors.toList()); + tableIndex.setColumnList(columnList); + } else { + TableIndex index = new TableIndex(); + index.setDatabaseName(databaseName); + index.setSchemaName(schemaName); + index.setTableName(tableName); + index.setName(keyName); + index.setUnique(!resultSet.getBoolean("Non_unique")); + index.setType(resultSet.getString("Index_type")); + index.setComment(resultSet.getString("Index_comment")); + List tableIndexColumns = new ArrayList<>(); + tableIndexColumns.add(getTableIndexColumn(resultSet)); + index.setColumnList(tableIndexColumns); + if ("PRIMARY".equalsIgnoreCase(keyName)) { + index.setType(OceanBaseIndexTypeEnum.PRIMARY_KEY.getName()); + } else if (index.getUnique()) { + index.setType(OceanBaseIndexTypeEnum.UNIQUE.getName()); + } else if ("SPATIAL".equalsIgnoreCase(index.getType())) { + index.setType(OceanBaseIndexTypeEnum.SPATIAL.getName()); + } else if ("FULLTEXT".equalsIgnoreCase(index.getType())) { + index.setType(OceanBaseIndexTypeEnum.FULLTEXT.getName()); + } else { + index.setType(OceanBaseIndexTypeEnum.NORMAL.getName()); + } + map.put(keyName, index); + } + } + return map.values().stream().collect(Collectors.toList()); + }); + + } + + private TableIndexColumn getTableIndexColumn(ResultSet resultSet) throws SQLException { + TableIndexColumn tableIndexColumn = new TableIndexColumn(); + tableIndexColumn.setColumnName(resultSet.getString("Column_name")); + tableIndexColumn.setOrdinalPosition(resultSet.getShort("Seq_in_index")); + tableIndexColumn.setCollation(resultSet.getString("Collation")); + tableIndexColumn.setCardinality(resultSet.getLong("Cardinality")); + tableIndexColumn.setSubPart(resultSet.getLong("Sub_part")); + String collation = resultSet.getString("Collation"); + if ("a".equalsIgnoreCase(collation)) { + tableIndexColumn.setAscOrDesc("ASC"); + } else if ("d".equalsIgnoreCase(collation)) { + tableIndexColumn.setAscOrDesc("DESC"); + } + return tableIndexColumn; + } + + public static String format(String tableName) { + return "`" + tableName + "`"; + } + } diff --git a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/builder/OceanBaseSqlBuilder.java b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/builder/OceanBaseSqlBuilder.java index a9ff9919c..745f3189c 100644 --- a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/builder/OceanBaseSqlBuilder.java +++ b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/builder/OceanBaseSqlBuilder.java @@ -1,145 +1,145 @@ -//package ai.chat2db.plugin.oceanbase.builder; -// -//import ai.chat2db.plugin.oceanbase.type.OceanBaseColumnTypeEnum; -//import ai.chat2db.plugin.oceanbase.type.OceanBaseIndexTypeEnum; -//import ai.chat2db.spi.SqlBuilder; -//import ai.chat2db.spi.jdbc.DefaultSqlBuilder; -//import ai.chat2db.spi.model.*; -//import org.apache.commons.lang3.StringUtils; -// -//public class OceanBaseSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { -// -// @Override -// public String buildCreateTableSql(Table table) { -// StringBuilder script = new StringBuilder(); -// script.append("CREATE TABLE "); -// if(StringUtils.isNotBlank(table.getDatabaseName())) { -// script.append("`").append(table.getName()).append("`").append("."); -// } -// script.append("`").append(table.getName()).append("`").append(" (").append("\n"); -// -// // append column -// for (TableColumn column : table.getColumnList()) { -// if(StringUtils.isBlank(column.getName())|| StringUtils.isBlank(column.getColumnType())){ -// continue; -// } -// OceanBaseColumnTypeEnum typeEnum = OceanBaseColumnTypeEnum.getByType(column.getColumnType()); -// script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n"); -// } -// -// // append primary key and index -// for (TableIndex tableIndex : table.getIndexList()) { -// if(StringUtils.isBlank(tableIndex.getName())|| StringUtils.isBlank(tableIndex.getType())){ -// continue; -// } -// OceanBaseIndexTypeEnum mysqlIndexTypeEnum = OceanBaseIndexTypeEnum.getByType(tableIndex.getType()); -// script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n"); -// } -// -// script = new StringBuilder(script.substring(0, script.length() - 2)); -// script.append("\n)"); -// -// -// if (StringUtils.isNotBlank(table.getEngine())) { -// script.append(" ENGINE=").append(table.getEngine()); -// } -// -// if (StringUtils.isNotBlank(table.getCharset())) { -// script.append(" DEFAULT CHARACTER SET=").append(table.getCharset()); -// } -// -// if (StringUtils.isNotBlank(table.getCollate())) { -// script.append(" COLLATE=").append(table.getCollate()); -// } -// -// if (table.getIncrementValue() != null) { -// script.append(" AUTO_INCREMENT=").append(table.getIncrementValue()); -// } -// -// if (StringUtils.isNotBlank(table.getComment())) { -// script.append(" COMMENT='").append(table.getComment()).append("'"); -// } -// -// if (StringUtils.isNotBlank(table.getPartition())) { -// script.append(" \n").append(table.getPartition()); -// } -// script.append(";"); -// -// return script.toString(); -// } -// -// @Override -// public String buildModifyTaleSql(Table oldTable, Table newTable) { -// StringBuilder script = new StringBuilder(); -// script.append("ALTER TABLE "); -// if(StringUtils.isNotBlank(oldTable.getDatabaseName())) { -// script.append("`").append(oldTable.getDatabaseName()).append("`").append("."); -// } -// script.append("`").append(oldTable.getName()).append("`").append("\n"); -// if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) { -// script.append("\t").append("RENAME TO ").append("`").append(newTable.getName()).append("`").append(",\n"); -// } -// if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) { -// script.append("\t").append("COMMENT=").append("'").append(newTable.getComment()).append("'").append(",\n"); -// } -// if (oldTable.getIncrementValue() != newTable.getIncrementValue()) { -// script.append("\t").append("AUTO_INCREMENT=").append(newTable.getIncrementValue()).append(",\n"); -// } -// -// // append modify column -// for (TableColumn tableColumn : newTable.getColumnList()) { -// if (StringUtils.isNotBlank(tableColumn.getEditStatus()) && StringUtils.isNotBlank(tableColumn.getColumnType())&& StringUtils.isNotBlank(tableColumn.getName())){ -// OceanBaseColumnTypeEnum typeEnum = OceanBaseColumnTypeEnum.getByType(tableColumn.getColumnType()); -// script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n"); -// } -// } -// -// // append modify index -// for (TableIndex tableIndex : newTable.getIndexList()) { -// if (StringUtils.isNotBlank(tableIndex.getEditStatus()) && StringUtils.isNotBlank(tableIndex.getType())) { -// OceanBaseIndexTypeEnum mysqlIndexTypeEnum = OceanBaseIndexTypeEnum.getByType(tableIndex.getType()); -// script.append("\t").append(mysqlIndexTypeEnum.buildModifyIndex(tableIndex)).append(",\n"); -// } -// } -// if(script.length()>2) { -// script = new StringBuilder(script.substring(0, script.length() - 2)); -// script.append(";"); -// } -// -// return script.toString(); -// } -// -// -// -// @Override -// public String pageLimit(String sql, int offset, int pageNo, int pageSize) { -// StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14); -// sqlBuilder.append(sql); -// if (offset == 0) { -// sqlBuilder.append("\n LIMIT "); -// sqlBuilder.append(pageSize); -// } else { -// sqlBuilder.append("\n LIMIT "); -// sqlBuilder.append(offset); -// sqlBuilder.append(","); -// sqlBuilder.append(pageSize); -// } -// return sqlBuilder.toString(); -// } -// -// -// -// -// @Override -// public String buildCreateDatabaseSql(Database database) { -// StringBuilder sqlBuilder = new StringBuilder(); -// sqlBuilder.append("CREATE DATABASE `"+database.getName()+"`"); -// if (StringUtils.isNotBlank(database.getCharset())) { -// sqlBuilder.append(" DEFAULT CHARACTER SET=").append(database.getCharset()); -// } -// if (StringUtils.isNotBlank(database.getCollation())) { -// sqlBuilder.append(" COLLATE=").append(database.getCollation()); -// } -// return sqlBuilder.toString(); -// } -//} +package ai.chat2db.plugin.oceanbase.builder; + +import ai.chat2db.plugin.oceanbase.type.OceanBaseColumnTypeEnum; +import ai.chat2db.plugin.oceanbase.type.OceanBaseIndexTypeEnum; +import ai.chat2db.spi.SqlBuilder; +import ai.chat2db.spi.jdbc.DefaultSqlBuilder; +import ai.chat2db.spi.model.*; +import org.apache.commons.lang3.StringUtils; + +public class OceanBaseSqlBuilder extends DefaultSqlBuilder implements SqlBuilder { + + @Override + public String buildCreateTableSql(Table table) { + StringBuilder script = new StringBuilder(); + script.append("CREATE TABLE "); + if(StringUtils.isNotBlank(table.getDatabaseName())) { + script.append("`").append(table.getName()).append("`").append("."); + } + script.append("`").append(table.getName()).append("`").append(" (").append("\n"); + + // append column + for (TableColumn column : table.getColumnList()) { + if(StringUtils.isBlank(column.getName())|| StringUtils.isBlank(column.getColumnType())){ + continue; + } + OceanBaseColumnTypeEnum typeEnum = OceanBaseColumnTypeEnum.getByType(column.getColumnType()); + script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n"); + } + + // append primary key and index + for (TableIndex tableIndex : table.getIndexList()) { + if(StringUtils.isBlank(tableIndex.getName())|| StringUtils.isBlank(tableIndex.getType())){ + continue; + } + OceanBaseIndexTypeEnum mysqlIndexTypeEnum = OceanBaseIndexTypeEnum.getByType(tableIndex.getType()); + script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n"); + } + + script = new StringBuilder(script.substring(0, script.length() - 2)); + script.append("\n)"); + + + if (StringUtils.isNotBlank(table.getEngine())) { + script.append(" ENGINE=").append(table.getEngine()); + } + + if (StringUtils.isNotBlank(table.getCharset())) { + script.append(" DEFAULT CHARACTER SET=").append(table.getCharset()); + } + + if (StringUtils.isNotBlank(table.getCollate())) { + script.append(" COLLATE=").append(table.getCollate()); + } + + if (table.getIncrementValue() != null) { + script.append(" AUTO_INCREMENT=").append(table.getIncrementValue()); + } + + if (StringUtils.isNotBlank(table.getComment())) { + script.append(" COMMENT='").append(table.getComment()).append("'"); + } + + if (StringUtils.isNotBlank(table.getPartition())) { + script.append(" \n").append(table.getPartition()); + } + script.append(";"); + + return script.toString(); + } + + @Override + public String buildModifyTaleSql(Table oldTable, Table newTable) { + StringBuilder script = new StringBuilder(); + script.append("ALTER TABLE "); + if(StringUtils.isNotBlank(oldTable.getDatabaseName())) { + script.append("`").append(oldTable.getDatabaseName()).append("`").append("."); + } + script.append("`").append(oldTable.getName()).append("`").append("\n"); + if (!StringUtils.equalsIgnoreCase(oldTable.getName(), newTable.getName())) { + script.append("\t").append("RENAME TO ").append("`").append(newTable.getName()).append("`").append(",\n"); + } + if (!StringUtils.equalsIgnoreCase(oldTable.getComment(), newTable.getComment())) { + script.append("\t").append("COMMENT=").append("'").append(newTable.getComment()).append("'").append(",\n"); + } + if (oldTable.getIncrementValue() != newTable.getIncrementValue()) { + script.append("\t").append("AUTO_INCREMENT=").append(newTable.getIncrementValue()).append(",\n"); + } + + // append modify column + for (TableColumn tableColumn : newTable.getColumnList()) { + if (StringUtils.isNotBlank(tableColumn.getEditStatus()) && StringUtils.isNotBlank(tableColumn.getColumnType())&& StringUtils.isNotBlank(tableColumn.getName())){ + OceanBaseColumnTypeEnum typeEnum = OceanBaseColumnTypeEnum.getByType(tableColumn.getColumnType()); + script.append("\t").append(typeEnum.buildModifyColumn(tableColumn)).append(",\n"); + } + } + + // append modify index + for (TableIndex tableIndex : newTable.getIndexList()) { + if (StringUtils.isNotBlank(tableIndex.getEditStatus()) && StringUtils.isNotBlank(tableIndex.getType())) { + OceanBaseIndexTypeEnum mysqlIndexTypeEnum = OceanBaseIndexTypeEnum.getByType(tableIndex.getType()); + script.append("\t").append(mysqlIndexTypeEnum.buildModifyIndex(tableIndex)).append(",\n"); + } + } + if(script.length()>2) { + script = new StringBuilder(script.substring(0, script.length() - 2)); + script.append(";"); + } + + return script.toString(); + } + + + + @Override + public String pageLimit(String sql, int offset, int pageNo, int pageSize) { + StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14); + sqlBuilder.append(sql); + if (offset == 0) { + sqlBuilder.append("\n LIMIT "); + sqlBuilder.append(pageSize); + } else { + sqlBuilder.append("\n LIMIT "); + sqlBuilder.append(offset); + sqlBuilder.append(","); + sqlBuilder.append(pageSize); + } + return sqlBuilder.toString(); + } + + + + + @Override + public String buildCreateDatabaseSql(Database database) { + StringBuilder sqlBuilder = new StringBuilder(); + sqlBuilder.append("CREATE DATABASE `"+database.getName()+"`"); + if (StringUtils.isNotBlank(database.getCharset())) { + sqlBuilder.append(" DEFAULT CHARACTER SET=").append(database.getCharset()); + } + if (StringUtils.isNotBlank(database.getCollation())) { + sqlBuilder.append(" COLLATE=").append(database.getCollation()); + } + return sqlBuilder.toString(); + } +} diff --git a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseCharsetEnum.java b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseCharsetEnum.java new file mode 100644 index 000000000..12de717cd --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseCharsetEnum.java @@ -0,0 +1,36 @@ +package ai.chat2db.plugin.oceanbase.type; + +import ai.chat2db.spi.model.Charset; + +import java.util.Arrays; +import java.util.List; + +public enum OceanBaseCharsetEnum { + + BINARY("binary", "binary"), + + GBK("gbk", "gbk_chinese_ci"), + + GB18030("gb18030", "gb18030_chinese_ci"), + + UTF16("utf16", "utf16_general_ci"), + + UTF8MB4("utf8mb4", "utf8mb4_general_ci"), + ; + + private Charset charset; + + OceanBaseCharsetEnum(String charsetName, String defaultCollationName) { + this.charset = new Charset(charsetName, defaultCollationName); + } + + + public Charset getCharset() { + return charset; + } + + public static List getCharsets() { + return Arrays.stream(OceanBaseCharsetEnum.values()).map(OceanBaseCharsetEnum::getCharset).collect(java.util.stream.Collectors.toList()); + } + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseCollationEnum.java b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseCollationEnum.java new file mode 100644 index 000000000..d5eaa5428 --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseCollationEnum.java @@ -0,0 +1,44 @@ +package ai.chat2db.plugin.oceanbase.type; + +import ai.chat2db.spi.model.Collation; + +import java.util.Arrays; +import java.util.List; + +public enum OceanBaseCollationEnum { + + UTF8MB4_GENERAL_CI("utf8mb4_general_ci"), + + UTF8MB4_BIN("utf8mb4_bin"), + + BINARY("binary"), + + GBK_CHINESE_CI("gbk_chinese_ci"), + + GBK_BIN("gbk_bin"), + + UTF16_GENERAL_CI("utf16_general_ci"), + + UTF16_BIN("utf16_bin"), + + GB18030_CHINESE_CI("gb18030_chinese_ci"), + + GB18030_BIN("gb18030_bin"), + ; + + private Collation collation; + + OceanBaseCollationEnum(String collationName) { + this.collation = new Collation(collationName); + } + + public Collation getCollation() { + return collation; + } + + + public static List getCollations() { + return Arrays.asList(OceanBaseCollationEnum.values()).stream().map(OceanBaseCollationEnum::getCollation).collect(java.util.stream.Collectors.toList()); + } + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseColumnTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseColumnTypeEnum.java index baca359b1..589ed2efc 100644 --- a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseColumnTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseColumnTypeEnum.java @@ -1,336 +1,286 @@ -//package ai.chat2db.plugin.oceanbase.type; -// -//import ai.chat2db.spi.ColumnBuilder; -//import ai.chat2db.spi.enums.EditStatus; -//import ai.chat2db.spi.model.ColumnType; -//import ai.chat2db.spi.model.TableColumn; -//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 OceanBaseColumnTypeEnum implements ColumnBuilder { -// -// BIT("BIT", true, false, true, false, false, false, true, true, false, false), -// -// TINYINT("TINYINT", false, false, true, true, false, false, true, true, false, false), -// -// TINYINT_UNSIGNED("TINYINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// SMALLINT("SMALLINT", false, false, true, true, false, false, true, true, false, false), -// -// SMALLINT_UNSIGNED("SMALLINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// MEDIUMINT("MEDIUMINT", false, false, true, true, false, false, true, true, false, false), -// -// MEDIUMINT_UNSIGNED("MEDIUMINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// INT("INT", false, false, true, true, false, false, true, true, false, false), -// -// -// INT_UNSIGNED("INT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// BIGINT("BIGINT", false, false, true, true, false, false, true, true, false, false), -// -// -// BIGINT_UNSIGNED("BIGINT UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// -// DECIMAL("DECIMAL", true, true, true, false, false, false, true, true, false, false), -// -// DECIMAL_UNSIGNED("DECIMAL UNSIGNED", true, true, true, false, false, false, true, true, false, false), -// -// -// FLOAT("FLOAT", true, true, true, false, false, false, true, true, false, false), -// -// FLOAT_UNSIGNED("FLOAT UNSIGNED", true, true, true, false, false, false, true, true, false, false), -// -// DOUBLE("DOUBLE", true, true, true, false, false, false, true, true, false, false), -// -// DOUBLE_UNSIGNED("DOUBLE UNSIGNED", true, true, true, false, false, false, true, true, false, false), -// DATE("DATE", false, false, true, false, false, false, true, true, false, false), -// DATETIME("DATETIME", true, false, true, false, false, false, true, true, true, false), -// -// TIMESTAMP("TIMESTAMP", true, false, true, false, false, false, true, true, true, false), -// TIME("TIME", true, false, true, false, false, false, true, true, false, false), -// YEAR("YEAR", false, false, true, false, false, false, true, true, false, false), -// CHAR("CHAR", true, false, true, false, true, true, true, true, false, false), -// -// VARCHAR("VARCHAR", true, false, true, false, true, true, true, true, false, false), -// -// BINARY("BINARY", true, false, true, false, false, false, true, true, false, false), -// -// VARBINARY("VARBINARY", true, false, true, false, false, false, true, true, false, false), -// -// TINYBLOB("TINYBLOB", false, false, true, false, false, false, true, false, false, false), -// -// BLOB("BLOB", false, false, true, false, false, false, true, false, false, false), -// -// MEDIUMBLOB("MEDIUMBLOB", false, false, true, false, false, false, true, false, false, false), -// -// LONGBLOB("LONGBLOB", false, false, true, false, false, false, true, false, false, false), -// -// TINYTEXT("TINYTEXT", false, false, true, false, true, true, true, false, false, false), -// -// TEXT("TEXT", false, false, true, false, true, true, true, false, false, false), -// -// MEDIUMTEXT("MEDIUMTEXT", false, false, true, false, true, true, true, false, false, false), -// -// LONGTEXT("LONGTEXT", false, false, true, false, true, true, true, false, false, false), -// -// -// ENUM("ENUM", false, false, true, false, true, true, true, true, true, true), -// -// -// BOOL("BOOL", false, false, true, true, false, false, true, true, false, false), -// -// INTEGER("INTEGER", false, false, true, true, false, false, true, true, false, false), -// -// INTEGER_UNSIGNED("INTEGER UNSIGNED", false, false, true, true, false, false, true, true, false, false), -// -// REAL("REAL", true, true, true, false, false, false, true, true, false, false), -// -// SET("SET", false, false, true, false, true, true, true, true, true, true), -// -// -// GEOMETRY("GEOMETRY", false, false, true, false, false, false, true, false, false, false), -// -// POINT("POINT", false, false, true, false, false, false, true, false, false, false), -// -// LINESTRING("LINESTRING", false, false, true, false, false, false, true, false, false, false), -// -// POLYGON("POLYGON", false, false, true, false, false, false, true, false, false, false), -// -// MULTIPOINT("MULTIPOINT", false, false, true, false, false, false, true, false, false, false), -// -// MULTILINESTRING("MULTILINESTRING", false, false, true, false, false, false, true, false, false, false), -// -// MULTIPOLYGON("MULTIPOLYGON", false, false, true, false, false, false, true, false, false, false), -// -// GEOMETRYCOLLECTION("GEOMETRYCOLLECTION", false, false, true, false, false, false, true, false, false, false), -// -// JSON("JSON", false, false, true, false, false, false, true, false, false, false); -// -// private ColumnType columnType; -// -// public static OceanBaseColumnTypeEnum getByType(String dataType) { -// return COLUMN_TYPE_MAP.get(dataType.toUpperCase()); -// } -// -// public ColumnType getColumnType() { -// return columnType; -// } -// -// -// OceanBaseColumnTypeEnum(String dataTypeName, boolean supportLength, boolean supportScale, boolean supportNullable, boolean supportAutoIncrement, boolean supportCharset, boolean supportCollation, boolean supportComments, boolean supportDefaultValue, boolean supportExtent, boolean supportValue) { -// this.columnType = new ColumnType(dataTypeName, supportLength, supportScale, supportNullable, supportAutoIncrement, supportCharset, supportCollation, supportComments, supportDefaultValue, supportExtent,supportValue,false); -// } -// -// private static Map COLUMN_TYPE_MAP = Maps.newHashMap(); -// -// static { -// for (OceanBaseColumnTypeEnum value : OceanBaseColumnTypeEnum.values()) { -// COLUMN_TYPE_MAP.put(value.getColumnType().getTypeName(), value); -// } -// } -// -// -// @Override -// public String buildCreateColumnSql(TableColumn column) { -// OceanBaseColumnTypeEnum type = COLUMN_TYPE_MAP.get(column.getColumnType().toUpperCase()); -// if (type == null) { -// return ""; -// } -// StringBuilder script = new StringBuilder(); -// -// script.append("`").append(column.getName()).append("`").append(" "); -// -// script.append(buildDataType(column, type)).append(" "); -// -// script.append(buildCharset(column,type)).append(" "); -// -// script.append(buildCollation(column,type)).append(" "); -// -// script.append(buildNullable(column,type)).append(" "); -// -// script.append(buildDefaultValue(column,type)).append(" "); -// -// script.append(buildExt(column,type)).append(" "); -// -// script.append(buildAutoIncrement(column,type)).append(" "); -// -// script.append(buildComment(column,type)).append(" "); -// -// return script.toString(); -// } -// -// private String buildCharset(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportCharset() || StringUtils.isEmpty(column.getCharSetName())){ -// return ""; -// } -// return StringUtils.join("CHARACTER SET ",column.getCharSetName()); -// } -// -// private String buildCollation(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportCollation() || StringUtils.isEmpty(column.getCollationName())){ -// return ""; -// } -// return StringUtils.join("COLLATE ",column.getCollationName()); -// } -// -// @Override -// public String buildModifyColumn(TableColumn tableColumn) { -// -// if (EditStatus.DELETE.name().equals(tableColumn.getEditStatus())) { -// return StringUtils.join("DROP COLUMN `", tableColumn.getName() + "`"); -// } -// if (EditStatus.ADD.name().equals(tableColumn.getEditStatus())) { -// return StringUtils.join("ADD COLUMN ", buildCreateColumnSql(tableColumn)); -// } -// if (EditStatus.MODIFY.name().equals(tableColumn.getEditStatus())) { -// if (!StringUtils.equalsIgnoreCase(tableColumn.getOldName(), tableColumn.getName())) { -// return StringUtils.join("CHANGE COLUMN `", tableColumn.getOldName(), "` ", buildCreateColumnSql(tableColumn)); -// } else { -// return StringUtils.join("MODIFY COLUMN ", buildCreateColumnSql(tableColumn)); -// } -// } -// return ""; -// } -// -// private String buildAutoIncrement(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportAutoIncrement()){ -// return ""; -// } -// if (column.getAutoIncrement() != null && column.getAutoIncrement()) { -// return "AUTO_INCREMENT"; -// } -// return ""; -// } -// -// private String buildComment(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.columnType.isSupportComments() || StringUtils.isEmpty(column.getComment())){ -// return ""; -// } -// return StringUtils.join("COMMENT '",column.getComment(),"'"); -// } -// -// private String buildExt(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.columnType.isSupportExtent() || StringUtils.isEmpty(column.getExtent())){ -// return ""; -// } -// return column.getComment(); -// } -// -// private String buildDefaultValue(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportDefaultValue() || StringUtils.isEmpty(column.getDefaultValue())){ -// return ""; -// } -// -// if("EMPTY_STRING".equalsIgnoreCase(column.getDefaultValue().trim())){ -// return StringUtils.join("DEFAULT ''"); -// } -// -// if("NULL".equalsIgnoreCase(column.getDefaultValue().trim())){ -// return StringUtils.join("DEFAULT NULL"); -// } -// -// if(Arrays.asList(CHAR,VARCHAR,BINARY,VARBINARY, SET,ENUM).contains(type)){ -// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); -// } -// -// if(Arrays.asList(DATE,TIME,YEAR).contains(type)){ -// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); -// } -// -// if(Arrays.asList(DATETIME,TIMESTAMP).contains(type)){ -// if("CURRENT_TIMESTAMP".equalsIgnoreCase(column.getDefaultValue().trim())){ -// return StringUtils.join("DEFAULT ",column.getDefaultValue()); -// } -// return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); -// } -// -// return StringUtils.join("DEFAULT ",column.getDefaultValue()); -// } -// -// private String buildNullable(TableColumn column, OceanBaseColumnTypeEnum type) { -// if(!type.getColumnType().isSupportNullable()){ -// return ""; -// } -// if (column.getNullable()!=null && 1==column.getNullable()) { -// return "NULL"; -// } else { -// return "NOT NULL"; -// } -// } -// -// private String buildDataType(TableColumn column, OceanBaseColumnTypeEnum type) { -// String columnType = type.columnType.getTypeName(); -// if (Arrays.asList(BINARY, VARBINARY, VARCHAR, CHAR).contains(type)) { -// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); -// } -// -// if (BIT.equals(type)) { -// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); -// } -// -// if (Arrays.asList(TIME, DATETIME, TIMESTAMP).contains(type)) { -// if (column.getColumnSize() == null || column.getColumnSize() == 0) { -// return columnType; -// } else { -// return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); -// } -// } -// -// -// if (Arrays.asList(DECIMAL, FLOAT, DOUBLE).contains(type)) { -// if (column.getColumnSize() == null || column.getDecimalDigits() == null) { -// return columnType; -// } -// if (column.getColumnSize() != null && column.getDecimalDigits() == null) { -// return StringUtils.join(columnType, "(", column.getColumnSize() + ")"); -// } -// if (column.getColumnSize() != null && column.getDecimalDigits() != null) { -// return StringUtils.join(columnType, "(", column.getColumnSize() + "," + column.getDecimalDigits() + ")"); -// } -// } -// -// if (Arrays.asList(DECIMAL_UNSIGNED, FLOAT_UNSIGNED, DECIMAL_UNSIGNED).contains(type)) { -// if (column.getColumnSize() == null || column.getDecimalDigits() == null) { -// return columnType; -// } -// if (column.getColumnSize() != null && column.getDecimalDigits() == null) { -// return unsignedDataType(columnType, "(" + column.getColumnSize() + ")"); -// } -// if (column.getColumnSize() != null && column.getDecimalDigits() != null) { -// return unsignedDataType(columnType, "(" + column.getColumnSize() + "," + column.getDecimalDigits() + ")"); -// } -// } -// -// if(Arrays.asList(SET,ENUM).contains(type)){ -// if(!StringUtils.isEmpty( column.getValue())){ -// return StringUtils.join(columnType,"(",column.getValue(),")"); -// } -// //List enumList = column. -// } -// -// return columnType; -// } -// -// private String unsignedDataType(String dataTypeName, String middle) { -// String[] split = dataTypeName.split(" "); -// if (split.length == 2) { -// return StringUtils.join(split[0], middle, split[1]); -// } -// return StringUtils.join(dataTypeName, middle); -// } -// -// public static List getTypes(){ -// return Arrays.stream(OceanBaseColumnTypeEnum.values()).map(columnTypeEnum -> -// columnTypeEnum.getColumnType() -// ).toList(); -// } -// -// -//} +package ai.chat2db.plugin.oceanbase.type; + +import ai.chat2db.spi.ColumnBuilder; +import ai.chat2db.spi.enums.EditStatus; +import ai.chat2db.spi.model.ColumnType; +import ai.chat2db.spi.model.TableColumn; +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 OceanBaseColumnTypeEnum implements ColumnBuilder { + + BIT("BIT", true, false, true, false, false, false, true, true, false, false), + + TINYINT("TINYINT", false, false, true, true, false, false, true, true, false, false), + + SMALLINT("SMALLINT", false, false, true, true, false, false, true, true, false, false), + + MEDIUMINT("MEDIUMINT", false, false, true, true, false, false, true, true, false, false), + + INT("INT", false, false, true, true, false, false, true, true, false, false), + + INTEGER("INTEGER", false, false, true, true, false, false, true, true, false, false), + + BIGINT("BIGINT", false, false, true, true, false, false, true, true, false, false), + + FLOAT("FLOAT", true, true, true, false, false, false, true, true, false, false), + + DOUBLE("DOUBLE", true, true, true, false, false, false, true, true, false, false), + + DECIMAL("DECIMAL", true, true, true, false, false, false, true, true, false, false), + + NUMERIC("NUMERIC", true, true, true, false, false, false, true, true, false, false), + + DATE("DATE", false, false, true, false, false, false, true, true, false, false), + + DATETIME("DATETIME", true, false, true, false, false, false, true, true, true, false), + + TIMESTAMP("TIMESTAMP", true, false, true, false, false, false, true, true, true, false), + + TIME("TIME", true, false, true, false, false, false, true, true, false, false), + + YEAR("YEAR", false, false, true, false, false, false, true, true, false, false), + + CHAR("CHAR", true, false, true, false, true, true, true, true, false, false), + + VARCHAR("VARCHAR", true, false, true, false, true, true, true, true, false, false), + + BINARY("BINARY", true, false, true, false, false, false, true, true, false, false), + + VARBINARY("VARBINARY", true, false, true, false, false, false, true, true, false, false), + + TINYBLOB("TINYBLOB", false, false, true, false, false, false, true, false, false, false), + + BLOB("BLOB", false, false, true, false, false, false, true, false, false, false), + + MEDIUMBLOB("MEDIUMBLOB", false, false, true, false, false, false, true, false, false, false), + + LONGBLOB("LONGBLOB", false, false, true, false, false, false, true, false, false, false), + + TINYTEXT("TINYTEXT", false, false, true, false, true, true, true, false, false, false), + + TEXT("TEXT", false, false, true, false, true, true, true, false, false, false), + + MEDIUMTEXT("MEDIUMTEXT", false, false, true, false, true, true, true, false, false, false), + + LONGTEXT("LONGTEXT", false, false, true, false, true, true, true, false, false, false), + + ENUM("ENUM", false, false, true, false, true, true, true, true, true, true), + + SET("SET", false, false, true, false, true, true, true, true, true, true), + + JSON("JSON", false, false, true, false, false, false, true, false, false, false); + + private ColumnType columnType; + + public static OceanBaseColumnTypeEnum getByType(String dataType) { + return COLUMN_TYPE_MAP.get(dataType.toUpperCase()); + } + + public ColumnType getColumnType() { + return columnType; + } + + + OceanBaseColumnTypeEnum(String dataTypeName, boolean supportLength, boolean supportScale, boolean supportNullable, boolean supportAutoIncrement, boolean supportCharset, boolean supportCollation, boolean supportComments, boolean supportDefaultValue, boolean supportExtent, boolean supportValue) { + this.columnType = new ColumnType(dataTypeName, supportLength, supportScale, supportNullable, supportAutoIncrement, supportCharset, supportCollation, supportComments, supportDefaultValue, supportExtent,supportValue,false); + } + + private static Map COLUMN_TYPE_MAP = Maps.newHashMap(); + + static { + for (OceanBaseColumnTypeEnum value : OceanBaseColumnTypeEnum.values()) { + COLUMN_TYPE_MAP.put(value.getColumnType().getTypeName(), value); + } + } + + + @Override + public String buildCreateColumnSql(TableColumn column) { + OceanBaseColumnTypeEnum type = COLUMN_TYPE_MAP.get(column.getColumnType().toUpperCase()); + if (type == null) { + return ""; + } + StringBuilder script = new StringBuilder(); + + script.append("`").append(column.getName()).append("`").append(" "); + + script.append(buildDataType(column, type)).append(" "); + + script.append(buildCharset(column,type)).append(" "); + + script.append(buildCollation(column,type)).append(" "); + + script.append(buildNullable(column,type)).append(" "); + + script.append(buildDefaultValue(column,type)).append(" "); + + script.append(buildExt(column,type)).append(" "); + + script.append(buildAutoIncrement(column,type)).append(" "); + + script.append(buildComment(column,type)).append(" "); + + return script.toString(); + } + + private String buildCharset(TableColumn column, OceanBaseColumnTypeEnum type) { + if(!type.getColumnType().isSupportCharset() || StringUtils.isEmpty(column.getCharSetName())){ + return ""; + } + return StringUtils.join("CHARACTER SET ",column.getCharSetName()); + } + + private String buildCollation(TableColumn column, OceanBaseColumnTypeEnum type) { + if(!type.getColumnType().isSupportCollation() || StringUtils.isEmpty(column.getCollationName())){ + return ""; + } + return StringUtils.join("COLLATE ",column.getCollationName()); + } + + @Override + public String buildModifyColumn(TableColumn tableColumn) { + + if (EditStatus.DELETE.name().equals(tableColumn.getEditStatus())) { + return StringUtils.join("DROP COLUMN `", tableColumn.getName() + "`"); + } + if (EditStatus.ADD.name().equals(tableColumn.getEditStatus())) { + return StringUtils.join("ADD COLUMN ", buildCreateColumnSql(tableColumn)); + } + if (EditStatus.MODIFY.name().equals(tableColumn.getEditStatus())) { + if (!StringUtils.equalsIgnoreCase(tableColumn.getOldName(), tableColumn.getName())) { + return StringUtils.join("CHANGE COLUMN `", tableColumn.getOldName(), "` ", buildCreateColumnSql(tableColumn)); + } else { + return StringUtils.join("MODIFY COLUMN ", buildCreateColumnSql(tableColumn)); + } + } + return ""; + } + + private String buildAutoIncrement(TableColumn column, OceanBaseColumnTypeEnum type) { + if(!type.getColumnType().isSupportAutoIncrement()){ + return ""; + } + if (column.getAutoIncrement() != null && column.getAutoIncrement()) { + return "AUTO_INCREMENT"; + } + return ""; + } + + private String buildComment(TableColumn column, OceanBaseColumnTypeEnum type) { + if(!type.columnType.isSupportComments() || StringUtils.isEmpty(column.getComment())){ + return ""; + } + return StringUtils.join("COMMENT '",column.getComment(),"'"); + } + + private String buildExt(TableColumn column, OceanBaseColumnTypeEnum type) { + if(!type.columnType.isSupportExtent() || StringUtils.isEmpty(column.getExtent())){ + return ""; + } + return column.getComment(); + } + + private String buildDefaultValue(TableColumn column, OceanBaseColumnTypeEnum type) { + if(!type.getColumnType().isSupportDefaultValue() || StringUtils.isEmpty(column.getDefaultValue())){ + return ""; + } + + if("EMPTY_STRING".equalsIgnoreCase(column.getDefaultValue().trim())){ + return StringUtils.join("DEFAULT ''"); + } + + if("NULL".equalsIgnoreCase(column.getDefaultValue().trim())){ + return StringUtils.join("DEFAULT NULL"); + } + + if(Arrays.asList(CHAR,VARCHAR,BINARY,VARBINARY, SET,ENUM).contains(type)){ + return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); + } + + if(Arrays.asList(DATE,TIME,YEAR).contains(type)){ + return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); + } + + if(Arrays.asList(DATETIME,TIMESTAMP).contains(type)){ + if("CURRENT_TIMESTAMP".equalsIgnoreCase(column.getDefaultValue().trim())){ + return StringUtils.join("DEFAULT ",column.getDefaultValue()); + } + return StringUtils.join("DEFAULT '",column.getDefaultValue(),"'"); + } + + return StringUtils.join("DEFAULT ",column.getDefaultValue()); + } + + private String buildNullable(TableColumn column, OceanBaseColumnTypeEnum type) { + if(!type.getColumnType().isSupportNullable()){ + return ""; + } + if (column.getNullable()!=null && 1==column.getNullable()) { + return "NULL"; + } else { + return "NOT NULL"; + } + } + + private String buildDataType(TableColumn column, OceanBaseColumnTypeEnum type) { + String columnType = type.columnType.getTypeName(); + if (Arrays.asList(BINARY, VARBINARY, VARCHAR, CHAR).contains(type)) { + return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); + } + + if (BIT.equals(type)) { + return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); + } + + if (Arrays.asList(TIME, DATETIME, TIMESTAMP).contains(type)) { + if (column.getColumnSize() == null || column.getColumnSize() == 0) { + return columnType; + } else { + return StringUtils.join(columnType, "(", column.getColumnSize(), ")"); + } + } + + + if (Arrays.asList(DECIMAL, FLOAT, DOUBLE).contains(type)) { + if (column.getColumnSize() == null || column.getDecimalDigits() == null) { + return columnType; + } + if (column.getColumnSize() != null && column.getDecimalDigits() == null) { + return StringUtils.join(columnType, "(", column.getColumnSize() + ")"); + } + if (column.getColumnSize() != null && column.getDecimalDigits() != null) { + return StringUtils.join(columnType, "(", column.getColumnSize() + "," + column.getDecimalDigits() + ")"); + } + } + + if(Arrays.asList(SET,ENUM).contains(type)){ + if(!StringUtils.isEmpty( column.getValue())){ + return StringUtils.join(columnType,"(",column.getValue(),")"); + } + //List enumList = column. + } + + return columnType; + } + + private String unsignedDataType(String dataTypeName, String middle) { + String[] split = dataTypeName.split(" "); + if (split.length == 2) { + return StringUtils.join(split[0], middle, split[1]); + } + return StringUtils.join(dataTypeName, middle); + } + + public static List getTypes(){ + return Arrays.stream(OceanBaseColumnTypeEnum.values()).map(columnTypeEnum -> + columnTypeEnum.getColumnType() + ).toList(); + } + + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseDefaultValueEnum.java b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseDefaultValueEnum.java new file mode 100644 index 000000000..88f1c2b4f --- /dev/null +++ b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseDefaultValueEnum.java @@ -0,0 +1,29 @@ +package ai.chat2db.plugin.oceanbase.type; + +import ai.chat2db.spi.model.DefaultValue; + +import java.util.Arrays; +import java.util.List; + +public enum OceanBaseDefaultValueEnum { + + EMPTY_STRING("EMPTY_STRING"), + NULL("NULL"), + CURRENT_TIMESTAMP("CURRENT_TIMESTAMP"), + ; + private DefaultValue defaultValue; + + OceanBaseDefaultValueEnum(String defaultValue) { + this.defaultValue = new DefaultValue(defaultValue); + } + + + public DefaultValue getDefaultValue() { + return defaultValue; + } + + public static List getDefaultValues() { + return Arrays.stream(OceanBaseDefaultValueEnum.values()).map(OceanBaseDefaultValueEnum::getDefaultValue).collect(java.util.stream.Collectors.toList()); + } + +} diff --git a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseIndexTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseIndexTypeEnum.java index be9268d67..a7c26f0eb 100644 --- a/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseIndexTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-oceanbase/src/main/java/ai/chat2db/plugin/oceanbase/type/OceanBaseIndexTypeEnum.java @@ -1,133 +1,134 @@ -//package ai.chat2db.plugin.oceanbase.type; -// -//import ai.chat2db.spi.enums.EditStatus; -//import ai.chat2db.spi.model.IndexType; -//import ai.chat2db.spi.model.TableIndex; -//import ai.chat2db.spi.model.TableIndexColumn; -//import org.apache.commons.lang3.StringUtils; -// -//import java.util.Arrays; -//import java.util.List; -// -//public enum OceanBaseIndexTypeEnum { -// -// PRIMARY_KEY("Primary", "PRIMARY KEY"), -// -// NORMAL("Normal", "INDEX"), -// -// UNIQUE("Unique", "UNIQUE INDEX"), -// -// FULLTEXT("Fulltext", "FULLTEXT INDEX"), -// -// SPATIAL("Spatial", "SPATIAL INDEX"); -// -// public String getName() { -// return name; -// } -// -// private String name; -// -// -// public String getKeyword() { -// return keyword; -// } -// -// private String keyword; -// -// public IndexType getIndexType() { -// return indexType; -// } -// -// public void setIndexType(IndexType indexType) { -// this.indexType = indexType; -// } -// -// private IndexType indexType; -// -// OceanBaseIndexTypeEnum(String name, String keyword) { -// this.name = name; -// this.keyword = keyword; -// this.indexType = new IndexType(name); -// } -// -// -// public static OceanBaseIndexTypeEnum getByType(String type) { -// for (OceanBaseIndexTypeEnum value : OceanBaseIndexTypeEnum.values()) { -// if (value.name.equalsIgnoreCase(type)) { -// return value; -// } -// } -// return null; -// } -// -// public String buildIndexScript(TableIndex tableIndex) { -// StringBuilder script = new StringBuilder(); -// -// script.append(keyword).append(" "); -// -// script.append(buildIndexName(tableIndex)).append(" "); -// -// script.append(buildIndexColumn(tableIndex)).append(" "); -// -// script.append(buildIndexComment(tableIndex)).append(" "); -// -// return script.toString(); -// } -// -// private String buildIndexComment(TableIndex tableIndex) { -// if(StringUtils.isBlank(tableIndex.getComment())){ -// return ""; -// }else { -// return StringUtils.join("COMMENT '",tableIndex.getComment(),"'"); -// } -// -// } -// -// private String buildIndexColumn(TableIndex tableIndex) { -// StringBuilder script = new StringBuilder(); -// script.append("("); -// for (TableIndexColumn column : tableIndex.getColumnList()) { -// if(StringUtils.isNotBlank(column.getColumnName())) { -// script.append("`").append(column.getColumnName()).append("`"); -// if (!StringUtils.isBlank(column.getAscOrDesc()) && !PRIMARY_KEY.equals(this)) { -// script.append(" ").append(column.getAscOrDesc()); -// } -// script.append(","); -// } -// } -// script.deleteCharAt(script.length() - 1); -// script.append(")"); -// return script.toString(); -// } -// -// private String buildIndexName(TableIndex tableIndex) { -// if(this.equals(PRIMARY_KEY)){ -// return ""; -// }else { -// return "`"+tableIndex.getName()+"`"; -// } -// } -// -// public String buildModifyIndex(TableIndex tableIndex) { -// if (EditStatus.DELETE.name().equals(tableIndex.getEditStatus())) { -// return buildDropIndex(tableIndex); -// } -// if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) { -// return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex)); -// } -// if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) { -// return StringUtils.join("ADD ", buildIndexScript(tableIndex)); -// } -// return ""; -// } -// -// private String buildDropIndex(TableIndex tableIndex) { -// if (OceanBaseIndexTypeEnum.PRIMARY_KEY.getName().equals(tableIndex.getType())) { -// return StringUtils.join("DROP PRIMARY KEY"); -// } -// return StringUtils.join("DROP INDEX `", tableIndex.getOldName(),"`"); -// } -// public static List getIndexTypes() { -// return Arrays.asList(OceanBaseIndexTypeEnum.values()).stream().map(OceanBaseIndexTypeEnum::getIndexType).collect(java.util.stream.Collectors.toList()); -// } -//} +package ai.chat2db.plugin.oceanbase.type; + +import ai.chat2db.spi.enums.EditStatus; +import ai.chat2db.spi.model.IndexType; +import ai.chat2db.spi.model.TableIndex; +import ai.chat2db.spi.model.TableIndexColumn; +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; +import java.util.List; + +public enum OceanBaseIndexTypeEnum { + + PRIMARY_KEY("Primary", "PRIMARY KEY"), + + NORMAL("Normal", "INDEX"), + + UNIQUE("Unique", "UNIQUE INDEX"), + + FULLTEXT("Fulltext", "FULLTEXT INDEX"), + + SPATIAL("Spatial", "SPATIAL INDEX"), + ; + + public String getName() { + return name; + } + + private String name; + + + public String getKeyword() { + return keyword; + } + + private String keyword; + + public IndexType getIndexType() { + return indexType; + } + + public void setIndexType(IndexType indexType) { + this.indexType = indexType; + } + + private IndexType indexType; + + OceanBaseIndexTypeEnum(String name, String keyword) { + this.name = name; + this.keyword = keyword; + this.indexType = new IndexType(name); + } + + + public static OceanBaseIndexTypeEnum getByType(String type) { + for (OceanBaseIndexTypeEnum value : OceanBaseIndexTypeEnum.values()) { + if (value.name.equalsIgnoreCase(type)) { + return value; + } + } + return null; + } + + public String buildIndexScript(TableIndex tableIndex) { + StringBuilder script = new StringBuilder(); + + script.append(keyword).append(" "); + + script.append(buildIndexName(tableIndex)).append(" "); + + script.append(buildIndexColumn(tableIndex)).append(" "); + + script.append(buildIndexComment(tableIndex)).append(" "); + + return script.toString(); + } + + private String buildIndexComment(TableIndex tableIndex) { + if(StringUtils.isBlank(tableIndex.getComment())){ + return ""; + }else { + return StringUtils.join("COMMENT '",tableIndex.getComment(),"'"); + } + + } + + private String buildIndexColumn(TableIndex tableIndex) { + StringBuilder script = new StringBuilder(); + script.append("("); + for (TableIndexColumn column : tableIndex.getColumnList()) { + if(StringUtils.isNotBlank(column.getColumnName())) { + script.append("`").append(column.getColumnName()).append("`"); + if (!StringUtils.isBlank(column.getAscOrDesc()) && !PRIMARY_KEY.equals(this)) { + script.append(" ").append(column.getAscOrDesc()); + } + script.append(","); + } + } + script.deleteCharAt(script.length() - 1); + script.append(")"); + return script.toString(); + } + + private String buildIndexName(TableIndex tableIndex) { + if(this.equals(PRIMARY_KEY)){ + return ""; + }else { + return "`"+tableIndex.getName()+"`"; + } + } + + public String buildModifyIndex(TableIndex tableIndex) { + if (EditStatus.DELETE.name().equals(tableIndex.getEditStatus())) { + return buildDropIndex(tableIndex); + } + if (EditStatus.MODIFY.name().equals(tableIndex.getEditStatus())) { + return StringUtils.join(buildDropIndex(tableIndex),",\n", "ADD ", buildIndexScript(tableIndex)); + } + if (EditStatus.ADD.name().equals(tableIndex.getEditStatus())) { + return StringUtils.join("ADD ", buildIndexScript(tableIndex)); + } + return ""; + } + + private String buildDropIndex(TableIndex tableIndex) { + if (OceanBaseIndexTypeEnum.PRIMARY_KEY.getName().equals(tableIndex.getType())) { + return StringUtils.join("DROP PRIMARY KEY"); + } + return StringUtils.join("DROP INDEX `", tableIndex.getOldName(),"`"); + } + public static List getIndexTypes() { + return Arrays.asList(OceanBaseIndexTypeEnum.values()).stream().map(OceanBaseIndexTypeEnum::getIndexType).collect(java.util.stream.Collectors.toList()); + } +}