Search in sources :

Example 46 with Table

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

the class MySqlDdlParser method parseCreateView.

protected void parseCreateView(Marker start) {
    tokens.canConsume("OR", "REPLACE");
    if (tokens.canConsume("ALGORITHM")) {
        tokens.consume('=');
        tokens.consumeAnyOf("UNDEFINED", "MERGE", "TEMPTABLE");
    }
    parseDefiner(tokens.mark());
    if (tokens.canConsume("SQL", "SECURITY")) {
        tokens.consumeAnyOf("DEFINER", "INVOKER");
    }
    tokens.consume("VIEW");
    TableId tableId = parseQualifiedTableName(start);
    if (skipViews) {
        // We don't care about the rest ...
        consumeRemainingStatement(start);
        signalCreateView(tableId, start);
        debugSkipped(start);
        return;
    }
    TableEditor table = databaseTables.editOrCreateTable(tableId);
    if (tokens.matches('(')) {
        List<String> columnNames = parseColumnNameList(start);
        // We know nothing other than the names ...
        columnNames.forEach(name -> {
            table.addColumn(Column.editor().name(name).create());
        });
    }
    tokens.canConsume("AS");
    // We should try to discover the types of the columns by looking at this select
    if (tokens.canConsume("SELECT")) {
        // If the SELECT clause is selecting qualified column names or columns names from a single table, then
        // we can look up the columns and use those to set the type and nullability of the view's columns ...
        Map<String, Column> selectedColumnsByAlias = parseColumnsInSelectClause(start);
        if (table.columns().isEmpty()) {
            selectedColumnsByAlias.forEach((columnName, fromTableColumn) -> {
                if (fromTableColumn != null && columnName != null)
                    table.addColumn(fromTableColumn.edit().name(columnName).create());
            });
        } else {
            List<Column> changedColumns = new ArrayList<>();
            table.columns().forEach(column -> {
                // Find the column from the SELECT statement defining the view ...
                Column selectedColumn = selectedColumnsByAlias.get(column.name());
                if (selectedColumn != null) {
                    changedColumns.add(column.edit().jdbcType(selectedColumn.jdbcType()).type(selectedColumn.typeName(), selectedColumn.typeExpression()).length(selectedColumn.length()).scale(selectedColumn.scale()).autoIncremented(selectedColumn.isAutoIncremented()).generated(selectedColumn.isGenerated()).optional(selectedColumn.isOptional()).create());
                }
            });
            changedColumns.forEach(table::addColumn);
        }
        // Parse the FROM clause to see if the view is only referencing a single table, and if so then update the view
        // with an equivalent primary key ...
        Map<String, Table> fromTables = parseSelectFromClause(start);
        if (fromTables.size() == 1) {
            Table fromTable = fromTables.values().stream().findFirst().get();
            List<String> fromTablePkColumnNames = fromTable.columnNames();
            List<String> viewPkColumnNames = new ArrayList<>();
            selectedColumnsByAlias.forEach((viewColumnName, fromTableColumn) -> {
                if (fromTablePkColumnNames.contains(fromTableColumn.name())) {
                    viewPkColumnNames.add(viewColumnName);
                }
            });
            if (viewPkColumnNames.size() == fromTablePkColumnNames.size()) {
                table.setPrimaryKeyNames(viewPkColumnNames);
            }
        }
    }
    // We don't care about the rest ...
    consumeRemainingStatement(start);
    // Update the table definition ...
    databaseTables.overwriteTable(table.create());
    signalCreateView(tableId, start);
    debugParsed(start);
}
Also used : TableId(io.debezium.relational.TableId) Table(io.debezium.relational.Table) Column(io.debezium.relational.Column) ArrayList(java.util.ArrayList) TableEditor(io.debezium.relational.TableEditor)

Example 47 with Table

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

the class DdlParserSql2003Test method shouldParseCreateTableStatementWithSingleGeneratedAndPrimaryKeyColumn.

@Test
public void shouldParseCreateTableStatementWithSingleGeneratedAndPrimaryKeyColumn() {
    String ddl = "CREATE TABLE foo ( " + System.lineSeparator() + " c1 INTEGER GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY, " + 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()).containsExactly("c1");
    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)

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