Search in sources :

Example 1 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project Mycat-Server by MyCATApache.

the class DruidMycatRouteStrategy method routeNormalSqlWithAST.

@Override
public RouteResultset routeNormalSqlWithAST(SchemaConfig schema, String stmt, RouteResultset rrs, String charset, LayerCachePool cachePool) throws SQLNonTransientException {
    /**
		 *  只有mysql时只支持mysql语法
		 */
    SQLStatementParser parser = null;
    if (schema.isNeedSupportMultiDBType()) {
        parser = new MycatStatementParser(stmt);
    } else {
        parser = new MySqlStatementParser(stmt);
    }
    MycatSchemaStatVisitor visitor = null;
    SQLStatement statement;
    /**
		 * 解析出现问题统一抛SQL语法错误
		 */
    try {
        statement = parser.parseStatement();
        visitor = new MycatSchemaStatVisitor();
    } catch (Exception t) {
        LOGGER.error("DruidMycatRouteStrategyError", t);
        throw new SQLSyntaxErrorException(t);
    }
    /**
		 * 检验unsupported statement
		 */
    checkUnSupportedStatement(statement);
    DruidParser druidParser = DruidParserFactory.create(schema, statement, visitor);
    druidParser.parser(schema, rrs, statement, stmt, cachePool, visitor);
    DruidShardingParseInfo ctx = druidParser.getCtx();
    rrs.setTables(ctx.getTables());
    /**
		 * DruidParser 解析过程中已完成了路由的直接返回
		 */
    if (rrs.isFinishedRoute()) {
        return rrs;
    }
    /**
		 * 没有from的select语句或其他
		 */
    if ((ctx.getTables() == null || ctx.getTables().size() == 0) && (ctx.getTableAliasMap() == null || ctx.getTableAliasMap().isEmpty())) {
        return RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode(), druidParser.getCtx().getSql());
    }
    if (druidParser.getCtx().getRouteCalculateUnits().size() == 0) {
        RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
        druidParser.getCtx().addRouteCalculateUnit(routeCalculateUnit);
    }
    SortedSet<RouteResultsetNode> nodeSet = new TreeSet<RouteResultsetNode>();
    for (RouteCalculateUnit unit : druidParser.getCtx().getRouteCalculateUnits()) {
        RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, druidParser.getCtx(), unit, rrs, isSelect(statement), cachePool);
        if (rrsTmp != null) {
            for (RouteResultsetNode node : rrsTmp.getNodes()) {
                nodeSet.add(node);
            }
        }
    }
    RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
    int i = 0;
    for (RouteResultsetNode aNodeSet : nodeSet) {
        nodes[i] = aNodeSet;
        if (statement instanceof MySqlInsertStatement && ctx.getTables().size() == 1 && schema.getTables().containsKey(ctx.getTables().get(0))) {
            RuleConfig rule = schema.getTables().get(ctx.getTables().get(0)).getRule();
            if (rule != null && rule.getRuleAlgorithm() instanceof SlotFunction) {
                aNodeSet.setStatement(ParseUtil.changeInsertAddSlot(aNodeSet.getStatement(), aNodeSet.getSlot()));
            }
        }
        i++;
    }
    rrs.setNodes(nodes);
    /**
		 *  subTables="t_order$1-2,t_order3"
		 *目前分表 1.6 开始支持 幵丏 dataNode 在分表条件下只能配置一个,分表条件下不支持join。
		 */
    if (rrs.isDistTable()) {
        return this.routeDisTable(statement, rrs);
    }
    return rrs;
}
Also used : DruidShardingParseInfo(io.mycat.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatStatementParser(io.mycat.route.parser.druid.MycatStatementParser) MycatSchemaStatVisitor(io.mycat.route.parser.druid.MycatSchemaStatVisitor) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLNonTransientException(java.sql.SQLNonTransientException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SlotFunction(io.mycat.route.function.SlotFunction) DruidParser(io.mycat.route.parser.druid.DruidParser) TreeSet(java.util.TreeSet) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RuleConfig(io.mycat.config.model.rule.RuleConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) RouteResultset(io.mycat.route.RouteResultset)

Example 2 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project zipkin by openzipkin.

the class SchemaTest method hasDependencies_missing.

@Test
public void hasDependencies_missing() throws SQLException {
    SQLSyntaxErrorException sqlException = new SQLSyntaxErrorException("SQL [select count(*) from `zipkin_dependencies`]; Table 'zipkin.zipkin_dependencies' doesn't exist\n" + "  Query is : select count(*) from `zipkin_dependencies`", "42S02", 1146);
    DataSource dataSource = mock(DataSource.class);
    // cheats to lower mock count: this exception is really thrown during execution of the query
    when(dataSource.getConnection()).thenThrow(new DataAccessException(sqlException.getMessage(), sqlException));
    assertThat(schema.hasPreAggregatedDependencies).isFalse();
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DataAccessException(org.jooq.exception.DataAccessException) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 3 with SQLSyntaxErrorException

use of java.sql.SQLSyntaxErrorException in project zipkin by openzipkin.

the class SchemaTest method hasIpv6_falseWhenKnownSQLState.

@Test
public void hasIpv6_falseWhenKnownSQLState() throws SQLException {
    SQLSyntaxErrorException sqlException = new SQLSyntaxErrorException("Unknown column 'zipkin_annotations.endpoint_ipv6' in 'field list'", "42S22", 1054);
    // cheats to lower mock count: this exception is really thrown during execution of the query
    when(dataSource.getConnection()).thenThrow(new DataAccessException(sqlException.getMessage(), sqlException));
    assertThat(schema.hasIpv6).isFalse();
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DataAccessException(org.jooq.exception.DataAccessException) Test(org.junit.Test)

Example 4 with SQLSyntaxErrorException

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

the class SQLSyntaxErrorExceptionTests method test3.

/**
     * Create SQLSyntaxErrorException with message, and SQLState
     */
@Test
public void test3() {
    SQLSyntaxErrorException ex = new SQLSyntaxErrorException(reason, state);
    assertTrue(ex.getMessage().equals(reason) && ex.getSQLState().equals(state) && ex.getCause() == null && ex.getErrorCode() == 0);
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 5 with SQLSyntaxErrorException

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

the class SQLSyntaxErrorExceptionTests method test1.

/**
     * Create SQLSyntaxErrorException with no-arg constructor
     */
@Test
public void test1() {
    SQLSyntaxErrorException ex = new SQLSyntaxErrorException();
    assertTrue(ex.getMessage() == null && ex.getSQLState() == null && ex.getCause() == null && ex.getErrorCode() == 0);
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

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