Repository navigation
Expand file tree
/
Copy pathTableTest.java
More file actions
182 lines (157 loc) · 6.9 KB
/
Copy pathTableTest.java
File metadata and controls
182 lines (157 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.schema;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.util.deparser.ExpressionDeParser;
import net.sf.jsqlparser.util.deparser.SelectDeParser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
public class TableTest {
@Test
public void tableIndexException() {
Table table = new Table().withName("bla")
.withDatabase(new Database(new Server("server", "instance"), "db"));
assertEquals("[server\\instance].db..bla", table.toString());
}
@Test
public void tableSetDatabase() {
Table table = new Table();
table.setName("testtable");
Database database = new Database("default");
table.setDatabase(database);
assertEquals("default..testtable", table.toString());
}
@Test
public void tableSetDatabaseIssue812() throws JSQLParserException {
String sql =
"SELECT * FROM MY_TABLE1 as T1, MY_TABLE2, (SELECT * FROM MY_DB.TABLE3) LEFT OUTER JOIN MY_TABLE4 "
+ " WHERE ID = (SELECT MAX(ID) FROM MY_TABLE5) AND ID2 IN (SELECT * FROM MY_TABLE6)";
Select select = (Select) CCJSqlParserUtil.parse(sql);
StringBuilder buffer = new StringBuilder();
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
final Database database = new Database("default");
SelectDeParser deparser = new SelectDeParser(expressionDeParser, buffer) {
@Override
public <S> StringBuilder visit(Table table, S parameters) {
table.setDatabase(database); // Exception
return null;
}
};
deparser.visit((PlainSelect) select, null);
}
@Test
public void testTableRemoveNameParts() {
Table table = new Table("link", "DICTIONARY");
assertThat(table.getFullyQualifiedName()).isEqualTo("link.DICTIONARY");
table.setSchemaName(null);
assertThat(table.getFullyQualifiedName()).isEqualTo("DICTIONARY");
}
@Test
public void testConstructorDelimitersInappropriateSize() {
assertThatThrownBy(
() -> new Table(List.of("a", "b", "c"), List.of("too", "many", "delimiters")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"the length of the delimiters list must be 1 less than nameParts");
}
@Test
void testBigQueryFullQuotedName() throws JSQLParserException {
String sqlStr = "select * from `d.s.t`";
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);
Table table = (Table) select.getFromItem();
assertEquals("\"d\"", table.getCatalogName());
assertEquals("\"s\"", table.getSchemaName());
assertEquals("\"t\"", table.getName());
assertEquals("d", table.getUnquotedDatabaseName());
assertEquals("s", table.getUnquotedSchemaName());
assertEquals("t", table.getUnquotedName());
sqlStr = "select * from `s.t`";
select = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);
table = (Table) select.getFromItem();
assertNull(table.getCatalogName());
assertEquals("\"s\"", table.getSchemaName());
assertEquals("\"t\"", table.getName());
assertNull(table.getUnquotedDatabaseName());
assertEquals("s", table.getUnquotedSchemaName());
assertEquals("t", table.getUnquotedName());
}
@Test
void testClone() {
Table t = new Table("a.b.c");
t.setResolvedTable(t);
Assertions.assertNotSame(t.clone(), t);
Assertions.assertNotEquals(t.clone(), t);
}
@Test
void testWithSchema() {
Table t = new Table("a");
t.setSchemaName("UNNAMED.session1");
Assertions.assertEquals("UNNAMED", t.getDatabaseName());
Assertions.assertEquals("session1", t.getSchemaName());
}
@ParameterizedTest
@CsvSource(delimiter = '|', value = {
"MYSQL | CREATE TABLE `t@x` (id int) | `t@x` | t@x",
"MYSQL | ALTER TABLE `t@x` ADD COLUMN c int | `t@x` | t@x",
"MYSQL | DROP INDEX `idx@x` ON t | `idx@x` | idx@x",
"POSTGRESQL | CREATE TABLE \"t@x\" (id int) | \"t@x\" | t@x",
"POSTGRESQL | DROP INDEX \"idx@x\" | \"idx@x\" | idx@x",
"SQLSERVER | CREATE TABLE [t@x] (id int) | [t@x] | t@x"})
void keepsAnAtSignInsideAQuotedName(Dialect dialect, String sql, String name,
String unquotedName) throws JSQLParserException {
Statement statement = CCJSqlParserUtil.parse(sql, parser -> parser.withDialect(dialect));
Table table = statement instanceof CreateTable ? ((CreateTable) statement).getTable()
: statement instanceof Alter ? ((Alter) statement).getTable()
: ((Drop) statement).getName();
assertEquals(name, table.getName());
assertEquals(unquotedName, table.getUnquotedName());
assertNull(table.getDBLinkName());
assertEquals(sql, statement.toString());
}
@Test
void readsAnOracleDatabaseLinkOutsideQuotes() throws JSQLParserException {
PlainSelect select = (PlainSelect) CCJSqlParserUtil
.parse("SELECT * FROM owner.orders@remote",
parser -> parser.withDialect(Dialect.ORACLE));
Table table = (Table) select.getFromItem();
assertEquals("owner", table.getSchemaName());
assertEquals("orders", table.getName());
assertEquals("remote", table.getDBLinkName());
Table quoted = new Table("\"a@b\"@remote");
assertEquals("\"a@b\"", quoted.getName());
assertEquals("remote", quoted.getDBLinkName());
}
@Test
void hasNoDatabaseLinkWithoutAnAtSignAfterTheName() {
assertNull(new Table("orders").getDBLinkName());
Table variable = new Table("@orders");
assertEquals("@orders", variable.getName());
assertNull(variable.getDBLinkName());
}
}