Search in sources :

Example 21 with Table

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

the class MySqlDdlParserTest method shouldParseAlterTableStatementThatAddsCharacterSetForColumns.

@Test
public void shouldParseAlterTableStatementThatAddsCharacterSetForColumns() {
    String ddl = "CREATE TABLE t ( col1 VARCHAR(25) ); ";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table t = tables.forTable(new TableId(null, null, "t"));
    assertThat(t).isNotNull();
    assertThat(t.columnNames()).containsExactly("col1");
    assertThat(t.primaryKeyColumnNames()).isEmpty();
    assertColumn(t, "col1", "VARCHAR", Types.VARCHAR, 25, null, true);
    ddl = "ALTER TABLE t MODIFY col1 VARCHAR(50) CHARACTER SET greek;";
    parser.parse(ddl, tables);
    Table t2 = tables.forTable(new TableId(null, null, "t"));
    assertThat(t2).isNotNull();
    assertThat(t2.columnNames()).containsExactly("col1");
    assertThat(t2.primaryKeyColumnNames()).isEmpty();
    assertColumn(t2, "col1", "VARCHAR", Types.VARCHAR, 50, "greek", true);
    ddl = "ALTER TABLE t MODIFY col1 VARCHAR(75) CHARSET utf8;";
    parser.parse(ddl, tables);
    Table t3 = tables.forTable(new TableId(null, null, "t"));
    assertThat(t3).isNotNull();
    assertThat(t3.columnNames()).containsExactly("col1");
    assertThat(t3.primaryKeyColumnNames()).isEmpty();
    assertColumn(t3, "col1", "VARCHAR", Types.VARCHAR, 75, "utf8", true);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test)

Example 22 with Table

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

the class MySqlDdlParserTest method shouldParseTimeWithNowDefault.

@Test
@FixFor("DBZ-169")
public void shouldParseTimeWithNowDefault() {
    String ddl = "CREATE TABLE t1 ( " + "c1 int primary key auto_increment, " + "c2 datetime, " + "c3 datetime on update now(), " + "c4 char(4));";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    assertThat(listener.total()).isEqualTo(1);
    Table t = tables.forTable(new TableId(null, null, "t1"));
    assertThat(t).isNotNull();
    assertThat(t.columnNames()).containsExactly("c1", "c2", "c3", "c4");
    assertThat(t.primaryKeyColumnNames()).containsExactly("c1");
    assertColumn(t, "c1", "INT", Types.INTEGER, -1, -1, false, true, true);
    assertColumn(t, "c2", "DATETIME", Types.TIMESTAMP, -1, -1, true, false, false);
    assertColumn(t, "c3", "DATETIME", Types.TIMESTAMP, -1, -1, true, false, true);
    assertColumn(t, "c4", "CHAR", Types.CHAR, 4, -1, true, false, false);
    assertThat(t.columnWithName("c1").position()).isEqualTo(1);
    assertThat(t.columnWithName("c2").position()).isEqualTo(2);
    assertThat(t.columnWithName("c3").position()).isEqualTo(3);
    assertThat(t.columnWithName("c4").position()).isEqualTo(4);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test) FixFor(io.debezium.doc.FixFor)

Example 23 with Table

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

the class MySqlDdlParserTest method shouldParseCreateTableWithTextType.

@Test
@FixFor("DBZ-428")
public void shouldParseCreateTableWithTextType() {
    String ddl = "CREATE TABLE DBZ428 (" + "limtext TEXT(20), " + "unltext TEXT);";
    parser.parse(ddl, tables);
    assertThat(tables.size()).isEqualTo(1);
    Table mytable = tables.forTable(new TableId(null, null, "DBZ428"));
    assertThat(mytable).isNotNull();
    assertColumn(mytable, "unltext", "TEXT", Types.VARCHAR, -1, -1, true, false, false);
    assertColumn(mytable, "limtext", "TEXT", Types.VARCHAR, 20, -1, true, false, false);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Test(org.junit.Test) FixFor(io.debezium.doc.FixFor)

Example 24 with Table

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

the class MySqlDdlParser method parseAlterTable.

protected void parseAlterTable(Marker start) {
    tokens.canConsume("IGNORE");
    tokens.consume("TABLE");
    TableId tableId = parseQualifiedTableName(start);
    TableEditor table = databaseTables.editTable(tableId);
    TableId oldTableId = null;
    if (table != null) {
        AtomicReference<TableId> newTableName = new AtomicReference<>(null);
        if (!tokens.matches(terminator()) && !tokens.matches("PARTITION")) {
            parseAlterSpecificationList(start, table, newTableName::set);
        }
        if (tokens.matches("PARTITION")) {
            parsePartitionOptions(start, table);
        }
        databaseTables.overwriteTable(table.create());
        if (newTableName.get() != null) {
            // the table was renamed ...
            Table renamed = databaseTables.renameTable(tableId, newTableName.get());
            if (renamed != null) {
                oldTableId = tableId;
                tableId = renamed.id();
            }
        }
    } else {
        Marker marker = tokens.mark();
        try {
            // We don't know about this table but we still have to parse the statement ...
            table = TableEditor.noOp(tableId);
            if (!tokens.matches(terminator()) && !tokens.matches("PARTITION")) {
                parseAlterSpecificationList(start, table, str -> {
                });
            }
            if (tokens.matches("PARTITION")) {
                parsePartitionOptions(start, table);
            }
            parseTableOptions(start, table);
        // do nothing with this
        } catch (ParsingException e) {
            tokens.rewind(marker);
            consumeRemainingStatement(start);
        }
    }
    signalAlterTable(tableId, oldTableId, start);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) ParsingException(io.debezium.text.ParsingException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Marker(io.debezium.text.TokenStream.Marker) TableEditor(io.debezium.relational.TableEditor)

Example 25 with Table

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

the class MySqlSchema method refreshSchemas.

/**
 * Discard any currently-cached schemas and rebuild them using the filters.
 */
protected void refreshSchemas() {
    tableSchemaByTableId.clear();
    // Create TableSchema instances for any existing table ...
    this.tables.tableIds().forEach(id -> {
        Table table = this.tables.forTable(id);
        TableSchema schema = schemaBuilder.create(schemaPrefix, getEnvelopeSchemaName(table), table, filters.columnFilter(), filters.columnMappers());
        tableSchemaByTableId.put(id, schema);
    });
}
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