Пример #1
0
  private Set<String> getDataNodeDbTypeMap(String dataNode) {
    Set<String> dbTypes = new HashSet<>();
    String[] dataNodeArr = SplitUtil.split(dataNode, ',', '$', '-');
    for (String node : dataNodeArr) {
      DataNodeConfig datanode = dataNodes.get(node);
      DataHostConfig datahost = dataHosts.get(datanode.getDataHost());
      dbTypes.add(datahost.getDbType());
    }

    return dbTypes;
  }
Пример #2
0
  private void loadSchemas(Element root) {
    NodeList list = root.getElementsByTagName("schema");
    for (int i = 0, n = list.getLength(); i < n; i++) {
      Element schemaElement = (Element) list.item(i);
      String name = schemaElement.getAttribute("name");
      String dataNode = schemaElement.getAttribute("dataNode");
      String checkSQLSchemaStr = schemaElement.getAttribute("checkSQLschema");
      String sqlMaxLimitStr = schemaElement.getAttribute("sqlMaxLimit");
      int sqlMaxLimit = -1;
      if (sqlMaxLimitStr != null && !sqlMaxLimitStr.isEmpty()) {
        sqlMaxLimit = Integer.valueOf(sqlMaxLimitStr);
      }
      // check dataNode already exists or not
      String defaultDbType = null;
      if (dataNode != null && !dataNode.isEmpty()) {
        List<String> dataNodeLst = new ArrayList<String>(1);
        dataNodeLst.add(dataNode);
        checkDataNodeExists(dataNodeLst);
        String dataHost = dataNodes.get(dataNode).getDataHost();
        defaultDbType = dataHosts.get(dataHost).getDbType();
      } else {
        dataNode = null;
      }
      Map<String, TableConfig> tables = loadTables(schemaElement);
      if (schemas.containsKey(name)) {
        throw new ConfigException("schema " + name + " duplicated!");
      }

      // 设置了table的不需要设置dataNode属性,没有设置table的必须设置dataNode属性
      if (dataNode == null && tables.size() == 0) {
        throw new ConfigException(
            "schema " + name + " didn't config tables,so you must set dataNode property!");
      }

      SchemaConfig schemaConfig =
          new SchemaConfig(
              name, dataNode, tables, sqlMaxLimit, "true".equalsIgnoreCase(checkSQLSchemaStr));
      if (defaultDbType != null) {
        schemaConfig.setDefaultDataNodeDbType(defaultDbType);
        if (!"mysql".equalsIgnoreCase(defaultDbType)) {
          schemaConfig.setNeedSupportMultiDBType(true);
        }
      }

      // 判断是否有不是mysql的数据库类型,方便解析判断是否启用多数据库分页语法解析

      for (String tableName : tables.keySet()) {
        TableConfig tableConfig = tables.get(tableName);
        if (isHasMultiDbType(tableConfig)) {
          schemaConfig.setNeedSupportMultiDBType(true);
          break;
        }
      }
      Map<String, String> dataNodeDbTypeMap = new HashMap<>();
      for (String dataNodeName : dataNodes.keySet()) {
        DataNodeConfig dataNodeConfig = dataNodes.get(dataNodeName);
        String dataHost = dataNodeConfig.getDataHost();
        DataHostConfig dataHostConfig = dataHosts.get(dataHost);
        if (dataHostConfig != null) {
          String dbType = dataHostConfig.getDbType();
          dataNodeDbTypeMap.put(dataNodeName, dbType);
        }
      }
      schemaConfig.setDataNodeDbTypeMap(dataNodeDbTypeMap);
      schemas.put(name, schemaConfig);
    }
  }