Search in sources :

Example 6 with MySqlInsertStatement

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

the class DruidMysqlCreateTableTest method isInsertHasSlot.

private boolean isInsertHasSlot(String sql) {
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    MySqlInsertStatement insertStatement = (MySqlInsertStatement) parser.parseStatement();
    List<SQLExpr> cc = insertStatement.getColumns();
    for (SQLExpr sqlExpr : cc) {
        SQLIdentifierExpr c = (SQLIdentifierExpr) sqlExpr;
        if ("_slot".equalsIgnoreCase(c.getName()) && cc.size() == insertStatement.getValues().getValues().size())
            return true;
    }
    return false;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) 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 7 with MySqlInsertStatement

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

the class MySqlStatementParser method parseInsert.

public SQLInsertStatement parseInsert() {
    MySqlInsertStatement insertStatement = new MySqlInsertStatement();
    if (lexer.token() == Token.INSERT) {
        lexer.nextToken();
        for (; ; ) {
            if (identifierEquals(LOW_PRIORITY)) {
                insertStatement.setLowPriority(true);
                lexer.nextToken();
                continue;
            }
            if (identifierEquals(DELAYED)) {
                insertStatement.setDelayed(true);
                lexer.nextToken();
                continue;
            }
            if (identifierEquals("HIGH_PRIORITY")) {
                insertStatement.setHighPriority(true);
                lexer.nextToken();
                continue;
            }
            if (identifierEquals(IGNORE)) {
                insertStatement.setIgnore(true);
                lexer.nextToken();
                continue;
            }
            if (identifierEquals("ROLLBACK_ON_FAIL")) {
                insertStatement.setRollbackOnFail(true);
                lexer.nextToken();
                continue;
            }
            break;
        }
        if (lexer.token() == Token.INTO) {
            lexer.nextToken();
        }
        SQLName tableName = this.exprParser.name();
        insertStatement.setTableName(tableName);
        if (lexer.token() == Token.IDENTIFIER && !identifierEquals("VALUE")) {
            insertStatement.setAlias(lexer.stringVal());
            lexer.nextToken();
        }
    }
    int columnSize = 0;
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        if (lexer.token() == (Token.SELECT)) {
            SQLSelect select = this.exprParser.createSelectParser().select();
            select.setParent(insertStatement);
            insertStatement.setQuery(select);
        } else {
            this.exprParser.exprList(insertStatement.getColumns(), insertStatement);
            columnSize = insertStatement.getColumns().size();
        }
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.VALUES || identifierEquals("VALUE")) {
        lexer.nextTokenLParen();
        parseValueClause(insertStatement.getValuesList(), columnSize);
    } else if (lexer.token() == Token.SET) {
        lexer.nextToken();
        SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause();
        insertStatement.getValuesList().add(values);
        for (; ; ) {
            SQLName name = this.exprParser.name();
            insertStatement.addColumn(name);
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            } else {
                accept(Token.COLONEQ);
            }
            values.addValue(this.exprParser.expr());
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    } else if (lexer.token() == (Token.SELECT)) {
        SQLSelect select = this.exprParser.createSelectParser().select();
        select.setParent(insertStatement);
        insertStatement.setQuery(select);
    } else if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        SQLSelect select = this.exprParser.createSelectParser().select();
        select.setParent(insertStatement);
        insertStatement.setQuery(select);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.ON) {
        lexer.nextToken();
        acceptIdentifier("DUPLICATE");
        accept(Token.KEY);
        accept(Token.UPDATE);
        exprParser.exprList(insertStatement.getDuplicateKeyUpdate(), insertStatement);
    }
    return insertStatement;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 8 with MySqlInsertStatement

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

the class WallVisitorUtils method checkInsertForMultiTenant.

