/** * Parse the supplied string, extracting up to the first 3 parts into a TableID. * * @param str the string representation of the table identifier; may not be null * @param useCatalogBeforeSchema {@code true} if the parsed string contains only 2 items and the first should be used as * the catalog and the second as the table name, or {@code false} if the first should be used as the schema and the * second as the table name * @return the table ID, or null if it could not be parsed */ public static TableId parse(String str, boolean useCatalogBeforeSchema) { String[] parts = TableIdParser.parse(str).stream() .toArray(String[]::new); return TableId.parse(parts, parts.length, useCatalogBeforeSchema); }
@Test(expected=IllegalArgumentException.class) public void escapedQuoteDoesntCloseQuotedIdentifier() { TableIdParser.parse("\"table\"\""); } }
@Test(expected=IllegalArgumentException.class) public void unclosedQuotingCharIsInvalid() { TableIdParser.parse("\"table"); }
@Test(expected=IllegalArgumentException.class) public void trailingSeparatorIsInvalid() { TableIdParser.parse("table."); }
@Test(expected=IllegalArgumentException.class) public void leadingSeparatorIsInvalid() { TableIdParser.parse(".table"); }
@Test public void canParseValidIdentifiers() { assertThat(TableIdParser.parse("s.a")).containsExactly("s", "a"); assertThat(TableIdParser.parse("s2.a")).containsExactly("s2", "a"); assertThat(TableIdParser.parse("table")).containsExactly("table"); assertThat(TableIdParser.parse(" table ")).containsExactly("table"); assertThat(TableIdParser.parse("schema.table")).containsExactly("schema", "table"); assertThat(TableIdParser.parse(" schema . table ")).containsExactly("schema", "table"); assertThat(TableIdParser.parse("catalog.schema.table")).containsExactly("catalog", "schema", "table"); assertThat(TableIdParser.parse("catalog . schema . table")).containsExactly("catalog", "schema", "table"); assertThat(TableIdParser.parse("\"table\"")).containsExactly("table"); assertThat(TableIdParser.parse("\"ta.ble\"")).containsExactly("ta.ble"); assertThat(TableIdParser.parse("\"ta ble\"")).containsExactly("ta ble"); assertThat(TableIdParser.parse("\"schema\".\"table\"")).containsExactly("schema", "table"); assertThat(TableIdParser.parse("\"cata . log\" . \"sche . ma\" . \"ta . ble\"")).containsExactly("cata . log", "sche . ma", "ta . ble"); assertThat(TableIdParser.parse("\"tab\"\"le\"")).containsExactly("tab\"le"); assertThat(TableIdParser.parse("\"tab\"\"\"\"le\"")).containsExactly("tab\"\"le"); assertThat(TableIdParser.parse("\"\"\"s\"\"\".\"\"\"a\"\"\"")).containsExactly("\"s\"", "\"a\""); }
/** * Parse the supplied string, extracting up to the first 3 parts into a TableID. * * @param str the string representation of the table identifier; may not be null * @param useCatalogBeforeSchema {@code true} if the parsed string contains only 2 items and the first should be used as * the catalog and the second as the table name, or {@code false} if the first should be used as the schema and the * second as the table name * @return the table ID, or null if it could not be parsed */ public static TableId parse(String str, boolean useCatalogBeforeSchema) { String[] parts = TableIdParser.parse(str).stream() .toArray(String[]::new); return TableId.parse(parts, parts.length, useCatalogBeforeSchema); }