7
7
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8
8
* #L%
9
9
*/
10
- /*
11
- * Copyright (C) 2019 JSQLParser.
12
- *
13
- * This library is free software; you can redistribute it and/or
14
- * modify it under the terms of the GNU Lesser General Public
15
- * License as published by the Free Software Foundation; either
16
- * version 2.1 of the License, or (at your option) any later version.
17
- *
18
- * This library is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
- * Lesser General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU Lesser General Public
24
- * License along with this library; if not, write to the Free Software
25
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26
- * MA 02110-1301 USA
27
- */
28
10
package net .sf .jsqlparser .schema ;
29
11
12
+ import net .sf .jsqlparser .JSQLParserException ;
13
+ import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
14
+ import net .sf .jsqlparser .statement .select .PlainSelect ;
15
+ import net .sf .jsqlparser .statement .select .Select ;
16
+ import net .sf .jsqlparser .util .deparser .ExpressionDeParser ;
17
+ import net .sf .jsqlparser .util .deparser .SelectDeParser ;
18
+ import static org .junit .Assert .assertEquals ;
30
19
import org .junit .Test ;
31
20
32
21
/**
@@ -42,4 +31,35 @@ public void tableIndexException() {
42
31
table .setDatabase (new Database (new Server ("server" , "instance" ), "db" ));
43
32
}
44
33
34
+ @ Test
35
+ public void tableSetDatabase () {
36
+ Table table = new Table ();
37
+ table .setName ("testtable" );
38
+ Database database = new Database ("default" );
39
+ table .setDatabase (database );
40
+ assertEquals ("default..testtable" , table .toString ());
41
+ }
42
+
43
+ @ Test
44
+ public void tableSetDatabaseIssue812 () throws JSQLParserException {
45
+ String sql = "SELECT * FROM MY_TABLE1 as T1, MY_TABLE2, (SELECT * FROM MY_DB.TABLE3) LEFT OUTER JOIN MY_TABLE4 "
46
+ + " WHERE ID = (SELECT MAX(ID) FROM MY_TABLE5) AND ID2 IN (SELECT * FROM MY_TABLE6)" ;
47
+
48
+ Select select = (Select ) CCJSqlParserUtil .parse (sql );
49
+ StringBuilder buffer = new StringBuilder ();
50
+ ExpressionDeParser expressionDeParser = new ExpressionDeParser ();
51
+ final Database database = new Database ("default" );
52
+ SelectDeParser deparser = new SelectDeParser (expressionDeParser , buffer ) {
53
+
54
+ @ Override
55
+ public void visit (Table tableName ) {
56
+ System .out .println (tableName );
57
+ tableName .setDatabase (database ); // Exception
58
+ System .out .println (tableName .getDatabase ());
59
+ }
60
+ };
61
+
62
+ deparser .visit ((PlainSelect ) select .getSelectBody ());
63
+
64
+ }
45
65
}
0 commit comments