001package gudusoft.gsqlparser.stmt.postgresql;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.TParseTreeVisitor;
005import gudusoft.gsqlparser.nodes.TRelationExpr;
006import gudusoft.gsqlparser.nodes.TSelectSqlNode;
007import gudusoft.gsqlparser.stmt.TSelectSqlStatement;
008
009/**
010 * A standalone {@code TABLE t} statement of the PostgreSQL family (PostgreSQL,
011 * DuckDB, GaussDB, EDB), statement type {@link ESqlStatementType#sstPostgresqlTable}.
012 * <p>
013 * {@code TABLE t} is the SQL-standard shorthand for {@code SELECT * FROM t}, so this
014 * statement is a {@link TSelectSqlStatement}: its tables, joins and wildcard result
015 * column are those of {@code SELECT * FROM t} (see {@link TSelectSqlStatement#isTableQuery()}),
016 * and every consumer of a select (resolver, lineage, table collectors) analyzes it as one.
017 * The statement type and {@link #getTableName()} are kept for existing callers.
018 */
019public class TPostgresqlTableStmt extends TSelectSqlStatement {
020    public TPostgresqlTableStmt(EDbVendor dbvendor) {
021        super(dbvendor);
022        sqlstatementtype = ESqlStatementType.sstPostgresqlTable;
023    }
024
025    /**
026     * @return the name of the table after the TABLE keyword, as written; null when
027     * the statement is a set operation (TABLE t1 UNION TABLE t2)
028     */
029    public String getTableName() {
030        return tableName;
031    }
032
033    private String tableName;
034
035    public int doParseStatement(TCustomSqlStatement psql) {
036        if (rootNode == null) return -1;
037        int ret = super.doParseStatement(psql);
038        if (rootNode instanceof TSelectSqlNode) {
039            TRelationExpr expr = ((TSelectSqlNode) rootNode).getRelationExpr();
040            if ((expr != null) && (expr.getRelationName() != null)) {
041                tableName = expr.getRelationName().toString();
042            }
043        }
044        return ret;
045    }
046
047    /**
048     * Visits this node as a {@code TPostgresqlTableStmt} and, inside that, as the
049     * {@link TSelectSqlStatement} it is.
050     */
051    public void accept(TParseTreeVisitor v){
052        v.preVisit(this);
053        super.accept(v);
054        v.postVisit(this);
055    }
056
057    /**
058     * Visits this node as a {@code TPostgresqlTableStmt} and, inside that, visits it
059     * and its children as the {@link TSelectSqlStatement} it is.
060     */
061    public void acceptChildren(TParseTreeVisitor v){
062        v.preVisit(this);
063        super.acceptChildren(v);
064        v.postVisit(this);
065    }
066}