Ejemplo n.º 1
0
 protected void formatTriple(Triple tp) {
   out.print(slotToString(tp.getSubject()));
   out.print(" ");
   out.print(slotToString(tp.getPredicate()));
   out.print(" ");
   out.print(slotToString(tp.getObject()));
 }
Ejemplo n.º 2
0
 @Override
 public void visit(SqlTable table) {
   out.print(table.getTableName());
   out.print(aliasToken());
   out.print(table.getAliasName());
   annotate(table);
 }
Ejemplo n.º 3
0
 private void genWHERE(SqlExprList conditions) {
   out.print("WHERE");
   out.print(" ");
   out.incIndent();
   conditionList(conditions);
   out.decIndent();
 }
Ejemplo n.º 4
0
  private void print(List<ColAlias> cols) {
    String sep = "";
    if (cols.size() == 0) {
      // Can happen - e.g. query with no variables.
      // log.info("No SELECT columns") ;
      out.print("*");
    }

    // Put common prefix on same line
    String currentPrefix = null;
    String splitMarker = ".";

    for (ColAlias c : cols) {
      out.print(sep);

      // Choose split points.
      String cn = c.getColumn().getFullColumnName();
      int j = cn.lastIndexOf(splitMarker);
      if (j == -1) currentPrefix = null;
      else {
        String x = cn.substring(0, j);
        if (currentPrefix != null && !x.equals(currentPrefix)) out.println();

        currentPrefix = x;
      }

      sep = ", ";
      out.print(c.getColumn().getFullColumnName());

      if (c.getAlias() != null) {
        out.print(aliasToken());
        out.print(c.getAlias().getColumnName());
      }
    }
  }
Ejemplo n.º 5
0
 public static void writeBase(IndentedWriter out, String base) {
   if (base != null) {
     out.print("@base ");
     out.pad(PREFIX_IRI);
     out.print("<");
     out.print(base);
     out.print(">");
     out.print(" .");
     out.println();
   }
 }
Ejemplo n.º 6
0
  @Override
  public void visit(SqlSelectBlock sqlSelectBlock) {
    // Need a rename and alias if:
    //   Not top
    //   Not merely a table inside.

    levelSelectBlock++;

    if (levelSelectBlock > 1) {
      // Alias needed.
      //            SqlRename rename = SqlRename.view("X", sqlSelectBlock) ;
      //            rename.visit(this) ;
      //            levelSelectBlock-- ;
      //            return ;
    }

    genPrefix(sqlSelectBlock);
    out.print("SELECT ");
    if (sqlSelectBlock.getDistinct()) out.print("DISTINCT ");
    if (annotate(sqlSelectBlock)) out.ensureStartOfLine();
    out.incIndent();
    genColumnPrefix(sqlSelectBlock);
    print(sqlSelectBlock.getCols());
    out.decIndent();
    out.ensureStartOfLine();

    // FROM
    out.print("FROM");
    if (!sqlSelectBlock.getSubNode().isTable()) out.println();
    else out.print(" ");
    out.incIndent();
    outputNode(sqlSelectBlock.getSubNode(), true);
    // sqlSelectBlock.getSubNode().visit(this) ;
    out.decIndent();
    out.ensureStartOfLine();

    // WHERE
    if (sqlSelectBlock.getConditions().size() > 0) genWHERE(sqlSelectBlock.getConditions());

    // LIMIT/OFFSET
    out.ensureStartOfLine();
    genLimitOffset(sqlSelectBlock);
    genSuffix(sqlSelectBlock);
    levelSelectBlock--;
  }
Ejemplo n.º 7
0
 public static void fmtSPARQL(IndentedWriter iOut, ExprList exprs, SerializationContext pmap) {
   FmtExprSPARQL fmt = new FmtExprSPARQL(iOut, pmap);
   String sep = "";
   for (Expr expr : exprs) {
     iOut.print(sep);
     sep = " , ";
     fmt.format(expr);
   }
 }
Ejemplo n.º 8
0
  // return true if annotation was output and it runs to end-of-line
  private boolean annotate(Annotations sqlNode, int indentationColumn) {
    if (!outputAnnotations) return false;

    boolean first = true;
    for (String s : sqlNode.getNotes()) {
      if (!first) out.println();
      first = false;
      out.pad(indentationColumn, true);
      if (commentSQLStyle) {
        out.print(" -- ");
        out.print(s);
      } else {
        out.print(" /* ");
        out.print(s);
        out.print(" */");
      }
    }
    return !commentSQLStyle || !first;
  }
Ejemplo n.º 9
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(" )");
    }
  }
Ejemplo n.º 10
0
  @Override
  public void write(IndentedWriter writer) {
    writer.println("SqlOpProject" + columnNames + "(");

    writer.incIndent();
    subOp.write(writer);
    writer.println();
    writer.decIndent();

    writer.print(")");
  }
Ejemplo n.º 11
0
  private void outputNode(SqlNode sqlNode, boolean mayNeedBrackets) {
    if (sqlNode.isTable()) {
      sqlNode.visit(this);
      return;
    }
    // boolean brackets = ( mayNeedBrackets && ( sqlNode.isSelectBlock() || sqlNode.isCoalesce() ) )
    // ;

    boolean brackets = false;
    brackets = brackets || (mayNeedBrackets && sqlNode.isCoalesce());

    // Work harder? ready for a better test.
    brackets = brackets || (mayNeedBrackets && sqlNode.isSelectBlock());

    // Need brackets if the subpart is a SELECT

    if (brackets) {
      out.print("( ");
      out.incIndent();
    }
    sqlNode.visit(this);
    if (brackets) {
      out.decIndent();
      out.ensureStartOfLine();
      out.print(")");
    }
    // Every derived table (SELECT ...) must have an alias.
    // Is there a more principled way to do this? .isDerived?
    //            if ( sqlNode.isRestrict() || sqlNode.isProject())
    //                out.print(+sqlNode.getAliasName()) ;
    if (sqlNode.getAliasName() != null) {
      out.print(aliasToken());
      out.print(sqlNode.getAliasName());
    }
    annotate(sqlNode);
  }
Ejemplo n.º 12
0
 public static void writePrefixes(IndentedWriter out, PrefixMap prefixMap) {
   if (prefixMap != null && !prefixMap.isEmpty()) {
     for (Map.Entry<String, String> e : prefixMap.getMappingCopyStr().entrySet()) {
       out.print("@prefix ");
       out.print(e.getKey());
       out.print(": ");
       out.pad(PREFIX_IRI);
       out.print("<");
       out.print(e.getValue());
       out.print(">");
       out.print(" .");
       out.println();
     }
   }
 }
Ejemplo n.º 13
0
  public void conditionList(SqlExprList conditions) {
    if (conditions.size() == 0) return;

    out.print("( ");

    String sep = "  AND ";

    boolean first = true;
    boolean lastAnnotated = false;

    for (SqlExpr c : conditions) {
      if (!first) {
        if (!allOnOneLine) out.println();
        out.print(sep);
      }
      boolean needsParens = !(c instanceof S_Equal);

      // TODO Interact with SqlExpr precedence printing
      if (needsParens) out.print("( ");
      out.print(c.asSQL());
      if (needsParens) out.print(" )");
      if (!allOnOneLine) lastAnnotated = annotate(c);
      first = false;
    }
    if (!allOnOneLine && lastAnnotated) out.println("");
    out.print(" )");
    first = true;

    if (allOnOneLine) {
      for (SqlExpr c : conditions) {
        if (c.hasNotes()) {
          if (!first) out.println();
          annotate(c);
          first = false;
        }
      }
    }
  }
Ejemplo n.º 14
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
  }