Skip to content

Commit 38aadee

Browse files
committed
fixes #812
1 parent 4763d45 commit 38aadee

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/main/java/net/sf/jsqlparser/schema/Table.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public Database getDatabase() {
6464

6565
public void setDatabase(Database database) {
6666
setIndex(DATABASE_IDX, database.getDatabaseName());
67-
setIndex(SERVER_IDX, database.getServer().getFullyQualifiedName());
67+
if (database.getServer() != null) {
68+
setIndex(SERVER_IDX, database.getServer().getFullyQualifiedName());
69+
}
6870
}
6971

7072
public String getSchemaName() {
@@ -113,7 +115,7 @@ private String getIndex(int idx) {
113115
public String getFullyQualifiedName() {
114116
StringBuilder fqn = new StringBuilder();
115117

116-
for (int i = partItems.size()-1 ; i >=0; i--) {
118+
for (int i = partItems.size() - 1; i >= 0; i--) {
117119
String part = partItems.get(i);
118120
if (part == null) {
119121
part = "";
@@ -162,4 +164,3 @@ public String toString() {
162164
+ ((hint != null) ? hint.toString() : "");
163165
}
164166
}
165-

src/test/java/net/sf/jsqlparser/schema/TableTest.java

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,15 @@
77
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
88
* #L%
99
*/
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-
*/
2810
package net.sf.jsqlparser.schema;
2911

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;
3019
import org.junit.Test;
3120

3221
/**
@@ -42,4 +31,35 @@ public void tableIndexException() {
4231
table.setDatabase(new Database(new Server("server", "instance"), "db"));
4332
}
4433

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+
}
4565
}

0 commit comments

Comments
 (0)