ShowCreateTableCalculator { public static String calculate(boolean showCreateIndex, String tableName, String tableSpace, AbstractTableManager tableManager) { Table t = tableManager.getTable(); if (t == null) { throw new TableDoesNotExistException(String.format("Table %s does not exist.", tableName)); } StringBuilder sb = new StringBuilder("CREATE TABLE " + tableSpace + "." + tableName); StringJoiner joiner = new StringJoiner(",", "(", ")"); for (Column c : t.getColumns()) { joiner.add(c.name + " " + ColumnTypes.typeToString(c.type) + autoIncrementColumn(t, c) + defaultClause(c)); } if (t.getPrimaryKey().length > 0) { joiner.add("PRIMARY KEY(" + String.join(",", t.getPrimaryKey()) + ")"); } if (showCreateIndex) { List<Index> indexes = tableManager.getAvailableIndexes(); if (!indexes.isEmpty()) { indexes.forEach(idx -> { if (idx.unique) { joiner.add("UNIQUE KEY " + idx.name + " (" + String.join(",", idx.columnNames) + ")"); } else { joiner.add("INDEX " + idx.name + "(" + String.join(",", idx.columnNames) + ")"); } }); } } sb.append(joiner.toString()); return sb.toString(); }  static String calculate(boolean showCreateIndex, String tableName, String tableSpace, AbstractTableManager tableManager); static TranslatedQuery calculateShowCreateTable(String query, String defaultTablespace, List<Object> parameters, DBManager manager);  }
@Test public void test() { Table t = Table.builder() .uuid("1234") .column("k1", ColumnTypes.INTEGER) .column("s1", ColumnTypes.STRING) .primaryKey("k1") .tablespace("ts1") .name("test3") .build(); AbstractTableManager tm = new MockTableManager(t, new ArrayList<>()); assertTrue(ShowCreateTableCalculator.calculate(false, "test3", "ts1", tm).equals("CREATE TABLE ts1.test3(k1 integer,s1 string,PRIMARY KEY(k1))")); t = Table.builder() .uuid("1234") .column("k1", ColumnTypes.INTEGER) .column("s1", ColumnTypes.STRING) .column("s2", ColumnTypes.NOTNULL_STRING) .primaryKey("k1", true) .primaryKey("s2") .tablespace("ts1") .name("test3") .build(); tm = new MockTableManager(t, new ArrayList<>()); assertTrue(ShowCreateTableCalculator.calculate(false, "test3", "ts1", tm).equals("CREATE TABLE ts1.test3(k1 integer auto_increment,s1 string,s2 string not null,PRIMARY KEY(k1,s2))")); t = Table.builder() .uuid("1234") .column("k1", ColumnTypes.INTEGER) .column("s1", ColumnTypes.NOTNULL_STRING) .column("l1", ColumnTypes.NOTNULL_LONG) .column("i1", ColumnTypes.NOTNULL_INTEGER) .primaryKey("k1", true) .tablespace("ts1") .name("test3") .build(); tm = new MockTableManager(t, new ArrayList<>()); assertTrue(ShowCreateTableCalculator.calculate(false, "test3", "ts1", tm).equals("CREATE TABLE ts1.test3(k1 integer auto_increment,s1 string not null,l1 long not null,i1 integer not null,PRIMARY KEY(k1))")); t = Table.builder() .uuid("1234") .column("ID", ColumnTypes.INTEGER) .column("s1", ColumnTypes.NOTNULL_STRING) .column("l1", ColumnTypes.NOTNULL_LONG) .column("i1", ColumnTypes.NOTNULL_INTEGER) .primaryKey("ID", true) .tablespace("ts1") .name("test4") .build(); tm = new MockTableManager(t, new ArrayList<>()); assertTrue(ShowCreateTableCalculator.calculate(false, "test4", "ts1", tm).equals("CREATE TABLE ts1.test4(id integer auto_increment,s1 string not null,l1 long not null,i1 integer not null,PRIMARY KEY(id))")); }