Search in sources :

Example 26 with SQLCharExpr

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

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)

Example 27 with SQLCharExpr

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

the class MycatStatementParser method parseLoadDataInFile.

// 此处注释掉,以修正后端jdbc方式时,delete语句解析出错的情况
// 
// public SQLSelectParser createSQLSelectParser()
// {
// return new MycatSelectParser(this.selectExprParser);
// }
@Override
protected MySqlLoadDataInFileStatement parseLoadDataInFile() {
    acceptIdentifier("DATA");
    LoadDataStatement stmt = new LoadDataStatement();
    if (identifierEquals(LOW_PRIORITY)) {
        stmt.setLowPriority(true);
        lexer.nextToken();
    }
    if (identifierEquals("CONCURRENT")) {
        stmt.setConcurrent(true);
        lexer.nextToken();
    }
    if (identifierEquals(LOCAL)) {
        stmt.setLocal(true);
        lexer.nextToken();
    }
    acceptIdentifier("INFILE");
    SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr();
    stmt.setFileName(fileName);
    if (lexer.token() == Token.REPLACE) {
        stmt.setReplicate(true);
        lexer.nextToken();
    }
    if (identifierEquals(IGNORE)) {
        stmt.setIgnore(true);
        lexer.nextToken();
    }
    accept(Token.INTO);
    accept(Token.TABLE);
    SQLName tableName = exprParser.name();
    stmt.setTableName(tableName);
    if (identifierEquals(CHARACTER)) {
        lexer.nextToken();
        accept(Token.SET);
        if (lexer.token() != Token.LITERAL_CHARS) {
            throw new ParserException("syntax error, illegal charset");
        }
        String charset = lexer.stringVal();
        lexer.nextToken();
        stmt.setCharset(charset);
    }
    if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) {
        lexer.nextToken();
        if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsTerminatedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("OPTIONALLY")) {
            stmt.setColumnsEnclosedOptionally(true);
            lexer.nextToken();
        }
        if (identifierEquals("ENCLOSED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsEnclosedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("ESCAPED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsEscaped(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
    }
    if (identifierEquals("LINES")) {
        lexer.nextToken();
        if (identifierEquals("STARTING")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setLinesStartingBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setLinesTerminatedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
    }
    if (identifierEquals(IGNORE)) {
        lexer.nextToken();
        stmt.setIgnoreLinesNumber(this.exprParser.expr());
        acceptIdentifier("LINES");
    }
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getColumns(), stmt);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.SET) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getSetList(), stmt);
    }
    return stmt;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLName(com.alibaba.druid.sql.ast.SQLName)

Example 28 with SQLCharExpr

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

the class DruidCreateTableParser method sharingTableCheck.

/**
 */
