/** * Refreshes this schema's content for a particular table * * @param connection a {@link JdbcConnection} instance, never {@code null} * @param tableId the table identifier; may not be null * @param refreshToastableColumns refreshes the cache of toastable columns for `tableId`, if {@code true} * @throws SQLException if there is a problem refreshing the schema from the database server */ protected void refresh(PostgresConnection connection, TableId tableId, boolean refreshToastableColumns) throws SQLException { Tables temp = new Tables(); connection.readSchema(temp, null, null, tableId::equals, null, true); // the table could be deleted before the event was processed if (temp.size() == 0) { LOGGER.warn("Refresh of {} was requested but the table no longer exists", tableId); return; } // overwrite (add or update) or views of the tables tables().overwriteTable(temp.forTable(tableId)); // refresh the schema refreshSchema(tableId); if (refreshToastableColumns) { // and refresh toastable columns info refreshToastableColumnsMap(connection, tableId); } }
for (TableChange entry : changes) { if (entry.getType() == TableChangeType.CREATE || entry.getType() == TableChangeType.ALTER) { schema.overwriteTable(entry.getTable()); schema.removeTable(entry.getId());
throws SQLException { Set<TableId> tableIdsBefore = new HashSet<>(tables.tableIds()); Collections.sort(columns); tables.overwriteTable(tableEntry.getKey(), columns, pkColumnNames, defaultCharsetName);
public Tables subset(TableFilter filter) { if (filter == null) return this; return lock.read(() -> { Tables result = new Tables(tableIdCaseInsensitive); tablesByTableId.forEach((tableId, table) -> { if (filter.isIncluded(tableId)) { result.overwriteTable(table); } }); return result; }); }
@Test public void shouldParseStatementsWithQuotedIdentifiers() { parser.parse(readFile("ddl/mysql-quoted.ddl"), tables); Testing.print(tables); assertThat(tables.size()).isEqualTo(4); assertThat(listener.total()).isEqualTo(10); assertThat(tables.forTable("connector_test_ro", null, "products")).isNotNull(); assertThat(tables.forTable("connector_test_ro", null, "products_on_hand")).isNotNull(); assertThat(tables.forTable("connector_test_ro", null, "customers")).isNotNull(); assertThat(tables.forTable("connector_test_ro", null, "orders")).isNotNull(); }
@Override public Tables clone() { return new Tables(this, tableIdCaseInsensitive); }
/** * Get the {@link Table} meta-data for the table with the given identifier, if that table exists and is * included by the filter configuration * * @param id the table identifier; may be null * @return the current table definition, or null if there is no table with the given identifier, if the identifier is null, * or if the table has been excluded by the filters */ public Table tableFor(TableId id) { return tableFilter.isIncluded(id) ? tables.forTable(id) : null; }
TableEditor table = databaseTables.editOrCreateTable(tableId); if (tokens.matches('(')) { List<String> columnNames = parseColumnNameList(start); databaseTables.overwriteTable(table.create());
+ ")"); conn.execute("SELECT * FROM person"); Tables tables = new Tables(); conn.readSchema(tables, DATABASE.getDatabaseName(), null, null, null, true); assertThat(tables.size()).isEqualTo(1); Table person = tables.forTable(DATABASE.getDatabaseName(), null, "person"); assertThat(person).isNotNull(); assertThat(person.filterColumns(col->col.isAutoIncremented())).isEmpty(); + ")"); conn.execute("SELECT * FROM product"); tables = new Tables(); conn.readSchema(tables, DATABASE.getDatabaseName(), null, null, null, true); assertThat(tables.size()).isEqualTo(2); Table product = tables.forTable(DATABASE.getDatabaseName(), null, "product"); assertThat(product).isNotNull(); assertThat(product.filterColumnNames(Column::isAutoIncremented)).containsOnly("id"); + ")"); conn.execute("SELECT * FROM purchased"); tables = new Tables(); conn.readSchema(tables, DATABASE.getDatabaseName(), null, null, null, true); assertThat(tables.size()).isEqualTo(3); Table purchased = tables.forTable(DATABASE.getDatabaseName(), null, "purchased"); assertThat(purchased).isNotNull(); assertThat(person.filterColumns(col->col.isAutoIncremented())).isEmpty();
@Override public void emitSchemaChangeEvent(Receiver receiver) throws InterruptedException { SchemaChangeEventType eventType = getSchemaChangeEventType(); if (eventType == null) { return; } Tables tables = new Tables(); OracleDdlParser parser = new OracleDdlParser(); parser.setCurrentDatabase(ddlLcr.getSourceDatabaseName()); parser.setCurrentSchema(ddlLcr.getObjectOwner()); parser.parse(ddlLcr.getDDLText(), tables); Set<TableId> changedTableIds = tables.drainChanges(); if (changedTableIds.isEmpty()) { throw new IllegalArgumentException("Couldn't parse DDL statement " + ddlLcr.getDDLText()); } Table table = tables.forTable(tableId); receiver.schemaChangeEvent(new SchemaChangeEvent(offsetContext.getPartition(), offsetContext.getOffset(), ddlLcr.getSourceDatabaseName(), ddlLcr.getObjectOwner(), ddlLcr.getDDLText(), table, eventType, false)); }
if (tokens.canConsume("LIKE")) { TableId originalId = parseQualifiedTableName(start); Table original = databaseTables.forTable(originalId); if (original != null) { databaseTables.overwriteTable(tableId, original.columns(), original.primaryKeyColumnNames(), original.defaultCharsetName()); return; if (onlyIfNotExists && databaseTables.forTable(tableId) != null) { return; TableEditor table = databaseTables.editOrCreateTable(tableId); databaseTables.overwriteTable(table.create()); signalCreateTable(tableId, start); debugParsed(start);
@Test public void testJsonValues() { String sql = "CREATE TABLE JSON_TABLE (" + " A JSON," + " B JSON NOT NULL" + ");"; MySqlValueConverters converters = new MySqlValueConverters(JdbcValueConverters.DecimalMode.DOUBLE, TemporalPrecisionMode.CONNECT, JdbcValueConverters.BigIntUnsignedMode.LONG); DdlParser parser = new MySqlAntlrDdlParser(); Tables tables = new Tables(); parser.parse(sql, tables); Table table = tables.forTable(new TableId(null, null, "JSON_TABLE")); // ColA - Nullable column Column colA= table.columnWithName("A"); Field fieldA = new Field(colA.name(), -1, converters.schemaBuilder(colA).optional().build()); assertThat(converters.converter(colA, fieldA).convert("{}")).isEqualTo("{}"); assertThat(converters.converter(colA, fieldA).convert("[]")).isEqualTo("[]"); assertThat(converters.converter(colA, fieldA).convert(new byte[0])).isNull(); assertThat(converters.converter(colA, fieldA).convert(null)).isNull(); assertThat(converters.converter(colA, fieldA).convert("{ \"key1\": \"val1\", \"key2\": {\"key3\":\"val3\"} }")).isEqualTo("{ \"key1\": \"val1\", \"key2\": {\"key3\":\"val3\"} }"); // ColB - NOT NUll column Column colB= table.columnWithName("B"); Field fieldB = new Field(colB.name(), -1, converters.schemaBuilder(colB).build()); assertThat(converters.converter(colB, fieldB).convert("{}")).isEqualTo("{}"); assertThat(converters.converter(colB, fieldB).convert("[]")).isEqualTo("[]"); assertThat(converters.converter(colB, fieldB).convert(new byte[0])).isEqualTo("{}"); assertThat(converters.converter(colB, fieldB).convert(null)).isEqualTo("{}"); assertThat(converters.converter(colB, fieldB).convert("{ \"key1\": \"val1\", \"key2\": {\"key3\":\"val3\"} }")).isEqualTo("{ \"key1\": \"val1\", \"key2\": {\"key3\":\"val3\"} }"); }
/** * Refreshes the schema content with a table constructed externally * * @param table constructed externally - typically from decoder metadata */ protected void refresh(Table table) { // overwrite (add or update) or views of the tables tables().overwriteTable(table); // and refresh the schema refreshSchema(table.id()); }
@Override public void exitCopyCreateTable(MySqlParser.CopyCreateTableContext ctx) { TableId tableId = parser.parseQualifiedTableId(ctx.tableName(0).fullId()); TableId originalTableId = parser.parseQualifiedTableId(ctx.tableName(1).fullId()); Table original = parser.databaseTables().forTable(originalTableId); if (original != null) { parser.databaseTables().overwriteTable(tableId, original.columns(), original.primaryKeyColumnNames(), original.defaultCharsetName()); parser.signalCreateTable(tableId, ctx); } super.exitCopyCreateTable(ctx); }
Set<TableId> tableIds = new HashSet<>(tables.tableIds()); TableEditor editor = tables.editTable(tableId); editor.tableId(tableIdWithCatalog); tables.overwriteTable(editor.create()); tables.removeTable(tableId);
@Test public void shouldGetExceptionOnParseAlterStatementsWithoutCreate() { String ddl = "ALTER TABLE foo ADD COLUMN c bigint;" + System.lineSeparator(); parser.parse(ddl, tables); assertThat(((MySqlAntlrDdlParser) parser).getParsingExceptionsFromWalker().size()).isEqualTo(1); assertThat(tables.size()).isEqualTo(0); }
LegacyDdlParser ddlParser = new DdlParserSql2003(); ddlParser.setCurrentSchema("db1"); // recover does this, so we need to as well Tables tables1 = new Tables(); Tables tables2 = new Tables(); Tables tables3 = new Tables(); assertThat(tables1.size()).isEqualTo(0); assertThat(tables1.size()).isEqualTo(3); ddlParser.parse(ddl, tables2); assertThat(tables2.size()).isEqualTo(3); ddlParser.parse(ddl, tables3); assertThat(tables3.size()).isEqualTo(3); history.record(source, position, "db1", ddl); ddlParser.parse(ddl, tables2); assertThat(tables2.size()).isEqualTo(2); ddlParser.parse(ddl, tables3); assertThat(tables3.size()).isEqualTo(2); history.record(source, position, "db1", ddl); ddlParser.parse(ddl, tables3); assertThat(tables3.size()).isEqualTo(3); Tables recoveredTables = new Tables(); setLogPosition(15); history.recover(source, position, recoveredTables, recoveryParser); recoveredTables = new Tables();
tokens.consume("TABLE"); TableId tableId = parseQualifiedTableName(start); TableEditor table = databaseTables.editTable(tableId); TableId oldTableId = null; if (table != null) { parsePartitionOptions(start, table); databaseTables.overwriteTable(table.create()); if (newTableName.get() != null) { Table renamed = databaseTables.renameTable(tableId, newTableName.get()); if (renamed != null) { oldTableId = tableId;
@Override public void enterDropView(MySqlParser.DropViewContext ctx) { ctx.fullId().stream().map(parser::parseQualifiedTableId).forEach(tableId -> { parser.databaseTables().removeTable(tableId); parser.signalDropView(tableId, ctx); }); super.enterDropView(ctx); } }