/** * Sets up the table without loading the data in it. * * @return {@code true} if CSV data should be loaded */ public boolean setupTable() { log.debug(String.format("setting up table '%s', policy='%s'", tableName, policy)); if (policy.equals(CREATE_TABLE_POLICY_NEVER)) { log.debug("policy='" + CREATE_TABLE_POLICY_NEVER + "', skipping setup"); return false; } synchronized (DIRECTORY_INIT_LOCK) { boolean tableExists = tableExists(); // check the field names match the column names if (policy.equals(CREATE_TABLE_POLICY_ON_MISSING_COLUMNS) && tableExists) { if (hasMatchingColumns()) { // all required columns were found log.debug("policy='" + CREATE_TABLE_POLICY_ON_MISSING_COLUMNS + "' and all column matched, skipping data load"); } else { log.debug("policy='" + CREATE_TABLE_POLICY_ON_MISSING_COLUMNS + "' and some columns are missing"); addMissingColumns(); } return false; } // else policy=always or table doesn't exist createTable(tableExists); return true; // load data } }
public boolean hasMatchingColumns() { Set<Column> missingColumns = getMissingColumns(true); if (missingColumns == null || missingColumns.size() > 0) { return false; } else { // all fields have a matching column, this looks not that bad log.debug(String.format("all fields matched for table '%s'", tableName)); return true; } }
Dialect dialect = directory.getDialect(); boolean nativeCase = directory.useNativeCase(); table = SQLHelper.addTable(tableName, dialect, nativeCase); SQLHelper.addColumn(table, sourceColumn, ColumnType.STRING, nativeCase); SQLHelper.addColumn(table, targetColumn, ColumnType.STRING, nativeCase); SQLHelper helper = new SQLHelper(connection, table, directory.getDescriptor().getCreateTablePolicy()); boolean loadData = helper.setupTable(); if (loadData && dataFileName != null) {
public Set<Column> getMissingColumns(Boolean breakAtFirstMissing) { try { Set<Column> missingColumns = new HashSet<>(); // Test whether there are new fields added in the schema that are // not present in the table schema. If so it is advised to // reinitialise the database. Set<String> columnNames = getPhysicalColumns(); // check the field names match the column names (case-insensitive) for (Column column : table.getColumns()) { // TODO: check types as well String fieldName = column.getPhysicalName(); if (!columnNames.contains(fieldName)) { log.debug(String.format("required field: %s is missing", fieldName)); missingColumns.add(column); if (breakAtFirstMissing) { return null; } } } return missingColumns; } catch (SQLException e) { log.warn("error while introspecting table: " + tableName, e); return null; } }
table = SQLHelper.addTable(tableName, dialect, useNativeCase()); SchemaManager schemaManager = Framework.getService(SchemaManager.class); schema = schemaManager.getSchema(getSchema()); type = ColumnType.AUTOINC; Column column = SQLHelper.addColumn(table, fieldName, type, useNativeCase()); if (isId) { if (descriptor.isAutoincrementIdField()) { SQLHelper helper = new SQLHelper(sqlConnection, table, descriptor.getCreateTablePolicy()); boolean loadData = helper.setupTable();
private void addMissingColumns() { try (Statement stmt = connection.createStatement()) { for (Column column : getMissingColumns(false)) { String alter = table.getAddColumnSql(column); if (logger.isLogEnabled()) { logger.log(alter); } stmt.execute(alter); } } catch (SQLException e) { throw new DirectoryException(String.format("Table '%s' alteration failed: %s", table, e.getMessage()), e); } }