/** * Create a {@link TableId} from the supplied schema and table names. By default, this method uses the supplied schema name * as the TableId's catalog, which often matches the catalog name in JDBC database metadata. * * @param schemaName the name of the schema; may be null if not specified * @param tableName the name of the table; should not be null * @return the table identifier; never null */ public TableId resolveTableId(String schemaName, String tableName) { return new TableId(schemaName, null, tableName); }
public ChangeTable(TableId sourceTableId, String captureInstance, int changeTableObjectId, Lsn startLsn, Lsn stopLsn) { super(); this.sourceTableId = sourceTableId; this.captureInstance = captureInstance; this.changeTableObjectId = changeTableObjectId; this.startLsn = startLsn; this.stopLsn = stopLsn; this.changeTableId = sourceTableId != null ? new TableId(sourceTableId.catalog(), CDC_SCHEMA, captureInstance + "_CT") : null; }
/** * Obtain the definition of the identified table. * * @param catalogName the name of the database catalog that contains the table; may be null if the JDBC driver does not * show a schema for this table * @param schemaName the name of the database schema that contains the table; may be null if the JDBC driver does not * show a schema for this table * @param tableName the name of the table * @return the table definition, or null if there was no definition for the identified table */ public Table forTable(String catalogName, String schemaName, String tableName) { return forTable(new TableId(catalogName, schemaName, tableName)); }
protected static TableId parse(String table) { TableId tableId = TableId.parse(table, false); if (tableId == null) { return null; } return tableId.schema() == null ? new TableId(tableId.catalog(), PUBLIC_SCHEMA_NAME, tableId.table()) : tableId; }
protected void assertNotAllowed(Predicate<TableId> filter, String dbName, String schemaName, String tableName) { TableId id = new TableId(dbName, schemaName, tableName); assertThat(filter.test(id)).isFalse(); } }
protected void assertNotAllowed(Predicate<TableId> filter, String dbName, String tableName) { TableId id = new TableId(dbName, null, tableName); assertThat(filter.test(id)).isFalse(); }
protected static TableId tableIdFromInsertStmt(String statement) { Matcher matcher = INSERT_TABLE_MATCHING_PATTERN.matcher(statement); assertTrue("Extraction of table name from insert statement failed: " + statement, matcher.matches()); TableId id = TableId.parse(matcher.group(1), false); if (id.schema() == null) { id = new TableId(id.catalog(), "public", id.table()); } return id; }
@Before public void beforeEach() { source = new SourceInfo("serverX", "databaseX"); source.update(1L, new TableId("catalogNameX", "schemaNameX", "tableNameX")); }
@Test @FixFor("DBZ-901") public void parseAlterTableTruncatedDefaulDateTime() { String sql = "CREATE TABLE TIME_TABLE (" + " A datetime(3) NOT NULL DEFAULT '0000-00-00 00:00:00.000'" + ");"; String alterSql = "ALTER TABLE TIME_TABLE ADD COLUMN B DATETIME(3) NOT NULL DEFAULT '1970-01-01 00:00:00';"; parser.parse(sql, tables); parser.parse(alterSql, tables); Table table = tables.forTable(new TableId(null, null, "TIME_TABLE")); assertThat(table.columnWithName("A").defaultValue()).isEqualTo((Date.from(Instant.ofEpochMilli(0)))); assertThat(table.columnWithName("B").defaultValue()).isEqualTo((Date.from(Instant.ofEpochMilli(0)))); }
@Test @FixFor("DBZ-688") public void parseGeomCollection() { String ddl = "CREATE TABLE geomtable (id int(11) PRIMARY KEY, collection GEOMCOLLECTION DEFAULT NULL)"; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); assertThat(tables.forTable(new TableId(null, null, "geomtable"))).isNotNull(); }
@Test public void parseRealDefaultValue() { String sql = "CREATE TABLE REAL_TABLE (\n" + " A REAL NOT NULL DEFAULT 1,\n" + " B REAL NULL DEFAULT NULL \n" + ");"; parser.parse(sql, tables); Table table = tables.forTable(new TableId(null, null, "REAL_TABLE")); assertThat(table.columnWithName("A").defaultValue()).isEqualTo(1f); assertThat(table.columnWithName("B").defaultValue()).isEqualTo(null); }
@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); }
@Test @FixFor("DBZ-439") public void shouldParseCreateTableWithDoublePrecisionKeyword() { String ddl = "CREATE TABLE DBZ439 (" + "limdouble DOUBLE PRECISION(20, 2)," + "unldouble DOUBLE PRECISION);"; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table mytable = tables.forTable(new TableId(null, null, "DBZ439")); assertThat(mytable).isNotNull(); assertColumn(mytable, "limdouble", "DOUBLE PRECISION", Types.DOUBLE, 20, 2, true, false, false); assertColumn(mytable, "unldouble", "DOUBLE PRECISION", Types.DOUBLE, -1, -1, true, false, false); }
@FixFor("DBZ-419") @Test public void shouldParseCreateTableWithUnnamedPrimaryKeyConstraint() { final String ddl = "CREATE TABLE IF NOT EXISTS tables_exception (table_name VARCHAR(100), create_date TIMESTAMP DEFAULT NOW(), enabled INT(1), retention int(1) default 30, CONSTRAINT PRIMARY KEY (table_name));"; parser.parse(ddl, tables); Testing.print(tables); Table t = tables.forTable(new TableId(null, null, "tables_exception")); assertThat(t).isNotNull(); assertThat(t.primaryKeyColumnNames()).containsExactly("table_name"); assertThat(tables.size()).isEqualTo(1); }
@Test @FixFor("DBZ-474") public void shouldParseCreateTableStatementWithCollate() { String ddl = "CREATE TABLE c1 (pk INT PRIMARY KEY, v1 CHAR(36) NOT NULL COLLATE utf8_unicode_ci);"; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table table = tables.forTable(new TableId(null, null, "c1")); assertThat(table).isNotNull(); assertColumn(table, "v1", "CHAR", Types.CHAR, 36, -1, false, false, false); Column column = table.columnWithName("v1"); assertThat(column.typeUsesCharset()).isTrue(); }
@Test public void shouldParseCreateTableStatementWithCharacterSetForColumns() { String ddl = "CREATE TABLE t ( col1 VARCHAR(25) CHARACTER SET greek ); "; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table t = tables.forTable(new TableId(null, null, "t")); assertThat(t).isNotNull(); assertThat(t.retrieveColumnNames()).containsExactly("col1"); assertThat(t.primaryKeyColumnNames()).isEmpty(); assertColumn(t, "col1", "VARCHAR", Types.VARCHAR, 25, -1, true, false, false); }
@Test public void parseNumericAndDecimalToDoubleDefaultValue() { String sql = "CREATE TABLE NUMERIC_DECIMAL_TABLE (\n" + " A NUMERIC NOT NULL DEFAULT 1.23,\n" + " B DECIMAL NOT NULL DEFAULT 2.321,\n" + " C NUMERIC NULL DEFAULT '12.678'\n" + ");"; parser.parse(sql, tables); Table table = tables.forTable(new TableId(null, null, "NUMERIC_DECIMAL_TABLE")); assertThat(table.columnWithName("A").defaultValue()).isEqualTo(1.23d); assertThat(table.columnWithName("B").defaultValue()).isEqualTo(2.321d); assertThat(table.columnWithName("C").defaultValue()).isEqualTo(12.678d); }
@Test public void shouldParseCreateUserTable() { String ddl = "CREATE TABLE IF NOT EXISTS user ( Host char(60) binary DEFAULT '' NOT NULL, User char(32) binary DEFAULT '' NOT NULL, Select_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Insert_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Update_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Delete_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Drop_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Reload_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Shutdown_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Process_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, File_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Grant_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, References_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Index_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Alter_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Show_db_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Super_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_tmp_table_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Lock_tables_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Execute_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Repl_slave_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Repl_client_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_view_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Show_view_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_routine_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Alter_routine_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_user_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Event_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Trigger_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, Create_tablespace_priv enum('N','Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, ssl_type enum('','ANY','X509', 'SPECIFIED') COLLATE utf8_general_ci DEFAULT '' NOT NULL, ssl_cipher BLOB NOT NULL, x509_issuer BLOB NOT NULL, x509_subject BLOB NOT NULL, max_questions int(11) unsigned DEFAULT 0 NOT NULL, max_updates int(11) unsigned DEFAULT 0 NOT NULL, max_connections int(11) unsigned DEFAULT 0 NOT NULL, max_user_connections int(11) unsigned DEFAULT 0 NOT NULL, plugin char(64) DEFAULT 'mysql_native_password' NOT NULL, authentication_string TEXT, password_expired ENUM('N', 'Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, password_last_changed timestamp NULL DEFAULT NULL, password_lifetime smallint unsigned NULL DEFAULT NULL, account_locked ENUM('N', 'Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL, PRIMARY KEY Host (Host,User) ) engine=MyISAM CHARACTER SET utf8 COLLATE utf8_bin comment='Users and global privileges';"; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table foo = tables.forTable(new TableId(null, null, "user")); assertThat(foo).isNotNull(); assertThat(foo.retrieveColumnNames()).contains("Host", "User", "Select_priv"); assertColumn(foo, "Host", "CHAR BINARY", Types.BINARY, 60, -1, false, false, false); parser.parse("DROP TABLE user", tables); assertThat(tables.size()).isEqualTo(0); }
@Test @FixFor("DBZ-429") public void parseTableWithNegativeDefault() { String ddl = "CREATE TABLE t (id INT NOT NULL, myvalue INT DEFAULT -10, PRIMARY KEY (`id`));"; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table t = tables.forTable(new TableId(null, null, "t")); assertThat(t).isNotNull(); assertThat(t.retrieveColumnNames()).containsExactly("id", "myvalue"); assertThat(t.primaryKeyColumnNames()).hasSize(1); assertColumn(t, "myvalue", "INT", Types.INTEGER, -1, -1, true, false, false); }
@FixFor("DBZ-160") @Test public void shouldParseCreateTableWithEnumDefault() { String ddl = "CREATE TABLE t ( c1 ENUM('a','b','c') NOT NULL DEFAULT 'b', c2 ENUM('a', 'b', 'c') NOT NULL DEFAULT 'a');"; parser.parse(ddl, tables); assertThat(tables.size()).isEqualTo(1); Table t = tables.forTable(new TableId(null, null, "t")); assertThat(t).isNotNull(); assertThat(t.retrieveColumnNames()).containsExactly("c1", "c2"); assertThat(t.primaryKeyColumnNames()).isEmpty(); assertColumn(t, "c1", "ENUM", Types.CHAR, 1, -1, false, false, false); assertColumn(t, "c2", "ENUM", Types.CHAR, 1, -1, false, false, false); }