AlterTableQueryGenerator implements QueryGenerator { public Optional<String> getQuery() { List<Optional<String>> queries = new LinkedList<>(); Optional<List<Optional<String>>> columnQuery = this.generateColumnQuery(); if (columnQuery.isPresent()) { queries.addAll(columnQuery.get()); } if (null != this.getNewMeta().getDetailedInfo() && null != this.getNewMeta().getDetailedInfo()) { Optional<String> tablePropertiesQuery = this.generateTablePropertiesQuery(this.getOldMeta().getDetailedInfo().getParameters(), this.getNewMeta().getDetailedInfo().getParameters()); queries.add(tablePropertiesQuery); } List<String> queryList = FluentIterable.from(queries).filter(new Predicate<Optional<String>>() { @Override public boolean apply(Optional<String> input) { return input.isPresent(); } }).transform(new Function<Optional<String>, String>() { @Override public String apply(Optional<String> input) { return input.get(); } }).toList(); if (!queryList.isEmpty()) { return Optional.of(Joiner.on(";\n").join(queryList)); } else { return Optional.absent(); } } AlterTableQueryGenerator(TableMeta oldMeta, TableMeta newMeta); TableMeta getOldMeta(); TableMeta getNewMeta(); String getQueryPerfix(); Optional<String> getQuery(); static List<String> SYSTEM_PROPERTY_LIST; }
@Test public void getQueryWithAlterColumn(){ String origMetaString = "{ " + " \"database\": \"default\", " + " \"table\": \"table2\", " + " \"columns\": [{ " + " \"name\": \"COL1\", " + " \"type\": \"TINYINT\", " + " \"comment\": \"\", " + " \"precision\": null, " + " \"scale\": null " + " }, { " + " \"name\": \"col2\", " + " \"type\": \"VARCHAR\", " + " \"comment\": \"\", " + " \"precision\": \"333\", " + " \"scale\": null " + " }, { " + " \"name\": \"col3\", " + " \"type\": \"DECIMAL\", " + " \"comment\": \"\", " + " \"precision\": \"33\", " + " \"scale\": \"3\" " + " }], " + " \"partitionInfo\": { " + " \"columns\": [] " + " }, " + " \"detailedInfo\": { " + " \"parameters\": {} " + " }, " + " \"storageInfo\": {} " + " }"; String newMetaString = "{ " + " \"database\": \"default\", " + " \"table\": \"table2\", " + " \"columns\": [{ " + " \"name\": \"col1\", " + " \"type\": \"TINYINT\", " + " \"comment\": \"\", " + " \"precision\": null, " + " \"scale\": null " + " }, { " + " \"name\": \"col3\", " + " \"type\": \"STRING\", " + " \"comment\": \"\", " + " \"precision\": \"333\", " + " \"scale\": null " + " }, { " + " \"name\": \"col4\", " + " \"type\": \"TINYINT\", " + " \"comment\": \"\", " + " \"precision\": null, " + " \"scale\": null " + " }], " + " \"partitionInfo\": { " + " \"columns\": [] " + " }, " + " \"detailedInfo\": { " + " \"parameters\": {} " + " }, " + " \"storageInfo\": {} " + " }"; Gson gson = new Gson(); TableMeta origTableMeta = gson.fromJson(origMetaString, TableMeta.class); TableMeta updatedTableMeta = gson.fromJson(newMetaString, TableMeta.class); LOG.info("origTableMeta : {},\n\nupdatedTableMeta : {}", origMetaString, updatedTableMeta); AlterTableQueryGenerator generator = new AlterTableQueryGenerator(origTableMeta, updatedTableMeta); Optional<String> query = generator.getQuery(); Assert.assertTrue(query.isPresent()); String hqlQuery = query.get(); LOG.info("hqlQuery : {}", hqlQuery); String expectedQuery = " ALTER TABLE `default`.`table2` CHANGE COLUMN `col2` `col3` STRING(333);\n" + " ALTER TABLE `default`.`table2` CHANGE COLUMN `col3` `col4` TINYINT"; Assert.assertEquals("Alter Edit table query did not match ", expectedQuery, hqlQuery); }