Пример #1
0
  @Override
  public void visit(SqlCoalesce sqlNode) {
    out.print("SELECT ");

    boolean first = true;
    SqlJoin join = sqlNode.getJoinNode();
    // Rough draft code.
    for (Var v : sqlNode.getCoalesceVars()) {
      if (!first) out.print(", ");
      SqlColumn col = sqlNode.getIdScope().findScopeForVar(v).getColumn();
      SqlColumn leftCol = join.getLeft().getIdScope().findScopeForVar(v).getColumn();
      SqlColumn rightCol = join.getRight().getIdScope().findScopeForVar(v).getColumn();

      out.print("COALESCE(");
      out.print(leftCol.getFullColumnName());
      out.print(", ");
      out.print(rightCol.getFullColumnName());

      out.print(")");
      out.print(aliasToken());
      out.print(col.getColumnName());
      first = false;
    }

    // And other vars we want.

    for (Var v : sqlNode.getNonCoalesceVars()) {
      if (!first) out.print(", ");
      first = false;

      // Need generated names.
      SqlColumn colSub = join.getIdScope().findScopeForVar(v).getColumn();
      SqlColumn col = sqlNode.getIdScope().findScopeForVar(v).getColumn();

      out.print(colSub.getFullColumnName());
      out.print(aliasToken());
      out.print(col.getColumnName());
    }
    out.ensureStartOfLine();

    out.incIndent(); // INC
    out.println("FROM");
    join.visit(this);
    out.ensureStartOfLine();
    // Alias and annotations handled by outputNode
  }
Пример #2
0
  protected void visitJoin(SqlJoin join, String joinOperatorName) {
    // TODO revisit this code.  Is it now needless complex?
    // Check brackets for more general SQL generation (safe mode - i.e. always bracketted?)
    SqlNode left = join.getLeft();
    SqlNode right = join.getRight();

    // Appearance: stop nesting too much.
    // Can we linearise the format? (drop indentation)
    if (left.isJoin() && left.getAliasName() == null) outputNode(left, false);
    else {
      out.incIndent();
      outputNode(left, true);
      out.decIndent();
    }

    out.println();
    // out.print(" ") ;

    out.print(joinOperatorName);
    annotate(join);
    out.println();

    // Aliasing and scoping - may need sub-SELECT - or just don't generate
    // such SqlNode structures, leaving only COALESCE as the sub-SELECT case

    boolean bracketsRight = true;
    //        if ( right.isInnerJoin() && join.isInnerJoin() && no conditions )
    //            bracketsRight = false ;

    if (bracketsRight)
      // Why?
      out.incIndent();
    outputNode(right, bracketsRight);
    if (bracketsRight) out.decIndent();
    out.println();
    out.print("ON ");
    if (join.getConditions().size() > 0) conditionList(join.getConditions());
    else {
      out.print(" ( ");
      out.print(leftJoinNoConditionsString());
      out.print(" )");
    }
  }
Пример #3
0
 protected void visitJoin(SqlJoin join) {
   visitJoin(join, join.getJoinType().sqlOperator());
 }