Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shanhexi committed Sep 24, 2023
2 parents d45354a + 254c125 commit 44cf601
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<TableColumn> columns(Connection connection, String databaseName, String schemaName, String tableName) {
String sql = String.format(SELECT_TABLE_COLUMNS, databaseName, tableName);
List<TableColumn> 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';";
Expand All @@ -139,6 +195,7 @@ public Table view(Connection connection, String databaseName, String schemaName,
});
}


@Override
public List<TableIndex> indexes(Connection connection, String databaseName, String schemaName, String tableName) {
StringBuilder queryBuf = new StringBuilder("SHOW INDEX FROM ");
Expand Down Expand Up @@ -174,9 +231,9 @@ public List<TableIndex> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,8 @@ public class TableColumn {


private String editStatus;

private String charSetName;

private String collationName;
}

0 comments on commit 44cf601

Please sign in to comment.