Search in sources :

Example 6 with SQLStatement

use of com.alibaba.cobar.parser.ast.stmt.SQLStatement in project cobar by alibaba.

the class ServerRouteTest method testRouteInsertLong.

public void testRouteInsertLong() throws Exception {
    StringBuilder sb = new StringBuilder("insert into offer_detail (offer_id, gmt) values ");
    for (int i = 0; i < 1024; ++i) {
        if (i > 0)
            sb.append(", ");
        sb.append("(" + i + ", now())");
    }
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = ServerRouter.route(schema, sb.toString(), null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 128);
    IndexedNodeNameAsserter nameAsserter = new IndexedNodeNameAsserter("detail_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, new ParseredSQLAsserter() {

        @Override
        protected void assertAST(SQLStatement stmt, int nodeIndex) {
            DMLInsertStatement insert = (DMLInsertStatement) stmt;
            List<RowExpression> rows = insert.getRowList();
            Assert.assertNotNull(rows);
            Assert.assertEquals(8, rows.size());
            List<Integer> vals = new ArrayList<Integer>(8);
            for (RowExpression row : rows) {
                int val = ((Number) row.getRowExprList().get(0).evaluation(null)).intValue();
                vals.add(val);
            }
            Assert.assertEquals(8, vals.size());
            for (int i = 8 * nodeIndex; i < 8 * nodeIndex + 8; ++i) {
                Assert.assertTrue(vals.contains(i));
            }
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) DMLInsertStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with SQLStatement

use of com.alibaba.cobar.parser.ast.stmt.SQLStatement in project cobar by alibaba.

the class MySQLDALParserTest method testdesc.

public void testdesc() throws Exception {
    String sql = "desc tb1";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDALParser parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    SQLStatement desc = (DescTableStatement) parser.desc();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(desc, sql);
    Assert.assertEquals("DESC tb1", output);
    sql = "desc db.tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    desc = (DescTableStatement) parser.desc();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(desc, sql);
    Assert.assertEquals("DESC db.tb1", output);
    sql = "describe db.tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    desc = (DescTableStatement) parser.desc();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(desc, sql);
    Assert.assertEquals("DESC db.tb1", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DescTableStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DescTableStatement) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement)

Aggregations

SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)7 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)3 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)1 TableConfig (com.alibaba.cobar.config.model.TableConfig)1 RuleConfig (com.alibaba.cobar.config.model.rule.RuleConfig)1 TableRuleConfig (com.alibaba.cobar.config.model.rule.TableRuleConfig)1 ConfigException (com.alibaba.cobar.config.util.ConfigException)1 InExpressionList (com.alibaba.cobar.parser.ast.expression.misc.InExpressionList)1 PlaceHolder (com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder)1 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)1 DALSetStatement (com.alibaba.cobar.parser.ast.stmt.dal.DALSetStatement)1 DescTableStatement (com.alibaba.cobar.parser.ast.stmt.ddl.DescTableStatement)1 DMLInsertReplaceStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertReplaceStatement)1 DMLInsertStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement)1 MySQLDALParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDALParser)1 MySQLDDLParser (com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLDDLParser)1