Ejemplo n.º 1
0
  /**
   * 根据show语句获取数据路由集合
   *
   * @param schema 数据库名
   * @param rrs 数据路由集合
   * @param stmt 执行的语句
   * @return RouteResultset数据路由集合
   * @throws SQLSyntaxErrorException
   * @author mycat
   */
  public RouteResultset analyseShowSQL(SchemaConfig schema, RouteResultset rrs, String stmt)
      throws SQLSyntaxErrorException {
    String upStmt = stmt.toUpperCase();
    int tabInd = upStmt.indexOf(" TABLES");
    if (tabInd > 0) { // show tables
      int[] nextPost = getSpecPos(upStmt, 0);
      if (nextPost[0] > 0) { // remove db info
        int end = getSpecEndPos(upStmt, tabInd);
        if (upStmt.indexOf(" FULL") > 0) {
          stmt = "SHOW FULL TABLES" + stmt.substring(end);
        } else {
          stmt = "SHOW TABLES" + stmt.substring(end);
        }
      }
      return routeToMultiNode(schema, false, false, null, rrs, schema.getMetaDataNodes(), stmt);
    }
    // show index or column
    int[] indx = getSpecPos(upStmt, 0);
    if (indx[0] > 0) {
      // has table
      int[] repPos = {indx[0] + indx[1], 0};
      String tableName = RouterUtil.getTableName(stmt, repPos);
      // IN DB pattern
      int[] indx2 = getSpecPos(upStmt, indx[0] + indx[1] + 1);
      if (indx2[0] > 0) { // find LIKE OR WHERE
        repPos[1] = getSpecEndPos(upStmt, indx2[0] + indx2[1]);
      }
      stmt = stmt.substring(0, indx[0]) + " FROM " + tableName + stmt.substring(repPos[1]);
      RouterUtil.routeForTableMeta(rrs, schema, tableName, stmt);
      return rrs;
    }
    // show create table tableName
    int[] createTabInd = getCreateTablePos(upStmt, 0);
    if (createTabInd[0] > 0) {
      int tableNameIndex = createTabInd[0] + createTabInd[1];
      if (upStmt.length() > tableNameIndex) {
        String tableName = stmt.substring(tableNameIndex).trim();
        int ind2 = tableName.indexOf('.');
        if (ind2 > 0) {
          tableName = tableName.substring(ind2 + 1);
        }
        RouterUtil.routeForTableMeta(rrs, schema, tableName, stmt);
        return rrs;
      }
    }

    return RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode(), stmt);
  }
Ejemplo n.º 2
0
 /**
  * 对Desc语句进行分析 返回数据路由集合
  *
  * @param schema 数据库名
  * @param rrs 数据路由集合
  * @param stmt 执行语句
  * @param ind 第一个' '的位置
  * @return RouteResultset(数据路由集合)
  * @author mycat
  */
 private static RouteResultset analyseDescrSQL(
     SchemaConfig schema, RouteResultset rrs, String stmt, int ind) {
   int[] repPos = {ind, 0};
   String tableName = RouterUtil.getTableName(stmt, repPos);
   stmt = stmt.substring(0, ind) + tableName + stmt.substring(repPos[1]);
   RouterUtil.routeForTableMeta(rrs, schema, tableName, stmt);
   return rrs;
 }