Search in sources :

Example 26 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project druid by alibaba.

the class MySqlInsertTest_25_time method test_insert.

public void test_insert() throws Exception {
    String sql = "INSERT INTO DB1.TB2 (col1, col2, col3) VALUES(1, Timestamp '2019-01-01:12:12:21', '3')";
    {
        List<Object> outParameters = new ArrayList<Object>();
        String psql = ParameterizedOutputVisitorUtils.parameterize(sql, JdbcConstants.MYSQL, outParameters);
        assertEquals("INSERT INTO DB1.TB2(col1, col2, col3)\n" + "VALUES (?, ?, ?)", psql);
        assertEquals(3, outParameters.size());
        String rsql = ParameterizedOutputVisitorUtils.restore(psql, JdbcConstants.MYSQL, outParameters);
        assertEquals("INSERT INTO DB1.TB2 (col1, col2, col3)\n" + "VALUES (1, '2019-01-01:12:12:21', '3')", rsql);
    }
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    assertEquals("INSERT INTO DB1.TB2 (col1, col2, col3)\n" + "VALUES (1, TIMESTAMP '2019-01-01:12:12:21', '3')", SQLUtils.toMySqlString(insertStmt));
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 27 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat_plus by coderczp.

the class ExplainHandler method isMycatSeq.

private static boolean isMycatSeq(String stmt, SchemaConfig schema) {
    if (pattern.matcher(stmt).find()) {
        return true;
    }
    SQLStatementParser parser = new MySqlStatementParser(stmt);
    MySqlInsertStatement statement = (MySqlInsertStatement) parser.parseStatement();
    String tableName = statement.getTableName().getSimpleName();
    TableConfig tableConfig = schema.getTables().get(tableName.toUpperCase());
    if (tableConfig == null) {
        return false;
    }
    if (tableConfig.isAutoIncrement()) {
        boolean isHasIdInSql = false;
        String primaryKey = tableConfig.getPrimaryKey();
        List<SQLExpr> columns = statement.getColumns();
        for (SQLExpr column : columns) {
            String columnName = column.toString();
            if (primaryKey.equalsIgnoreCase(columnName)) {
                isHasIdInSql = true;
                break;
            }
        }
        if (!isHasIdInSql) {
            return true;
        }
    }
    return false;
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableConfig(io.mycat.config.model.TableConfig) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 28 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat_plus by coderczp.

the class ParseUtil method changeInsertAddSlot.

public static String changeInsertAddSlot(String sql, int slotValue) {
    SQLStatementParser parser = new MycatStatementParser(sql);
    MySqlInsertStatement insert = (MySqlInsertStatement) parser.parseStatement();
    insert.getColumns().add(new SQLIdentifierExpr("_slot"));
    insert.getValues().getValues().add(new SQLIntegerExpr(slotValue));
    return insert.toString();
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatStatementParser(io.mycat.route.parser.druid.MycatStatementParser) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)

Example 29 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat_plus by coderczp.

the class BatchInsertSequence method route.

@Override
public void route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool) {
    int rs = ServerParse.parse(realSQL);
    this.sqltype = rs & 0xff;
    this.sysConfig = sysConfig;
    this.schema = schema;
    this.charset = charset;
    this.sc = sc;
    this.cachePool = cachePool;
    try {
        MySqlStatementParser parser = new MySqlStatementParser(realSQL);
        SQLStatement statement = parser.parseStatement();
        MySqlInsertStatement insert = (MySqlInsertStatement) statement;
        if (insert.getValuesList() != null) {
            String tableName = StringUtil.getTableName(realSQL).toUpperCase();
            TableConfig tableConfig = schema.getTables().get(tableName);
            // 获得表的主键字段
            String primaryKey = tableConfig.getPrimaryKey();
            SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
            sqlIdentifierExpr.setName(primaryKey);
            insert.getColumns().add(sqlIdentifierExpr);
            if (sequenceHandler == null) {
                int seqHandlerType = MycatServer.getInstance().getConfig().getSystem().getSequnceHandlerType();
                switch(seqHandlerType) {
                    case SystemConfig.SEQUENCEHANDLER_MYSQLDB:
                        sequenceHandler = IncrSequenceMySQLHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_LOCALFILE:
                        sequenceHandler = IncrSequencePropHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_LOCAL_TIME:
                        sequenceHandler = IncrSequenceTimeHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED:
                        sequenceHandler = DistributedSequenceHandler.getInstance(MycatServer.getInstance().getConfig().getSystem());
                        break;
                    case SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT:
                        sequenceHandler = IncrSequenceZKHandler.getInstance();
                        break;
                    default:
                        throw new java.lang.IllegalArgumentException("Invalid sequnce handler type " + seqHandlerType);
                }
            }
            for (ValuesClause vc : insert.getValuesList()) {
                SQLIntegerExpr sqlIntegerExpr = new SQLIntegerExpr();
                long value = sequenceHandler.nextId(tableName.toUpperCase());
                // 插入生成的sequence值
                sqlIntegerExpr.setNumber(value);
                vc.addValue(sqlIntegerExpr);
            }
            String insertSql = insert.toString();
            this.executeSql = insertSql;
        }
    } catch (Exception e) {
        LOGGER.error("BatchInsertSequence.route(......)", e);
    }
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) TableConfig(io.mycat.config.model.TableConfig) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 30 with MySqlInsertStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement in project Mycat-Server by MyCATApache.

the class ParseUtil method changeInsertAddSlot.

public static String changeInsertAddSlot(String sql, int slotValue) {
    SQLStatementParser parser = new MycatStatementParser(sql);
    MySqlInsertStatement insert = (MySqlInsertStatement) parser.parseStatement();
    insert.getColumns().add(new SQLIdentifierExpr("_slot"));
    insert.getValues().getValues().add(new SQLIntegerExpr(slotValue));
    return insert.toString();
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatStatementParser(io.mycat.route.parser.druid.MycatStatementParser) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)

Aggregations

MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)47 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)39 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)33 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)14 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)8 TableConfig (io.mycat.config.model.TableConfig)8 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)7 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SQLNonTransientException (java.sql.SQLNonTransientException)6 RouteResultset (io.mycat.route.RouteResultset)5 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)4 RuleConfig (io.mycat.config.model.rule.RuleConfig)3 RouteResultsetNode (io.mycat.route.RouteResultsetNode)3 SlotFunction (io.mycat.route.function.SlotFunction)3 MycatStatementParser (io.mycat.route.parser.druid.MycatStatementParser)3 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)3