Search in sources :

Example 1 with SQLQueryExpr

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

the class MySqlStatementParser method parserParameters.

/**
     * parse create procedure parameters
     * 
     * @param parameters
     */
private void parserParameters(List<SQLParameter> parameters) {
    if (lexer.token() == Token.RPAREN) {
        return;
    }
    for (; ; ) {
        SQLParameter parameter = new SQLParameter();
        if (lexer.token() == Token.CURSOR) {
            lexer.nextToken();
            parameter.setName(this.exprParser.name());
            accept(Token.IS);
            SQLSelect select = this.createSQLSelectParser().select();
            SQLDataTypeImpl dataType = new SQLDataTypeImpl();
            dataType.setName("CURSOR");
            parameter.setDataType(dataType);
            parameter.setDefaultValue(new SQLQueryExpr(select));
        } else if (lexer.token() == Token.IN || lexer.token() == Token.OUT || lexer.token() == Token.INOUT) {
            if (lexer.token() == Token.IN) {
                parameter.setParamType(ParameterType.IN);
            } else if (lexer.token() == Token.OUT) {
                parameter.setParamType(ParameterType.OUT);
            } else if (lexer.token() == Token.INOUT) {
                parameter.setParamType(ParameterType.INOUT);
            }
            lexer.nextToken();
            parameter.setName(this.exprParser.name());
            parameter.setDataType(this.exprParser.parseDataType());
        } else {
            // default parameter type is in
            parameter.setParamType(ParameterType.DEFAULT);
            parameter.setName(this.exprParser.name());
            parameter.setDataType(this.exprParser.parseDataType());
            if (lexer.token() == Token.COLONEQ) {
                lexer.nextToken();
                parameter.setDefaultValue(this.exprParser.expr());
            }
        }
        parameters.add(parameter);
        if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI) {
            lexer.nextToken();
        }
        if (lexer.token() != Token.BEGIN && lexer.token() != Token.RPAREN) {
            continue;
        }
        break;
    }
}
Also used : SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLParameter(com.alibaba.druid.sql.ast.SQLParameter) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl)

Example 2 with SQLQueryExpr

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

the class SQLServerStatementParser method parseInsert0.

protected void parseInsert0(SQLInsertInto insert, boolean acceptSubQuery) {
    SQLServerInsertStatement insertStatement = (SQLServerInsertStatement) insert;
    SQLServerTop top = this.getExprParser().parseTop();
    if (top != null) {
        insertStatement.setTop(top);
    }
    if (lexer.token() == Token.INTO) {
        lexer.nextToken();
    }
    SQLName tableName = this.exprParser.name();
    insertStatement.setTableName(tableName);
    if (lexer.token() == Token.LITERAL_ALIAS) {
        insertStatement.setAlias(as());
    }
    parseInsert0_hinits(insertStatement);
    if (lexer.token() == Token.IDENTIFIER && !lexer.stringVal().equalsIgnoreCase("OUTPUT")) {
        insertStatement.setAlias(lexer.stringVal());
        lexer.nextToken();
    }
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        this.exprParser.exprList(insertStatement.getColumns(), insertStatement);
        accept(Token.RPAREN);
    }
    SQLServerOutput output = this.getExprParser().parserOutput();
    if (output != null) {
        insertStatement.setOutput(output);
    }
    if (lexer.token() == Token.VALUES) {
        lexer.nextToken();
        for (; ; ) {
            accept(Token.LPAREN);
            SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause();
            this.exprParser.exprList(values.getValues(), values);
            insertStatement.getValuesList().add(values);
            accept(Token.RPAREN);
            if (!parseCompleteValues && insertStatement.getValuesList().size() >= parseValuesSize) {
                lexer.skipToEOF();
                break;
            }
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            } else {
                break;
            }
        }
    } else if (acceptSubQuery && (lexer.token() == Token.SELECT || lexer.token() == Token.LPAREN)) {
        SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
        insertStatement.setQuery(queryExpr.getSubQuery());
    } else if (lexer.token() == Token.DEFAULT) {
        lexer.nextToken();
        accept(Token.VALUES);
        insertStatement.setDefaultValues(true);
    }
}
Also used : SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement) SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLServerOutput(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput)

Example 3 with SQLQueryExpr

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

the class WallVisitorUtils method getValue.

