Search in sources :

Example 16 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class SQLUtils method addSelectItem.

public static String addSelectItem(String selectSql, String expr, String alias, boolean first, String dbType) {
    List<SQLStatement> stmtList = parseStatements(selectSql, dbType);
    if (stmtList.size() == 0) {
        throw new IllegalArgumentException("not support empty-statement :" + selectSql);
    }
    if (stmtList.size() > 1) {
        throw new IllegalArgumentException("not support multi-statement :" + selectSql);
    }
    SQLStatement stmt = stmtList.get(0);
    SQLExpr columnExpr = toSQLExpr(expr, dbType);
    addSelectItem(stmt, columnExpr, alias, first);
    return toSQLString(stmt, dbType);
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 17 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class MySqlExprParser method additiveRest.

public SQLExpr additiveRest(SQLExpr expr) {
    if (lexer.token() == Token.PLUS) {
        lexer.nextToken();
        SQLExpr rightExp = multiplicative();
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Add, rightExp, JdbcConstants.MYSQL);
        expr = additiveRest(expr);
    } else if (lexer.token() == Token.SUB) {
        lexer.nextToken();
        SQLExpr rightExp = multiplicative();
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Subtract, rightExp, JdbcConstants.MYSQL);
        expr = additiveRest(expr);
    }
    return expr;
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 18 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class MySqlExprParser method parseAssignItem.

public SQLAssignItem parseAssignItem() {
    SQLAssignItem item = new SQLAssignItem();
    SQLExpr var = primary();
    String ident = null;
    if (var instanceof SQLIdentifierExpr) {
        ident = ((SQLIdentifierExpr) var).getName();
        if ("GLOBAL".equalsIgnoreCase(ident)) {
            ident = lexer.stringVal();
            lexer.nextToken();
            var = new SQLVariantRefExpr(ident, true);
        } else if ("SESSION".equalsIgnoreCase(ident)) {
            ident = lexer.stringVal();
            lexer.nextToken();
            var = new SQLVariantRefExpr(ident, false);
        } else {
            var = new SQLVariantRefExpr(ident);
        }
    }
    if ("NAMES".equalsIgnoreCase(ident)) {
    // skip
    } else if ("CHARACTER".equalsIgnoreCase(ident)) {
        var = new SQLIdentifierExpr("CHARACTER SET");
        accept(Token.SET);
        if (lexer.token() == Token.EQ) {
            lexer.nextToken();
        }
    } else {
        if (lexer.token() == Token.COLONEQ) {
            lexer.nextToken();
        } else {
            accept(Token.EQ);
        }
    }
    if (lexer.token() == Token.ON) {
        lexer.nextToken();
        item.setValue(new SQLIdentifierExpr("ON"));
    } else {
        item.setValue(this.expr());
    }
    item.setTarget(var);
    return item;
}
Also used : SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 19 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class MySqlExprParser method notRationalRest.

public SQLExpr notRationalRest(SQLExpr expr) {
    if (identifierEquals("REGEXP")) {
        lexer.nextToken();
        SQLExpr rightExp = primary();
        rightExp = relationalRest(rightExp);
        return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRegExp, rightExp, JdbcConstants.MYSQL);
    }
    return super.notRationalRest(expr);
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 20 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class MySqlExprParser method primary.

public SQLExpr primary() {
    final Token tok = lexer.token();
    if (identifierEquals("outfile")) {
        lexer.nextToken();
        SQLExpr file = primary();
        SQLExpr expr = new MySqlOutFileExpr(file);
        return primaryRest(expr);
    }
    switch(tok) {
        case LITERAL_ALIAS:
            String aliasValue = lexer.stringVal();
            lexer.nextToken();
            return primaryRest(new SQLCharExpr(aliasValue));
        case VARIANT:
            SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal());
            lexer.nextToken();
            if (varRefExpr.getName().equalsIgnoreCase("@@global")) {
                accept(Token.DOT);
                varRefExpr = new SQLVariantRefExpr(lexer.stringVal(), true);
                lexer.nextToken();
            } else if (varRefExpr.getName().equals("@") && lexer.token() == Token.LITERAL_CHARS) {
                varRefExpr.setName("@'" + lexer.stringVal() + "'");
                lexer.nextToken();
            } else if (varRefExpr.getName().equals("@@") && lexer.token() == Token.LITERAL_CHARS) {
                varRefExpr.setName("@@'" + lexer.stringVal() + "'");
                lexer.nextToken();
            }
            return primaryRest(varRefExpr);
        case VALUES:
            lexer.nextToken();
            if (lexer.token() != Token.LPAREN) {
                throw new ParserException("syntax error, illegal values clause");
            }
            return this.methodRest(new SQLIdentifierExpr("VALUES"), true);
        case BINARY:
            lexer.nextToken();
            if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI || lexer.token() == Token.EOF) {
                return new SQLIdentifierExpr("BINARY");
            } else {
                SQLUnaryExpr binaryExpr = new SQLUnaryExpr(SQLUnaryOperator.BINARY, expr());
                return primaryRest(binaryExpr);
            }
        case CACHE:
        case GROUP:
            lexer.nextToken();
            return primaryRest(new SQLIdentifierExpr(lexer.stringVal()));
        default:
            return super.primary();
    }
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) Token(com.alibaba.druid.sql.parser.Token) MySqlOutFileExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)422 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)71 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)68 SQLName (com.alibaba.druid.sql.ast.SQLName)47 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)33 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)32 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)30 ArrayList (java.util.ArrayList)30 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)28 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)25 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)25 ParserException (com.alibaba.druid.sql.parser.ParserException)25 SQLObject (com.alibaba.druid.sql.ast.SQLObject)24 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)23 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)20 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)17 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)16 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)15 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)15