Search in sources :

Example 6 with SQLExpr

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

the class MySqlExprParser method parseInterval.

protected SQLExpr parseInterval() {
    accept(Token.INTERVAL);
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr("INTERVAL");
        if (lexer.token() != Token.RPAREN) {
            exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
        }
        accept(Token.RPAREN);
        if (// 
        methodInvokeExpr.getParameters().size() == 1 && lexer.token() == Token.IDENTIFIER) {
            SQLExpr value = methodInvokeExpr.getParameters().get(0);
            String unit = lexer.stringVal();
            lexer.nextToken();
            MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr();
            intervalExpr.setValue(value);
            intervalExpr.setUnit(MySqlIntervalUnit.valueOf(unit.toUpperCase()));
            return intervalExpr;
        } else {
            return primaryRest(methodInvokeExpr);
        }
    } else {
        SQLExpr value = expr();
        if (lexer.token() != Token.IDENTIFIER) {
            throw new ParserException("Syntax error");
        }
        String unit = lexer.stringVal();
        lexer.nextToken();
        MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr();
        intervalExpr.setValue(value);
        intervalExpr.setUnit(MySqlIntervalUnit.valueOf(unit.toUpperCase()));
        return intervalExpr;
    }
}
Also used : MySqlIntervalExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 7 with SQLExpr

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

the class MySqlExprParser method parsePartition.

public SQLPartition parsePartition() {
    accept(Token.PARTITION);
    SQLPartition partitionDef = new SQLPartition();
    partitionDef.setName(this.name());
    SQLPartitionValue values = this.parsePartitionValues();
    if (values != null) {
        partitionDef.setValues(values);
    }
    for (; ; ) {
        boolean storage = false;
        if (identifierEquals("DATA")) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            partitionDef.setDataDirectory(this.expr());
        } else if (lexer.token() == Token.TABLESPACE) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLName tableSpace = this.name();
            partitionDef.setTableSpace(tableSpace);
        } else if (lexer.token() == Token.INDEX) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            partitionDef.setIndexDirectory(this.expr());
        } else if (identifierEquals("MAX_ROWS")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr maxRows = this.primary();
            partitionDef.setMaxRows(maxRows);
        } else if (identifierEquals("MIN_ROWS")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr minRows = this.primary();
            partitionDef.setMaxRows(minRows);
        } else if (//
        identifierEquals("ENGINE") || (storage = (lexer.token() == Token.STORAGE || identifierEquals("STORAGE")))) {
            if (storage) {
                lexer.nextToken();
            }
            acceptIdentifier("ENGINE");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLName engine = this.name();
            partitionDef.setEngine(engine);
        } else if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr comment = this.primary();
            partitionDef.setComment(comment);
        } else {
            break;
        }
    }
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        for (; ; ) {
            acceptIdentifier("SUBPARTITION");
            SQLName subPartitionName = this.name();
            SQLSubPartition subPartition = new SQLSubPartition();
            subPartition.setName(subPartitionName);
            partitionDef.addSubPartition(subPartition);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
        accept(Token.RPAREN);
    }
    return partitionDef;
}
Also used : SQLPartition(com.alibaba.druid.sql.ast.SQLPartition) SQLSubPartition(com.alibaba.druid.sql.ast.SQLSubPartition) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLPartitionValue(com.alibaba.druid.sql.ast.SQLPartitionValue) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 8 with SQLExpr

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

the class MySqlExprParser method primaryRest.

