Search in sources :

Example 1 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class DdlParser method parseAliasedTableInFrom.

/**
 * Parse a potentially qualified table name along with an optional alias.
 *
 * @param start the start of the statement
 * @param tablesByAlias the map to which this method should add the table keyed by its alias (or name if there is no alias);
 *            may not be null
 */
private void parseAliasedTableInFrom(Marker start, Map<String, Table> tablesByAlias) {
    Table fromTable = databaseTables.forTable(parseQualifiedTableName(start));
    // Aliases in JOIN clauses don't have to be preceded by AS, but can simply be the alias followed by the 'ON' clause
    if (tokens.matches("AS", TokenStream.ANY_VALUE, "ON") || tokens.matches(TokenStream.ANY_VALUE, "ON")) {
        tokens.canConsume("AS");
        String alias = tokens.consume();
        if (fromTable != null) {
            tablesByAlias.put(alias, fromTable);
            return;
        }
    }
    if (fromTable != null)
        tablesByAlias.put(fromTable.id().table(), fromTable);
}
Also used : Table(io.debezium.relational.Table)

Example 2 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class DdlParser method parseColumnsInSelectClause.

/**
 * Parse the column information in the SELECT clause. This statement stops before consuming the FROM clause.
 *
 * @param start the start of the statement
 * @return the map of resolved Columns keyed by the column alias (or name) used in the SELECT statement; never null but
 *         possibly
 *         empty if we couldn't parse the SELECT clause correctly
 */
protected Map<String, Column> parseColumnsInSelectClause(Marker start) {
    // Parse the column names ...
    Map<String, String> tableAliasByColumnAlias = new LinkedHashMap<>();
    Map<String, String> columnNameByAliases = new LinkedHashMap<>();
    parseColumnName(start, tableAliasByColumnAlias, columnNameByAliases);
    while (tokens.canConsume(',')) {
        parseColumnName(start, tableAliasByColumnAlias, columnNameByAliases);
    }
    // Parse the FROM clause, but we'll back up to the start of this before we return ...
    Marker startOfFrom = tokens.mark();
    Map<String, Column> columnsByName = new LinkedHashMap<>();
    Map<String, Table> fromTablesByAlias = parseSelectFromClause(start);
    Table singleTable = fromTablesByAlias.size() == 1 ? fromTablesByAlias.values().stream().findFirst().get() : null;
    tableAliasByColumnAlias.forEach((columnAlias, tableAlias) -> {
        // Resolve the alias into the actual column name in the referenced table ...
        String columnName = columnNameByAliases.getOrDefault(columnAlias, columnAlias);
        Column column = null;
        if (tableAlias == null) {
            // The column was not qualified with a table, so there should be a single table ...
            column = singleTable == null ? null : singleTable.columnWithName(columnName);
        } else {
            // The column was qualified with a table, so look it up ...
            Table table = fromTablesByAlias.get(tableAlias);
            column = table == null ? null : table.columnWithName(columnName);
        }
        if (column == null) {
            // Check to see whether the column name contains a constant value, in which case we need to create an
            // artificial column ...
            column = createColumnFromConstant(columnAlias, columnName);
        }
        // column may be null
        columnsByName.put(columnAlias, column);
    });
    tokens.rewind(startOfFrom);
    return columnsByName;
}
Also used : Table(io.debezium.relational.Table) Column(io.debezium.relational.Column) Marker(io.debezium.text.TokenStream.Marker) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class DdlParserSql2003Test method shouldParseCreateTableStatementWithSingleGeneratedColumnAsPrimaryKey.

@Test
public void shouldParseCreateTableStatementWithSingleGeneratedColumnAsPrimaryKey() {
    String ddl = "CREATE TABLE my.foo ( " + System.lineSeparator() + " c1 INTEGER GENERATED ALWAYS AS IDENTITY NOT NULL, " + System.lineSeparator() + " c2 VARCHAR(22), " + System.lineSeparator() + " PRIMARY KEY (c1)" + System.lineSeparator() + "); " + System.lineSeparator();
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table foo = tables.forTable(new TableId("my", null, "foo"));
    assertThat(foo).isNotNull();
    assertThat(foo.columnNames()).containsExactly("c1", "c2");
    assertThat(foo.primaryKeyColumnNames()).containsExactly("c1");
    assertColumn(foo, "c1", "INTEGER", Types.INTEGER, -1, -1, false, true, true);
    assertColumn(foo, "c2", "VARCHAR", Types.VARCHAR, 22, -1, true, false, false);
    parser.parse("DROP TABLE my.foo", tables);
    assertThat(tables.size()).isEqualTo(0);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test)

