private QueryCommand buildSubquery(Expression projected) {
    Criteria criteria = null;
    for (ForeignKey fk : this.childTable.getForeignKeys()) {
      if (fk.getPrimaryKey().getParent().equals(this.parent)) {
        List<String> refColumns = fk.getReferenceColumns();
        if (refColumns == null) {
          refColumns = ODataSQLBuilder.getColumnNames(childTable.getPrimaryKey().getColumns());
        }

        List<String> pkColumns =
            ODataSQLBuilder.getColumnNames(parent.getPrimaryKey().getColumns());
        List<Criteria> critList = new ArrayList<Criteria>();

        for (int i = 0; i < refColumns.size(); i++) {
          critList.add(
              new CompareCriteria(
                  new ElementSymbol(pkColumns.get(i), this.parentGroup),
                  CompareCriteria.EQ,
                  new ElementSymbol(refColumns.get(i), this.childGroup)));
        }

        criteria = critList.get(0);
        for (int i = 1; i < critList.size(); i++) {
          criteria = new CompoundCriteria(CompoundCriteria.AND, criteria, critList.get(i));
        }
      }
    }
    Select s1 = new Select();
    s1.addSymbol(projected);
    From f1 = new From();
    f1.addGroup(this.childGroup);
    Query q1 = new Query();
    q1.setSelect(s1);
    q1.setFrom(f1);
    q1.setCriteria(criteria);

    return q1;
  }
Esempio n. 2
0
  public Object lookupCodeValue(
      CommandContext context,
      String codeTableName,
      String returnElementName,
      String keyElementName,
      Object keyValue)
      throws BlockedException, TeiidComponentException, TeiidProcessingException {
    // we are not using a resolved form of a lookup, so we canonicallize with upper case
    codeTableName = codeTableName.toUpperCase();
    keyElementName = keyElementName.toUpperCase();
    returnElementName = returnElementName.toUpperCase();
    String matTableName =
        CODE_PREFIX
            + codeTableName
            + ElementSymbol.SEPARATOR
            + keyElementName
            + ElementSymbol.SEPARATOR
            + returnElementName;

    TupleSource ts = context.getCodeLookup(matTableName, keyValue);
    if (ts == null) {
      QueryMetadataInterface metadata = context.getMetadata();

      TempMetadataID id =
          context
              .getGlobalTableStore()
              .getCodeTableMetadataId(
                  codeTableName, returnElementName, keyElementName, matTableName);

      ElementSymbol keyElement = new ElementSymbol(keyElementName, new GroupSymbol(matTableName));
      ElementSymbol returnElement =
          new ElementSymbol(returnElementName, new GroupSymbol(matTableName));
      keyElement.setType(
          DataTypeManager.getDataTypeClass(
              metadata.getElementType(
                  metadata.getElementID(
                      codeTableName + ElementSymbol.SEPARATOR + keyElementName))));
      returnElement.setType(
          DataTypeManager.getDataTypeClass(
              metadata.getElementType(
                  metadata.getElementID(
                      codeTableName + ElementSymbol.SEPARATOR + returnElementName))));

      Query query =
          RelationalPlanner.createMatViewQuery(
              id, matTableName, Arrays.asList(returnElement), true);
      query.setCriteria(
          new CompareCriteria(keyElement, CompareCriteria.EQ, new Constant(keyValue)));

      ts = registerQuery(context, context.getTempTableStore(), query);
    }
    try {
      List<?> row = ts.nextTuple();
      Object result = null;
      if (row != null) {
        result = row.get(0);
      }
      ts.closeSource();
      return result;
    } catch (BlockedException e) {
      context.putCodeLookup(matTableName, keyValue, ts);
      throw e;
    }
  }