Search in sources :

Example 6 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project jdk8u_jdk by JetBrains.

the class SQLSyntaxErrorExceptionTests method test.

/**
     * Create SQLSyntaxErrorException and setting all objects to null
     */
@Test
public void test() {
    SQLSyntaxErrorException e = new SQLSyntaxErrorException(null, null, errorCode, null);
    assertTrue(e.getMessage() == null && e.getSQLState() == null && e.getCause() == null && e.getErrorCode() == errorCode);
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 7 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project Mycat_plus by coderczp.

the class DruidInsertParser method statementParse.

/**
 * 考虑因素:isChildTable、批量、是否分片
 */
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlInsertStatement insert = (MySqlInsertStatement) stmt;
    String tableName = StringUtil.removeBackquote(insert.getTableName().getSimpleName()).toUpperCase();
    ctx.addTable(tableName);
    if (RouterUtil.isNoSharding(schema, tableName)) {
        // 整个schema都不分库或者该表不拆分
        RouterUtil.routeForTableMeta(rrs, schema, tableName, rrs.getStatement());
        rrs.setFinishedRoute(true);
        return;
    }
    TableConfig tc = schema.getTables().get(tableName);
    if (tc == null) {
        String msg = "can't find table define in schema " + tableName + " schema:" + schema.getName();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    } else {
        // childTable的insert直接在解析过程中完成路由
        if (tc.isChildTable()) {
            parserChildTable(schema, rrs, tableName, insert);
            return;
        }
        String partitionColumn = tc.getPartitionColumn();
        if (partitionColumn != null) {
            // 拆分表必须给出column list,否则无法寻找分片字段的值
            if (insert.getColumns() == null || insert.getColumns().size() == 0) {
                throw new SQLSyntaxErrorException("partition table, insert must provide ColumnList");
            }
            // 批量insert
            if (isMultiInsert(insert)) {
                // String msg = "multi insert not provided" ;
                // LOGGER.warn(msg);
                // throw new SQLNonTransientException(msg);
                parserBatchInsert(schema, rrs, partitionColumn, tableName, insert);
            } else {
                parserSingleInsert(schema, rrs, partitionColumn, tableName, insert);
            }
        }
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) TableConfig(io.mycat.config.model.TableConfig) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)

Example 8 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project Mycat_plus by coderczp.

the class DruidMycatRouteStrategy method getDisTable.