public final SQLExpr primaryRest(SQLExpr expr) {
    if (expr == null) {
        throw new IllegalArgumentException("expr");
    }
    if (lexer.token() == Token.LITERAL_CHARS) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equalsIgnoreCase("x")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                expr = new SQLHexExpr(charValue);
                return primaryRest(expr);
            } else if (ident.equalsIgnoreCase("b")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                expr = new SQLBinaryExpr(charValue);
                return primaryRest(expr);
            } else if (ident.startsWith("_")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                MySqlCharExpr mysqlCharExpr = new MySqlCharExpr(charValue);
                mysqlCharExpr.setCharset(identExpr.getName());
                if (identifierEquals("COLLATE")) {
                    lexer.nextToken();
                    String collate = lexer.stringVal();
                    mysqlCharExpr.setCollate(collate);
                    accept(Token.IDENTIFIER);
                }
                expr = mysqlCharExpr;
                return primaryRest(expr);
            }
        } else if (expr instanceof SQLCharExpr) {
            SQLMethodInvokeExpr concat = new SQLMethodInvokeExpr("CONCAT");
            concat.addParameter(expr);
            do {
                String chars = lexer.stringVal();
                concat.addParameter(new SQLCharExpr(chars));
                lexer.nextToken();
            } while (lexer.token() == Token.LITERAL_CHARS || lexer.token() == Token.LITERAL_ALIAS);
            expr = concat;
        }
    } else if (lexer.token() == Token.IDENTIFIER) {
        if (expr instanceof SQLHexExpr) {
            if ("USING".equalsIgnoreCase(lexer.stringVal())) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error, illegal hex");
                }
                String charSet = lexer.stringVal();
                lexer.nextToken();
                expr.getAttributes().put("USING", charSet);
                return primaryRest(expr);
            }
        } else if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("syntax error");
            }
            String collate = lexer.stringVal();
            lexer.nextToken();
            SQLBinaryOpExpr binaryExpr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.COLLATE, new SQLIdentifierExpr(collate), JdbcConstants.MYSQL);
            expr = binaryExpr;
            return primaryRest(expr);
        } else if (expr instanceof SQLVariantRefExpr) {
            if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error");
                }
                String collate = lexer.stringVal();
                lexer.nextToken();
                expr.putAttribute("COLLATE", collate);
                return primaryRest(expr);
            }
        } else if (expr instanceof SQLIntegerExpr) {
            SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
            String binaryString = lexer.stringVal();
            if (intExpr.getNumber().intValue() == 0 && binaryString.startsWith("b")) {
                lexer.nextToken();
                expr = new SQLBinaryExpr(binaryString.substring(1));
                return primaryRest(expr);
            }
        }
    }
    if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
        String ident = identExpr.getName();
        if ("EXTRACT".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("syntax error");
            }
            String unitVal = lexer.stringVal();
            MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal.toUpperCase());
            lexer.nextToken();
            accept(Token.FROM);
            SQLExpr value = expr();
            MySqlExtractExpr extract = new MySqlExtractExpr();
            extract.setValue(value);
            extract.setUnit(unit);
            accept(Token.RPAREN);
            expr = extract;
            return primaryRest(expr);
        } else if ("SUBSTRING".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            for (; ; ) {
                SQLExpr param = expr();
                methodInvokeExpr.addParameter(param);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else if (lexer.token() == Token.FROM) {
                    lexer.nextToken();
                    SQLExpr from = expr();
                    methodInvokeExpr.addParameter(from);
                    if (lexer.token() == Token.FOR) {
                        lexer.nextToken();
                        SQLExpr forExpr = expr();
                        methodInvokeExpr.addParameter(forExpr);
                    }
                    break;
                } else if (lexer.token() == Token.RPAREN) {
                    break;
                } else {
                    throw new ParserException("syntax error");
                }
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("TRIM".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            if (lexer.token() == Token.IDENTIFIER) {
                String flagVal = lexer.stringVal();
                if ("LEADING".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.getAttributes().put("TRIM_TYPE", "LEADING");
                } else if ("BOTH".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.getAttributes().put("TRIM_TYPE", "BOTH");
                } else if ("TRAILING".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.putAttribute("TRIM_TYPE", "TRAILING");
                }
            }
            SQLExpr param = expr();
            methodInvokeExpr.addParameter(param);
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLExpr from = expr();
                methodInvokeExpr.putAttribute("FROM", from);
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("MATCH".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();
            if (lexer.token() == Token.RPAREN) {
                lexer.nextToken();
            } else {
                exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
                accept(Token.RPAREN);
            }
            acceptIdentifier("AGAINST");
            accept(Token.LPAREN);
            SQLExpr against = primary();
            matchAgainstExpr.setAgainst(against);
            if (lexer.token() == Token.IN) {
                lexer.nextToken();
                if (identifierEquals("NATURAL")) {
                    lexer.nextToken();
                    acceptIdentifier("LANGUAGE");
                    acceptIdentifier("MODE");
                    if (lexer.token() == Token.WITH) {
                        lexer.nextToken();
                        acceptIdentifier("QUERY");
                        acceptIdentifier("EXPANSION");
                        matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
                    } else {
                        matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE);
                    }
                } else if (identifierEquals("BOOLEAN")) {
                    lexer.nextToken();
                    acceptIdentifier("MODE");
                    matchAgainstExpr.setSearchModifier(SearchModifier.IN_BOOLEAN_MODE);
                } else {
                    throw new ParserException("TODO");
                }
            } else if (lexer.token() == Token.WITH) {
                throw new ParserException("TODO");
            }
            accept(Token.RPAREN);
            expr = matchAgainstExpr;
            return primaryRest(expr);
        } else if (("CONVERT".equalsIgnoreCase(ident)) || ("CHAR".equalsIgnoreCase(ident))) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            if (lexer.token() != Token.RPAREN) {
                exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
            }
            if (identifierEquals("USING")) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error");
                }
                String charset = lexer.stringVal();
                lexer.nextToken();
                methodInvokeExpr.putAttribute("USING", charset);
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("POSITION".equalsIgnoreCase(ident)) {
            accept(Token.LPAREN);
            SQLExpr subStr = this.primary();
            accept(Token.IN);
            SQLExpr str = this.expr();
            accept(Token.RPAREN);
            SQLMethodInvokeExpr locate = new SQLMethodInvokeExpr("LOCATE");
            locate.addParameter(subStr);
            locate.addParameter(str);
            expr = locate;
            return primaryRest(expr);
        }
    }
    if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) {
        lexer.nextToken();
        MySqlUserName userName = new MySqlUserName();
        if (expr instanceof SQLCharExpr) {
            userName.setUserName(((SQLCharExpr) expr).toString());
        } else {
            userName.setUserName(((SQLIdentifierExpr) expr).getName());
        }
        if (lexer.token() == Token.LITERAL_CHARS) {
            userName.setHost("'" + lexer.stringVal() + "'");
        } else {
            userName.setHost(lexer.stringVal());
        }
        lexer.nextToken();
        return userName;
    }
    if (lexer.token() == Token.ERROR) {
        throw new ParserException("syntax error, token: " + lexer.token() + " " + lexer.stringVal() + ", pos : " + lexer.pos());
    }
    return super.primaryRest(expr);
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLBinaryExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryExpr) SQLHexExpr(com.alibaba.druid.sql.ast.expr.SQLHexExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) MySqlMatchAgainstExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MySqlIntervalUnit(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalUnit) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) MySqlExtractExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr) MySqlUserName(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName)

