Exemplo n.º 1
0
    private String allTableQuery() {
      boolean first = true;
      StringBuilder allTable = new StringBuilder();
      allTable.append("( ");
      for (PredicateNode predicate : adaptor.getPredicates()) {
        if (first) {
          first = false;
        } else {
          allTable.append(" UNION ALL ");
        }

        allTable.append(" SELECT * from " + adaptor.getTableFor(predicate));
      }

      allTable.append(")");
      return allTable.toString();
    }
Exemplo n.º 2
0
    public MPTable mapPredicateTable(final MappableNodePattern<PredicateNode> predicate) {
      if (predicate.isVariable()) {
        throw new IllegalArgumentException("predicate must not " + "be a variable");
      }

      String tableName;
      String alias;

      if (predicate.isVariable()) {
        tableName = allTableQuery();
        alias = "ap_" + ++allMap;
      } else {
        tableName = adaptor.getTableFor(predicate.getNode());
      }

      if (tableName == null) {
        /* No predicate found.. create table that returns no results */
        alias = "np_" + nonexistantMappings.size();
        tableName = "(SELECT p AS s, p AS o from tMap where 1=0)";
        if (!nonexistantMappings.containsKey(predicate.getNode())) {
          alias = "np_" + nonexistantMappings.size();
          LOG.debug("No table for '" + predicate.getNode() + "'.  Using empty table as " + alias);
          tableName = "(SELECT p AS s, p AS o from tMap where 1=0)";
          nonexistantMappings.put(predicate.getNode(), new MPTable(tableName, alias));
          predicateMap.put(predicate.getNode().toString(), new ArrayList<String>());
          return nonexistantMappings.get(predicate.getNode());
        } else {
          List<String> aliases = predicateMap.get(predicate.getNode().toString());
          String primaryAlias = nonexistantMappings.get(predicate.getNode()).alias();
          alias = primaryAlias + "_" + aliases.size();
          LOG.debug("Nonexistant predicate already encountered.  " + " Using alias " + alias);
          return new MPTable(tableName, alias);
        }
      } else if (predicateMap.containsKey(predicate.getNode().toString())) {
        LOG.debug("Predicate already encountered.  " + "Making new table alias");
        List<String> aliases = predicateMap.get(predicate.getNode().toString());
        alias = tableName + "_" + aliases.size();
        aliases.add(alias);
      } else {
        LOG.debug("Predicate never encountered.  " + "Will refer to it by its own name");
        ArrayList<String> aliases = new ArrayList<String>();
        aliases.add(tableName);
        predicateMap.put(predicate.getNode().toString(), aliases);
        alias = tableName;
      }

      LOG.debug(
          "Mapping predicate "
              + predicate.getNode().getValue()
              + " to "
              + tableName
              + " as "
              + alias);
      MPTable table = new MPTable(tableName, alias);
      return table;
    }