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,11 +129,17 @@ public class Table implements Serializable, Comparable<Table>, Cloneable { ...@@ -129,11 +129,17 @@ public class Table implements Serializable, Comparable<Table>, Cloneable {
}) })
.collect(Collectors.joining(",\n")); .collect(Collectors.joining(",\n"));
String primaryKeyStr = List<String> columnKeys =
columns.stream() columns.stream()
.filter(Column::isKeyFlag) .filter(Column::isKeyFlag)
.map(Column::getName) .map(Column::getName)
.map(t -> String.format("`%s`", t)) .map(t -> String.format("`%s`", t))
.collect(Collectors.toList());
String primaryKeyStr =
columnKeys.isEmpty()
? ""
: columnKeys.stream()
.collect( .collect(
Collectors.joining( Collectors.joining(
",", ",\n PRIMARY KEY ( ", " ) NOT ENFORCED\n")); ",", ",\n PRIMARY KEY ( ", " ) NOT ENFORCED\n"));
......
...@@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test; ...@@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test;
class TableTest { class TableTest {
private Table table; private Table table;
private Table tableWithoutKey;
private String flinkConfig; private String flinkConfig;
@BeforeEach @BeforeEach
...@@ -59,7 +60,29 @@ class TableTest { ...@@ -59,7 +60,29 @@ class TableTest {
.comment("comment \"abc\"") .comment("comment \"abc\"")
.build()); .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); table = new Table("TableNameOrigin", "SchemaOrigin", columns);
tableWithoutKey = new Table("TableNameOrigin", "SchemaOrigin", columnWithoutKey);
flinkConfig = flinkConfig =
"${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd"; "${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd";
...@@ -79,6 +102,16 @@ class TableTest { ...@@ -79,6 +102,16 @@ class TableTest {
+ ") WITH (\n" + ") WITH (\n"
+ "${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd)" + "${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd)"
+ "\n")); + "\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 @Test
...@@ -104,6 +137,7 @@ class TableTest { ...@@ -104,6 +137,7 @@ class TableTest {
+ " `column3` DOUBLE NOT NULL COMMENT 'comment abc',\n" + " `column3` DOUBLE NOT NULL COMMENT 'comment abc',\n"
+ " PRIMARY KEY ( `column1`,`column2` ) NOT ENFORCED\n" + " PRIMARY KEY ( `column1`,`column2` ) NOT ENFORCED\n"
+ ") WITH (\n" + ") WITH (\n"
+ "SchemaOrigin=schemaName, TableNameOrigin=tableName, ${abc}=abc, ${}=null, bcd=bcd)\n")); + "SchemaOrigin=schemaName, TableNameOrigin=tableName, ${abc}=abc, ${}=null, bcd=bcd)"
+ "\n"));
} }
} }
...@@ -132,11 +132,17 @@ public class MySqlDriver extends AbstractJdbcDriver { ...@@ -132,11 +132,17 @@ public class MySqlDriver extends AbstractJdbcDriver {
}) })
.collect(Collectors.joining(",\n")); .collect(Collectors.joining(",\n"));
String primaryKeyStr = List<String> columnKeys =
table.getColumns().stream() table.getColumns().stream()
.filter(Column::isKeyFlag) .filter(Column::isKeyFlag)
.map(Column::getName) .map(Column::getName)
.map(t -> String.format("`%s`", t)) .map(t -> String.format("`%s`", t))
.collect(Collectors.toList());
String primaryKeyStr =
columnKeys.isEmpty()
? ""
: columnKeys.stream()
.collect(Collectors.joining(",", ",\n PRIMARY KEY (", ")\n")); .collect(Collectors.joining(",", ",\n PRIMARY KEY (", ")\n"));
return MessageFormat.format( return MessageFormat.format(
......
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