Пример #1
0
  // From a list of params, this method generates the where clause of the SQL
  // statement. ie) select * from table WHERE ____
  public String parsePageParam(DataObj showObj, List<String> getParams) {
    StringBuilder queryParam = new StringBuilder(512);
    boolean firstIteration = true;
    Modules m = new Modules();
    for (String param : getParams) {
      if (firstIteration) {
        firstIteration = false;
      } else {
        queryParam.append(" and ");
      }
      List<Integer> fkIndices = showObj.indexForForeignKeysOfType(param);
      // check if param is a module
      if (m.isModule(param)) {
        String tableName = m.modNameToTableName(param);
        fkIndices = showObj.indexForForeignKeysOfType(tableName);
        if (fkIndices.size() > 0) {
          boolean firstIndex = true;
          for (int fkIndex : fkIndices) {
            if (firstIndex) {
              firstIndex = false;
            } else {
              queryParam.append(" or ");
            }
            String foreignKeyField = showObj.getFieldName(fkIndex);
            queryParam.append(showObj.getName() + "." + foreignKeyField + " = ? ");
          }
        }
        // could not find the foreign key because it isthis object
        else {
          queryParam.append(showObj.getName() + ".ID = ? ");
        }

      }
      // else check if param is a field of dataobj
      else if (showObj.isField(param)) {
        queryParam.append(showObj.getName() + "." + param + " = ? ");
      }
      // else check if param is a foreign key dependency of dataobj
      else if (fkIndices.size() > 0) {
        boolean firstIndex = true;
        for (int fkIndex : fkIndices) {
          if (firstIndex) {
            firstIndex = false;
          } else {
            queryParam.append(" or ");
          }
          String foreignKeyField = showObj.getFieldName(fkIndex);
          queryParam.append(showObj.getName() + "." + foreignKeyField + " = ? ");
        }

      }
      // check if param is the same name as that object.
      // TODO: in this case, it's get by id which has already been
      // generated. either skip this generation or keep it
      // for later to make communication with cordova easier
      else if (showObj.getName().equals(param)) {
        queryParam.append(showObj.getName() + ".ID = ? ");
      }
      // else there is no match. really, this is an error in the config.
      // TODO: have parser check that one of these three conditions is true
      // now, handle it such that set select * from x where true
      else {
        queryParam.append(" true ");
      }
    }
    return queryParam.toString();
  }