private SQLExprTableSource getDisTable(SQLTableSource tableSource, RouteResultsetNode node) throws SQLSyntaxErrorException {
    if (node.getSubTableName() == null) {
        String msg = " sub table not exists for " + node.getName() + " on " + tableSource;
        LOGGER.error("DruidMycatRouteStrategyError " + msg);
        throw new SQLSyntaxErrorException(msg);
    }
    SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
    sqlIdentifierExpr.setParent(tableSource.getParent());
    sqlIdentifierExpr.setName(node.getSubTableName());
    SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
    return from2;
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 9 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project Mycat_plus by coderczp.

the class RouteService method route.

public RouteResultset route(SystemConfig sysconf, SchemaConfig schema, int sqlType, String stmt, String charset, ServerConnection sc) throws SQLNonTransientException {
    RouteResultset rrs = null;
    String cacheKey = null;
    /**
     *  SELECT 类型的SQL, 检测
     */
    if (sqlType == ServerParse.SELECT) {
        cacheKey = schema.getName() + stmt;
        rrs = (RouteResultset) sqlRouteCache.get(cacheKey);
        if (rrs != null) {
            checkMigrateRule(schema.getName(), rrs, sqlType);
            return rrs;
        }
    }
    /*!mycat: sql = select name from aa */
    /*!mycat: schema = test */
    // boolean isMatchOldHint = stmt.startsWith(OLD_MYCAT_HINT);
    // boolean isMatchNewHint = stmt.startsWith(NEW_MYCAT_HINT);
    // if (isMatchOldHint || isMatchNewHint ) {
    int hintLength = RouteService.isHintSql(stmt);
    if (hintLength != -1) {
        int endPos = stmt.indexOf("*/");
        if (endPos > 0) {
            // 用!mycat:内部的语句来做路由分析
            // int hintLength = isMatchOldHint ? OLD_MYCAT_HINT.length() : NEW_MYCAT_HINT.length();
            String hint = stmt.substring(hintLength, endPos).trim();
            int firstSplitPos = hint.indexOf(HINT_SPLIT);
            if (firstSplitPos > 0) {
                Map hintMap = parseHint(hint);
                String hintType = (String) hintMap.get(MYCAT_HINT_TYPE);
                String hintSql = (String) hintMap.get(hintType);
                if (hintSql.length() == 0) {
                    LOGGER.warn("comment int sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                    throw new SQLSyntaxErrorException("comment int sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                }
                String realSQL = stmt.substring(endPos + "*/".length()).trim();
                HintHandler hintHandler = HintHandlerFactory.getHintHandler(hintType);
                if (hintHandler != null) {
                    if (hintHandler instanceof HintSQLHandler) {
                        /**
                         * 修复 注解SQL的 sqlType 与 实际SQL的 sqlType 不一致问题, 如: hint=SELECT,real=INSERT
                         * fixed by zhuam
                         */
                        int hintSqlType = ServerParse.parse(hintSql) & 0xff;
                        rrs = hintHandler.route(sysconf, schema, sqlType, realSQL, charset, sc, tableId2DataNodeCache, hintSql, hintSqlType, hintMap);
                    } else {
                        rrs = hintHandler.route(sysconf, schema, sqlType, realSQL, charset, sc, tableId2DataNodeCache, hintSql, sqlType, hintMap);
                    }
                } else {
                    LOGGER.warn("TODO , support hint sql type : " + hintType);
                }
            } else {
                // fixed by runfriends@126.com
                LOGGER.warn("comment in sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                throw new SQLSyntaxErrorException("comment in sql must meet :/*!mcat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
            }
        }
    } else {
        stmt = stmt.trim();
        rrs = RouteStrategyFactory.getRouteStrategy().route(sysconf, schema, sqlType, stmt, charset, sc, tableId2DataNodeCache);
    }
    if (rrs != null && sqlType == ServerParse.SELECT && rrs.isCacheAble()) {
        sqlRouteCache.putIfAbsent(cacheKey, rrs);
    }
    checkMigrateRule(schema.getName(), rrs, sqlType);
    return rrs;
}
Also used : HintHandler(io.mycat.route.handler.HintHandler) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) HintSQLHandler(io.mycat.route.handler.HintSQLHandler) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 10 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project Mycat_plus by coderczp.

the class RouterUtil method routeToDDLNode.

/**
 * 修复DDL路由
 *
 * @return RouteResultset
 * @author aStoneGod
 */
public static RouteResultset routeToDDLNode(RouteResultset rrs, int sqlType, String stmt, SchemaConfig schema) throws SQLSyntaxErrorException {
    stmt = getFixedSql(stmt);
    String tablename = "";
    final String upStmt = stmt.toUpperCase();
    if (upStmt.startsWith("CREATE")) {
        if (upStmt.contains("CREATE INDEX ") || upStmt.contains("CREATE UNIQUE INDEX ")) {
            tablename = RouterUtil.getTableName(stmt, RouterUtil.getCreateIndexPos(upStmt, 0));
        } else {
            tablename = RouterUtil.getTableName(stmt, RouterUtil.getCreateTablePos(upStmt, 0));
        }
    } else if (upStmt.startsWith("DROP")) {
        if (upStmt.contains("DROP INDEX ")) {
            tablename = RouterUtil.getTableName(stmt, RouterUtil.getDropIndexPos(upStmt, 0));
        } else {
            tablename = RouterUtil.getTableName(stmt, RouterUtil.getDropTablePos(upStmt, 0));
        }
    } else if (upStmt.startsWith("ALTER")) {
        tablename = RouterUtil.getTableName(stmt, RouterUtil.getAlterTablePos(upStmt, 0));
    } else if (upStmt.startsWith("TRUNCATE")) {
        tablename = RouterUtil.getTableName(stmt, RouterUtil.getTruncateTablePos(upStmt, 0));
    }
    tablename = tablename.toUpperCase();
    if (schema.getTables().containsKey(tablename)) {
        if (ServerParse.DDL == sqlType) {
            List<String> dataNodes = new ArrayList<>();
            Map<String, TableConfig> tables = schema.getTables();
            TableConfig tc = tables.get(tablename);
            if (tables != null && (tc != null)) {
                dataNodes = tc.getDataNodes();
            }
            boolean isSlotFunction = tc.getRule() != null && tc.getRule().getRuleAlgorithm() instanceof SlotFunction;
            Iterator<String> iterator1 = dataNodes.iterator();
            int nodeSize = dataNodes.size();
            RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSize];
            if (isSlotFunction) {
                stmt = changeCreateTable(schema, tablename, stmt);
            }
            for (int i = 0; i < nodeSize; i++) {
                String name = iterator1.next();
                nodes[i] = new RouteResultsetNode(name, sqlType, stmt);
                nodes[i].setSource(rrs);
                if (rrs.getDataNodeSlotMap().containsKey(name)) {
                    nodes[i].setSlot(rrs.getDataNodeSlotMap().get(name));
                } else if (isSlotFunction) {
                    nodes[i].setSlot(-1);
                }
            }
            rrs.setNodes(nodes);
        }
        return rrs;
    } else if (schema.getDataNode() != null) {
        // 默认节点ddl
        RouteResultsetNode[] nodes = new RouteResultsetNode[1];
        nodes[0] = new RouteResultsetNode(schema.getDataNode(), sqlType, stmt);
        nodes[0].setSource(rrs);
        rrs.setNodes(nodes);
        return rrs;
    }
    // both tablename and defaultnode null
    LOGGER.error("table not in schema----" + tablename);
    throw new SQLSyntaxErrorException("op table not in schema----" + tablename);
}
Also used : RouteResultsetNode(io.mycat.route.RouteResultsetNode) ArrayList(java.util.ArrayList) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) TableConfig(io.mycat.config.model.TableConfig) SlotFunction(io.mycat.route.function.SlotFunction)

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