Search in sources :

Example 61 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class HiveSchemaStatVisitor method visit.

@Override
public boolean visit(HiveInsertStatement x) {
    if (repository != null && x.getParent() == null) {
        repository.resolve(x);
    }
    SQLWithSubqueryClause with = x.getWith();
    if (with != null) {
        with.accept(this);
    }
    setMode(x, TableStat.Mode.Insert);
    SQLExprTableSource tableSource = x.getTableSource();
    SQLExpr tableName = tableSource.getExpr();
    if (tableName instanceof SQLName) {
        TableStat stat = getTableStat((SQLName) tableName);
        stat.incrementInsertCount();
        List<SQLExpr> columns = x.getColumns();
        for (SQLExpr column : columns) {
            if (column instanceof SQLIdentifierExpr) {
                addColumn((SQLName) tableName, ((SQLIdentifierExpr) column).normalizedName());
            }
        }
    }
    for (SQLAssignItem partition : x.getPartitions()) {
        partition.accept(this);
    }
    accept(x.getQuery());
    return false;
}
Also used : SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) SQLName(com.alibaba.druid.sql.ast.SQLName) TableStat(com.alibaba.druid.stat.TableStat) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 62 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class SQLSelectBuilderImpl method from.

@Override
public SQLSelectBuilderImpl from(String table, String alias) {
    SQLSelectQueryBlock queryBlock = getQueryBlock();
    SQLExprTableSource from = new SQLExprTableSource(new SQLIdentifierExpr(table), alias);
    queryBlock.setFrom(from);
    return this;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 63 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class PGSelectParser method query.

@Override
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
    if (lexer.token() == Token.VALUES) {
        return valuesQuery(acceptUnion);
    }
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        if (select instanceof SQLSelectQueryBlock) {
            ((SQLSelectQueryBlock) select).setParenthesized(true);
        }
        accept(Token.RPAREN);
        return queryRest(select, acceptUnion);
    }
    PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
    if (lexer.hasComment() && lexer.isKeepComments()) {
        queryBlock.addBeforeComment(lexer.readAndResetComments());
    }
    if (lexer.token() == Token.SELECT) {
        lexer.nextToken();
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == Token.DISTINCT) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
            lexer.nextToken();
            if (lexer.token() == Token.ON) {
                lexer.nextToken();
                for (; ; ) {
                    SQLExpr expr = this.createExprParser().expr();
                    queryBlock.getDistinctOn().add(expr);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    } else {
                        break;
                    }
                }
            }
        } else if (lexer.token() == Token.ALL) {
            queryBlock.setDistionOption(SQLSetQuantifier.ALL);
            lexer.nextToken();
        }
        parseSelectList(queryBlock);
        if (lexer.token() == Token.INTO) {
            lexer.nextToken();
            if (lexer.token() == Token.TEMPORARY) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.TEMPORARY);
            } else if (lexer.token() == Token.TEMP) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.TEMP);
            } else if (lexer.token() == Token.UNLOGGED) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.UNLOGGED);
            }
            if (lexer.token() == Token.TABLE) {
                lexer.nextToken();
            }
            SQLExpr name = this.createExprParser().name();
            queryBlock.setInto(new SQLExprTableSource(name));
        }
    }
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    if (lexer.token() == Token.WINDOW) {
        this.parseWindow(queryBlock);
    }
    queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
    for (; ; ) {
        if (lexer.token() == Token.LIMIT) {
            SQLLimit limit = new SQLLimit();
            lexer.nextToken();
            if (lexer.token() == Token.ALL) {
                limit.setRowCount(new SQLIdentifierExpr("ALL"));
                lexer.nextToken();
            } else {
                limit.setRowCount(expr());
            }
            queryBlock.setLimit(limit);
        } else if (lexer.token() == Token.OFFSET) {
            SQLLimit limit = queryBlock.getLimit();
            if (limit == null) {
                limit = new SQLLimit();
                queryBlock.setLimit(limit);
            }
            lexer.nextToken();
            SQLExpr offset = expr();
            limit.setOffset(offset);
            if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
                lexer.nextToken();
            }
        } else {
            break;
        }
    }
    if (lexer.token() == Token.FETCH) {
        lexer.nextToken();
        PGSelectQueryBlock.FetchClause fetch = new PGSelectQueryBlock.FetchClause();
        if (lexer.token() == Token.FIRST) {
            fetch.setOption(PGSelectQueryBlock.FetchClause.Option.FIRST);
        } else if (lexer.token() == Token.NEXT) {
            fetch.setOption(PGSelectQueryBlock.FetchClause.Option.NEXT);
        } else {
            throw new ParserException("expect 'FIRST' or 'NEXT'. " + lexer.info());
        }
        SQLExpr count = expr();
        fetch.setCount(count);
        if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'ROW' or 'ROWS'. " + lexer.info());
        }
        if (lexer.token() == Token.ONLY) {
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'ONLY'. " + lexer.info());
        }
        queryBlock.setFetch(fetch);
    }
    if (lexer.token() == Token.FOR) {
        lexer.nextToken();
        PGSelectQueryBlock.ForClause forClause = new PGSelectQueryBlock.ForClause();
        if (lexer.token() == Token.UPDATE) {
            forClause.setOption(PGSelectQueryBlock.ForClause.Option.UPDATE);
            lexer.nextToken();
        } else if (lexer.token() == Token.SHARE) {
            forClause.setOption(PGSelectQueryBlock.ForClause.Option.SHARE);
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'FIRST' or 'NEXT'. " + lexer.info());
        }
        if (lexer.token() == Token.OF) {
            for (; ; ) {
                SQLExpr expr = this.createExprParser().expr();
                forClause.getOf().add(expr);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else {
                    break;
                }
            }
        }
        if (lexer.token() == Token.NOWAIT) {
            lexer.nextToken();
            forClause.setNoWait(true);
        } else if (lexer.identifierEquals(FnvHash.Constants.SKIP)) {
            lexer.nextToken();
            acceptIdentifier("LOCKED");
            forClause.setSkipLocked(true);
        }
        queryBlock.setForClause(forClause);
    }
    return queryRest(queryBlock, acceptUnion);
}
Also used : SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) PGSelectQueryBlock(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)

