From 4742c208b0a054430906b049c1e2d83a5c2b4992 Mon Sep 17 00:00:00 2001 From: SwallowGG <1558143046@qq.com> Date: Sun, 24 Sep 2023 12:30:51 +0800 Subject: [PATCH 1/2] update --- .../chat2db/plugin/mysql/MysqlMetaData.java | 61 ++++++++++++++++++- .../mysql/type/MysqlColumnTypeEnum.java | 2 +- .../ai/chat2db/spi/model/TableColumn.java | 4 ++ 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java index 8d88888b0..b1fb55d01 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java +++ b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/MysqlMetaData.java @@ -17,6 +17,7 @@ import ai.chat2db.spi.model.*; import ai.chat2db.spi.sql.SQLExecutor; import jakarta.validation.constraints.NotEmpty; +import org.apache.commons.lang3.StringUtils; public class MysqlMetaData extends DefaultMetaService implements MetaData { @Override @@ -120,6 +121,61 @@ public Procedure procedure(Connection connection, @NotEmpty String databaseName, }); } + 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) { + 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; + }); + } + + private void setColumnSize(TableColumn column,String columnType){ + try { + if (columnType.contains("(")) { + String size = columnType.substring(columnType.indexOf("(") + 1, columnType.indexOf(")")); + 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){ + + } + } + + + 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';"; @@ -139,6 +195,7 @@ public Table view(Connection connection, String databaseName, String schemaName, }); } + @Override public List indexes(Connection connection, String databaseName, String schemaName, String tableName) { StringBuilder queryBuf = new StringBuilder("SHOW INDEX FROM "); @@ -174,9 +231,9 @@ public List indexes(Connection connection, String databaseName, Stri index.setType(MysqlIndexTypeEnum.UNIQUE.getName()); } else if ("SPATIAL".equalsIgnoreCase(index.getType())) { index.setType(MysqlIndexTypeEnum.SPATIAL.getName()); - }else if("FULLTEXT".equalsIgnoreCase(index.getType())){ + } else if ("FULLTEXT".equalsIgnoreCase(index.getType())) { index.setType(MysqlIndexTypeEnum.FULLTEXT.getName()); - }else { + } else { index.setType(MysqlIndexTypeEnum.NORMAL.getName()); } map.put(keyName, index); diff --git a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java index b153fd2ef..e9f15e53c 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java @@ -231,7 +231,7 @@ private String buildNullable(TableColumn column,MysqlColumnTypeEnum type) { if(!type.getColumnType().isSupportNullable()){ return ""; } - if (1==column.getNullable()) { + if (column.getNullable()!=null && 1==column.getNullable()) { return "NULL"; } else { return "NOT NULL"; diff --git a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/TableColumn.java b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/TableColumn.java index bf07225ee..740dfa8c9 100644 --- a/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/TableColumn.java +++ b/chat2db-server/chat2db-spi/src/main/java/ai/chat2db/spi/model/TableColumn.java @@ -161,4 +161,8 @@ public class TableColumn { private String editStatus; + + private String charSetName; + + private String collationName; } From 254c1259c14ba2e4fd943290783b25a2c37eca02 Mon Sep 17 00:00:00 2001 From: SwallowGG <1558143046@qq.com> Date: Sun, 24 Sep 2023 14:38:04 +0800 Subject: [PATCH 2/2] update --- .../ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java | 4 ++-- .../java/ai/chat2db/plugin/mysql/type/MysqlIndexTypeEnum.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java index e9f15e53c..eb7d16259 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlColumnTypeEnum.java @@ -56,9 +56,9 @@ public enum MysqlColumnTypeEnum implements ColumnBuilder { TIMESTAMP("TIMESTAMP", true, false, true, false, false, false, true, true, true), TIME("TIME", true, false, true, false, false, false, true, true, false), YEAR("YEAR", false, false, true, false, false, false, true, true, false), - CHAR("CHAR", true, false, true, false, false, true, true, true, false), + CHAR("CHAR", true, false, true, false, true, true, true, true, false), - VARCHAR("VARCHAR", true, false, true, false, false, true, true, true, false), + VARCHAR("VARCHAR", true, false, true, false, true, true, true, true, false), BINARY("BINARY", true, false, true, false, false, false, true, true, false), diff --git a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlIndexTypeEnum.java b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlIndexTypeEnum.java index 530786e46..c8674010d 100644 --- a/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlIndexTypeEnum.java +++ b/chat2db-server/chat2db-plugins/chat2db-mysql/src/main/java/ai/chat2db/plugin/mysql/type/MysqlIndexTypeEnum.java @@ -38,7 +38,7 @@ public String getKeyword() { public static MysqlIndexTypeEnum getByType(String type) { for (MysqlIndexTypeEnum value : MysqlIndexTypeEnum.values()) { - if (value.name.equals(type)) { + if (value.name.equalsIgnoreCase(type)) { return value; } }