@Override public DataStructureTableModel createTable(String location, String name, String hash) throws DataStructuresException { DataStructureTableModel tableModel = new DataStructureTableModel(); tableModel.setLocation(location); tableModel.setName(name); tableModel.setType(IDataStructureModel.TYPE_TABLE); tableModel.setHash(hash); tableModel.setCreatedBy(UserFacade.getName()); tableModel.setCreatedAt(new Timestamp(new java.util.Date().getTime())); try { Connection connection = null; try { connection = dataSource.getConnection(); tablePersistenceManager.insert(connection, tableModel); return tableModel; } finally { if (connection != null) { connection.close(); } } } catch (SQLException e) { throw new DataStructuresException(e); } }
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)); } }
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()); TABLES_SYNCHRONIZED.add(tableModel.getLocation()); } catch (DataStructuresException e) { throw new SynchronizationException(e);
String type = structure.get("type").getAsString(); if ("table".equalsIgnoreCase(type)) { DataStructureTableModel table = new DataStructureTableModel(); setTableAttributes(location, result, structure, type, table); result.getTables().add(table); String tableName = structure.get("table").getAsString(); for (DataStructureTableModel table : result.getTables()) { if (table.getName().equals(tableName)) { if (table.getConstraints().getForeignKeys() != null ) { list.addAll(Arrays.asList(table.getConstraints().getForeignKeys())); table.getConstraints().setForeignKeys(list.toArray(new DataStructureTableConstraintForeignKeyModel[]{})); table.getDependencies().add(dependencyModel); break;
@Override public void updateTable(String location, String name, String hash) throws DataStructuresException { try { Connection connection = null; try { connection = dataSource.getConnection(); DataStructureTableModel tableModel = getTable(location); tableModel.setName(name); tableModel.setHash(hash); tablePersistenceManager.update(connection, tableModel); } finally { if (connection != null) { connection.close(); } } } catch (SQLException e) { throw new DataStructuresException(e); } }
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) { String name = columnModel.getName(); if (tableModel.getConstraints() != null) { if (tableModel.getConstraints().getPrimaryKey() != null) { createTableBuilder.primaryKey(tableModel.getConstraints().getPrimaryKey().getColumns()); if (tableModel.getConstraints().getForeignKeys() != null) { for (DataStructureTableConstraintForeignKeyModel foreignKey : tableModel.getConstraints().getForeignKeys()) { createTableBuilder.foreignKey(foreignKey.getName(), foreignKey.getColumns(), foreignKey.getReferencedTable(), foreignKey.getReferencedColumns()); if (tableModel.getConstraints().getUniqueIndices() != null) { for (DataStructureTableConstraintUniqueModel uniqueIndex : tableModel.getConstraints().getUniqueIndices()) { createTableBuilder.unique(uniqueIndex.getName(), uniqueIndex.getColumns()); if (tableModel.getConstraints().getChecks() != null) { for (DataStructureTableConstraintCheckModel check : tableModel.getConstraints().getChecks()) { createTableBuilder.check(check.getName(), check.getExpression());
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; if (tableModel.getConstraints().getForeignKeys() != null) { for (DataStructureTableConstraintForeignKeyModel foreignKeyModel : tableModel.getConstraints().getForeignKeys()) { 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);
List<DataStructureTableModel> tableModels = dataStructuresCoreService.getTables(); for (DataStructureTableModel tableModel : tableModels) { if (!TABLES_SYNCHRONIZED.contains(tableModel.getLocation())) { dataStructuresCoreService.removeTable(tableModel.getLocation()); executeTableDrop(connection, tableModel); logger.warn("Cleaned up Table [{}] from location: {}", tableModel.getName(), tableModel.getLocation());
/** * Creates a table model from the raw content. * * @param content * the table definition * @return the table model instance */ public static DataStructureTableModel parseTable(String content) { DataStructureTableModel result = GsonHelper.GSON.fromJson(content, DataStructureTableModel.class); result.setHash(DigestUtils.md5Hex(content)); return result; }
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); } }
/** * Register predelivered table. * * @param tableModelPath * the table model path * @throws IOException * Signals that an I/O exception has occurred. */ public void registerPredeliveredTable(String tableModelPath) throws IOException { String json = loadResourceContent(tableModelPath); DataStructureTableModel tableModel = dataStructuresCoreService.parseTable(json); tableModel.setLocation(tableModelPath); TABLES_PREDELIVERED.put(tableModelPath, tableModel); }
/** * Creates a table model from the raw content. * * @param bytes * the table definition * @return the table model instance */ public static DataStructureTableModel parseTable(byte[] bytes) { DataStructureTableModel result = GsonHelper.GSON.fromJson( new InputStreamReader(new ByteArrayInputStream(bytes), StandardCharsets.UTF_8), DataStructureTableModel.class); result.setHash(DigestUtils.md5Hex(bytes)); return result; }
/** * 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); } }
tableModel.setLocation(registryPath); synchronizeTable(tableModel); return;