Example 64 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class SQLServerStatementParser method parseSet.

public SQLStatement parseSet() {
    accept(Token.SET);
    if (lexer.identifierEquals(FnvHash.Constants.TRANSACTION)) {
        lexer.nextToken();
        acceptIdentifier("ISOLATION");
        acceptIdentifier("LEVEL");
        SQLServerSetTransactionIsolationLevelStatement stmt = new SQLServerSetTransactionIsolationLevelStatement();
        if (lexer.identifierEquals("READ")) {
            lexer.nextToken();
            if (lexer.identifierEquals("UNCOMMITTED")) {
                stmt.setLevel("READ UNCOMMITTED");
                lexer.nextToken();
            } else if (lexer.identifierEquals("COMMITTED")) {
                stmt.setLevel("READ COMMITTED");
                lexer.nextToken();
            } else {
                throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal() + ", " + lexer.info());
            }
        } else if (lexer.identifierEquals("SERIALIZABLE")) {
            stmt.setLevel("SERIALIZABLE");
            lexer.nextToken();
        } else if (lexer.identifierEquals("SNAPSHOT")) {
            stmt.setLevel("SNAPSHOT");
            lexer.nextToken();
        } else if (lexer.identifierEquals("REPEATABLE")) {
            lexer.nextToken();
            if (lexer.identifierEquals("READ")) {
                stmt.setLevel("REPEATABLE READ");
                lexer.nextToken();
            } else {
                throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal() + ", " + lexer.info());
            }
        } else {
            throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal() + ", " + lexer.info());
        }
        return stmt;
    }
    if (lexer.identifierEquals(FnvHash.Constants.STATISTICS)) {
        lexer.nextToken();
        SQLSetStatement stmt = new SQLSetStatement();
        if (lexer.identifierEquals("IO") || lexer.identifierEquals("XML") || lexer.identifierEquals("PROFILE") || lexer.identifierEquals("TIME")) {
            SQLExpr target = new SQLIdentifierExpr("STATISTICS " + lexer.stringVal().toUpperCase());
            lexer.nextToken();
            if (lexer.token() == Token.ON) {
                stmt.set(target, new SQLIdentifierExpr("ON"));
                lexer.nextToken();
            } else if (lexer.identifierEquals(FnvHash.Constants.OFF)) {
                stmt.set(target, new SQLIdentifierExpr("OFF"));
                lexer.nextToken();
            }
        }
        return stmt;
    }
    if (lexer.identifierEquals(FnvHash.Constants.IDENTITY_INSERT)) {
        SQLSetStatement stmt = new SQLSetStatement();
        stmt.setOption(SQLSetStatement.Option.IDENTITY_INSERT);
        lexer.nextToken();
        SQLName table = this.exprParser.name();
        if (lexer.token() == Token.ON) {
            SQLExpr value = new SQLIdentifierExpr("ON");
            stmt.set(table, value);
            lexer.nextToken();
        } else if (lexer.identifierEquals(FnvHash.Constants.OFF)) {
            SQLExpr value = new SQLIdentifierExpr("OFF");
            stmt.set(table, value);
            lexer.nextToken();
        }
        return stmt;
    }
    if (lexer.token() == Token.VARIANT) {
        SQLSetStatement stmt = new SQLSetStatement(getDbType());
        parseAssignItems(stmt.getItems(), stmt);
        return stmt;
    } else {
        SQLSetStatement stmt = new SQLSetStatement();
        SQLExpr target = this.exprParser.expr();
        if (lexer.token() == Token.ON) {
            stmt.set(target, new SQLIdentifierExpr("ON"));
            lexer.nextToken();
        } else if (lexer.identifierEquals("OFF")) {
            stmt.set(target, new SQLIdentifierExpr("OFF"));
            lexer.nextToken();
        } else {
            stmt.set(target, this.exprParser.expr());
        }
        return stmt;
    }
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 65 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class SchemaRepository method findTable.

public SchemaObject findTable(String tableName) {
    if (tableName.indexOf('.') != -1) {
        SQLExpr expr = SQLUtils.toSQLExpr(tableName, dbType);
        if (!(expr instanceof SQLIdentifierExpr)) {
            return findTable((SQLName) expr);
        }
    }
    SchemaObject object = getDefaultSchema().findTable(tableName);
    if (object != null) {
        return object;
    }
    String ddl = loadDDL(tableName);
    if (ddl == null) {
        return null;
    }
    DbType schemaDbType = this.schemaDbType;
    if (schemaDbType == null) {
        schemaDbType = dbType;
    }
    SchemaObject schemaObject = acceptDDL(ddl, schemaDbType);
    if (schemaObject != null) {
        return schemaObject;
    }
    return getDefaultSchema().findTable(tableName);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) DbType(com.alibaba.druid.DbType)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)152 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)68 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)45 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)19 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)17 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)16 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)15 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)13 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)10 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)10 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)9 ArrayList (java.util.ArrayList)9 SQLName (com.alibaba.druid.sql.ast.SQLName)8