Search in sources :

Example 6 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class XMLSchemaLoader method processChildTables.

private void processChildTables(Map<String, TableConfig> tables, TableConfig parentTable, String dataNodes, Element tableNode) {
    // parse child tables
    NodeList childNodeList = tableNode.getChildNodes();
    for (int j = 0; j < childNodeList.getLength(); j++) {
        Node theNode = childNodeList.item(j);
        if (!theNode.getNodeName().equals("childTable")) {
            continue;
        }
        Element childTbElement = (Element) theNode;
        // 读取子表信息
        String cdTbName = childTbElement.getAttribute("name").toUpperCase();
        String primaryKey = childTbElement.hasAttribute("primaryKey") ? childTbElement.getAttribute("primaryKey").toUpperCase() : null;
        boolean autoIncrement = false;
        if (childTbElement.hasAttribute("autoIncrement")) {
            autoIncrement = Boolean.parseBoolean(childTbElement.getAttribute("autoIncrement"));
        }
        boolean needAddLimit = true;
        if (childTbElement.hasAttribute("needAddLimit")) {
            needAddLimit = Boolean.parseBoolean(childTbElement.getAttribute("needAddLimit"));
        }
        String subTables = childTbElement.getAttribute("subTables");
        // 子表join键,和对应的parent的键,父子表通过这个关联
        String joinKey = childTbElement.getAttribute("joinKey").toUpperCase();
        String parentKey = childTbElement.getAttribute("parentKey").toUpperCase();
        TableConfig table = new TableConfig(cdTbName, primaryKey, autoIncrement, needAddLimit, TableConfig.TYPE_GLOBAL_DEFAULT, dataNodes, getDbType(dataNodes), null, false, parentTable, true, joinKey, parentKey, subTables);
        if (tables.containsKey(table.getName())) {
            throw new ConfigException("table " + table.getName() + " duplicated!");
        }
        tables.put(table.getName(), table);
        // 对于子表的子表,递归处理
        processChildTables(tables, table, dataNodes, childTbElement);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) TableConfig(io.mycat.config.model.TableConfig) ConfigException(io.mycat.config.util.ConfigException)

Example 7 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class ConfigTest method testDynamicYYYYMMTable.

/**
 * 测试 动态日期表
 *
 * @throws Exception
 */
@Test
public void testDynamicYYYYMMTable() throws Exception {
    SchemaConfig sc = this.schemas.get("dbtest1");
    Map<String, TableConfig> tbm = sc.getTables();
    Assert.assertTrue(tbm.size() == 32);
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) TableConfig(io.mycat.config.model.TableConfig) Test(org.junit.Test)

Example 8 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

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 9 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class DruidCreateTableParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement) stmt;
    if (createStmt.getQuery() != null) {
        String msg = "create table from other table not supported :" + stmt;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String tableName = StringUtil.removeBackquote(createStmt.getTableSource().toString().toUpperCase());
    if (schema.getTables().containsKey(tableName)) {
        TableConfig tableConfig = schema.getTables().get(tableName);
        AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
        if (algorithm instanceof SlotFunction) {
            SQLColumnDefinition column = new SQLColumnDefinition();
            column.setDataType(new SQLCharacterDataType("int"));
            column.setName(new SQLIdentifierExpr("_slot"));
            column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
            ((SQLCreateTableStatement) stmt).getTableElementList().add(column);
            String sql = createStmt.toString();
            rrs.setStatement(sql);
            ctx.setSql(sql);
        }
    }
    ctx.addTable(tableName);
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLNonTransientException(java.sql.SQLNonTransientException) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) TableConfig(io.mycat.config.model.TableConfig) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SlotFunction(io.mycat.route.function.SlotFunction)

Example 10 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class DruidInsertParser method parserChildTable.

private RouteResultset parserChildTable(SchemaConfig schema, RouteResultset rrs, String tableName, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    TableConfig tc = schema.getTables().get(tableName);
    String joinKey = tc.getJoinKey();
    int joinKeyIndex = getJoinKeyIndex(insertStmt.getColumns(), joinKey);
    if (joinKeyIndex == -1) {
        String inf = "joinKey not provided :" + tc.getJoinKey() + "," + insertStmt;
        LOGGER.warn(inf);
        throw new SQLNonTransientException(inf);
    }
    if (isMultiInsert(insertStmt)) {
        String msg = "ChildTable multi insert not provided";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String joinKeyVal = insertStmt.getValues().getValues().get(joinKeyIndex).toString();
    String sql = insertStmt.toString();
    // try to route by ER parent partion key
    RouteResultset theRrs = RouterUtil.routeByERParentKey(null, schema, ServerParse.INSERT, sql, rrs, tc, joinKeyVal);
    if (theRrs != null) {
        rrs.setFinishedRoute(true);
        return theRrs;
    }
    // route by sql query root parent's datanode
    String findRootTBSql = tc.getLocateRTableKeySql().toLowerCase() + joinKeyVal;
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("find root parent's node sql " + findRootTBSql);
    }
    FetchStoreNodeOfChildTableHandler fetchHandler = new FetchStoreNodeOfChildTableHandler();
    String dn = fetchHandler.execute(schema.getName(), findRootTBSql, tc.getRootParent().getDataNodes());
    if (dn == null) {
        throw new SQLNonTransientException("can't find (root) parent sharding node for sql:" + sql);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("found partion node for child table to insert " + dn + " sql :" + sql);
    }
    return RouterUtil.routeToSingleNode(rrs, dn, sql);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) FetchStoreNodeOfChildTableHandler(io.mycat.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler) TableConfig(io.mycat.config.model.TableConfig) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

TableConfig (io.mycat.config.model.TableConfig)84 SchemaConfig (io.mycat.config.model.SchemaConfig)26 SQLNonTransientException (java.sql.SQLNonTransientException)26 Test (org.junit.Test)18 RuleConfig (io.mycat.config.model.rule.RuleConfig)16 RouteResultset (io.mycat.route.RouteResultset)14 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)14 HashMap (java.util.HashMap)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SlotFunction (io.mycat.route.function.SlotFunction)12 ArrayList (java.util.ArrayList)12 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)8 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)8 ConfigException (io.mycat.config.util.ConfigException)8 RouteResultsetNode (io.mycat.route.RouteResultsetNode)8 HashSet (java.util.HashSet)8 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)6 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)6 CacheService (io.mycat.cache.CacheService)6