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

test: add actionType and refact clinet-base and common Table class #1588

Merged
merged 14 commits into from
Feb 1, 2023
Prev Previous commit
Next Next commit
style: spotless
  • Loading branch information
leechor committed Feb 1, 2023
commit c1b5e6618ef89c8ea68b126b377f973f8a2f8f76
55 changes: 2 additions & 53 deletions dinky-common/src/main/java/org/dinky/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.beans.Transient;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -144,58 +143,8 @@ public String getFlinkDDL(String flinkConfig, String tableName) {

@Transient
public String getFlinkTableSql(String catalogName, String flinkConfig) {
StringBuilder sb = new StringBuilder("DROP TABLE IF EXISTS ");
String fullSchemaName = catalogName + "." + schema + "." + name;
sb.append(name + ";\n");
sb.append("CREATE TABLE IF NOT EXISTS " + name + " (\n");
List<String> pks = new ArrayList<>();
for (int i = 0; i < columns.size(); i++) {
String type = columns.get(i).getFlinkType();
sb.append(" ");
if (i > 0) {
sb.append(",");
}
sb.append("`" + columns.get(i).getName() + "` " + type);
if (Asserts.isNotNullString(columns.get(i).getComment())) {
if (columns.get(i).getComment().contains("\'")
| columns.get(i).getComment().contains("\"")) {
sb.append(
" COMMENT '"
+ columns.get(i).getComment().replaceAll("\"|'", "")
+ "'");
} else {
sb.append(" COMMENT '" + columns.get(i).getComment() + "'");
}
}
sb.append("\n");
if (columns.get(i).isKeyFlag()) {
pks.add(columns.get(i).getName());
}
}
StringBuilder pksb = new StringBuilder("PRIMARY KEY ( ");
for (int i = 0; i < pks.size(); i++) {
if (i > 0) {
pksb.append(",");
}
pksb.append("`" + pks.get(i) + "`");
}
pksb.append(" ) NOT ENFORCED\n");
if (pks.size() > 0) {
sb.append(" ,");
sb.append(pksb);
}
sb.append(")");
if (Asserts.isNotNullString(comment)) {
if (comment.contains("\'") | comment.contains("\"")) {
sb.append(" COMMENT '" + comment.replaceAll("\"|'", "") + "'\n");
} else {
sb.append(" COMMENT '" + comment + "'\n");
}
}
sb.append(" WITH (\n");
sb.append(getFlinkTableWith(flinkConfig));
sb.append("\n);\n");
return sb.toString();
String createSql = getFlinkDDL(getFlinkTableWith(flinkConfig), name);
return String.format("DROP TABLE IF EXISTS %s;\n%s", name, createSql);
}

@Override
Expand Down
20 changes: 18 additions & 2 deletions dinky-common/src/test/java/org/dinky/model/TableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void getFlinkDDL() {
+ " `column3` DOUBLE NOT NULL COMMENT 'comment abc',\n"
+ " PRIMARY KEY ( `column1`,`column2` ) NOT ENFORCED\n"
+ ") WITH (\n"
+ "${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd)\n"));
+ "${schemaName}=schemaName, ${tableName}=tableName, ${abc}=abc, ${}=null, bcd=bcd)"
+ "\n"));
}

@Test
Expand All @@ -88,6 +89,21 @@ void getFlinkTableWith() {
equalTo(
"SchemaOrigin=schemaName, TableNameOrigin=tableName, ${abc}=abc, ${}=null, "
+ "bcd=bcd"));
System.out.println(result);
}

@Test
void getFlinkTableSql() {
String result = table.getFlinkTableSql("CatalogName", flinkConfig);
assertThat(
result,
equalTo(
"DROP TABLE IF EXISTS TableNameOrigin;\n"
+ "CREATE TABLE IF NOT EXISTS TableNameOrigin (\n"
+ " `column1` INT NOT NULL COMMENT 'comment abc',\n"
+ " `column2` STRING COMMENT 'comment abc',\n"
+ " `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"));
}
}