public static Object getValue(WallVisitor visitor, SQLExpr x) {
    if (x != null && x.getAttributes().containsKey(EVAL_VALUE)) {
        return getValueFromAttributes(visitor, x);
    }
    if (x instanceof SQLBinaryOpExpr) {
        return getValue(visitor, (SQLBinaryOpExpr) x);
    }
    if (x instanceof SQLBooleanExpr) {
        return ((SQLBooleanExpr) x).getValue();
    }
    if (x instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) x).getNumber();
    }
    if (x instanceof SQLCharExpr) {
        return ((SQLCharExpr) x).getText();
    }
    if (x instanceof SQLNCharExpr) {
        return ((SQLNCharExpr) x).getText();
    }
    if (x instanceof SQLNotExpr) {
        Object result = getValue(visitor, ((SQLNotExpr) x).getExpr());
        if (result instanceof Boolean) {
            return !((Boolean) result).booleanValue();
        }
    }
    if (x instanceof SQLQueryExpr) {
        if (isSimpleCountTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
            return Integer.valueOf(1);
        }
        if (isSimpleCaseTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
            SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) ((SQLQueryExpr) x).getSubQuery().getQuery();
            SQLCaseExpr caseExpr = (SQLCaseExpr) queryBlock.getSelectList().get(0).getExpr();
            Object result = getValue(caseExpr);
            if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
                boolean leftIsName = false;
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    SQLExpr left = ((SQLBinaryOpExpr) x.getParent()).getLeft();
                    if (left instanceof SQLName) {
                        leftIsName = true;
                    }
                }
                if (!leftIsName && result != null) {
                    addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
                }
            }
            return result;
        }
    }
    String dbType = null;
    if (visitor != null) {
        dbType = visitor.getDbType();
    }
    if (//
    x instanceof SQLMethodInvokeExpr || //
    x instanceof SQLBetweenExpr || //
    x instanceof SQLInListExpr || //
    x instanceof SQLUnaryExpr) {
        return eval(visitor, dbType, x, Collections.emptyList());
    }
    if (x instanceof SQLCaseExpr) {
        if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
            SQLCaseExpr caseExpr = (SQLCaseExpr) x;
            boolean leftIsName = false;
            if (caseExpr.getParent() instanceof SQLBinaryOpExpr) {
                SQLExpr left = ((SQLBinaryOpExpr) caseExpr.getParent()).getLeft();
                if (left instanceof SQLName) {
                    leftIsName = true;
                }
            }
            if (!leftIsName && caseExpr.getValueExpr() == null && caseExpr.getItems().size() > 0) {
                SQLCaseExpr.Item item = caseExpr.getItems().get(0);
                Object conditionVal = getValue(visitor, item.getConditionExpr());
                Object itemVal = getValue(visitor, item.getValueExpr());
                if (conditionVal instanceof Boolean && itemVal != null) {
                    addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
                }
            }
        }
        return eval(visitor, dbType, x, Collections.emptyList());
    }
    return null;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) Item(com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLCaseExpr(com.alibaba.druid.sql.ast.expr.SQLCaseExpr)

Example 4 with SQLQueryExpr

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

the class OracleStatementParser method parserParameters.

private void parserParameters(List<SQLParameter> parameters) {
    for (; ; ) {
        SQLParameter parameter = new SQLParameter();
        if (lexer.token() == Token.CURSOR) {
            lexer.nextToken();
            parameter.setName(this.exprParser.name());
            accept(Token.IS);
            SQLSelect select = this.createSQLSelectParser().select();
            SQLDataTypeImpl dataType = new SQLDataTypeImpl();
            dataType.setName("CURSOR");
            parameter.setDataType(dataType);
            parameter.setDefaultValue(new SQLQueryExpr(select));
        } else {
            parameter.setName(this.exprParser.name());
            parameter.setDataType(this.exprParser.parseDataType());
            if (lexer.token() == Token.COLONEQ) {
                lexer.nextToken();
                parameter.setDefaultValue(this.exprParser.expr());
            }
        }
        parameters.add(parameter);
        if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI) {
            lexer.nextToken();
        }
        if (lexer.token() != Token.BEGIN && lexer.token() != Token.RPAREN) {
            continue;
        }
        break;
    }
}
Also used : SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLParameter(com.alibaba.druid.sql.ast.SQLParameter) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl)

Example 5 with SQLQueryExpr

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

the class PGSQLStatementParser method parseInsert.

public PGInsertStatement parseInsert() {
    PGInsertStatement stmt = new PGInsertStatement();
    if (lexer.token() == Token.INSERT) {
        lexer.nextToken();
        accept(Token.INTO);
        SQLName tableName = this.exprParser.name();
        stmt.setTableName(tableName);
        if (lexer.token() == Token.IDENTIFIER) {
            stmt.setAlias(lexer.stringVal());
            lexer.nextToken();
        }
    }
    if (lexer.token() == Token.DEFAULT) {
        lexer.nextToken();
        accept(Token.VALUES);
        stmt.setDefaultValues(true);
    }
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getColumns(), stmt);
        accept(Token.RPAREN);
    }
    if (lexer.token() == (Token.VALUES)) {
        lexer.nextToken();
        for (; ; ) {
            accept(Token.LPAREN);
            SQLInsertStatement.ValuesClause valuesCaluse = new SQLInsertStatement.ValuesClause();
            this.exprParser.exprList(valuesCaluse.getValues(), valuesCaluse);
            stmt.addValueCause(valuesCaluse);
            accept(Token.RPAREN);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    } else if (lexer.token() == (Token.SELECT)) {
        SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
        stmt.setQuery(queryExpr.getSubQuery());
    }
    if (lexer.token() == Token.RETURNING) {
        lexer.nextToken();
        SQLExpr returning = this.exprParser.expr();
        stmt.setReturning(returning);
    }
    return stmt;
}
Also used : SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) PGInsertStatement(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGInsertStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)8 SQLName (com.alibaba.druid.sql.ast.SQLName)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLParameter (com.alibaba.druid.sql.ast.SQLParameter)2 SQLBetweenExpr (com.alibaba.druid.sql.ast.expr.SQLBetweenExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLCaseExpr (com.alibaba.druid.sql.ast.expr.SQLCaseExpr)1 Item (com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLExistsExpr (com.alibaba.druid.sql.ast.expr.SQLExistsExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)1 SQLNotExpr (com.alibaba.druid.sql.ast.expr.SQLNotExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLUnaryExpr (com.alibaba.druid.sql.ast.expr.SQLUnaryExpr)1