Skip to content

Commit ba6c54d

Browse files
author
Pap Lőrinc
committed
Corrected Sql Server multi-part table and column names (database.schema.table.column) in the select statement to accept 4 levels with empty inner parts;
1 parent b4d547e commit ba6c54d

27 files changed

+759
-447
lines changed

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
* Copyright (C) 2004 - 2013 JSQLParser
66
* %%
77
* This program is free software: you can redistribute it and/or modify
8-
* it under the terms of the GNU Lesser General Public License as
9-
* published by the Free Software Foundation, either version 2.1 of the
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
1010
* License, or (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Lesser Public License for more details.
16-
*
17-
* You should have received a copy of the GNU General Lesser Public
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
1818
* License along with this program. If not, see
1919
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
2020
* #L%

src/main/java/net/sf/jsqlparser/expression/SignedExpression.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
* Copyright (C) 2004 - 2013 JSQLParser
66
* %%
77
* This program is free software: you can redistribute it and/or modify
8-
* it under the terms of the GNU Lesser General Public License as
9-
* published by the Free Software Foundation, either version 2.1 of the
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
1010
* License, or (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Lesser Public License for more details.
16-
*
17-
* You should have received a copy of the GNU General Lesser Public
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
1818
* License along with this program. If not, see
1919
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
2020
* #L%

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

Lines changed: 74 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,92 @@
55
* Copyright (C) 2004 - 2013 JSQLParser
66
* %%
77
* This program is free software: you can redistribute it and/or modify
8-
* it under the terms of the GNU Lesser General Public License as
9-
* published by the Free Software Foundation, either version 2.1 of the
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
1010
* License, or (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Lesser Public License for more details.
16-
*
17-
* You should have received a copy of the GNU General Lesser Public
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
1818
* License along with this program. If not, see
1919
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
2020
* #L%
2121
*/
2222
package net.sf.jsqlparser.schema;
2323

24-
import net.sf.jsqlparser.expression.Expression;
25-
import net.sf.jsqlparser.expression.ExpressionVisitor;
24+
import net.sf.jsqlparser.expression.*;
25+
import net.sf.jsqlparser.statement.select.*;
2626

2727
/**
2828
* A column. It can have the table name it belongs to.
2929
*/
30-
public class Column implements Expression {
31-
32-
private String columnName = "";
33-
private Table table;
34-
35-
public Column() {
36-
}
37-
38-
public Column(Table table, String columnName) {
39-
this.table = table;
40-
this.columnName = columnName;
41-
}
42-
43-
public Column(String columnName) {
44-
this(null, columnName);
45-
}
46-
47-
public String getColumnName() {
48-
return columnName;
49-
}
50-
51-
public Table getTable() {
52-
return table;
53-
}
54-
55-
public void setColumnName(String string) {
56-
columnName = string;
57-
}
58-
59-
public void setTable(Table table) {
60-
this.table = table;
61-
}
62-
63-
/**
64-
* @return the name of the column, prefixed with 'tableName' and '.'
65-
*/
66-
public String getFullyQualifiedName() {
67-
68-
String columnWholeName;
69-
String tableWholeName = null;
70-
71-
if (table != null) {
72-
tableWholeName = table.getFullyQualifiedName();
73-
}
74-
if (tableWholeName != null && tableWholeName.length() != 0) {
75-
columnWholeName = tableWholeName + "." + columnName;
76-
} else {
77-
columnWholeName = columnName;
78-
}
79-
80-
return columnWholeName;
81-
82-
}
83-
84-
@Override
85-
public void accept(ExpressionVisitor expressionVisitor) {
86-
expressionVisitor.visit(this);
87-
}
88-
89-
@Override
90-
public String toString() {
91-
return getFullyQualifiedName();
92-
}
93-
}
30+
public class Column implements SelectItem, Expression, MultiPartName {
31+
32+
private Table table;
33+
private String columnName;
34+
35+
public Column() {
36+
}
37+
38+
public Column(Table table, String columnName) {
39+
setTable(table);
40+
setColumnName(columnName);
41+
}
42+
43+
public Column(String columnName) {
44+
this(null, columnName);
45+
}
46+
47+
public Table getTable() {
48+
return table;
49+
}
50+
51+
public void setTable(Table table) {
52+
this.table = table;
53+
}
54+
55+
public String getColumnName() {
56+
return columnName;
57+
}
58+
59+
public void setColumnName(String string) {
60+
columnName = string;
61+
}
62+
63+
@Override
64+
public String getFullyQualifiedName() {
65+
String fqn = "";
66+
67+
if (table != null) {
68+
fqn += table.getFullyQualifiedName();
69+
}
70+
if (!fqn.isEmpty()) {
71+
fqn += ".";
72+
}
73+
74+
75+
if (columnName != null) {
76+
fqn += columnName;
77+
}
78+
79+
return fqn;
80+
}
81+
82+
@Override
83+
public void accept(ExpressionVisitor expressionVisitor) {
84+
expressionVisitor.visit(this);
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return getFullyQualifiedName();
90+
}
91+
92+
@Override
93+
public void accept(SelectItemVisitor selectItemVisitor) {
94+
selectItemVisitor.visit(this);
95+
}
96+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.schema;
23+
24+
public class Database implements MultiPartName {
25+
private Server server;
26+
private String databaseName;
27+
28+
public Database(String databaseName) {
29+
setDatabaseName(databaseName);
30+
}
31+
32+
public Database(Server server, String databaseName) {
33+
setServer(server);
34+
setDatabaseName(databaseName);
35+
}
36+
37+
public Server getServer() {
38+
return server;
39+
}
40+
41+
public void setServer(Server server) {
42+
this.server = server;
43+
}
44+
45+
public String getDatabaseName() {
46+
return databaseName;
47+
}
48+
49+
public void setDatabaseName(String databaseName) {
50+
this.databaseName = databaseName;
51+
}
52+
53+
@Override
54+
public String getFullyQualifiedName() {
55+
String fqn = "";
56+
57+
if (server != null) {
58+
fqn += server.getFullyQualifiedName();
59+
}
60+
if (!fqn.isEmpty()) {
61+
fqn += ".";
62+
}
63+
64+
if (databaseName != null) {
65+
fqn += databaseName;
66+
}
67+
68+
return fqn;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return getFullyQualifiedName();
74+
}
75+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.schema;
23+
24+
public interface MultiPartName {
25+
String getFullyQualifiedName();
26+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.schema;
23+
24+
import java.util.regex.*;
25+
26+
public class Server implements MultiPartName {
27+
public static final Pattern SERVER_PATTERN = Pattern.compile("\\[([^\\]]+?)(?:\\\\([^\\]]+))?\\]");
28+
29+
private String serverName;
30+
private String instanceName;
31+
32+
33+
public Server(String serverAndInstanceName) {
34+
if (serverAndInstanceName != null) {
35+
final Matcher matcher = SERVER_PATTERN.matcher(serverAndInstanceName);
36+
if (!matcher.find()) {
37+
throw new IllegalArgumentException(String.format("%s is not a valid database reference", serverAndInstanceName));
38+
}
39+
setServerName(matcher.group(1));
40+
setInstanceName(matcher.group(2));
41+
}
42+
}
43+
44+
public Server(String serverName, String instanceName) {
45+
setServerName(serverName);
46+
setInstanceName(instanceName);
47+
}
48+
49+
public String getServerName() {
50+
return serverName;
51+
}
52+
53+
public void setServerName(String serverName) {
54+
this.serverName = serverName;
55+
}
56+
57+
public String getInstanceName() {
58+
return instanceName;
59+
}
60+
61+
public void setInstanceName(String instanceName) {
62+
this.instanceName = instanceName;
63+
}
64+
65+
@Override
66+
public String getFullyQualifiedName() {
67+
if (serverName != null && !serverName.isEmpty() && instanceName != null && !instanceName.isEmpty()) {
68+
return String.format("[%s\\%s]", serverName, instanceName);
69+
} else if (serverName != null && !serverName.isEmpty()) {
70+
return String.format("[%s]", serverName);
71+
} else {
72+
return "";
73+
}
74+
}
75+
76+
@Override
77+
public String toString() {
78+
return getFullyQualifiedName();
79+
}
80+
}

0 commit comments

Comments
 (0)