예제 #1
0
  private void readProperties(Properties info) {
    Object[] list = new Object[info.size()];
    info.keySet().toArray(list);
    DbSettings s = null;

    // 可在info中配三种参数,相关文档见:E:\H2\my-h2\my-h2-docs\999 可配置的参数汇总.java中的1、2、3项
    for (Object k : list) {
      String key = StringUtils.toUpperEnglish(k.toString());
      if (prop.containsKey(key)) {
        throw DbException.get(ErrorCode.DUPLICATE_PROPERTY_1, key);
      }
      Object value = info.get(k);
      // 支持org.h2.command.dml.SetTypes中的参数和ConnectionInfo与connectionTime相关的参数
      if (isKnownSetting(key)) {
        prop.put(key, value);
      } else {
        if (s == null) {
          s = getDbSettings();
        }
        // org.h2.constant.DbSettings中的参数
        if (s.containsKey(key)) {
          prop.put(key, value);
        }
      }
    }
  }
예제 #2
0
 public DbSettings getDbSettings() {
   DbSettings defaultSettings = DbSettings.getDefaultSettings();
   HashMap<String, String> s = New.hashMap();
   for (Object k : prop.keySet()) {
     String key = k.toString();
     if (!isKnownSetting(key) && defaultSettings.containsKey(key)) {
       s.put(key, prop.getProperty(key));
     }
   }
   return DbSettings.getInstance(s);
 }
