Search in sources :

Example 21 with SQLCharExpr

use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLInsertVisitor method supplyAutoIncrementColumn.

private void supplyAutoIncrementColumn(final Collection<String> autoIncrementColumns, final String tableName, final List<SQLExpr> columns, final List<SQLExpr> values) {
    boolean isPreparedStatement = !getParameters().isEmpty();
    GeneratedKeyContext generatedKeyContext = getParseContext().getParsedResult().getGeneratedKeyContext();
    if (isPreparedStatement) {
        generatedKeyContext.getColumns().addAll(autoIncrementColumns);
    }
    TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName);
    for (String each : autoIncrementColumns) {
        SQLExpr sqlExpr;
        Object id = tableRule.generateId(each);
        generatedKeyContext.putValue(each, id);
        if (isPreparedStatement) {
            sqlExpr = new SQLVariantRefExpr("?");
            getParameters().add(id);
            ((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1);
        } else {
            sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id);
        }
        getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters());
        columns.add(new SQLIdentifierExpr(each));
        values.add(sqlExpr);
    }
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) GeneratedKeyContext(com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 22 with SQLCharExpr

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

the class PGExprParser method parseInterval.

@Override
protected SQLExpr parseInterval() {
    accept(Token.INTERVAL);
    PGIntervalExpr intervalExpr = new PGIntervalExpr();
    if (lexer.token() != Token.LITERAL_CHARS) {
        return new SQLIdentifierExpr("INTERVAL");
    }
    intervalExpr.setValue(new SQLCharExpr(lexer.stringVal()));
    lexer.nextToken();
    return intervalExpr;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) PGIntervalExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGIntervalExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 23 with SQLCharExpr

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

the class SQLStatementParser method parseCreateView.

public SQLCreateViewStatement parseCreateView() {
    SQLCreateViewStatement createView = new SQLCreateViewStatement(getDbType());
    if (lexer.token() == Token.CREATE) {
        lexer.nextToken();
    }
    if (lexer.token() == Token.OR) {
        lexer.nextToken();
        accept(Token.REPLACE);
        createView.setOrReplace(true);
    }
    if (identifierEquals("ALGORITHM")) {
        lexer.nextToken();
        accept(Token.EQ);
        String algorithm = lexer.stringVal();
        createView.setAlgorithm(algorithm);
        lexer.nextToken();
    }
    if (identifierEquals("DEFINER")) {
        lexer.nextToken();
        accept(Token.EQ);
        SQLName definer = this.exprParser.name();
        createView.setDefiner(definer);
        lexer.nextToken();
    }
    if (identifierEquals("SQL")) {
        lexer.nextToken();
        acceptIdentifier("SECURITY");
        String sqlSecurity = lexer.stringVal();
        createView.setSqlSecurity(sqlSecurity);
        lexer.nextToken();
    }
    this.accept(Token.VIEW);
    if (lexer.token() == Token.IF || identifierEquals("IF")) {
        lexer.nextToken();
        accept(Token.NOT);
        accept(Token.EXISTS);
        createView.setIfNotExists(true);
    }
    createView.setName(exprParser.name());
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        for (; ; ) {
            SQLCreateViewStatement.Column column = new SQLCreateViewStatement.Column();
            SQLExpr expr = this.exprParser.expr();
            column.setExpr(expr);
            if (lexer.token() == Token.COMMENT) {
                lexer.nextToken();
                column.setComment((SQLCharExpr) exprParser.primary());
            }
            column.setParent(createView);
            createView.addColumn(column);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
            } else {
                break;
            }
        }
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
        SQLCharExpr comment = (SQLCharExpr) exprParser.primary();
        createView.setComment(comment);
    }
    this.accept(Token.AS);
    createView.setSubQuery(new SQLSelectParser(this.exprParser).select());
    return createView;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 24 with SQLCharExpr

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

the class OracleExprParser method methodRest.

@Override
protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
    if (acceptLPAREN) {
        accept(Token.LPAREN);
    }
    if (lexer.token() == Token.PLUS) {
        lexer.nextToken();
        accept(Token.RPAREN);
        return new OracleOuterExpr(expr);
    }
    if (expr instanceof SQLIdentifierExpr) {
        String methodName = ((SQLIdentifierExpr) expr).getName();
        SQLMethodInvokeExpr methodExpr = new SQLMethodInvokeExpr(methodName);
        if ("trim".equalsIgnoreCase(methodName)) {
            if (//
            identifierEquals("LEADING") || //
            identifierEquals("TRAILING") || identifierEquals("BOTH")) {
                methodExpr.putAttribute("trim_option", lexer.stringVal());
                lexer.nextToken();
            }
            if (lexer.token() != Token.FROM) {
                SQLExpr trim_character = this.primary();
                trim_character.setParent(methodExpr);
                if (trim_character instanceof SQLCharExpr) {
                    methodExpr.putAttribute("trim_character", trim_character);
                } else {
                    methodExpr.getParameters().add(trim_character);
                }
            }
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLExpr trim_source = this.expr();
                methodExpr.addParameter(trim_source);
            }
            accept(Token.RPAREN);
            return primaryRest(methodExpr);
        }
    }
    return super.methodRest(expr, false);
}
Also used : OracleOuterExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleOuterExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 25 with SQLCharExpr

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

the class WallVisitorUtils method check.

public static boolean check(WallVisitor visitor, SQLBinaryOpExpr x) {
    if (x.getOperator() == SQLBinaryOperator.BooleanOr || x.getOperator() == SQLBinaryOperator.BooleanAnd) {
        List<SQLExpr> groupList = SQLUtils.split(x);
        for (SQLExpr item : groupList) {
            item.accept(visitor);
        }
        return false;
    }
    if (x.getOperator() == SQLBinaryOperator.Add || x.getOperator() == SQLBinaryOperator.Concat) {
        List<SQLExpr> groupList = SQLUtils.split(x);
        if (groupList.size() >= 4) {
            int chrCount = 0;
            for (int i = 0; i < groupList.size(); ++i) {
                SQLExpr item = groupList.get(i);
                if (item instanceof SQLMethodInvokeExpr) {
                    SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) item;
                    String methodName = methodExpr.getMethodName().toLowerCase();
                    if ("chr".equals(methodName) || "char".equals(methodName)) {
                        if (methodExpr.getParameters().get(0) instanceof SQLLiteralExpr) {
                            chrCount++;
                        }
                    }
                } else if (item instanceof SQLCharExpr) {
                    if (((SQLCharExpr) item).getText().length() > 5) {
                        chrCount = 0;
                        continue;
                    }
                }
                if (chrCount >= 4) {
                    addViolation(visitor, ErrorCode.EVIL_CONCAT, "evil concat", x);
                    break;
                }
            }
        }
    }
    return true;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint)

Aggregations

SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)37 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)16 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)15 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)8 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)8 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)8 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)7 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)6 TableConfig (io.mycat.config.model.TableConfig)6 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)6 SlotFunction (io.mycat.route.function.SlotFunction)6 SQLNonTransientException (java.sql.SQLNonTransientException)6 ParserException (com.alibaba.druid.sql.parser.ParserException)5 SQLName (com.alibaba.druid.sql.ast.SQLName)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)4 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4 RouteResultsetNode (io.mycat.route.RouteResultsetNode)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)3