Skip to content

Commit 62a0341

Browse files
committed
fixes #849
1 parent 1d2c261 commit 62a0341

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ Also I would like to know about needed examples or documentation stuff.
6666

6767
## Extensions in the latest SNAPSHOT version 3.1
6868

69+
* introduced more positions for *!* instead of *NOT*
70+
* allowed more complex expressions within *if* function
71+
* introduced multicolumn alias like *select * from mytab as tab(c1, c2)*
72+
** additional type definition is possible (issue #849)
6973

7074
## Extensions of JSqlParser releases
7175

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
*/
1010
package net.sf.jsqlparser.expression;
1111

12+
import java.util.List;
13+
import java.util.Objects;
14+
import net.sf.jsqlparser.statement.create.table.ColDataType;
15+
1216
public class Alias {
1317

1418
private String name;
1519
private boolean useAs = true;
20+
private List<AliasColumn> aliasColumns;
1621

1722
public Alias(String name) {
1823
this.name = name;
@@ -39,8 +44,48 @@ public void setUseAs(boolean useAs) {
3944
this.useAs = useAs;
4045
}
4146

47+
public List<AliasColumn> getAliasColumns() {
48+
return aliasColumns;
49+
}
50+
51+
public void setAliasColumns(List<AliasColumn> aliasColumns) {
52+
this.aliasColumns = aliasColumns;
53+
}
54+
4255
@Override
4356
public String toString() {
44-
return (useAs ? " AS " : " ") + name;
57+
String alias = (useAs ? " AS " : " ") + name;
58+
59+
if (aliasColumns != null && !aliasColumns.isEmpty()) {
60+
String ac = "";
61+
for (AliasColumn col : aliasColumns) {
62+
if (ac.length() > 0) {
63+
ac += ", ";
64+
}
65+
ac += col.name;
66+
if (col.colDataType != null) {
67+
ac += " " + col.colDataType.toString();
68+
}
69+
}
70+
alias += "(" + ac + ")";
71+
}
72+
73+
return alias;
74+
}
75+
76+
public static class AliasColumn {
77+
78+
public final String name;
79+
public final ColDataType colDataType;
80+
81+
public AliasColumn(String name, ColDataType colDataType) {
82+
Objects.requireNonNull(name);
83+
this.name = name;
84+
this.colDataType = colDataType;
85+
}
86+
87+
public AliasColumn(String name) {
88+
this(name, null);
89+
}
4590
}
4691
}

src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public void visit(SelectExpressionItem selectExpressionItem) {
184184
if (selectExpressionItem.getAlias() != null) {
185185
buffer.append(selectExpressionItem.getAlias().toString());
186186
}
187-
188187
}
189188

190189
@Override

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,11 +1454,22 @@ AllTableColumns AllTableColumns():
14541454
Alias Alias():
14551455
{ String name = null;
14561456
Token token = null;
1457-
boolean useAs = false; }
1457+
boolean useAs = false;
1458+
Alias alias;
1459+
String colname;
1460+
ColDataType colDataType = null;
1461+
}
14581462
{
14591463
[<K_AS> { useAs = true; } ]
14601464
( name=RelObjectName() | token=<S_CHAR_LITERAL> { name=token.image; } )
1461-
{ return new Alias(name,useAs); }
1465+
{ alias = new Alias(name,useAs); }
1466+
1467+
[ LOOKAHEAD(2) "(" { List<Alias.AliasColumn> list = new ArrayList<Alias.AliasColumn>(); }
1468+
colname = RelObjectName() [ colDataType = ColDataType() ] { list.add(new Alias.AliasColumn(colname, colDataType)); }
1469+
("," { colDataType=null; } colname = RelObjectName() [ colDataType = ColDataType()] { list.add(new Alias.AliasColumn(colname, colDataType)); } )*
1470+
")" { alias.setAliasColumns(list); } ]
1471+
1472+
{ return alias; }
14621473
}
14631474

14641475
MySQLIndexHint MySQLIndexHint():

src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,4 +3829,14 @@ public void testIssue848_3() throws JSQLParserException {
38293829
public void testIssue848_4() throws JSQLParserException {
38303830
assertSqlCanBeParsedAndDeparsed("select c1 from T1 where someFunc(select f1 from t2 where t2.id = T1.key) = 10", true);
38313831
}
3832+
3833+
@Test
3834+
public void testMultiColumnAliasIssue849() throws JSQLParserException {
3835+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable AS mytab2(col1, col2)");
3836+
}
3837+
3838+
@Test
3839+
public void testMultiColumnAliasIssue849_2() throws JSQLParserException {
3840+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM crosstab('select rowid, attribute, value from ct where attribute = ''att2'' or attribute = ''att3'' order by 1,2') AS ct(row_name text, category_1 text, category_2 text, category_3 text)");
3841+
}
38323842
}

0 commit comments

Comments
 (0)