예제 #1
0
  // makes requests params
  // ie con.query('SELECT * ... WHERE = ?', [requst params], fcn())
  public String makeReqParams(DataObj showObj, List<String> pageParams) {
    StringBuilder reqParams = new StringBuilder(128);
    reqParams.append("[");
    boolean firstIteration = true;
    Modules m = new Modules();
    for (String param : pageParams) {
      if (firstIteration) {
        firstIteration = false;
      } else {
        reqParams.append(",");
      }

      // check if param is a module
      // see how many fkeys this param stands for
      List<Integer> fkIndices;
      if (m.isModule(param)) {
        String tableName = m.modNameToTableName(param);
        fkIndices = showObj.indexForForeignKeysOfType(tableName);
      } else {
        fkIndices = showObj.indexForForeignKeysOfType(param);
      }
      // for every fk, add another req param
      // because each fk will have a ? in the where clause
      if (fkIndices.size() > 0) {
        boolean firstIndex = true;
        for (int fkIndex : fkIndices) {
          if (firstIndex) {
            firstIndex = false;
          } else {
            reqParams.append(" , ");
          }
          reqParams.append("req.params." + param);
        }
      } else {
        reqParams.append("req.params." + param);
      }
    }
    reqParams.append("]");
    return reqParams.toString();
  }
예제 #2
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();
  }