private void addDataStructureModelsFromSchema(DataStructureSchemaModel schemaModel) { for (DataStructureTableModel tableModel : schemaModel.getTables()) { DATA_STRUCTURE_MODELS.put(tableModel.getName(), tableModel); } for (DataStructureViewModel viewModel : schemaModel.getViews()) { DATA_STRUCTURE_MODELS.put(viewModel.getName(), viewModel); } }
try { if (!dataStructuresCoreService.existsTable(tableModel.getLocation())) { DataStructureTableModel duplicated = dataStructuresCoreService.getTableByName(tableModel.getName()); if (duplicated != null) { throw new SynchronizationException( format("Table [{0}] defined by the model at: [{1}] has already been defined by the model at: [{2}]", tableModel.getName(), tableModel.getLocation(), duplicated.getLocation())); dataStructuresCoreService.createTable(tableModel.getLocation(), tableModel.getName(), tableModel.getHash()); DATA_STRUCTURE_MODELS.put(tableModel.getName(), tableModel); logger.info("Synchronized a new Table [{}] from location: {}", tableModel.getName(), tableModel.getLocation()); } else { DataStructureTableModel existing = dataStructuresCoreService.getTable(tableModel.getLocation()); if (!tableModel.equals(existing)) { dataStructuresCoreService.updateTable(tableModel.getLocation(), tableModel.getName(), tableModel.getHash()); DATA_STRUCTURE_MODELS.put(tableModel.getName(), tableModel); logger.info("Synchronized a modified Table [{}] from location: {}", tableModel.getName(), tableModel.getLocation());
/** * Execute table update. * * @param connection * the connection * @param tableModel * the table model * @throws SQLException * the SQL exception */ public void executeTableUpdate(Connection connection, DataStructureTableModel tableModel) throws SQLException { logger.info("Processing Update Table: " + tableModel.getName()); if (SqlFactory.getNative(connection).exists(connection, tableModel.getName())) { if (SqlFactory.getNative(connection).count(connection, tableModel.getName()) == 0) { executeTableDrop(connection, tableModel); executeTableCreate(connection, tableModel); } else { executeTableAlter(connection, tableModel); } } else { executeTableCreate(connection, tableModel); } }
private static void setTableAttributes(String location, DataStructureSchemaModel result, JsonObject structure, String type, DataStructureTableModel table) { table.setLocation(location); table.setName(structure.get("name").getAsString()); table.setType(type); table.setCreatedAt(result.getCreatedAt()); table.setCreatedBy(result.getCreatedBy()); table.setHash(result.getHash()); JsonElement columnElement = structure.get("columns"); if (columnElement.isJsonObject()) { JsonObject column = columnElement.getAsJsonObject(); DataStructureTableColumnModel columnModel = new DataStructureTableColumnModel(); setColumnAttributes(column, columnModel); table.getColumns().add(columnModel); } else if (columnElement.isJsonArray()) { JsonArray columns = columnElement.getAsJsonArray(); for (int j=0; j<columns.size(); j++) { JsonObject column = columns.get(j).getAsJsonObject(); DataStructureTableColumnModel columnModel = new DataStructureTableColumnModel(); setColumnAttributes(column, columnModel); table.getColumns().add(columnModel); } } else { throw new IllegalArgumentException(format("Error in parsing columns of table [{0}] in schema [{1}]", table.getName(), location)); } }
String tableName = structure.get("table").getAsString(); for (DataStructureTableModel table : result.getTables()) { if (table.getName().equals(tableName)) {
logger.info("Processing Drop Table: " + tableModel.getName()); if (SqlFactory.getNative(connection).exists(connection, tableModel.getName())) { String sql = SqlFactory.getNative(connection).select().column("COUNT(*)").from(tableModel.getName()) .build(); PreparedStatement statement = connection.prepareStatement(sql); logger.error( format("Drop operation for the non empty Table [{0}] will not be executed. Delete all the records in the table first.", tableModel.getName())); return; sql = SqlFactory.getNative(connection).drop().constraint(foreignKeyModel.getName()).fromTable(tableModel.getName()).build(); executeUpdate(connection, sql); sql = SqlFactory.getNative(connection).drop().table(tableModel.getName()).build(); executeUpdate(connection, sql);
dataStructuresCoreService.removeTable(tableModel.getLocation()); executeTableDrop(connection, tableModel); logger.warn("Cleaned up Table [{}] from location: {}", tableModel.getName(), tableModel.getLocation());
logger.info("Processing Create Table: " + tableModel.getName()); CreateTableBuilder createTableBuilder = SqlFactory.getNative(connection).create().table(tableModel.getName()); List<DataStructureTableColumnModel> columns = tableModel.getColumns(); for (DataStructureTableColumnModel columnModel : columns) {