private static void checkInsertForMultiTenant(WallVisitor visitor, SQLInsertInto x) {
    TenantCallBack tenantCallBack = visitor.getConfig().getTenantCallBack();
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantCallBack == null && (tenantTablePattern == null || tenantTablePattern.length() == 0)) {
        return;
    }
    if (x == null) {
        throw new IllegalStateException("x is null");
    }
    SQLExprTableSource tableSource = x.getTableSource();
    String alias = null;
    String matchTableName = null;
    String tenantColumn = null;
    SQLExpr tableExpr = tableSource.getExpr();
    if (tableExpr instanceof SQLIdentifierExpr) {
        String tableName = ((SQLIdentifierExpr) tableExpr).getName();
        if (tenantCallBack != null) {
            tenantColumn = tenantCallBack.getTenantColumn(StatementType.INSERT, tableName);
        }
        if (StringUtils.isEmpty(tenantColumn) && ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
            tenantColumn = visitor.getConfig().getTenantColumn();
        }
        if (!StringUtils.isEmpty(tenantColumn)) {
            matchTableName = tableName;
            alias = tableSource.getAlias();
        }
    }
    if (matchTableName == null) {
        return;
    }
    SQLExpr item = null;
    if (alias != null) {
        item = new SQLPropertyExpr(new SQLIdentifierExpr(alias), tenantColumn);
    } else {
        item = new SQLIdentifierExpr(tenantColumn);
    }
    SQLExpr value = generateTenantValue(visitor, alias, StatementType.INSERT, matchTableName);
    // add insert item and value
    x.getColumns().add(item);
    List<ValuesClause> valuesClauses = null;
    ValuesClause valuesClause = null;
    if (x instanceof MySqlInsertStatement) {
        valuesClauses = ((MySqlInsertStatement) x).getValuesList();
    } else if (x instanceof SQLServerInsertStatement) {
        valuesClauses = ((MySqlInsertStatement) x).getValuesList();
    } else {
        valuesClause = x.getValues();
    }
    if (valuesClauses != null && valuesClauses.size() > 0) {
        for (ValuesClause clause : valuesClauses) {
            clause.addValue(value);
        }
    }
    if (valuesClause != null) {
        valuesClause.addValue(value);
    }
    // insert .. select
    SQLSelect select = x.getQuery();
    if (select != null) {
        List<SQLSelectQueryBlock> queryBlocks = splitSQLSelectQuery(select.getQuery());
        for (SQLSelectQueryBlock queryBlock : queryBlocks) {
            queryBlock.getSelectList().add(new SQLSelectItem(value));
        }
    }
    visitor.setSqlModified(true);
}
Also used : TenantCallBack(com.alibaba.druid.wall.WallConfig.TenantCallBack) SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 9 with MySqlInsertStatement

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

the class MySqlInsertTest_7 method test_0.

public void test_0() throws Exception {
    String sql = "insert into Apply (applicant_id, applytime, applytype, approver_id, end, process, start) values (?, ?, ?, ?, ?, ?, ?)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(7, insertStmt.getColumns().size());
    Assert.assertEquals(1, insertStmt.getValuesList().size());
    Assert.assertEquals(7, insertStmt.getValuesList().get(0).getValues().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(//
    "INSERT INTO Apply (applicant_id, applytime, applytype, approver_id, end" + //
    "\n\t, process, start)" + //
    "\nVALUES (?, ?, ?, ?, ?" + //
    "\n\t, ?, ?)", SQLUtils.toMySqlString(insertStmt));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) 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 10 with MySqlInsertStatement

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

the class MySqlInsertTest_8 method test_0.

public void test_0() throws Exception {
    String sql = "insert into dd.table1(d,e) select * from bb.table3";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(2, insertStmt.getColumns().size());
    Assert.assertEquals(0, insertStmt.getValuesList().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(//
    "INSERT INTO dd.table1 (d, e)" + //
    "\nSELECT *" + //
    "\nFROM bb.table3", SQLUtils.toMySqlString(insertStmt));
    Assert.assertEquals(//
    "insert into dd.table1 (d, e)" + //
    "\nselect *" + //
    "\nfrom bb.table3", SQLUtils.toMySqlString(insertStmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) 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)

Aggregations

MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)24 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)19 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)16 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)12 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)4 TableConfig (io.mycat.config.model.TableConfig)3 SQLNonTransientException (java.sql.SQLNonTransientException)3 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 RouteResultset (io.mycat.route.RouteResultset)2 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLBinaryExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1