Search in sources :

Example 11 with SQLIntegerExpr

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

the class SQLServerExprParser method parseColumnRest.

public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
    if (lexer.token() == Token.IDENTITY) {
        lexer.nextToken();
        SQLColumnDefinition.Identity identity = new SQLColumnDefinition.Identity();
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            SQLIntegerExpr seed = (SQLIntegerExpr) this.primary();
            accept(Token.COMMA);
            SQLIntegerExpr increment = (SQLIntegerExpr) this.primary();
            accept(Token.RPAREN);
            identity.setSeed((Integer) seed.getNumber());
            identity.setIncrement((Integer) increment.getNumber());
        }
        if (lexer.token() == Token.NOT) {
            lexer.nextToken();
            if (lexer.token() == Token.NULL) {
                lexer.nextToken();
                column.setDefaultExpr(new SQLNullExpr());
            } else {
                accept(Token.FOR);
                identifierEquals("REPLICATION ");
                identity.setNotForReplication(true);
            }
        }
        column.setIdentity(identity);
    }
    return super.parseColumnRest(column);
}
Also used : SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 12 with SQLIntegerExpr

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

the class WallVisitorUtils method isSimpleConstExpr.

private static boolean isSimpleConstExpr(SQLExpr sqlExpr) {
    List<SQLExpr> parts = getParts(sqlExpr);
    if (parts.isEmpty()) {
        return false;
    }
    for (SQLExpr part : parts) {
        if (isFirst(part)) {
            Object evalValue = part.getAttribute(EVAL_VALUE);
            if (evalValue == null) {
                if (part instanceof SQLBooleanExpr) {
                    evalValue = ((SQLBooleanExpr) part).getValue();
                } else if (part instanceof SQLNumericLiteralExpr) {
                    evalValue = ((SQLNumericLiteralExpr) part).getNumber();
                } else if (part instanceof SQLCharExpr) {
                    evalValue = ((SQLCharExpr) part).getText();
                } else if (part instanceof SQLNCharExpr) {
                    evalValue = ((SQLNCharExpr) part).getText();
                }
            }
            Boolean result = SQLEvalVisitorUtils.castToBoolean(evalValue);
            if (result != null && result) {
                return true;
            }
        }
        boolean isSimpleConstExpr = false;
        if (part == sqlExpr || part instanceof SQLLiteralExpr) {
            isSimpleConstExpr = true;
        } else if (part instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) part;
            if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality || binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual || binaryOpExpr.getOperator() == SQLBinaryOperator.GreaterThan) {
                if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr && binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
                    isSimpleConstExpr = true;
                }
            }
        }
        if (!isSimpleConstExpr) {
            return false;
        }
    }
    return true;
}
Also used : SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 13 with SQLIntegerExpr

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

the class SQLExprParserTest method test_binary.

public void test_binary() throws Exception {
    SQLExprParser exprParser = new SQLExprParser("AGE > 5");
    SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) exprParser.expr();
    Assert.assertEquals(SQLBinaryOperator.GreaterThan, binaryOpExpr.getOperator());
    SQLIdentifierExpr left = (SQLIdentifierExpr) binaryOpExpr.getLeft();
    SQLIntegerExpr right = (SQLIntegerExpr) binaryOpExpr.getRight();
    Assert.assertEquals("AGE", left.getName());
    Assert.assertEquals(5, right.getNumber().intValue());
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExprParser(com.alibaba.druid.sql.parser.SQLExprParser)

Example 14 with SQLIntegerExpr

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

the class MySQLSelectVisitor method visit.

public boolean visit(final SQLOrderBy x) {
    for (SQLSelectOrderByItem each : x.getItems()) {
        SQLExpr expr = each.getExpr();
        OrderByType orderByType = null == each.getType() ? OrderByType.ASC : OrderByType.valueOf(each.getType());
        if (expr instanceof SQLIntegerExpr) {
            getParseContext().addOrderByColumn(((SQLIntegerExpr) expr).getNumber().intValue(), orderByType);
        } else if (expr instanceof SQLIdentifierExpr) {
            getParseContext().addOrderByColumn(Optional.<String>absent(), ((SQLIdentifierExpr) expr).getName(), orderByType);
        } else if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr sqlPropertyExpr = (SQLPropertyExpr) expr;
            getParseContext().addOrderByColumn(Optional.of(sqlPropertyExpr.getOwner().toString()), sqlPropertyExpr.getName(), orderByType);
        }
    }
    return super.visit(x);
}
Also used : SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) OrderByType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn.OrderByType)

