protected void updatePositions() { AtomicInteger position = new AtomicInteger(1); sortedColumns.replaceAll((name, defn) -> { // Decrement the position ... int nextPosition = position.getAndIncrement(); if (defn.position() != nextPosition) { return defn.edit().position(nextPosition).create(); } return defn; }); }
public Table getTableSchemaFromChangeTable(ChangeTable changeTable) throws SQLException { final DatabaseMetaData metadata = connection().getMetaData(); final TableId changeTableId = changeTable.getChangeTableId(); List<ColumnEditor> columnEditors = new ArrayList<>(); try (ResultSet rs = metadata.getColumns(realDatabaseName, changeTableId.schema(), changeTableId.table(), null)) { while (rs.next()) { readTableColumn(rs, changeTableId, null).ifPresent(columnEditors::add); } } // The first 5 columns and the last column of the change table are CDC metadata final List<Column> columns = columnEditors.subList(CHANGE_TABLE_DATA_COLUMN_OFFSET, columnEditors.size() - 1).stream() .map(c -> c.position(c.position() - CHANGE_TABLE_DATA_COLUMN_OFFSET).create()) .collect(Collectors.toList()); final List<String> pkColumnNames = new ArrayList<>(); prepareQuery(GET_LIST_OF_KEY_COLUMNS, ps -> ps.setInt(1, changeTable.getChangeTableObjectId()), rs -> { while (rs.next()) { pkColumnNames.add(rs.getString(2)); } }); Collections.sort(columns); return Table.editor() .tableId(changeTable.getSourceTableId()) .addColumns(columns) .setPrimaryKeyNames(pkColumnNames) .create(); }
protected void add(Column defn) { if (defn != null) { Column existing = columnWithName(defn.name()); int position = existing != null ? existing.position() : sortedColumns.size() + 1; sortedColumns.put(defn.name().toLowerCase(), defn.edit().position(position).create()); } assert positionsAreValid(); }
/** * Returns a {@link ColumnEditor} representing the current record of the given result set of column metadata, if * included in the column whitelist. */ protected Optional<ColumnEditor> readTableColumn(ResultSet columnMetadata, TableId tableId, ColumnNameFilter columnFilter) throws SQLException { final String columnName = columnMetadata.getString(4); if (columnFilter == null || columnFilter.matches(tableId.catalog(), tableId.schema(), tableId.table(), columnName)) { final ColumnEditor column = Column.editor().name(columnName); column.jdbcType(columnMetadata.getInt(5)); column.type(columnMetadata.getString(6)); column.length(columnMetadata.getInt(7)); if (columnMetadata.getObject(9) != null) { column.scale(columnMetadata.getInt(9)); } column.optional(isNullable(columnMetadata.getInt(11))); column.position(columnMetadata.getInt(17)); column.autoIncremented("YES".equalsIgnoreCase(columnMetadata.getString(23))); String autogenerated = null; try { autogenerated = columnMetadata.getString(24); } catch (SQLException e) { // ignore, some drivers don't have this index - e.g. Postgres } column.generated("YES".equalsIgnoreCase(autogenerated)); column.nativeType(resolveNativeType(column.typeName())); return Optional.of(column); } return Optional.empty(); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowAddingPrimaryKeyColumnWhenNotFound() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).position(1).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).position(1).create(); editor.addColumns(c1, c2, c3); editor.setPrimaryKeyNames("C1", "WOOPS"); }
@Before public void beforeEach() { column = Column.editor().name("firstName").jdbcType(Types.VARCHAR).type("VARCHAR").position(1).create(); column2 = Column.editor().name("lastName").jdbcType(Types.VARCHAR).type("VARCHAR").position(2).create(); column3 = Column.editor().name("otherColumn").jdbcType(Types.VARCHAR).type("VARCHAR").position(3).create(); fullyQualifiedNames = tableId + "." + column.name() + "," + tableId + "." + column3.name() + ","; }
columnEditor.position(v.getInteger("position")) .optional(v.getBoolean("optional")) .autoIncremented(v.getBoolean("autoIncremented"))
@Test public void shouldFindNonExistingColumnByNameIndependentOfCase() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).position(1).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).position(1).create(); editor.addColumns(c1, c2, c3); editor.columns().forEach(col -> { assertThat(editor.columnWithName(col.name())).isNotNull(); assertThat(editor.columnWithName(col.name().toUpperCase())).isNotNull(); assertThat(editor.columnWithName(col.name().toLowerCase())).isNotNull(); }); assertThat(editor.columnWithName("WOOPS")).isNull(); }
protected void createColumnWithAllFieldsSetToNonDefaults() { column = editor.name("price") .type("NUMBER") .jdbcType(Types.DOUBLE) .length(5) .scale(2) .position(4) .optional(true) .autoIncremented(true) .generated(true) .create(); }
@Override public ColumnEditor edit() { final ColumnEditor editor = Column.editor() .name(name()) .type(typeName(), typeExpression()) .jdbcType(jdbcType()) .nativeType(nativeType) .charsetName(charsetName) .length(length()) .scale(scale().orElse(null)) .position(position()) .optional(isOptional()) .autoIncremented(isAutoIncremented()) .generated(isGenerated()); if (hasDefaultValue()) { editor.defaultValue(defaultValue()); } return editor; } }
@Test public void shouldAllowAddingPrimaryKeyColumnWhenFound() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).position(1).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).position(1).create(); editor.addColumns(c1, c2, c3); editor.setPrimaryKeyNames("C1"); c1 = editor.columnWithName(c1.name()); c2 = editor.columnWithName(c2.name()); c3 = editor.columnWithName(c3.name()); assertThat(c1.position()).isEqualTo(1); assertThat(c2.position()).isEqualTo(2); assertThat(c3.position()).isEqualTo(3); table = editor.create(); assertThat(table.retrieveColumnNames()).containsExactly("C1", "C2", "C3"); assertThat(table.columns()).containsExactly(c1, c2, c3); assertThat(table.primaryKeyColumnNames()).containsOnly("C1"); assertValidPositions(editor); }
@Test(expected = IllegalArgumentException.class) public void shouldNotReorderColumnIfNameDoesNotMatch() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).autoIncremented(true).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).autoIncremented(true).create(); editor.addColumns(c1, c2, c3); editor.reorderColumn("WOOPS", "C2"); assertValidPositions(editor); }
@Test public void shouldFindGeneratedColumns() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).generated(true).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).generated(true).create(); editor.addColumns(c1, c2, c3); editor.setPrimaryKeyNames("C1"); table = editor.create(); assertThat(table.retrieveColumnNames()).containsExactly("C1", "C2", "C3"); table.columns().forEach(col -> { assertThat(table.isGenerated(col.name())).isEqualTo(col.isGenerated()); }); assertValidPositions(editor); }
@Test public void shouldFindAutoIncrementedColumns() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).autoIncremented(true).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).autoIncremented(true).create(); editor.addColumns(c1, c2, c3); editor.setPrimaryKeyNames("C1"); table = editor.create(); assertThat(table.retrieveColumnNames()).containsExactly("C1", "C2", "C3"); table.columns().forEach(col -> { assertThat(table.isAutoIncremented(col.name())).isEqualTo(col.isAutoIncremented()); }); assertValidPositions(editor); }
@Test public void shouldRemoveColumnByName() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).autoIncremented(true).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).autoIncremented(true).create(); editor.addColumns(c1, c2, c3); editor.removeColumn("C2"); assertThat(editor.columns()).containsExactly(editor.columnWithName("C1"), editor.columnWithName("C3")); assertValidPositions(editor); }
@Test public void shouldReorderColumns() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).autoIncremented(true).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).autoIncremented(true).create();
protected void updatePositions() { AtomicInteger position = new AtomicInteger(1); sortedColumns.replaceAll((name, defn) -> { // Decrement the position ... int nextPosition = position.getAndIncrement(); if (defn.position() != nextPosition) { return defn.edit().position(nextPosition).create(); } return defn; }); }
protected void add(Column defn) { if (defn != null) { Column existing = columnWithName(defn.name()); int position = existing != null ? existing.position() : sortedColumns.size() + 1; sortedColumns.put(defn.name().toLowerCase(), defn.edit().position(position).create()); } assert positionsAreValid(); }
/** * Returns a {@link ColumnEditor} representing the current record of the given result set of column metadata, if * included in the column whitelist. */ protected Optional<ColumnEditor> readTableColumn(ResultSet columnMetadata, TableId tableId, ColumnNameFilter columnFilter) throws SQLException { final String columnName = columnMetadata.getString(4); if (columnFilter == null || columnFilter.matches(tableId.catalog(), tableId.schema(), tableId.table(), columnName)) { final ColumnEditor column = Column.editor().name(columnName); column.jdbcType(columnMetadata.getInt(5)); column.type(columnMetadata.getString(6)); column.length(columnMetadata.getInt(7)); if (columnMetadata.getObject(9) != null) { column.scale(columnMetadata.getInt(9)); } column.optional(isNullable(columnMetadata.getInt(11))); column.position(columnMetadata.getInt(17)); column.autoIncremented("YES".equalsIgnoreCase(columnMetadata.getString(23))); String autogenerated = null; try { autogenerated = columnMetadata.getString(24); } catch (SQLException e) { // ignore, some drivers don't have this index - e.g. Postgres } column.generated("YES".equalsIgnoreCase(autogenerated)); column.nativeType(resolveNativeType(column.typeName())); return Optional.of(column); } return Optional.empty(); }
@Override public ColumnEditor edit() { final ColumnEditor editor = Column.editor() .name(name()) .type(typeName(), typeExpression()) .jdbcType(jdbcType()) .nativeType(nativeType) .charsetName(charsetName) .length(length()) .scale(scale().orElse(null)) .position(position()) .optional(isOptional()) .autoIncremented(isAutoIncremented()) .generated(isGenerated()); if (hasDefaultValue()) { editor.defaultValue(defaultValue()); } return editor; } }