Search in sources :

Example 21 with MySqlInsertStatement

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

the class MySqlInsertTest_4 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(3, insertStmt.getColumns().size());
    Assert.assertEquals(3, insertStmt.getValuesList().size());
    Assert.assertEquals(3, insertStmt.getValuesList().get(0).getValues().size());
    Assert.assertEquals(3, insertStmt.getValuesList().get(1).getValues().size());
    Assert.assertEquals(3, insertStmt.getValuesList().get(2).getValues().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(//
    "INSERT INTO tbl_name (a, b, c)" + "\nVALUES (1, 2, 3)," + "\n\t(4, 5, 6)," + "\n\t(7, 8, 9)", 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 22 with MySqlInsertStatement

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

the class MySqlInsertTest_5 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT INTO tbl_name (a,b,c) VALUES(1,2,3,4,5,6,7,8,9);";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(3, insertStmt.getColumns().size());
    Assert.assertEquals(1, insertStmt.getValuesList().size());
    Assert.assertEquals(9, insertStmt.getValuesList().get(0).getValues().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(//
    "INSERT INTO tbl_name (a, b, c)" + //
    "\nVALUES (1, 2, 3, 4, 5" + "\n\t, 6, 7, 8, 9)", 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 23 with MySqlInsertStatement

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

the class MySqlInsertTest_6 method test_0.

public void test_0() throws Exception {
    String sql = //
    "insert into document(the_key,the_namespace,Gmt_create,Gmt_modify,Expired_time,the_value) " + //
    "values (?,?,now(),now(),date_add(now(),interval ? second),?) " + "on duplicate key update Gmt_modify = values(Gmt_modify),Expired_time = values(Expired_time),the_value = values(the_value)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(6, insertStmt.getColumns().size());
    Assert.assertEquals(1, insertStmt.getValuesList().size());
    Assert.assertEquals(6, insertStmt.getValuesList().get(0).getValues().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(//
    "INSERT INTO document (the_key, the_namespace, Gmt_create, Gmt_modify, Expired_time" + //
    "\n\t, the_value)" + //
    "\nVALUES (?, ?, now(), now(), date_add(now(), INTERVAL ? SECOND)" + //
    "\n\t, ?)" + "\nON DUPLICATE KEY UPDATE Gmt_modify = VALUES(Gmt_modify), Expired_time = VALUES(Expired_time), the_value = VALUES(the_value)", SQLUtils.toMySqlString(insertStmt));
    Assert.assertEquals(//
    "insert into document (the_key, the_namespace, Gmt_create, Gmt_modify, Expired_time" + //
    "\n\t, the_value)" + //
    "\nvalues (?, ?, now(), now(), date_add(now(), interval ? second)" + //
    "\n\t, ?)" + "\non duplicate key update Gmt_modify = VALUES(Gmt_modify), Expired_time = VALUES(Expired_time), the_value = VALUES(the_value)", 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)

Example 24 with MySqlInsertStatement

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

the class SQLMergeTest method merge2.

private String merge2() {
    String sql = "INSERT INTO T (F1, F2, F3, F4, F5) VALUES (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)";
    StringBuilder out = new StringBuilder();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(out) {

        public boolean visit(SQLInListExpr x) {
            x.getExpr().accept(this);
            if (x.isNot()) {
                print(" NOT IN (##)");
            } else {
                print(" IN (##)");
            }
            return false;
        }

        @Override
        public boolean visit(MySqlInsertStatement x) {
            print("INSERT ");
            if (x.isLowPriority()) {
                print("LOW_PRIORITY ");
            }
            if (x.isDelayed()) {
                print("DELAYED ");
            }
            if (x.isHighPriority()) {
                print("HIGH_PRIORITY ");
            }
            if (x.isIgnore()) {
                print("IGNORE ");
            }
            print("INTO ");
            x.getTableSource().accept(this);
            if (x.getColumns().size() > 0) {
                print(" (");
                for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
                    if (i != 0) {
                        print(", ");
                    }
                    x.getColumns().get(i).accept(this);
                }
                print(")");
            }
            if (x.getValuesList().size() != 0) {
                print(" VALUES ");
                int size = x.getValuesList().size();
                if (size == 0) {
                    print("()");
                } else {
                    for (int i = 0; i < 1; ++i) {
                        if (i != 0) {
                            print(", ");
                        }
                        x.getValuesList().get(i).accept(this);
                    }
                }
            }
            if (x.getQuery() != null) {
                print(" ");
                x.getQuery().accept(this);
            }
            if (x.getDuplicateKeyUpdate().size() != 0) {
                print(" ON DUPLICATE KEY UPDATE ");
                printAndAccept(x.getDuplicateKeyUpdate(), ", ");
            }
            return false;
        }
    };
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    for (SQLStatement statement : statementList) {
        statement.accept(visitor);
        visitor.println();
    }
    return out.toString();
}
Also used : SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) 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