Example 4 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class ColumnMappers method mapperFor.

/**
 * Get the value mapping function for the given column.
 *
 * @param tableId the identifier of the table to which the column belongs; may not be null
 * @param column the column; may not be null
 * @return the mapping function, or null if there is no mapping function
 */
public ColumnMapper mapperFor(TableId tableId, Column column) {
    ColumnId id = new ColumnId(tableId, column.name());
    Optional<MapperRule> matchingRule = rules.stream().filter(rule -> rule.matches(id)).findFirst();
    if (matchingRule.isPresent()) {
        return matchingRule.get().mapper;
    }
    return null;
}
Also used : ColumnId(io.debezium.relational.ColumnId) Predicates(io.debezium.function.Predicates) Predicate(java.util.function.Predicate) Configuration(io.debezium.config.Configuration) TableId(io.debezium.relational.TableId) ArrayList(java.util.ArrayList) List(java.util.List) Immutable(io.debezium.annotation.Immutable) Selectors(io.debezium.relational.Selectors) Column(io.debezium.relational.Column) ConnectException(org.apache.kafka.connect.errors.ConnectException) ValueConverter(io.debezium.relational.ValueConverter) ColumnId(io.debezium.relational.ColumnId) Optional(java.util.Optional) Table(io.debezium.relational.Table) Strings(io.debezium.util.Strings)

Example 5 with Table

use of io.debezium.relational.Table in project debezium by debezium.

the class MySqlSchemaTest method assertHistoryRecorded.

protected void assertHistoryRecorded() {
    MySqlSchema duplicate = build.storeDatabaseHistoryInFile(TEST_FILE_PATH).createSchemas();
    duplicate.loadHistory(source);
    // Make sure table is defined in each ...
    assertThat(duplicate.tables()).isEqualTo(mysql.tables());
    for (int i = 0; i != 2; ++i) {
        duplicate.tables().tableIds().forEach(tableId -> {
            TableSchema dupSchema = duplicate.schemaFor(tableId);
            TableSchema schema = mysql.schemaFor(tableId);
            assertThat(schema).isEqualTo(dupSchema);
            Table dupTable = duplicate.tables().forTable(tableId);
            Table table = mysql.tables().forTable(tableId);
            assertThat(table).isEqualTo(dupTable);
        });
        mysql.tables().tableIds().forEach(tableId -> {
            TableSchema dupSchema = duplicate.schemaFor(tableId);
            TableSchema schema = mysql.schemaFor(tableId);
            assertThat(schema).isEqualTo(dupSchema);
            Table dupTable = duplicate.tables().forTable(tableId);
            Table table = mysql.tables().forTable(tableId);
            assertThat(table).isEqualTo(dupTable);
        });
        duplicate.refreshSchemas();
    }
}
Also used : Table(io.debezium.relational.Table) TableSchema(io.debezium.relational.TableSchema)

Aggregations

Table (io.debezium.relational.Table)47 TableId (io.debezium.relational.TableId)39 Test (org.junit.Test)34 FixFor (io.debezium.doc.FixFor)17 Column (io.debezium.relational.Column)6 TableSchema (io.debezium.relational.TableSchema)4 TableEditor (io.debezium.relational.TableEditor)3 ArrayList (java.util.ArrayList)3 Predicates (io.debezium.function.Predicates)2 ParsingException (io.debezium.text.ParsingException)2 Marker (io.debezium.text.TokenStream.Marker)2 Strings (io.debezium.util.Strings)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ConnectException (org.apache.kafka.connect.errors.ConnectException)2 Immutable (io.debezium.annotation.Immutable)1 Configuration (io.debezium.config.Configuration)1 RecordsForTable (io.debezium.connector.mysql.RecordMakers.RecordsForTable)1 PostgresConnection (io.debezium.connector.postgresql.connection.PostgresConnection)1 BufferedBlockingConsumer (io.debezium.function.BufferedBlockingConsumer)1