Search in sources :

Example 31 with SQLCharExpr

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

the class SQLColumnDefinition method setComment.

public void setComment(String comment) {
    SQLCharExpr expr;
    if (comment == null) {
        expr = null;
    } else {
        expr = new SQLCharExpr(comment);
    }
    this.setComment(expr);
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr)

Example 32 with SQLCharExpr

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

the class ToChar method eval.

public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
    final List<SQLExpr> arguments = x.getArguments();
    if (arguments.size() != 2) {
        return EVAL_ERROR;
    }
    for (SQLExpr arg : arguments) {
        arg.accept(visitor);
    }
    Object v0 = arguments.get(0).getAttributes().get(EVAL_VALUE);
    Object v1 = arguments.get(1).getAttributes().get(EVAL_VALUE);
    if (v0 instanceof Date && v1 instanceof String) {
        Date date = ((Date) v0);
        String format = ((SQLCharExpr) arguments.get(1)).getText();
        if (format.equals("yyyymmdd")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            return dateFormat.format(date);
        }
    }
    return null;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SimpleDateFormat(java.text.SimpleDateFormat) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Date(java.util.Date)

Example 33 with SQLCharExpr

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

the class MySqlInsertTest_0 method test_0.

public void test_0() throws Exception {
    String sql = // 
    "INSERT INTO t1 (name) VALUES (" + // 
    "'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + // 
    "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + "eeeeeeeeeeeeeeeeee\\\\1')";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLInsertStatement insertStmt = (SQLInsertStatement) stmt;
    SQLCharExpr charExpr = (SQLCharExpr) insertStmt.getValues().getValues().get(0);
    String text = charExpr.getText();
    Assert.assertEquals('1', text.charAt(charExpr.getText().length() - 1));
    Assert.assertEquals('\\', text.charAt(charExpr.getText().length() - 2));
    // print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("mytable")));
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 34 with SQLCharExpr

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

the class DruidCreateTableParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement) stmt;
    if (createStmt.getQuery() != null) {
        String msg = "create table from other table not supported :" + stmt;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String tableName = StringUtil.removeBackquote(createStmt.getTableSource().toString().toUpperCase());
    if (schema.getTables().containsKey(tableName)) {
        TableConfig tableConfig = schema.getTables().get(tableName);
        AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
        if (algorithm instanceof SlotFunction) {
            SQLColumnDefinition column = new SQLColumnDefinition();
            column.setDataType(new SQLCharacterDataType("int"));
            column.setName(new SQLIdentifierExpr("_slot"));
            column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
            ((SQLCreateTableStatement) stmt).getTableElementList().add(column);
            String sql = createStmt.toString();
            rrs.setStatement(sql);
            ctx.setSql(sql);
        }
    }
    ctx.addTable(tableName);
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLNonTransientException(java.sql.SQLNonTransientException) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) TableConfig(io.mycat.config.model.TableConfig) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SlotFunction(io.mycat.route.function.SlotFunction)

Example 35 with SQLCharExpr

use of com.alibaba.druid.sql.ast.expr.SQLCharExpr 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

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