001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.evaluation;
007:
008: import java.util.List;
009:
010: /**
011: * Facilitates the building of a SQL query.
012: *
013: * @author James Leigh
014: *
015: */
016: public class SqlQueryBuilder {
017: private QueryBuilderFactory factory;
018: private boolean distinct;
019: private SqlExprBuilder select;
020: private SqlJoinBuilder from;
021: private StringBuilder group = new StringBuilder();
022: private SqlExprBuilder order;
023: private SqlQueryBuilder union;
024: private Integer offset;
025: private Integer limit;
026:
027: public SqlQueryBuilder(QueryBuilderFactory factory) {
028: super ();
029: this .factory = factory;
030: select = factory.createSqlExprBuilder();
031: order = factory.createSqlExprBuilder();
032: }
033:
034: public List<Object> findParameters(List<Object> parameters) {
035: parameters.addAll(select.getParameters());
036: if (from != null) {
037: from.findParameters(parameters);
038: }
039: if (union != null) {
040: union.findParameters(parameters);
041: }
042: parameters.addAll(order.getParameters());
043: return parameters;
044: }
045:
046: public void distinct() {
047: distinct = true;
048: }
049:
050: public SqlExprBuilder select() {
051: if (!select.isEmpty())
052: select.append(",\n ");
053: return select;
054: }
055:
056: public SqlJoinBuilder from(String table, String alias) {
057: assert from == null : alias;
058: return from = factory.createSqlJoinBuilder(table, alias);
059: }
060:
061: public SqlJoinBuilder from(String alias) {
062: assert from == null : alias;
063: return from = factory.createSqlJoinBuilder(null, alias);
064: }
065:
066: public SqlExprBuilder filter() {
067: assert from != null;
068: return from.on();
069: }
070:
071: public SqlQueryBuilder groupBy(String... expressions) {
072: for (String expr : expressions) {
073: if (group.length() == 0) {
074: group.append("\nGROUP BY ");
075: } else {
076: group.append(", ");
077: }
078: group.append(expr);
079: }
080: return this ;
081: }
082:
083: public SqlQueryBuilder union() {
084: assert union == null : union;
085: return union = factory.createSqlQueryBuilder();
086: }
087:
088: public boolean isEmpty() {
089: return select.isEmpty() && from == null;
090: }
091:
092: @Override
093: public String toString() {
094: StringBuilder sb = new StringBuilder();
095: sb.append("SELECT ");
096: if (distinct) {
097: sb.append("DISTINCT ");
098: }
099: if (select.isEmpty()) {
100: sb.append("*");
101: } else {
102: sb.append(select.toSql());
103: }
104: if (from != null) {
105: sb.append("\nFROM ").append(from.getFromClause());
106: if (!from.on().isEmpty()) {
107: sb.append("\nWHERE ");
108: sb.append(from.on().toSql());
109: }
110: }
111: sb.append(group);
112: if (union != null && !union.isEmpty()) {
113: sb.append("\nUNION ALL ");
114: sb.append(union.toString());
115: }
116: if (!order.isEmpty()) {
117: sb.append("\nORDER BY ").append(order.toSql());
118: }
119: if (limit != null) {
120: sb.append("\nLIMIT ").append(limit);
121: }
122: if (offset != null) {
123: sb.append("\nOFFSET ").append(offset);
124: }
125: return sb.toString();
126: }
127:
128: public SqlExprBuilder orderBy() {
129: if (!order.isEmpty())
130: order.append(",\n ");
131: return order;
132: }
133:
134: public void offset(Integer offset) {
135: this .offset = offset;
136: if (limit == null) {
137: limit = Integer.MAX_VALUE;
138: }
139: }
140:
141: public void limit(Integer limit) {
142: this.limit = limit;
143: }
144: }
|