Search in sources :

Example 11 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project dble by actiontech.

the class DQLRouteTest method test.

@Test
public void test() throws Exception {
    String stmt = "select * from `offer` where id = 100";
    SchemaConfig schema = schemaMap.get("mysqldb");
    RouteResultset rrs = new RouteResultset(stmt, 7);
    SQLStatementParser parser = null;
    parser = new MySqlStatementParser(stmt);
    SQLStatement statement;
    ServerSchemaStatVisitor visitor = null;
    try {
        statement = parser.parseStatement();
        visitor = new ServerSchemaStatVisitor();
    } catch (Exception t) {
        throw new SQLSyntaxErrorException(t);
    }
    ctx = new DruidShardingParseInfo();
    List<RouteCalculateUnit> taskList = visitorParse(rrs, statement, visitor);
    Assert.assertEquals(true, !taskList.get(0).getTablesAndConditions().isEmpty());
}
Also used : DruidShardingParseInfo(com.actiontech.dble.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(com.actiontech.dble.route.parser.druid.RouteCalculateUnit) ServerSchemaStatVisitor(com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) Test(org.junit.Test)

Example 12 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project dble by actiontech.

the class NonBlockingSession method executeMultiResultSet.

private void executeMultiResultSet(PlanNode node) {
    init();
    HandlerBuilder builder = new HandlerBuilder(node, this);
    try {
        // no next
        builder.build(false);
    } catch (SQLSyntaxErrorException e) {
        LOGGER.info(String.valueOf(source) + " execute plan is : " + node, e);
        source.writeErrMessage(ErrorCode.ER_YES, "optimizer build error");
    } catch (NoSuchElementException e) {
        LOGGER.info(String.valueOf(source) + " execute plan is : " + node, e);
        this.terminate();
        source.writeErrMessage(ErrorCode.ER_NO_VALID_CONNECTION, "no valid connection");
    } catch (MySQLOutPutException e) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.valueOf(source) + " execute plan is : " + node, e);
        }
        this.terminate();
        source.writeErrMessage(e.getSqlState(), e.getMessage(), e.getErrorCode());
    } catch (Exception e) {
        LOGGER.info(String.valueOf(source) + " execute plan is : " + node, e);
        this.terminate();
        source.writeErrMessage(ErrorCode.ER_HANDLE_DATA, e.toString());
    }
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) HandlerBuilder(com.actiontech.dble.backend.mysql.nio.handler.builder.HandlerBuilder) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 13 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.

the class MySQLDataNode method setHeartbeat.

private void setHeartbeat(String heartbeat) {
    if (heartbeat == null) {
        heartbeatAST = null;
        placeHolderToStringer = null;
        return;
    }
    try {
        final Set<PlaceHolder> plist = new HashSet<PlaceHolder>(1, 1);
        SQLStatement ast = SQLParserDelegate.parse(heartbeat);
        ast.accept(new EmptySQLASTVisitor() {

            @Override
            public void visit(PlaceHolder node) {
                plist.add(node);
            }
        });
        if (plist.isEmpty()) {
            heartbeatAST = null;
            placeHolderToStringer = null;
            return;
        }
        Map<PlaceHolder, Object> phm = new HashMap<PlaceHolder, Object>(plist.size(), 1);
        for (PlaceHolder ph : plist) {
            final String content = ph.getName();
            final int low = Integer.parseInt(content.substring(content.indexOf('(') + 1, content.indexOf(',')).trim());
            final int high = Integer.parseInt(content.substring(content.indexOf(',') + 1, content.indexOf(')')).trim());
            phm.put(ph, new Object() {

                private Random rnd = new Random();

                @Override
                public String toString() {
                    return String.valueOf(rnd.nextInt(high - low + 1) + low);
                }
            });
        }
        heartbeatAST = ast;
        placeHolderToStringer = phm;
    } catch (SQLSyntaxErrorException e) {
        throw new ConfigException("heartbeat syntax err: " + heartbeat, e);
    }
}
Also used : EmptySQLASTVisitor(com.alibaba.cobar.parser.visitor.EmptySQLASTVisitor) PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder) HashMap(java.util.HashMap) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ConfigException(com.alibaba.cobar.config.util.ConfigException) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) Random(java.util.Random) HashSet(java.util.HashSet)

Example 14 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.

the class MySQLDDLParser method columnDefinition.