예제 #3
0
 private void readSettingsFromURL() {
   // 如url=jdbc:h2:tcp://localhost:9092/test9;optimize_distinct=true;early_filter=true;nested_joins=false
   DbSettings defaultSettings = DbSettings.getDefaultSettings();
   int idx = url.indexOf(';'); // 用";"号来分隔参数,第一个";"号表明url和参数的分界,之后的";"号用来分隔多个参数
   if (idx >= 0) {
     // optimize_distinct=true;early_filter=true;nested_joins=false
     String settings = url.substring(idx + 1);
     // jdbc:h2:tcp://localhost:9092/test9
     url = url.substring(0, idx);
     // [optimize_distinct=true, early_filter=true, nested_joins=false]
     String[] list = StringUtils.arraySplit(settings, ';', false);
     for (String setting : list) {
       if (setting.length() == 0) {
         continue;
       }
       int equal = setting.indexOf('=');
       if (equal < 0) {
         throw getFormatException();
       }
       String value = setting.substring(equal + 1);
       String key = setting.substring(0, equal);
       key = StringUtils.toUpperEnglish(key);
       // info中除了可以配三种参数外(相关文档见:E:\H2\my-h2\my-h2-docs\999 可配置的参数汇总.java中的1、2、3项)
       // 还可以配其他参数,但是被忽略
       // 但是url中只能配三种参数
       if (!isKnownSetting(key) && !defaultSettings.containsKey(key)) {
         throw DbException.get(ErrorCode.UNSUPPORTED_SETTING_1, key);
       }
       // 不能与info中的参数重复(如果值相同就不会报错)
       // 例子见my.test.ConnectionInfoTest
       String old = prop.getProperty(key);
       if (old != null && !old.equals(value)) {
         throw DbException.get(ErrorCode.DUPLICATE_PROPERTY_1, key);
       }
       prop.setProperty(key, value);
     }
   }
 }
  protected DataStore getKeyList(
      TableIdentifier tbl, boolean getOwnFk, boolean includeNumericRuleValue) {
    if (cancel) return null;

    String cols[] = null;
    String refColName = null;
    DbSettings dbSettings = dbConnection.getDbSettings();

    if (getOwnFk) {
      refColName = "REFERENCES";
    } else {
      refColName = "REFERENCED BY";
    }
    int types[];
    int sizes[];

    if (includeNumericRuleValue) {
      cols =
          new String[] {
            "FK_NAME",
            "COLUMN",
            refColName,
            "UPDATE_RULE",
            "DELETE_RULE",
            "DEFERRABLE",
            "ENABLED",
            "VALIDATED",
            "UPDATE_RULE_VALUE",
            "DELETE_RULE_VALUE",
            "DEFER_RULE_VALUE"
          };
      types =
          new int[] {
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.INTEGER,
            Types.INTEGER,
            Types.INTEGER
          };
      sizes = new int[] {25, 10, 30, 12, 12, 15, 5, 5, 1, 1, 1};
    } else {
      cols =
          new String[] {
            "FK_NAME",
            "COLUMN",
            refColName,
            "UPDATE_RULE",
            "DELETE_RULE",
            "DEFERRABLE",
            "ENABLED",
            "VALIDATED"
          };
      types =
          new int[] {
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR,
            Types.VARCHAR
          };
      sizes = new int[] {25, 10, 30, 12, 12, 15, 5, 5};
    }
    DataStore ds = new DataStore(cols, types, sizes);
    if (tbl == null) return ds;

    DataStore rawList = null;

    try {
      int tableCol;
      int fkNameCol;
      int colCol;
      int fkColCol;
      int deleteActionCol = 10;
      int updateActionCol = 9;
      int schemaCol;
      int catalogCol;

      if (getOwnFk) {
        rawList = getRawKeyList(tbl, false);
        tableCol = 2;
        schemaCol = 1;
        catalogCol = 0;
        fkNameCol = 11;
        colCol = 7;
        fkColCol = 3;
      } else {
        rawList = getRawKeyList(tbl, true);
        tableCol = 6;
        schemaCol = 5;
        catalogCol = 4;
        fkNameCol = 11;
        colCol = 3;
        fkColCol = 7;
      }

      if (rawList == null) {
        // this means retrieval was cancelled
        this.cancel = true;
        return null;
      }

      for (int rawRow = 0; rawRow < rawList.getRowCount(); rawRow++) {
        String tname = rawList.getValueAsString(rawRow, tableCol);
        String schema = rawList.getValueAsString(rawRow, schemaCol);
        String catalog = rawList.getValueAsString(rawRow, catalogCol);
        TableIdentifier tid = new TableIdentifier(catalog, schema, tname, false);
        tid.setNeverAdjustCase(true);
        String tableName = tid.getTableExpression(dbConnection);

        String fk_col = rawList.getValueAsString(rawRow, fkColCol);
        String col = rawList.getValueAsString(rawRow, colCol);
        String fk_name = rawList.getValueAsString(rawRow, fkNameCol);

        int updateAction =
            rawList.getValueAsInt(rawRow, updateActionCol, DatabaseMetaData.importedKeyNoAction);
        String updActionDesc = dbSettings.getRuleDisplay(updateAction);
        int deleteAction =
            rawList.getValueAsInt(rawRow, deleteActionCol, DatabaseMetaData.importedKeyNoAction);
        String delActionDesc = dbSettings.getRuleDisplay(deleteAction);

        int deferrableCode =
            rawList.getValueAsInt(rawRow, 13, DatabaseMetaData.importedKeyNoAction);
        String deferrable = dbSettings.getRuleDisplay(deferrableCode);

        int row = ds.addRow();
        ds.setValue(row, COLUMN_IDX_FK_DEF_FK_NAME, fk_name.trim());
        ds.setValue(row, COLUMN_IDX_FK_DEF_COLUMN_NAME, col.trim());
        ds.setValue(row, COLUMN_IDX_FK_DEF_REFERENCE_COLUMN_NAME, tableName + "." + fk_col);
        ds.setValue(row, COLUMN_IDX_FK_DEF_UPDATE_RULE, updActionDesc);
        ds.setValue(row, COLUMN_IDX_FK_DEF_DELETE_RULE, delActionDesc);
        ds.setValue(row, COLUMN_IDX_FK_DEF_DEFERRABLE, deferrable);
        if (includeNumericRuleValue) {
          ds.setValue(row, COLUMN_IDX_FK_DEF_DELETE_RULE_VALUE, Integer.valueOf(deleteAction));
          ds.setValue(row, COLUMN_IDX_FK_DEF_UPDATE_RULE_VALUE, Integer.valueOf(updateAction));
          ds.setValue(
              row, COLUMN_IDX_FK_DEF_DEFERRABLE_RULE_VALUE, Integer.valueOf(deferrableCode));
        }

        if (containsStatusCol) {
          ds.setValue(row, COLUMN_IDX_FK_DEF_ENABLED, rawList.getValue(rawRow, "ENABLED"));
          ds.setValue(row, COLUMN_IDX_FK_DEF_VALIDATED, rawList.getValue(rawRow, "VALIDATED"));
        }

        if (cancel) {
          LogMgr.logWarning(
              "DefaultFKHandler.getKeyList()", "Processing of rows has been cancelled");
          break;
        }
      }
      ds.resetStatus();
    } catch (Exception e) {
      LogMgr.logError("FKHandler.getKeyList()", "Error when retrieving foreign keys", e);
      ds.reset();
    }
    return ds;
  }