Unverified Commit de772eb5 authored by Licho's avatar Licho Committed by GitHub

[FIX]: maybe have empty primary key statement when no primary key (#1767)

* fix: maybe have empty primary sql statement * Spotless Apply --------- Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent e6aeea73
......@@ -129,14 +129,20 @@ public class Table implements Serializable, Comparable<Table>, Cloneable {
})
.collect(Collectors.joining(",\n"));
String primaryKeyStr =
List<String> columnKeys =
columns.stream()
.filter(Column::isKeyFlag)
.map(Column::getName)
.map(t -> String.format("`%s`", t))
.collect(
Collectors.joining(
",", ",\n PRIMARY KEY ( ", " ) NOT ENFORCED\n"));
.collect(Collectors.toList());
String primaryKeyStr =
columnKeys.isEmpty()
? ""
: columnKeys.stream()
.collect(
Collectors.joining(
",", ",\n PRIMARY KEY ( ", " ) NOT ENFORCED\n"));
String result =
MessageFormat.format(
......
......@@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test;
class TableTest {
private Table table;
private Table tableWithoutKey;
private String flinkConfig;
@BeforeEach
......@@ -59,7 +60,29 @@ class TableTest {
.comment("comment \"abc\"")
.build());
List<Column> columnWithoutKey =
Arrays.asList(
Column.builder()
.name("column1")
.type("int")
.javaType(ColumnType.INT)
.comment("comment abc")
.build(),
Column.builder()
.name("column2")
.type("varchar")
.javaType(ColumnType.STRING)
.comment("comment 'abc'")
.build(),
Column.builder()
.name("column3")
.type("double")
.javaType(ColumnType.DOUBLE)
.comment("comment \"abc\"")
.build());
table = new Table("TableNameOrigin", "SchemaOrigin", columns);
tableWithoutKey = new Table("TableNameOrigin", "SchemaOrigin", columnWithoutKey);
flinkConfig =
"${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd";
......@@ -79,6 +102,16 @@ class TableTest {
+ ") WITH (\n"
+ "${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd)"
+ "\n"));
result = tableWithoutKey.getFlinkDDL(flinkConfig, "NewTableNameWithoutKey");
assertThat(
result,
equalTo(
"CREATE TABLE IF NOT EXISTS NewTableNameWithoutKey (\n"
+ " `column1` INT NOT NULL COMMENT 'comment abc',\n"
+ " `column2` STRING COMMENT 'comment abc',\n"
+ " `column3` DOUBLE NOT NULL COMMENT 'comment abc') WITH (\n"
+ "${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd)\n"));
}
@Test
......@@ -104,6 +137,7 @@ class TableTest {
+ " `column3` DOUBLE NOT NULL COMMENT 'comment abc',\n"
+ " PRIMARY KEY ( `column1`,`column2` ) NOT ENFORCED\n"
+ ") WITH (\n"
+ "SchemaOrigin=schemaName, TableNameOrigin=tableName, ${abc}=abc, ${}=null, bcd=bcd)\n"));
+ "SchemaOrigin=schemaName, TableNameOrigin=tableName, ${abc}=abc, ${}=null, bcd=bcd)"
+ "\n"));
}
}
......@@ -132,12 +132,18 @@ public class MySqlDriver extends AbstractJdbcDriver {
})
.collect(Collectors.joining(",\n"));
String primaryKeyStr =
List<String> columnKeys =
table.getColumns().stream()
.filter(Column::isKeyFlag)
.map(Column::getName)
.map(t -> String.format("`%s`", t))
.collect(Collectors.joining(",", ",\n PRIMARY KEY (", ")\n"));
.collect(Collectors.toList());
String primaryKeyStr =
columnKeys.isEmpty()
? ""
: columnKeys.stream()
.collect(Collectors.joining(",", ",\n PRIMARY KEY (", ")\n"));
return MessageFormat.format(
"CREATE TABLE IF NOT EXISTS `{0}`.`{1}` (\n{2}{3})\n ENGINE={4}{5}{6};",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment