Search in sources :

Example 11 with SQLNonTransientException

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

the class RouterUtil method routeByERParentKey.

/**
	 * 根据 ER分片规则获取路由集合
	 *
	 * @param stmt            执行的语句
	 * @param rrs      		     数据路由集合
	 * @param tc	      	     表实体
	 * @param joinKeyVal      连接属性
	 * @return RouteResultset(数据路由集合)	 *
	 * @throws SQLNonTransientException,IllegalShardingColumnValueException
	 * @author mycat
	 */
public static RouteResultset routeByERParentKey(ServerConnection sc, SchemaConfig schema, int sqlType, String stmt, RouteResultset rrs, TableConfig tc, String joinKeyVal) throws SQLNonTransientException {
    // table's partition key
    if (tc.isSecondLevel() && //判断是否为二级子表(父表不再有父表)
    tc.getParentTC().getPartitionColumn().equals(tc.getParentKey())) {
        // using
        // parent
        // rule to
        // find
        // datanode
        Set<ColumnRoutePair> parentColVal = new HashSet<ColumnRoutePair>(1);
        ColumnRoutePair pair = new ColumnRoutePair(joinKeyVal);
        parentColVal.add(pair);
        Set<String> dataNodeSet = ruleCalculate(tc.getParentTC(), parentColVal, rrs.getDataNodeSlotMap());
        if (dataNodeSet.isEmpty() || dataNodeSet.size() > 1) {
            throw new SQLNonTransientException("parent key can't find  valid datanode ,expect 1 but found: " + dataNodeSet.size());
        }
        String dn = dataNodeSet.iterator().next();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("found partion node (using parent partion rule directly) for child table to insert  " + dn + " sql :" + stmt);
        }
        return RouterUtil.routeToSingleNode(rrs, dn, stmt);
    }
    return null;
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair)

Example 12 with SQLNonTransientException

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

the class ServerLoadDataInfileHandler method tryDirectRoute.

