/** * Builds up the CDC event schema for the given table and stores it in this schema. */ protected void buildAndRegisterSchema(Table table) { if (tableFilter.isIncluded(table.id())) { TableSchema schema = schemaBuilder.create(schemaPrefix, getEnvelopeSchemaName(table), table, columnFilter, columnMappers); schemasByTableId.put(table.id(), schema); } }
/** * Obtain an editor for the table with the given ID. This method does not lock or modify the set of table definitions, so use * with caution. The resulting editor can be used to modify the table definition, but when completed the new {@link Table} * needs to be added back to this object via {@link #overwriteTable(Table)}. * * @param tableId the identifier of the table * @return the editor for the table, or null if there is no table with the specified ID */ public TableEditor editOrCreateTable(TableId tableId) { Table table = forTable(tableId); return table == null ? Table.editor().tableId(tableId) : table.edit(); }
/** * Get the columns that are not included in the primary key for this table. * @return the list of columns that are not part of the primary key; never null but possibly empty */ default List<Column> nonPrimaryKeyColumns() { return filterColumns(col->!isPrimaryKeyColumn(col.name())); }
@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); }
public Tables subset(TableFilter filter) { if (filter == null) return this; return lock.read(() -> { Tables result = new Tables(tableIdCaseInsensitive); tablesByTableId.forEach((tableId, table) -> { if (filter.isIncluded(tableId)) { result.overwriteTable(table); } }); return result; }); }
/** * Obtain the definition of the identified table. * * @param catalogName the name of the database catalog that contains the table; may be null if the JDBC driver does not * show a schema for this table * @param schemaName the name of the database schema that contains the table; may be null if the JDBC driver does not * show a schema for this table * @param tableName the name of the table * @return the table definition, or null if there was no definition for the identified table */ public Table forTable(String catalogName, String schemaName, String tableName) { return forTable(new TableId(catalogName, schemaName, tableName)); }
@Override public TableEditor edit() { return new TableEditorImpl().tableId(id) .setColumns(columnDefs) .setPrimaryKeyNames(pkColumnNames) .setDefaultCharsetName(defaultCharsetName); } }
/** * Determine if the values in the named column is optional. * * @param columnName the name of the column * @return {@code true} if a column exists in this table and is optional, or {@code false} otherwise */ default boolean isOptional(String columnName) { Column column = columnWithName(columnName); return column == null ? false : column.isOptional(); }
/** * Get the {@link Table} meta-data for the table with the given identifier, if that table exists and is * included by the filter configuration * * @param id the table identifier; may be null * @return the current table definition, or null if there is no table with the given identifier, if the identifier is null, * or if the table has been excluded by the filters */ public Table tableFor(TableId id) { return tableFilter.isIncluded(id) ? tables.forTable(id) : null; }
@Test public void shouldCreateFilterWithSchemaBlacklistAndNoTableFilter() { filter = Selectors.tableSelector() .excludeSchemas("sc1,sc2") .build(); assertNotAllowed(filter, "db1", "sc1", "A"); assertNotAllowed(filter, "db2", "sc2", "A"); assertAllowed(filter, "db1", "sc3", "A"); assertAllowed(filter, "db2", "sc4", "A"); }
@Test public void shouldCreateFilterWithSchemaWhitelistAndNoTableFilter() { filter = Selectors.tableSelector() .includeSchemas("sc1,sc2") .build(); assertAllowed(filter, "db1", "sc1", "A"); assertAllowed(filter, "db2", "sc2", "A"); assertNotAllowed(filter, "db1", "sc3", "A"); assertNotAllowed(filter, "db2", "sc4", "A"); }
@Test public void shouldCreateFilterWithDatabaseBlacklistAndNoTableFilter() { filter = Selectors.tableSelector() .excludeDatabases("db1,db2") .build(); assertNotAllowed(filter, "db1", "A"); assertNotAllowed(filter, "db2", "A"); assertAllowed(filter, "db3", "A"); assertAllowed(filter, "db4", "A"); }
@Test public void shouldCreateFilterWithDatabaseWhitelistAndNoTableFilter() { filter = Selectors.tableSelector() .includeDatabases("db1,db2") .build(); assertAllowed(filter, "db1", "A"); assertAllowed(filter, "db2", "A"); assertNotAllowed(filter, "db3", "A"); assertNotAllowed(filter, "db4", "A"); }
/** * Determine if the values in the named column is generated by the database. * * @param columnName the name of the column * @return {@code true} if a column exists in this table and its values are generated, or {@code false} otherwise */ default boolean isGenerated(String columnName) { Column column = columnWithName(columnName); return column == null ? false : column.isGenerated(); }
/** * Determine if the named column is auto-incremented. * * @param columnName the name of the column * @return {@code true} if a column exists in this table and it is auto-incremented, or {@code false} otherwise */ default boolean isAutoIncremented(String columnName) { Column column = columnWithName(columnName); return column == null ? false : column.isAutoIncremented(); }
/** * Obtain an editor for the table with the given ID. This method does not lock the set of table definitions, so use * with caution. The resulting editor can be used to modify the table definition, but when completed the new {@link Table} * needs to be added back to this object via {@link #overwriteTable(Table)}. * * @param tableId the identifier of the table * @return the editor for the table, or null if there is no table with the specified ID */ public TableEditor editTable(TableId tableId) { Table table = forTable(tableId); return table == null ? null : table.edit(); }
/** * Create an empty set of definitions. * * @param tableIdCaseInsensitive - true if lookup is case insensitive (typical for MySQL on Windows) */ public Tables(boolean tableIdCaseInsensitive) { this.tableIdCaseInsensitive = tableIdCaseInsensitive; this.tablesByTableId = new TablesById(tableIdCaseInsensitive); this.changes = new TableIds(tableIdCaseInsensitive); }
@Override public ColumnEditorImpl optional(boolean optional) { this.optional = optional; if (optional && !hasDefaultValue()) { // Optional columns have implicit NULL default value defaultValue(null); } return this; }
/** * Get the columns that make up the primary key for this table. * @return the list of columns that make up the primary key; never null but possibly empty */ default List<Column> primaryKeyColumns() { return filterColumns(col->isPrimaryKeyColumn(col.name())); }