Search in sources :

Example 6 with TableId

use of io.debezium.relational.TableId 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 7 with TableId

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

the class MySqlDdlParserTest method shouldParseAlterTableThatChangesMultipleColumns.

@FixFor("DBZ-204")
@Test
public void shouldParseAlterTableThatChangesMultipleColumns() {
    String ddl = "CREATE TABLE `s`.`test` (a INT(11) NULL, b INT NULL, c INT NULL, INDEX i1(b));";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table t = tables.forTable(new TableId(null, "s", "test"));
    assertThat(t).isNotNull();
    assertThat(t.columnNames()).containsExactly("a", "b", "c");
    assertThat(t.primaryKeyColumnNames()).isEmpty();
    assertColumn(t, "a", "INT", Types.INTEGER, 11, -1, true, false, false);
    assertColumn(t, "b", "INT", Types.INTEGER, -1, -1, true, false, false);
    assertColumn(t, "c", "INT", Types.INTEGER, -1, -1, true, false, false);
    ddl = "ALTER TABLE `s`.`test` CHANGE COLUMN `a` `d` BIGINT(20) NOT NULL AUTO_INCREMENT";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    t = tables.forTable(new TableId(null, "s", "test"));
    assertThat(t).isNotNull();
    assertThat(t.columnNames()).containsExactly("d", "b", "c");
    assertThat(t.primaryKeyColumnNames()).isEmpty();
    assertColumn(t, "d", "BIGINT", Types.BIGINT, 20, -1, false, true, true);
    assertColumn(t, "b", "INT", Types.INTEGER, -1, -1, true, false, false);
    assertColumn(t, "c", "INT", Types.INTEGER, -1, -1, true, false, false);
    ddl = "ALTER TABLE `s`.`test` DROP INDEX i1";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test) FixFor(io.debezium.doc.FixFor)

Example 8 with TableId

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

the class MySqlDdlParserTest method shouldParseCreateTableStatementWithSingleGeneratedAndPrimaryKeyColumn.

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

Example 9 with TableId

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

the class MySqlDdlParserTest method shouldParseAlterTableStatementWithColumnNamedColumnWithoutColumnWord.

@Test
@FixFor({ "DBZ-408", "DBZ-412" })
public void shouldParseAlterTableStatementWithColumnNamedColumnWithoutColumnWord() {
    String ddl = "CREATE TABLE `mytable` ( " + System.lineSeparator() + " `def` int(11) unsigned NOT NULL AUTO_INCREMENT, " + System.lineSeparator() + " PRIMARY KEY (`def`) " + System.lineSeparator() + " ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    parser.parse(ddl, tables);
    ddl = "ALTER TABLE `mytable` " + "ADD `column` varchar(255) NOT NULL DEFAULT '', " + "ADD `ghi` varchar(255) NOT NULL DEFAULT '', " + "ADD jkl varchar(255) NOT NULL DEFAULT '' ;";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table mytable = tables.forTable(new TableId(null, null, "mytable"));
    assertThat(mytable).isNotNull();
    assertColumn(mytable, "column", "VARCHAR", Types.VARCHAR, 255, -1, false, false, false);
    assertColumn(mytable, "ghi", "VARCHAR", Types.VARCHAR, 255, -1, false, false, false);
    assertColumn(mytable, "jkl", "VARCHAR", Types.VARCHAR, 255, -1, false, false, false);
    ddl = "ALTER TABLE `mytable` " + "MODIFY `column` varchar(1023) NOT NULL DEFAULT '';";
    parser.parse(ddl, tables);
    ddl = "ALTER TABLE `mytable` " + "ALTER `column` DROP DEFAULT;";
    parser.parse(ddl, tables);
    ddl = "ALTER TABLE `mytable` " + "CHANGE `column` newcol varchar(1023) NOT NULL DEFAULT '';";
    parser.parse(ddl, tables);
    ddl = "ALTER TABLE `mytable` " + "CHANGE newcol `column` varchar(255) NOT NULL DEFAULT '';";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    mytable = tables.forTable(new TableId(null, null, "mytable"));
    assertThat(mytable).isNotNull();
    assertColumn(mytable, "column", "VARCHAR", Types.VARCHAR, 255, -1, false, false, false);
    assertColumn(mytable, "ghi", "VARCHAR", Types.VARCHAR, 255, -1, false, false, false);
    assertColumn(mytable, "jkl", "VARCHAR", Types.VARCHAR, 255, -1, false, false, false);
    ddl = "ALTER TABLE `mytable` " + "DROP `column`, " + "DROP `ghi`, " + "DROP jkl";
    parser.parse(ddl, tables);
    mytable = tables.forTable(new TableId(null, null, "mytable"));
    List<String> mytableColumnNames = mytable.columns().stream().map(Column::name).collect(Collectors.toList());
    assertThat(mytableColumnNames).containsOnly("def");
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test) FixFor(io.debezium.doc.FixFor)