private RouteResultset tryDirectRoute(String sql, String[] lineList) {
    RouteResultset rrs = new RouteResultset(sql, ServerParse.INSERT);
    rrs.setLoadData(true);
    if (tableConfig == null && schema.getDataNode() != null) {
        //走默认节点
        RouteResultsetNode rrNode = new RouteResultsetNode(schema.getDataNode(), ServerParse.INSERT, sql);
        rrNode.setSource(rrs);
        rrs.setNodes(new RouteResultsetNode[] { rrNode });
        return rrs;
    } else if (tableConfig != null && tableConfig.isGlobalTable()) {
        ArrayList<String> dataNodes = tableConfig.getDataNodes();
        RouteResultsetNode[] rrsNodes = new RouteResultsetNode[dataNodes.size()];
        for (int i = 0, dataNodesSize = dataNodes.size(); i < dataNodesSize; i++) {
            String dataNode = dataNodes.get(i);
            RouteResultsetNode rrNode = new RouteResultsetNode(dataNode, ServerParse.INSERT, sql);
            rrsNodes[i] = rrNode;
            if (rrs.getDataNodeSlotMap().containsKey(dataNode)) {
                rrsNodes[i].setSlot(rrs.getDataNodeSlotMap().get(dataNode));
            }
            rrsNodes[i].setSource(rrs);
        }
        rrs.setNodes(rrsNodes);
        return rrs;
    } else if (tableConfig != null) {
        DruidShardingParseInfo ctx = new DruidShardingParseInfo();
        ctx.addTable(tableName);
        if (partitionColumnIndex == -1 || partitionColumnIndex >= lineList.length) {
            return null;
        } else {
            String value = lineList[partitionColumnIndex];
            RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
            routeCalculateUnit.addShardingExpr(tableName, getPartitionColumn(), parseFieldString(value, loadData.getEnclose()));
            ctx.addRouteCalculateUnit(routeCalculateUnit);
            try {
                SortedSet<RouteResultsetNode> nodeSet = new TreeSet<RouteResultsetNode>();
                for (RouteCalculateUnit unit : ctx.getRouteCalculateUnits()) {
                    RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, ctx, unit, rrs, false, tableId2DataNodeCache);
                    if (rrsTmp != null) {
                        for (RouteResultsetNode node : rrsTmp.getNodes()) {
                            nodeSet.add(node);
                        }
                    }
                }
                RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
                int i = 0;
                for (Iterator<RouteResultsetNode> iterator = nodeSet.iterator(); iterator.hasNext(); ) {
                    nodes[i] = (RouteResultsetNode) iterator.next();
                    i++;
                }
                rrs.setNodes(nodes);
                return rrs;
            } catch (SQLNonTransientException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return null;
}
Also used : DruidShardingParseInfo(io.mycat.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) ArrayList(java.util.ArrayList) SQLNonTransientException(java.sql.SQLNonTransientException) TreeSet(java.util.TreeSet) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 13 with SQLNonTransientException

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

the class DruidUpdateParserTest method throwExceptionParse.

public void throwExceptionParse(String sql, boolean throwException) throws NoSuchMethodException {
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement sqlStatement = statementList.get(0);
    MySqlUpdateStatement update = (MySqlUpdateStatement) sqlStatement;
    SchemaConfig schemaConfig = mock(SchemaConfig.class);
    Map<String, TableConfig> tables = mock(Map.class);
    TableConfig tableConfig = mock(TableConfig.class);
    String tableName = "hotnews";
    when((schemaConfig).getTables()).thenReturn(tables);
    when(tables.get(tableName)).thenReturn(tableConfig);
    when(tableConfig.getParentTC()).thenReturn(null);
    RouteResultset routeResultset = new RouteResultset(sql, 11);
    Class c = DruidUpdateParser.class;
    Method method = c.getDeclaredMethod("confirmShardColumnNotUpdated", new Class[] { SQLUpdateStatement.class, SchemaConfig.class, String.class, String.class, String.class, RouteResultset.class });
    method.setAccessible(true);
    try {
        method.invoke(c.newInstance(), update, schemaConfig, tableName, "ID", "", routeResultset);
        if (throwException) {
            System.out.println("未抛异常,解析通过则不对!");
            Assert.assertTrue(false);
        } else {
            System.out.println("未抛异常,解析通过,此情况分片字段可能在update语句中但是实际不会被更新");
            Assert.assertTrue(true);
        }
    } catch (Exception e) {
        if (throwException) {
            System.out.println(e.getCause().getClass());
            Assert.assertTrue(e.getCause() instanceof SQLNonTransientException);
            System.out.println("抛异常原因为SQLNonTransientException则正确");
        } else {
            System.out.println("抛异常,需要检查");
            Assert.assertTrue(false);
        }
    }
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) Method(java.lang.reflect.Method) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLNonTransientException(java.sql.SQLNonTransientException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) SQLNonTransientException(java.sql.SQLNonTransientException) TableConfig(io.mycat.config.model.TableConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) DruidUpdateParser(io.mycat.route.parser.druid.impl.DruidUpdateParser) RouteResultset(io.mycat.route.RouteResultset)

Example 14 with SQLNonTransientException

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

the class SQLInvalidAuthorizationSpecExceptionTests method test13.

/**
     * Create SQLInvalidAuthorizationSpecException and validate it is an
     * instance of SQLNonTransientException
     */
@Test
public void test13() {
    Exception ex = new SQLInvalidAuthorizationSpecException();
    assertTrue(ex instanceof SQLNonTransientException);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLInvalidAuthorizationSpecException(java.sql.SQLInvalidAuthorizationSpecException) SQLException(java.sql.SQLException) SQLNonTransientException(java.sql.SQLNonTransientException) SQLInvalidAuthorizationSpecException(java.sql.SQLInvalidAuthorizationSpecException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 15 with SQLNonTransientException

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

the class SQLIntegrityConstraintViolationExceptionTests method test13.

/**
     * Create SQLIntegrityConstraintViolationException and validate it is an instance of
     * SQLNonTransientException
     */
@Test
public void test13() {
    Exception ex = new SQLIntegrityConstraintViolationException();
    assertTrue(ex instanceof SQLNonTransientException);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SQLException(java.sql.SQLException) SQLNonTransientException(java.sql.SQLNonTransientException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Aggregations

SQLNonTransientException (java.sql.SQLNonTransientException)49 Test (org.testng.annotations.Test)19 BaseTest (util.BaseTest)19 TableConfig (io.mycat.config.model.TableConfig)12 RouteResultset (io.mycat.route.RouteResultset)8 SQLException (java.sql.SQLException)8 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 ColumnRoutePair (io.mycat.sqlengine.mpp.ColumnRoutePair)6 SlotFunction (io.mycat.route.function.SlotFunction)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)4 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)4 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)3 SchemaConfig (io.mycat.config.model.SchemaConfig)3 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)3 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)2 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)2 MySqlUpdateStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement)2