Example #1
0
  /**
   * 修复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")) {
      tablename = RouterUtil.getTableName(stmt, RouterUtil.getCreateTablePos(upStmt, 0));
    } else if (upStmt.startsWith("DROP")) {
      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;
        if (tables != null && (tc = tables.get(tablename)) != null) {
          dataNodes = tc.getDataNodes();
        }
        Iterator<String> iterator1 = dataNodes.iterator();
        int nodeSize = dataNodes.size();
        RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSize];

        for (int i = 0; i < nodeSize; i++) {
          String name = iterator1.next();
          nodes[i] = new RouteResultsetNode(name, sqlType, stmt);
        }
        rrs.setNodes(nodes);
      }
      return rrs;
    } else if (schema.getDataNode() != null) { // 默认节点ddl
      RouteResultsetNode[] nodes = new RouteResultsetNode[1];
      nodes[0] = new RouteResultsetNode(schema.getDataNode(), sqlType, stmt);
      rrs.setNodes(nodes);
      return rrs;
    }
    // 不在,返回null
    LOGGER.error("table not in schema----" + tablename);
    throw new SQLSyntaxErrorException("op table not in schema----" + tablename);
  }
Example #2
0
 /**
  * 获取第一个节点作为路由
  *
  * @param rrs 数据路由集合
  * @param dataNode 数据库所在节点
  * @param stmt 执行语句
  * @return 数据路由集合
  * @author mycat
  */
 public static RouteResultset routeToSingleNode(RouteResultset rrs, String dataNode, String stmt) {
   if (dataNode == null) {
     return rrs;
   }
   RouteResultsetNode[] nodes = new RouteResultsetNode[1];
   nodes[0] = new RouteResultsetNode(dataNode, rrs.getSqlType(), stmt); // rrs.getStatement()
   rrs.setNodes(nodes);
   rrs.setFinishedRoute(true);
   if (rrs.getCanRunInReadDB() != null) {
     nodes[0].setCanRunInReadDB(rrs.getCanRunInReadDB());
   }
   return rrs;
 }
Example #3
0
  public static void routeForTableMeta(
      RouteResultset rrs, SchemaConfig schema, String tableName, String sql) {
    String dataNode = null;
    if (isNoSharding(schema, tableName)) { // 不分库的直接从schema中获取dataNode
      dataNode = schema.getDataNode();
    } else {
      dataNode = getMetaReadDataNode(schema, tableName);
    }

    RouteResultsetNode[] nodes = new RouteResultsetNode[1];
    nodes[0] = new RouteResultsetNode(dataNode, rrs.getSqlType(), sql);
    if (rrs.getCanRunInReadDB() != null) {
      nodes[0].setCanRunInReadDB(rrs.getCanRunInReadDB());
    }
    rrs.setNodes(nodes);
  }
Example #4
0
 public static RouteResultset routeToMultiNode(
     boolean cache, RouteResultset rrs, Collection<String> dataNodes, String stmt) {
   RouteResultsetNode[] nodes = new RouteResultsetNode[dataNodes.size()];
   int i = 0;
   RouteResultsetNode node;
   for (String dataNode : dataNodes) {
     node = new RouteResultsetNode(dataNode, rrs.getSqlType(), stmt);
     if (rrs.getCanRunInReadDB() != null) {
       node.setCanRunInReadDB(rrs.getCanRunInReadDB());
     }
     nodes[i++] = node;
   }
   rrs.setCacheAble(cache);
   rrs.setNodes(nodes);
   return rrs;
 }