Example 10 with TableId

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

the class MySqlDdlParserTest method shouldParseFulltextKeyInCreateTable.

@FixFor("DBZ-193")
@Test
public void shouldParseFulltextKeyInCreateTable() {
    parser.parse(readFile("ddl/mysql-dbz-193.ddl"), tables);
    Testing.print(tables);
    // 1 table
    assertThat(tables.size()).isEqualTo(1);
    assertThat(listener.total()).isEqualTo(1);
    listener.forEach(this::printEvent);
    Table t = tables.forTable(new TableId(null, null, "roles"));
    assertThat(t).isNotNull();
    assertThat(t.columnNames()).containsExactly("id", "name", "context", "organization_id", "client_id", "scope_action_ids");
    assertThat(t.primaryKeyColumnNames()).containsExactly("id");
    assertColumn(t, "id", "VARCHAR", Types.VARCHAR, 32, -1, false, false, false);
    assertColumn(t, "name", "VARCHAR", Types.VARCHAR, 100, -1, false, false, false);
    assertColumn(t, "context", "VARCHAR", Types.VARCHAR, 20, -1, false, false, false);
    assertColumn(t, "organization_id", "INT", Types.INTEGER, 11, -1, true, false, false);
    assertColumn(t, "client_id", "VARCHAR", Types.VARCHAR, 32, -1, false, false, false);
    assertColumn(t, "scope_action_ids", "TEXT", Types.VARCHAR, -1, -1, false, false, false);
    assertThat(t.columnWithName("id").position()).isEqualTo(1);
    assertThat(t.columnWithName("name").position()).isEqualTo(2);
    assertThat(t.columnWithName("context").position()).isEqualTo(3);
    assertThat(t.columnWithName("organization_id").position()).isEqualTo(4);
    assertThat(t.columnWithName("client_id").position()).isEqualTo(5);
    assertThat(t.columnWithName("scope_action_ids").position()).isEqualTo(6);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test) FixFor(io.debezium.doc.FixFor)

Aggregations

TableId (io.debezium.relational.TableId)63 Table (io.debezium.relational.Table)39 Test (org.junit.Test)34 FixFor (io.debezium.doc.FixFor)18 Column (io.debezium.relational.Column)7 TableEditor (io.debezium.relational.TableEditor)7 ArrayList (java.util.ArrayList)4 ConnectException (org.apache.kafka.connect.errors.ConnectException)4 TableSchema (io.debezium.relational.TableSchema)3 ParsingException (io.debezium.text.ParsingException)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 HashSet (java.util.HashSet)3 List (java.util.List)3 SourceRecord (org.apache.kafka.connect.source.SourceRecord)3 Predicates (io.debezium.function.Predicates)2 ColumnEditor (io.debezium.relational.ColumnEditor)2 Marker (io.debezium.text.TokenStream.Marker)2 Strings (io.debezium.util.Strings)2 Connection (java.sql.Connection)2