DdlChanges implements DdlParserListener { public void groupEventsByDatabase(DatabaseEventConsumer consumer) { if (isEmpty()) { return; } if (databaseNames.size() <= 1) { consumer.consume(databaseNames.iterator().next(), events); return; } List<Event> dbEvents = new ArrayList<>(); String currentDatabase = null; for (Event event : events) { String dbName = getDatabase(event); if (currentDatabase == null || dbName.equals(currentDatabase)) { currentDatabase = dbName; dbEvents.add(event); } else { consumer.consume(currentDatabase, dbEvents); } } } DdlChanges();  DdlChanges(String terminator); DdlChanges reset(); @Override void handle(Event event); void groupStatementStringsByDatabase(DatabaseStatementStringConsumer consumer); void groupStatementsByDatabase(DatabaseStatementConsumer consumer); void groupEventsByDatabase(DatabaseEventConsumer consumer); boolean isEmpty(); boolean applyToMoreDatabasesThan(String name); @Override String toString(); boolean anyMatch(Predicate<String> databaseFilter, Predicate<TableId> tableFilter);  }
@Test public void shouldParseMultipleStatementsWithDefaultDatabase() { parser.setCurrentSchema("mydb"); String ddl = "CREATE TABLE foo ( " + System.lineSeparator() + " c1 INTEGER NOT NULL, " + System.lineSeparator() + " c2 VARCHAR(22) " + System.lineSeparator() + "); " + System.lineSeparator() + "-- This is a comment" + System.lineSeparator() + "DROP TABLE foo;" + System.lineSeparator(); parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(0); changes.groupEventsByDatabase((dbName, list) -> { assertThat(dbName).isEqualTo("mydb"); assertThat(list.size()).isEqualTo(2); assertThat(list.get(0).type()).isEqualTo(EventType.CREATE_TABLE); assertThat(list.get(1).type()).isEqualTo(EventType.DROP_TABLE); }); }
@Test public void shouldParseMultipleStatementsWithFullyQualifiedDatabase() { parser.setCurrentSchema("mydb"); String ddl = "CREATE TABLE other.foo ( " + System.lineSeparator() + " c1 INTEGER NOT NULL, " + System.lineSeparator() + " c2 VARCHAR(22) " + System.lineSeparator() + "); " + System.lineSeparator() + "-- This is a comment" + System.lineSeparator() + "DROP TABLE other.foo;" + System.lineSeparator(); parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(0); changes.groupEventsByDatabase((dbName, list) -> { assertThat(dbName).isEqualTo("other"); assertThat(list.size()).isEqualTo(2); assertThat(list.get(0).type()).isEqualTo(EventType.CREATE_TABLE); assertThat(list.get(1).type()).isEqualTo(EventType.DROP_TABLE); }); }