Skip to content

Commit

Permalink
fixes #849
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Sep 20, 2019
1 parent 1d2c261 commit 62a0341
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ Also I would like to know about needed examples or documentation stuff.

## Extensions in the latest SNAPSHOT version 3.1

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

## Extensions of JSqlParser releases

Expand Down
47 changes: 46 additions & 1 deletion src/main/java/net/sf/jsqlparser/expression/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
*/
package net.sf.jsqlparser.expression;

import java.util.List;
import java.util.Objects;
import net.sf.jsqlparser.statement.create.table.ColDataType;

public class Alias {

private String name;
private boolean useAs = true;
private List<AliasColumn> aliasColumns;

public Alias(String name) {
this.name = name;
Expand All @@ -39,8 +44,48 @@ public void setUseAs(boolean useAs) {
this.useAs = useAs;
}

public List<AliasColumn> getAliasColumns() {
return aliasColumns;
}

public void setAliasColumns(List<AliasColumn> aliasColumns) {
this.aliasColumns = aliasColumns;
}

@Override
public String toString() {
return (useAs ? " AS " : " ") + name;
String alias = (useAs ? " AS " : " ") + name;

if (aliasColumns != null && !aliasColumns.isEmpty()) {
String ac = "";
for (AliasColumn col : aliasColumns) {
if (ac.length() > 0) {
ac += ", ";
}
ac += col.name;
if (col.colDataType != null) {
ac += " " + col.colDataType.toString();
}
}
alias += "(" + ac + ")";
}

return alias;
}

public static class AliasColumn {

public final String name;
public final ColDataType colDataType;

public AliasColumn(String name, ColDataType colDataType) {
Objects.requireNonNull(name);
this.name = name;
this.colDataType = colDataType;
}

public AliasColumn(String name) {
this(name, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public void visit(SelectExpressionItem selectExpressionItem) {
if (selectExpressionItem.getAlias() != null) {
buffer.append(selectExpressionItem.getAlias().toString());
}

}

@Override
Expand Down
15 changes: 13 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1454,11 +1454,22 @@ AllTableColumns AllTableColumns():
Alias Alias():
{ String name = null;
Token token = null;
boolean useAs = false; }
boolean useAs = false;
Alias alias;
String colname;
ColDataType colDataType = null;
}
{
[<K_AS> { useAs = true; } ]
( name=RelObjectName() | token=<S_CHAR_LITERAL> { name=token.image; } )
{ return new Alias(name,useAs); }
{ alias = new Alias(name,useAs); }

[ LOOKAHEAD(2) "(" { List<Alias.AliasColumn> list = new ArrayList<Alias.AliasColumn>(); }
colname = RelObjectName() [ colDataType = ColDataType() ] { list.add(new Alias.AliasColumn(colname, colDataType)); }
("," { colDataType=null; } colname = RelObjectName() [ colDataType = ColDataType()] { list.add(new Alias.AliasColumn(colname, colDataType)); } )*
")" { alias.setAliasColumns(list); } ]

{ return alias; }
}

MySQLIndexHint MySQLIndexHint():
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3829,4 +3829,14 @@ public void testIssue848_3() throws JSQLParserException {
public void testIssue848_4() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("select c1 from T1 where someFunc(select f1 from t2 where t2.id = T1.key) = 10", true);
}

@Test
public void testMultiColumnAliasIssue849() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable AS mytab2(col1, col2)");
}

@Test
public void testMultiColumnAliasIssue849_2() throws JSQLParserException {
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)");
}
}

0 comments on commit 62a0341

Please sign in to comment.