Skip to content

Commit dfd2fe5

Browse files
committed
fixes #311
1 parent 84bf065 commit dfd2fe5

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ public String getFullyQualifiedName() {
6565
StringBuilder fqn = new StringBuilder();
6666

6767
if (table != null) {
68-
fqn.append(table.getFullyQualifiedName());
68+
if (table.getAlias() != null) {
69+
fqn.append(table.getAlias().getName());
70+
} else {
71+
fqn.append(table.getFullyQualifiedName());
72+
}
6973
}
70-
if (fqn.length()>0) {
74+
if (fqn.length() > 0) {
7175
fqn.append('.');
7276
}
7377
if (columnName != null) {
@@ -85,4 +89,4 @@ public void accept(ExpressionVisitor expressionVisitor) {
8589
public String toString() {
8690
return getFullyQualifiedName();
8791
}
88-
}
92+
}

src/test/java/net/sf/jsqlparser/util/SelectUtilsTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package net.sf.jsqlparser.util;
22

3+
import java.util.Arrays;
4+
import java.util.List;
35
import net.sf.jsqlparser.JSQLParserException;
6+
import net.sf.jsqlparser.expression.Alias;
47
import net.sf.jsqlparser.expression.Expression;
58
import net.sf.jsqlparser.expression.LongValue;
69
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
@@ -99,4 +102,24 @@ public void testBuildSelectFromTableWithGroupBy() {
99102
assertEquals("SELECT * FROM mytable GROUP BY b", select.toString());
100103
}
101104

105+
@Test
106+
public void testTableAliasIssue311() {
107+
Table table1 = new Table("mytable1");
108+
table1.setAlias(new Alias("tab1"));
109+
Table table2 = new Table("mytable2");
110+
table2.setAlias(new Alias("tab2"));
111+
112+
List<? extends Expression> colunas = Arrays.asList(new Column(table1, "col1"), new Column(table1, "col2"), new Column(table1, "col3"), new Column(table2, "b1"), new Column(table2, "b2"));
113+
114+
Select select = SelectUtils.buildSelectFromTableAndExpressions(table1, colunas.toArray(new Expression[colunas.size()]));
115+
116+
final EqualsTo equalsTo = new EqualsTo();
117+
equalsTo.setLeftExpression(new Column(table1, "col1"));
118+
equalsTo.setRightExpression(new Column(table2, "b1"));
119+
Join addJoin = SelectUtils.addJoin(select, table2, equalsTo);
120+
addJoin.setLeft(true);
121+
122+
assertEquals("SELECT tab1.col1, tab1.col2, tab1.col3, tab2.b1, tab2.b2 FROM mytable1 AS tab1 LEFT JOIN mytable2 AS tab2 ON tab1.col1 = tab2.b1",
123+
select.toString());
124+
}
102125
}

0 commit comments

Comments
 (0)