private void sharingTableCheck(MySqlCreateTableStatement createStmt) throws SQLNonTransientException {
    // ALLOW InnoDB ONLY
    SQLObject engine = createStmt.getTableOptions().get("ENGINE");
    if (engine != null) {
        String strEngine;
        if (engine instanceof SQLCharExpr) {
            strEngine = ((SQLCharExpr) engine).getText();
        } else if (engine instanceof SQLIdentifierExpr) {
            strEngine = ((SQLIdentifierExpr) engine).getSimpleName();
        } else {
            strEngine = engine.toString();
        }
        if (!"InnoDB".equalsIgnoreCase(strEngine)) {
            String msg = "create table only can use ENGINE InnoDB,others not supported:" + createStmt;
            LOGGER.info(msg);
            throw new SQLNonTransientException(msg);
        }
    }
    // DISABLE DATA DIRECTORY
    if (createStmt.getTableOptions().get("DATA DIRECTORY") != null) {
        String msg = "create table with DATA DIRECTORY  not supported:" + createStmt;
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLNonTransientException(java.sql.SQLNonTransientException) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 29 with SQLCharExpr

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

the class ServerLoadDataInfileHandler method buildResultSet.

private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
    statement.setLocal(true);
    // druid will filter path, reset it now
    SQLLiteralExpr fn = new SQLCharExpr(fileName);
    statement.setFileName(fn);
    // replace IGNORE X LINES in SQL to avoid  IGNORING X LINE in every node.
    String srcStatement = this.ignoreLinesDelete(statement.toString());
    RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
    rrs.setLoadData(true);
    rrs.setStatement(srcStatement);
    rrs.setFinishedRoute(true);
    int size = routeMap.size();
    RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
    int index = 0;
    for (Map.Entry<String, LoadData> entry : routeMap.entrySet()) {
        RouteResultsetNode rrNode = new RouteResultsetNode(entry.getKey(), ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
        rrNode.setStatement(srcStatement);
        LoadData newLoadData = new LoadData();
        ObjectUtil.copyProperties(loadData, newLoadData);
        newLoadData.setLocal(true);
        LoadData loadData1 = entry.getValue();
        if (loadData1.getFileName() != null) {
            newLoadData.setFileName(loadData1.getFileName());
        } else {
            newLoadData.setData(loadData1.getData());
        }
        rrNode.setLoadData(newLoadData);
        routeResultsetNodes[index] = rrNode;
        index++;
    }
    rrs.setNodes(routeResultsetNodes);
    return rrs;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) LoadData(com.actiontech.dble.sqlengine.mpp.LoadData) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) RouteResultset(com.actiontech.dble.route.RouteResultset)

Example 30 with SQLCharExpr

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

the class SqlParser method visitColumn.

/**
 * 解析字段
 *
 * @param expr sql expr
 * @param fieldItem 字段属性
 */
private static void visitColumn(SQLExpr expr, FieldItem fieldItem) {
    if (expr instanceof SQLIdentifierExpr) {
        // 无owner
        SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
        if (fieldItem.getFieldName() == null) {
            fieldItem.setFieldName(identifierExpr.getName());
            fieldItem.setExpr(identifierExpr.toString());
        }
        ColumnItem columnItem = new ColumnItem();
        columnItem.setColumnName(identifierExpr.getName());
        fieldItem.getOwners().add(null);
        fieldItem.addColumn(columnItem);
    } else if (expr instanceof SQLPropertyExpr) {
        // 有owner
        SQLPropertyExpr sqlPropertyExpr = (SQLPropertyExpr) expr;
        if (fieldItem.getFieldName() == null) {
            fieldItem.setFieldName(sqlPropertyExpr.getName());
            fieldItem.setExpr(sqlPropertyExpr.toString());
        }
        fieldItem.getOwners().add(sqlPropertyExpr.getOwnernName());
        ColumnItem columnItem = new ColumnItem();
        columnItem.setColumnName(sqlPropertyExpr.getName());
        columnItem.setOwner(sqlPropertyExpr.getOwnernName());
        fieldItem.addColumn(columnItem);
    } else if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
        fieldItem.setMethod(true);
        for (SQLExpr sqlExpr : methodInvokeExpr.getArguments()) {
            visitColumn(sqlExpr, fieldItem);
        }
    } else if (expr instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr sqlBinaryOpExpr = (SQLBinaryOpExpr) expr;
        fieldItem.setBinaryOp(true);
        visitColumn(sqlBinaryOpExpr.getLeft(), fieldItem);
        visitColumn(sqlBinaryOpExpr.getRight(), fieldItem);
    } else if (expr instanceof SQLCaseExpr) {
        SQLCaseExpr sqlCaseExpr = (SQLCaseExpr) expr;
        fieldItem.setMethod(true);
        sqlCaseExpr.getItems().forEach(item -> visitColumn(item.getConditionExpr(), fieldItem));
    } else if (expr instanceof SQLCharExpr) {
        SQLCharExpr sqlCharExpr = (SQLCharExpr) expr;
        String owner = null;
        String columnName = null;
        if (sqlCharExpr.getParent() instanceof SQLCaseExpr.Item) {
            owner = ((SQLPropertyExpr) ((SQLCaseExpr.Item) sqlCharExpr.getParent()).getValueExpr()).getOwnernName();
            columnName = ((SQLPropertyExpr) ((SQLCaseExpr.Item) sqlCharExpr.getParent()).getValueExpr()).getName();
        }
        if (fieldItem.getFieldName() == null) {
            fieldItem.setFieldName(columnName);
            fieldItem.setExpr(sqlCharExpr.toString());
        }
        ColumnItem columnItem = new ColumnItem();
        columnItem.setColumnName(columnName);
        columnItem.setOwner(owner);
        fieldItem.getOwners().add(owner);
        fieldItem.addColumn(columnItem);
    }
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) ColumnItem(com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.ColumnItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLCaseExpr(com.alibaba.druid.sql.ast.expr.SQLCaseExpr)

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