Example 15 with SQLIntegerExpr

use of com.alibaba.druid.sql.ast.expr.SQLIntegerExpr in project Mycat-Server by MyCATApache.

the class DruidInsertParser method parserBatchInsert.

/**
	 * insert into .... select .... 或insert into table() values (),(),....
	 * @param schema
	 * @param rrs
	 * @param insertStmt
	 * @throws SQLNonTransientException
	 */
private void parserBatchInsert(SchemaConfig schema, RouteResultset rrs, String partitionColumn, String tableName, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    //insert into table() values (),(),....
    if (insertStmt.getValuesList().size() > 1) {
        //字段列数
        int columnNum = insertStmt.getColumns().size();
        int shardingColIndex = getShardingColIndex(insertStmt, partitionColumn);
        if (shardingColIndex == -1) {
            String msg = "bad insert sql (sharding column:" + partitionColumn + " not provided," + insertStmt;
            LOGGER.warn(msg);
            throw new SQLNonTransientException(msg);
        } else {
            List<ValuesClause> valueClauseList = insertStmt.getValuesList();
            Map<Integer, List<ValuesClause>> nodeValuesMap = new HashMap<Integer, List<ValuesClause>>();
            Map<Integer, Integer> slotsMap = new HashMap<>();
            TableConfig tableConfig = schema.getTables().get(tableName);
            AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
            for (ValuesClause valueClause : valueClauseList) {
                if (valueClause.getValues().size() != columnNum) {
                    String msg = "bad insert sql columnSize != valueSize:" + columnNum + " != " + valueClause.getValues().size() + "values:" + valueClause;
                    LOGGER.warn(msg);
                    throw new SQLNonTransientException(msg);
                }
                SQLExpr expr = valueClause.getValues().get(shardingColIndex);
                String shardingValue = null;
                if (expr instanceof SQLIntegerExpr) {
                    SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
                    shardingValue = intExpr.getNumber() + "";
                } else if (expr instanceof SQLCharExpr) {
                    SQLCharExpr charExpr = (SQLCharExpr) expr;
                    shardingValue = charExpr.getText();
                }
                Integer nodeIndex = algorithm.calculate(shardingValue);
                if (algorithm instanceof SlotFunction) {
                    slotsMap.put(nodeIndex, ((SlotFunction) algorithm).slotValue());
                }
                //没找到插入的分片
                if (nodeIndex == null) {
                    String msg = "can't find any valid datanode :" + tableName + " -> " + partitionColumn + " -> " + shardingValue;
                    LOGGER.warn(msg);
                    throw new SQLNonTransientException(msg);
                }
                if (nodeValuesMap.get(nodeIndex) == null) {
                    nodeValuesMap.put(nodeIndex, new ArrayList<ValuesClause>());
                }
                nodeValuesMap.get(nodeIndex).add(valueClause);
            }
            RouteResultsetNode[] nodes = new RouteResultsetNode[nodeValuesMap.size()];
            int count = 0;
            for (Map.Entry<Integer, List<ValuesClause>> node : nodeValuesMap.entrySet()) {
                Integer nodeIndex = node.getKey();
                List<ValuesClause> valuesList = node.getValue();
                insertStmt.setValuesList(valuesList);
                nodes[count] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), insertStmt.toString());
                if (algorithm instanceof SlotFunction) {
                    nodes[count].setSlot(slotsMap.get(nodeIndex));
                    nodes[count].setStatement(ParseUtil.changeInsertAddSlot(nodes[count].getStatement(), nodes[count].getSlot()));
                }
                nodes[count++].setSource(rrs);
            }
            rrs.setNodes(nodes);
            rrs.setFinishedRoute(true);
        }
    } else if (insertStmt.getQuery() != null) {
        // insert into .... select ....
        String msg = "TODO:insert into .... select .... not supported!";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) HashMap(java.util.HashMap) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SlotFunction(io.mycat.route.function.SlotFunction) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(io.mycat.route.RouteResultsetNode) TableConfig(io.mycat.config.model.TableConfig) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Aggregations

SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)24 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)16 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)9 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)5 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)4 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 List (java.util.List)4 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)3 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)3 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)3 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)2