|
9 | 9 | */ |
10 | 10 | package net.sf.jsqlparser.schema; |
11 | 11 |
|
| 12 | +import net.sf.jsqlparser.JSQLParserException; |
| 13 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 14 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 15 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
12 | 16 | import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.parallel.ResourceLock; |
| 18 | +import org.junit.jupiter.api.parallel.Resources; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.Arguments; |
| 21 | +import org.junit.jupiter.params.provider.MethodSource; |
| 22 | +import org.junit.jupiter.params.provider.ValueSource; |
13 | 23 |
|
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collections; |
14 | 26 | import java.util.List; |
| 27 | +import java.util.stream.Stream; |
15 | 28 |
|
| 29 | +import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed; |
| 30 | +import static net.sf.jsqlparser.test.TestUtils.assertStatementCanBeDeparsedAs; |
16 | 31 | import static org.assertj.core.api.Assertions.assertThat; |
17 | 32 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNull; |
18 | 34 |
|
19 | 35 | /** |
20 | 36 | * |
21 | 37 | * @author tw |
22 | 38 | */ |
| 39 | +@ResourceLock(Resources.SYSTEM_PROPERTIES) |
23 | 40 | public class ColumnTest { |
24 | 41 |
|
25 | 42 | @Test |
@@ -54,4 +71,202 @@ public void testConstructorNamePartsAndDelimiters() { |
54 | 71 | assertThat(table.getNamePartDelimiters()).containsExactly(".", ":"); |
55 | 72 | } |
56 | 73 |
|
| 74 | + @ParameterizedTest |
| 75 | + @MethodSource("qualifiedQuotedNames") |
| 76 | + void testQualifiedQuotedNames(String name, List<String> tableParts, String columnName) { |
| 77 | + assertColumn(new Column(name), name, tableParts, columnName); |
| 78 | + Column column = new Column(); |
| 79 | + column.setName(name, true); |
| 80 | + assertColumn(column, name, tableParts, columnName); |
| 81 | + assertColumn(new Column().withColumnName(name), name, tableParts, columnName); |
| 82 | + } |
| 83 | + |
| 84 | + @ParameterizedTest |
| 85 | + @MethodSource("separatedQuotedNames") |
| 86 | + void testQuotedNameParts(String name, List<String> tableParts, String columnName) { |
| 87 | + List<String> nameParts = new ArrayList<>(tableParts); |
| 88 | + Collections.reverse(nameParts); |
| 89 | + nameParts.add(columnName); |
| 90 | + assertColumn(new Column(nameParts), name, tableParts, columnName); |
| 91 | + assertColumn(new Column(nameParts, Collections.nCopies(nameParts.size() - 1, ".")), |
| 92 | + name, tableParts, columnName); |
| 93 | + } |
| 94 | + |
| 95 | + @ParameterizedTest |
| 96 | + @MethodSource("parsedQualifiedQuotedNames") |
| 97 | + void testParsedQualifiedQuotedNames(String name, List<String> tableParts, String columnName) |
| 98 | + throws JSQLParserException { |
| 99 | + String sql = "SELECT " + name + " FROM t1"; |
| 100 | + PlainSelect select = (PlainSelect) assertSqlCanBeParsedAndDeparsed(sql, false, |
| 101 | + parser -> parser.withSquareBracketQuotation(true)); |
| 102 | + assertColumn(select.getSelectItem(0).getExpression(Column.class), name, tableParts, |
| 103 | + columnName); |
| 104 | + PlainSelect reparsed = (PlainSelect) CCJSqlParserUtil.parse(select.toString(), |
| 105 | + parser -> parser.withSquareBracketQuotation(true)); |
| 106 | + assertColumn(reparsed.getSelectItem(0).getExpression(Column.class), name, tableParts, |
| 107 | + columnName); |
| 108 | + } |
| 109 | + |
| 110 | + @ParameterizedTest |
| 111 | + @MethodSource("wholeQuotedNames") |
| 112 | + void testWholeQuotedDottedNames(String name, String expected, List<String> tableParts, |
| 113 | + String columnName) throws JSQLParserException { |
| 114 | + assertColumn(new Column(name), expected, tableParts, columnName); |
| 115 | + PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse("SELECT " + name + " FROM t1", |
| 116 | + parser -> parser.withSquareBracketQuotation(true)); |
| 117 | + assertColumn(select.getSelectItem(0).getExpression(Column.class), expected, tableParts, |
| 118 | + columnName); |
| 119 | + assertStatementCanBeDeparsedAs(select, "SELECT " + expected + " FROM t1"); |
| 120 | + } |
| 121 | + |
| 122 | + @ParameterizedTest |
| 123 | + @ValueSource(strings = {"id", "`id`", "\"id\"", "[id]"}) |
| 124 | + void testUnqualifiedNames(String name) { |
| 125 | + Column column = new Column(name); |
| 126 | + assertEquals(name, column.getColumnName()); |
| 127 | + assertEquals(name, column.toString()); |
| 128 | + assertEquals("id", column.getUnquotedColumnName()); |
| 129 | + assertNull(column.getTable()); |
| 130 | + } |
| 131 | + |
| 132 | + @ParameterizedTest |
| 133 | + @MethodSource("legacyQualifiedTableNames") |
| 134 | + void testLegacyQualifiedTableNames(String tableName, String expected, |
| 135 | + List<String> tableParts) { |
| 136 | + assertColumn(new Column(List.of(tableName, "id")), expected, tableParts, "id"); |
| 137 | + assertColumn(new Column(List.of(tableName, "id"), List.of(".")), expected, |
| 138 | + tableParts, "id"); |
| 139 | + } |
| 140 | + |
| 141 | + @ParameterizedTest |
| 142 | + @ValueSource(booleans = {false, true}) |
| 143 | + void testBigQueryQualifiedTableColumn(boolean explicitDialect) throws JSQLParserException { |
| 144 | + PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse( |
| 145 | + "SELECT `project.dataset.table`.id FROM `project.dataset.table`", parser -> { |
| 146 | + if (explicitDialect) { |
| 147 | + parser.withDialect(Dialect.BIGQUERY); |
| 148 | + } |
| 149 | + }); |
| 150 | + Column column = select.getSelectItem(0).getExpression(Column.class); |
| 151 | + assertColumn(column, "\"project\".\"dataset\".\"table\".id", |
| 152 | + List.of("\"table\"", "\"dataset\"", "\"project\""), "id"); |
| 153 | + assertEquals("project", column.getUnquotedCatalogName()); |
| 154 | + assertEquals("dataset", column.getUnquotedSchemaName()); |
| 155 | + assertEquals("table", column.getUnquotedTableName()); |
| 156 | + assertEquals(select.getFromItem(Table.class).getNameParts(), |
| 157 | + column.getTable().getNameParts()); |
| 158 | + assertStatementCanBeDeparsedAs(select, |
| 159 | + "SELECT \"project\".\"dataset\".\"table\".id FROM \"project\".\"dataset\".\"table\""); |
| 160 | + } |
| 161 | + |
| 162 | + @ParameterizedTest |
| 163 | + @ValueSource(strings = {"t1.`id`", "`t1.id`", "\"t1.id\"", "[t1.id]", "s.t1.id"}) |
| 164 | + void testExplicitlyDisabledSplitting(String name) { |
| 165 | + Column column = new Column(); |
| 166 | + column.setName(name, false); |
| 167 | + assertEquals(name, column.getColumnName()); |
| 168 | + assertEquals(name, column.toString()); |
| 169 | + assertNull(column.getTable()); |
| 170 | + } |
| 171 | + |
| 172 | + @ParameterizedTest |
| 173 | + @ValueSource(strings = {"false", "OFF", "0"}) |
| 174 | + void testSystemPropertyDisablesSplitting(String value) { |
| 175 | + String previous = System.getProperty("SPLIT_NAMES_ON_DELIMITER"); |
| 176 | + try { |
| 177 | + System.setProperty("SPLIT_NAMES_ON_DELIMITER", value); |
| 178 | + for (String name : List.of("t1.`id`", "`t1.id`", "\"t1.id\"", "[t1.id]", "s.t1.id")) { |
| 179 | + Column column = new Column(name); |
| 180 | + assertEquals(name, column.getColumnName()); |
| 181 | + assertEquals(name, column.toString()); |
| 182 | + assertNull(column.getTable()); |
| 183 | + } |
| 184 | + } finally { |
| 185 | + if (previous == null) { |
| 186 | + System.clearProperty("SPLIT_NAMES_ON_DELIMITER"); |
| 187 | + } else { |
| 188 | + System.setProperty("SPLIT_NAMES_ON_DELIMITER", previous); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static void assertColumn(Column column, String name, List<String> tableParts, |
| 194 | + String columnName) { |
| 195 | + assertEquals(name, column.toString()); |
| 196 | + assertEquals(name, column.getFullyQualifiedName()); |
| 197 | + assertEquals(columnName, column.getColumnName()); |
| 198 | + assertThat(column.getTable().getNameParts()).containsExactlyElementsOf(tableParts); |
| 199 | + assertEquals(tableParts.get(0), column.getTableName()); |
| 200 | + assertEquals(tableParts.size() > 1 ? tableParts.get(1) : null, column.getSchemaName()); |
| 201 | + assertEquals(tableParts.size() > 2 ? tableParts.get(2) : null, column.getCatalogName()); |
| 202 | + } |
| 203 | + |
| 204 | + static Stream<Arguments> qualifiedQuotedNames() { |
| 205 | + return Stream.of( |
| 206 | + Arguments.of("t1.`id`", List.of("t1"), "`id`"), |
| 207 | + Arguments.of("t1.\"id\"", List.of("t1"), "\"id\""), |
| 208 | + Arguments.of("t1.[id]", List.of("t1"), "[id]"), |
| 209 | + Arguments.of("`t1`.id", List.of("`t1`"), "id"), |
| 210 | + Arguments.of("\"t1\".id", List.of("\"t1\""), "id"), |
| 211 | + Arguments.of("[t1].id", List.of("[t1]"), "id"), |
| 212 | + Arguments.of("`t1`.`id`", List.of("`t1`"), "`id`"), |
| 213 | + Arguments.of("\"t1\".\"id\"", List.of("\"t1\""), "\"id\""), |
| 214 | + Arguments.of("[t1].[id]", List.of("[t1]"), "[id]"), |
| 215 | + Arguments.of("s.t1.`id`", List.of("t1", "s"), "`id`"), |
| 216 | + Arguments.of("`s`.`t1`.`id`", List.of("`t1`", "`s`"), "`id`"), |
| 217 | + Arguments.of("\"s\".\"t1\".\"id\"", List.of("\"t1\"", "\"s\""), "\"id\""), |
| 218 | + Arguments.of("[s].[t1].[id]", List.of("[t1]", "[s]"), "[id]"), |
| 219 | + Arguments.of("[s].`t1`.\"id\"", List.of("`t1`", "[s]"), "\"id\""), |
| 220 | + Arguments.of("t1.`i.d`", List.of("t1"), "`i.d`"), |
| 221 | + Arguments.of("t1.`i``.d`", List.of("t1"), "`i``.d`"), |
| 222 | + Arguments.of("t1.\"i\"\".d\"", List.of("t1"), "\"i\"\".d\""), |
| 223 | + Arguments.of("t1.[i]].d]", List.of("t1"), "[i]].d]"), |
| 224 | + Arguments.of("`t.1`.id", List.of("`t.1`"), "id"), |
| 225 | + Arguments.of("`s.1`.`t.1`.`i.d`", List.of("`t.1`", "`s.1`"), "`i.d`"), |
| 226 | + Arguments.of("\"s.1\".\"t.1\".\"i.d\"", List.of("\"t.1\"", "\"s.1\""), "\"i.d\""), |
| 227 | + Arguments.of("[s.1].[t.1].[i.d]", List.of("[t.1]", "[s.1]"), "[i.d]"), |
| 228 | + Arguments.of("`t``.1`.`i``.d`", List.of("`t``.1`"), "`i``.d`"), |
| 229 | + Arguments.of("\"t\"\".1\".\"i\"\".d\"", List.of("\"t\"\".1\""), "\"i\"\".d\""), |
| 230 | + Arguments.of("[t]].1].[i]].d]", List.of("[t]].1]"), "[i]].d]"), |
| 231 | + Arguments.of("`t\".1`.\"i`.d\"", List.of("`t\".1`"), "\"i`.d\""), |
| 232 | + Arguments.of("t1.id", List.of("t1"), "id"), |
| 233 | + Arguments.of("s.t1.id", List.of("t1", "s"), "id")); |
| 234 | + } |
| 235 | + |
| 236 | + static Stream<Arguments> separatedQuotedNames() { |
| 237 | + // A single table token containing dots keeps the legacy BigQuery interpretation. |
| 238 | + return qualifiedQuotedNames() |
| 239 | + .filter(arguments -> { |
| 240 | + List<?> tableParts = (List<?>) arguments.get()[1]; |
| 241 | + return tableParts.size() > 1 || !((String) tableParts.get(0)).contains("."); |
| 242 | + }); |
| 243 | + } |
| 244 | + |
| 245 | + static Stream<Arguments> parsedQualifiedQuotedNames() { |
| 246 | + return separatedQuotedNames() |
| 247 | + .filter(arguments -> !((String) arguments.get()[0]).contains("]]")); |
| 248 | + } |
| 249 | + |
| 250 | + static Stream<Arguments> legacyQualifiedTableNames() { |
| 251 | + return Stream.of( |
| 252 | + Arguments.of("schema.table", "schema.table.id", List.of("table", "schema")), |
| 253 | + Arguments.of("`schema.table`", "\"schema\".\"table\".id", |
| 254 | + List.of("\"table\"", "\"schema\"")), |
| 255 | + Arguments.of("`project.dataset.table`", "\"project\".\"dataset\".\"table\".id", |
| 256 | + List.of("\"table\"", "\"dataset\"", "\"project\""))); |
| 257 | + } |
| 258 | + |
| 259 | + static Stream<Arguments> wholeQuotedNames() { |
| 260 | + return Stream.of( |
| 261 | + Arguments.of("`t1.id`", "\"t1\".\"id\"", List.of("\"t1\""), "\"id\""), |
| 262 | + Arguments.of("\"t1.id\"", "\"t1\".\"id\"", List.of("\"t1\""), "\"id\""), |
| 263 | + Arguments.of("[t1.id]", "\"t1\".\"id\"", List.of("\"t1\""), "\"id\""), |
| 264 | + Arguments.of("`s.t1.id`", "\"s\".\"t1\".\"id\"", List.of("\"t1\"", "\"s\""), |
| 265 | + "\"id\""), |
| 266 | + Arguments.of("\"s.t1.id\"", "\"s\".\"t1\".\"id\"", List.of("\"t1\"", "\"s\""), |
| 267 | + "\"id\""), |
| 268 | + Arguments.of("[s.t1.id]", "\"s\".\"t1\".\"id\"", List.of("\"t1\"", "\"s\""), |
| 269 | + "\"id\"")); |
| 270 | + } |
| 271 | + |
57 | 272 | } |
0 commit comments