Ejemplo n.º 1
0
  /**
   * This method first traverses the set of included business tables and renders those tables to the
   * SQL string buffer. Second, it traverses the list of joins and renders those in the WHERE
   * clause. Finally, it traverses the constraints and adds them to the where or having clauses.
   *
   * @param query sql query model
   * @param usedBusinessTables used business tables in query
   * @param model the current business model
   * @param path the join path
   * @param conditions the where conditions
   * @param databaseMeta database metadata
   * @param locale locale string
   */
  protected void generateFromAndWhere(
      SQLQueryModel query,
      List<LogicalTable> usedBusinessTables,
      LogicalModel model,
      Path path,
      List<Constraint> conditions,
      Map<LogicalTable, String> tableAliases,
      Map<Constraint, SqlOpenFormula> constraintFormulaMap,
      Map<String, Object> parameters,
      boolean genAsPreparedStatement,
      DatabaseMeta databaseMeta,
      String locale)
      throws PentahoMetadataException {

    // Boolean delayConditionOnOuterJoin = null;
    // Object val = null;
    // FROM TABLES
    for (int i = 0; i < usedBusinessTables.size(); i++) {
      LogicalTable businessTable = usedBusinessTables.get(i);
      String schemaName = null;
      if (businessTable.getProperty(SqlPhysicalTable.TARGET_SCHEMA) != null) {
        schemaName =
            databaseMeta.quoteField(
                (String) businessTable.getProperty(SqlPhysicalTable.TARGET_SCHEMA));
      }
      // ToDo: Allow table-level override of delaying conditions.
      //      val = businessTable.getProperty("delay_table_outer_join_conditions");
      //      if ( (val != null) && (val instanceof Boolean) ) {
      //        delayConditionOnOuterJoin = (Boolean)val;
      //      } else {
      //        delayConditionOnOuterJoin = null;
      //      }

      // this code allows subselects to drive the physical model.
      // TODO: make this key off a metadata flag vs. the
      // beginning of the table name.

      String tableName = (String) businessTable.getProperty(SqlPhysicalTable.TARGET_TABLE);
      TargetTableType type =
          (TargetTableType) businessTable.getProperty(SqlPhysicalTable.TARGET_TABLE_TYPE);
      if (type == TargetTableType.INLINE_SQL) {
        tableName = "(" + tableName + ")"; // $NON-NLS-1$ //$NON-NLS-2$
      } else {
        tableName = databaseMeta.getQuotedSchemaTableCombination(schemaName, tableName);
      }
      query.addTable(tableName, databaseMeta.quoteField(tableAliases.get(businessTable)));
    }

    // JOIN CONDITIONS
    if (path != null) {
      for (int i = 0; i < path.size(); i++) {
        LogicalRelationship relation = path.getRelationship(i);
        String joinFormula =
            getJoin(
                model,
                relation,
                tableAliases,
                parameters,
                genAsPreparedStatement,
                databaseMeta,
                locale);
        String joinOrderKey = relation.getJoinOrderKey();
        JoinType joinType;
        switch (RelationshipType.getJoinType(relation.getRelationshipType())) {
          case LEFT_OUTER:
            joinType = JoinType.LEFT_OUTER_JOIN;
            break;
          case RIGHT_OUTER:
            joinType = JoinType.RIGHT_OUTER_JOIN;
            break;
          case FULL_OUTER:
            joinType = JoinType.FULL_OUTER_JOIN;
            break;
          default:
            joinType = JoinType.INNER_JOIN;
            break;
        }

        String leftTableName =
            databaseMeta.getQuotedSchemaTableCombination(
                (String) relation.getFromTable().getProperty(SqlPhysicalTable.TARGET_SCHEMA),
                (String) relation.getFromTable().getProperty(SqlPhysicalTable.TARGET_TABLE));
        String leftTableAlias = tableAliases.get(relation.getFromTable());
        String rightTableName =
            databaseMeta.getQuotedSchemaTableCombination(
                (String) relation.getToTable().getProperty(SqlPhysicalTable.TARGET_SCHEMA),
                (String) relation.getToTable().getProperty(SqlPhysicalTable.TARGET_TABLE));
        String rightTableAlias = tableAliases.get(relation.getToTable());

        query.addJoin(
            leftTableName,
            leftTableAlias,
            rightTableName,
            rightTableAlias,
            joinType,
            joinFormula,
            joinOrderKey);
        // query.addWhereFormula(joinFormula, "AND"); //$NON-NLS-1$
      }
    }

    // WHERE CONDITIONS
    if (conditions != null) {
      boolean first = true;
      for (Constraint condition : conditions) {
        SqlOpenFormula formula = constraintFormulaMap.get(condition);

        // configure formula to use table aliases
        formula.setTableAliases(tableAliases);

        // The ones with aggregates in it are for the HAVING clause
        if (!formula.hasAggregate()) {

          String sqlFormula = formula.generateSQL(locale);
          String[] usedTables = formula.getLogicalTableIDs();
          query.addWhereFormula(sqlFormula, condition.getCombinationType().toString(), usedTables);
          first = false;
        } else {
          query.addHavingFormula(
              formula.generateSQL(locale), condition.getCombinationType().toString());
        }
      }
    }
  }