// column_definition:
// data_type [NOT NULL | NULL] [DEFAULT default_value]
// [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
// [COMMENT 'string']
// [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
// [reference_definition]
private ColumnDefinition columnDefinition() throws SQLSyntaxErrorException {
    DataType dataType = dataType();
    boolean notNull = false;
    Expression defaultVal = null;
    boolean autoIncrement = false;
    ColumnDefinition.SpecialIndex sindex = null;
    ColumnDefinition.ColumnFormat format = null;
    LiteralString comment = null;
    if (lexer.token() == KW_NOT) {
        lexer.nextToken();
        match(LITERAL_NULL);
        notNull = true;
    } else if (lexer.token() == LITERAL_NULL) {
        lexer.nextToken();
    }
    if (lexer.token() == KW_DEFAULT) {
        lexer.nextToken();
        defaultVal = exprParser.expression();
        if (!(defaultVal instanceof Literal)) {
            throw new SQLSyntaxErrorException("default value of column must be a literal: " + defaultVal);
        }
    }
    if (lexer.token() == IDENTIFIER && "AUTO_INCREMENT".equals(lexer.stringValueUppercase())) {
        lexer.nextToken();
        autoIncrement = true;
    }
    switch(lexer.token()) {
        case KW_UNIQUE:
            if (lexer.nextToken() == KW_KEY) {
                lexer.nextToken();
            }
            sindex = ColumnDefinition.SpecialIndex.UNIQUE;
            break;
        case KW_PRIMARY:
            lexer.nextToken();
        case KW_KEY:
            match(KW_KEY);
            sindex = ColumnDefinition.SpecialIndex.PRIMARY;
            break;
    }
    if (lexer.token() == IDENTIFIER && "COMMENT".equals(lexer.stringValueUppercase())) {
        lexer.nextToken();
        comment = (LiteralString) exprParser.expression();
    }
    if (lexer.token() == IDENTIFIER && "COLUMN_FORMAT".equals(lexer.stringValueUppercase())) {
        switch(lexer.nextToken()) {
            case KW_DEFAULT:
                lexer.nextToken();
                format = ColumnDefinition.ColumnFormat.DEFAULT;
                break;
            case IDENTIFIER:
                SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
                if (si != null) {
                    switch(si) {
                        case FIXED:
                            lexer.nextToken();
                            format = ColumnDefinition.ColumnFormat.FIXED;
                            break;
                        case DYNAMIC:
                            lexer.nextToken();
                            format = ColumnDefinition.ColumnFormat.DYNAMIC;
                            break;
                    }
                }
        }
    }
    return new ColumnDefinition(dataType, notNull, defaultVal, autoIncrement, sindex, comment, format);
}
Also used : LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) Expression(com.alibaba.cobar.parser.ast.expression.Expression) Literal(com.alibaba.cobar.parser.ast.expression.primary.literal.Literal) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DataType(com.alibaba.cobar.parser.ast.fragment.ddl.datatype.DataType) ColumnDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition)

Example 15 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project cobar by alibaba.

the class MySQLDDLParser method createTableDefs.

private void createTableDefs(DDLCreateTableStatement stmt) throws SQLSyntaxErrorException {
    if (lexer.token() != PUNC_LEFT_PAREN) {
        return;
    }
    match(PUNC_LEFT_PAREN);
    IndexDefinition indexDef;
    Identifier id;
    for (int i = 0; lexer.token() != PUNC_RIGHT_PAREN; ++i) {
        if (i > 0) {
            match(PUNC_COMMA);
        }
        switch(lexer.token()) {
            case KW_PRIMARY:
                lexer.nextToken();
                match(KW_KEY);
                indexDef = indexDefinition();
                stmt.setPrimaryKey(indexDef);
                break;
            case KW_INDEX:
            case KW_KEY:
                lexer.nextToken();
                if (lexer.token() == IDENTIFIER) {
                    id = identifier();
                } else {
                    id = null;
                }
                indexDef = indexDefinition();
                stmt.addIndex(id, indexDef);
                break;
            case KW_UNIQUE:
                switch(lexer.nextToken()) {
                    case KW_INDEX:
                    case KW_KEY:
                        lexer.nextToken();
                        break;
                }
                if (lexer.token() == IDENTIFIER) {
                    id = identifier();
                } else {
                    id = null;
                }
                indexDef = indexDefinition();
                stmt.addUniqueIndex(id, indexDef);
                break;
            case KW_FULLTEXT:
                switch(lexer.nextToken()) {
                    case KW_INDEX:
                    case KW_KEY:
                        lexer.nextToken();
                        break;
                }
                if (lexer.token() == IDENTIFIER) {
                    id = identifier();
                } else {
                    id = null;
                }
                indexDef = indexDefinition();
                if (indexDef.getIndexType() != null) {
                    throw new SQLSyntaxErrorException("FULLTEXT INDEX can specify no index_type");
                }
                stmt.addFullTextIndex(id, indexDef);
                break;
            case KW_SPATIAL:
                switch(lexer.nextToken()) {
                    case KW_INDEX:
                    case KW_KEY:
                        lexer.nextToken();
                        break;
                }
                if (lexer.token() == IDENTIFIER) {
                    id = identifier();
                } else {
                    id = null;
                }
                indexDef = indexDefinition();
                if (indexDef.getIndexType() != null) {
                    throw new SQLSyntaxErrorException("SPATIAL INDEX can specify no index_type");
                }
                stmt.addSpatialIndex(id, indexDef);
                break;
            case KW_CHECK:
                lexer.nextToken();
                match(PUNC_LEFT_PAREN);
                Expression expr = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
                stmt.addCheck(expr);
                break;
            case IDENTIFIER:
                Identifier columnName = identifier();
                ColumnDefinition columnDef = columnDefinition();
                stmt.addColumnDefinition(columnName, columnDef);
                break;
            default:
                throw new SQLSyntaxErrorException("unsupportted column definition");
        }
    }
    match(PUNC_RIGHT_PAREN);
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) IndexDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexDefinition) Expression(com.alibaba.cobar.parser.ast.expression.Expression) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ColumnDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition)

Aggregations

SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)55 Test (org.junit.Test)14 Test (org.testng.annotations.Test)14 BaseTest (util.BaseTest)14 DataAccessException (org.jooq.exception.DataAccessException)9 HashMap (java.util.HashMap)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)6 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)6 TableConfig (io.mycat.config.model.TableConfig)6 SQLNonTransientException (java.sql.SQLNonTransientException)6 Expression (com.alibaba.cobar.parser.ast.expression.Expression)5 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 SchemaConfig (io.mycat.config.model.SchemaConfig)4 DruidShardingParseInfo (io.mycat.route.parser.druid.DruidShardingParseInfo)4 MycatSchemaStatVisitor (io.mycat.route.parser.druid.MycatSchemaStatVisitor)4 MycatStatementParser (io.mycat.route.parser.druid.MycatStatementParser)4 SQLException (java.sql.SQLException)4 DataSource (javax.sql.DataSource)4