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);
}
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;
}
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);
}
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;
}
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();
}
}
Aggregations