JSqlParser icon indicating copy to clipboard operation
JSqlParser copied to clipboard

Table alias Problem

Open arlindonatal opened this issue 10 years ago • 4 comments

I have a problem with table alias.

`Table table1 = new Table("mytable1"); table1.setAlias(new Alias("tab1"));

    Table table2 = new Table("mytable2");
    table2.setAlias(new Alias("tab2"));

    List<Expression> colunas = Arrays.asList(new Column(table1, "col1"), new Column(table1, "col2"), new Column(table1, "col3"), new Column(table2, "b1"), new Column(table2, "b2"));

    Select select = SelectUtils.buildSelectFromTableAndExpressions(table1, colunas.toArray(new Expression[colunas.size()]));

    final EqualsTo equalsTo = new EqualsTo();
    equalsTo.setLeftExpression(new Column(table1, "col1"));
    equalsTo.setRightExpression(new Column(table2, "b1"));
    Join addJoin = SelectUtils.addJoin(select, table2, equalsTo);
    addJoin.setLeft(true);

    System.out.println( select.toString() );`

The result is:

SELECT mytable1.col1, mytable1.col2, mytable1.col3, mytable2.b1, mytable2.b2 FROM mytable1 AS tab1 LEFT JOIN mytable2 AS tab2 ON mytable1.col1 = mytable2.b1

Not replaced table alias in column and in left clause.

arlindonatal avatar Jul 22 '16 00:07 arlindonatal

It was a problem with Column.toString. The deparser did the job already right.

wumpz avatar Jul 22 '16 07:07 wumpz

I'm not sure the fix is correct. Because the column's FQN should contain the table name, not its alias. So, perhaps you should fix Column's toString() #332. But the Column's getFullyQualifiedName() should remain as before #311 fix.

wheredevel avatar Sep 26 '16 06:09 wheredevel

Good point. I will look into it.

wumpz avatar Sep 26 '16 09:09 wumpz

Good point. I will look into it.

wumpz avatar Sep 26 '16 12:09 wumpz