@Override public void write(IndentedWriter writer) { writer.println("SqlOpProject" + columnNames + "("); writer.incIndent(); subOp.write(writer); writer.println(); writer.decIndent(); writer.print(")"); }
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()); } } }
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(" )"); } }
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(); } }
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; } } } }
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(); } } }
@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 }
@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--; }
// 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; }
protected void genLimitOffset(SqlSelectBlock sqlSelectBlock) { if (sqlSelectBlock.getLength() >= 0) out.println("LIMIT " + sqlSelectBlock.getLength()); if (sqlSelectBlock.getStart() >= 0) out.println("OFFSET " + sqlSelectBlock.getStart()); }