Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mysql buildCreateTableSql error #1147

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;


public class MysqlSqlBuilder extends DefaultSqlBuilder implements SqlBuilder {
Expand All @@ -26,53 +27,37 @@ public String buildCreateTableSql(Table table) {
}
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;
}
MysqlColumnTypeEnum typeEnum = MysqlColumnTypeEnum.getByType(column.getColumnType());
script.append("\t").append(typeEnum.buildCreateColumnSql(column)).append(",\n");
// Append columns
String columnsScript = table.getColumnList().stream()
.filter(column -> StringUtils.isNotBlank(column.getName()) && StringUtils.isNotBlank(column.getColumnType()))
.map(column -> "\t" + MysqlColumnTypeEnum.getByType(column.getColumnType()).buildCreateColumnSql(column))
.collect(Collectors.joining(",\n"));
if (StringUtils.isNotBlank(columnsScript)) {
script.append(columnsScript);
}

// append primary key and index
for (TableIndex tableIndex : table.getIndexList()) {
if (StringUtils.isBlank(tableIndex.getName()) || StringUtils.isBlank(tableIndex.getType())) {
continue;
}
MysqlIndexTypeEnum mysqlIndexTypeEnum = MysqlIndexTypeEnum.getByType(tableIndex.getType());
script.append("\t").append("").append(mysqlIndexTypeEnum.buildIndexScript(tableIndex)).append(",\n");
// Append primary key and index
String indexesScript = table.getIndexList().stream()
.filter(index -> StringUtils.isNotBlank(index.getName()) && StringUtils.isNotBlank(index.getType()))
.map(index -> "\t" + MysqlIndexTypeEnum.getByType(index.getType()).buildIndexScript(index))
.collect(Collectors.joining(",\n"));

if (StringUtils.isNotBlank(indexesScript)) {
script.append(indexesScript);
// Remove trailing comma and newline
script.setLength(script.length() - 2);
}

script = new StringBuilder(script.substring(0, script.length() - 2));
script.append("\n)");

// Append table options
script.append(StringUtils.isNotBlank(table.getEngine()) ? " ENGINE=" + table.getEngine() : "");
script.append(StringUtils.isNotBlank(table.getCharset()) ? " DEFAULT CHARACTER SET=" + table.getCharset() : "");
script.append(StringUtils.isNotBlank(table.getCollate()) ? " COLLATE=" + table.getCollate() : "");
script.append(table.getIncrementValue() != null ? " AUTO_INCREMENT=" + table.getIncrementValue() : "");
script.append(StringUtils.isNotBlank(table.getComment()) ? " COMMENT='" + table.getComment() + "'" : "");
script.append(StringUtils.isNotBlank(table.getPartition()) ? " \n" + table.getPartition() : "");

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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private long addDBCache(Long dataSourceId, String databaseName, String schemaNam
Connection connection = Chat2DBContext.getConnection();
long n = 0;
try (ResultSet resultSet = connection.getMetaData().getTables(databaseName, schemaName, null,
new String[]{"TABLE", "SYSTEM TABLE"})) {
new String[]{"TABLE", "SYSTEM TABLE","PARTITIONED TABLE"})) {
List<TableCacheDO> cacheDOS = new ArrayList<>();
while (resultSet.next()) {
TableCacheDO tableCacheDO = new TableCacheDO();
Expand Down