Example 9 with SQLExpr

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

the class MySqlStatementParser method parseShowColumns.

private MySqlShowColumnsStatement parseShowColumns() {
    MySqlShowColumnsStatement stmt = new MySqlShowColumnsStatement();
    if (lexer.token() == Token.FROM) {
        lexer.nextToken();
        SQLName table = exprParser.name();
        stmt.setTable(table);
        if (lexer.token() == Token.FROM || lexer.token() == Token.IN) {
            lexer.nextToken();
            SQLName database = exprParser.name();
            stmt.setDatabase(database);
        }
    }
    if (lexer.token() == Token.LIKE) {
        lexer.nextToken();
        SQLExpr like = exprParser.expr();
        stmt.setLike(like);
    }
    if (lexer.token() == Token.WHERE) {
        lexer.nextToken();
        SQLExpr where = exprParser.expr();
        stmt.setWhere(where);
    }
    return stmt;
}
Also used : MySqlShowColumnsStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 10 with SQLExpr

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

the class MySqlStatementParser method parseValueClause.

private void parseValueClause(List<ValuesClause> valueClauseList, int columnSize) {
    for (int i = 0; ; ++i) {
        if (lexer.token() != Token.LPAREN) {
            throw new ParserException("syntax error, expect ')'");
        }
        lexer.nextTokenValue();
        if (lexer.token() != Token.RPAREN) {
            List<SQLExpr> valueExprList;
            if (columnSize > 0) {
                valueExprList = new ArrayList<SQLExpr>(columnSize);
            } else {
                valueExprList = new ArrayList<SQLExpr>();
            }
            for (; ; ) {
                SQLExpr expr;
                if (lexer.token() == Token.LITERAL_INT) {
                    expr = new SQLIntegerExpr(lexer.integerValue());
                    lexer.nextTokenComma();
                } else if (lexer.token() == Token.LITERAL_CHARS) {
                    expr = new SQLCharExpr(lexer.stringVal());
                    lexer.nextTokenComma();
                } else if (lexer.token() == Token.LITERAL_NCHARS) {
                    expr = new SQLNCharExpr(lexer.stringVal());
                    lexer.nextTokenComma();
                } else {
                    expr = exprParser.expr();
                }
                if (lexer.token() == Token.COMMA) {
                    valueExprList.add(expr);
                    lexer.nextTokenValue();
                    continue;
                } else if (lexer.token() == Token.RPAREN) {
                    valueExprList.add(expr);
                    break;
                } else {
                    expr = this.exprParser.primaryRest(expr);
                    if (lexer.token() != Token.COMMA && lexer.token() != Token.RPAREN) {
                        expr = this.exprParser.exprRest(expr);
                    }
                    valueExprList.add(expr);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    } else {
                        break;
                    }
                }
            }
            SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(valueExprList);
            valueClauseList.add(values);
        } else {
            SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(new ArrayList<SQLExpr>(0));
            valueClauseList.add(values);
        }
        if (lexer.token() != Token.RPAREN) {
            throw new ParserException("syntax error");
        }
        if (!parseCompleteValues && valueClauseList.size() >= parseValuesSize) {
            lexer.skipToEOF();
            break;
        }
        lexer.nextTokenComma();
        if (lexer.token() == Token.COMMA) {
            lexer.nextTokenLParen();
            continue;
        } else {
            break;
        }
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

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