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.ArrayList;
009: import java.util.List;
010:
011: import org.openrdf.query.algebra.MathExpr;
012: import org.openrdf.sail.rdbms.algebra.SqlCompare;
013:
014: /**
015: * Assemblies an SQL expression.
016: *
017: * @author James Leigh
018: *
019: */
020: public class SqlExprBuilder {
021: protected class Mark {
022: int length;
023: int size;
024:
025: Mark(int length, int size) {
026: this .length = length;
027: this .size = size;
028: }
029: }
030:
031: private static final String NULL = " NULL ";
032: private QueryBuilderFactory factory;
033: private List<Object> parameters = new ArrayList<Object>();
034: private StringBuilder where = new StringBuilder();
035:
036: public SqlExprBuilder(QueryBuilderFactory factory) {
037: super ();
038: this .factory = factory;
039: }
040:
041: public SqlBracketBuilder abs() {
042: where.append(" ABS");
043: return open();
044: }
045:
046: public SqlExprBuilder and() {
047: if (!isEmpty()) {
048: where.append("\n AND ");
049: }
050: return this ;
051: }
052:
053: public SqlExprBuilder append(CharSequence sql) {
054: where.append(sql);
055: return this ;
056: }
057:
058: public void appendBoolean(boolean booleanValue) {
059: if (booleanValue) {
060: where.append(" 1=1 ");
061: } else {
062: where.append(" 0=1 ");
063: }
064: }
065:
066: public SqlExprBuilder appendNull() {
067: where.append(getSqlNull());
068: return this ;
069: }
070:
071: public SqlExprBuilder appendNumeric(double doubleValue) {
072: where.append(" ? ");
073: parameters.add(doubleValue);
074: return this ;
075: }
076:
077: public void appendOperator(SqlCompare.Operator op) {
078: switch (op) {
079: case GE:
080: where.append(" >= ");
081: break;
082: case GT:
083: where.append(" > ");
084: break;
085: case LE:
086: where.append(" <= ");
087: break;
088: case LT:
089: where.append(" < ");
090: break;
091: }
092: }
093:
094: public void as(String column) {
095: where.append(" AS ").append(column);
096: }
097:
098: public SqlExprBuilder number(Number time) {
099: where.append(" ? ");
100: parameters.add(time);
101: return this ;
102: }
103:
104: public SqlCaseBuilder caseBegin() {
105: return new SqlCaseBuilder(this );
106: }
107:
108: public SqlExprBuilder column(String alias, String column) {
109: where.append(alias).append(".").append(column);
110: return this ;
111: }
112:
113: public SqlExprBuilder columnEquals(String alias, String column,
114: Number id) {
115: return column(alias, column).eq().number(id);
116: }
117:
118: public SqlExprBuilder columnEquals(String alias, String column,
119: String label) {
120: return column(alias, column).eq().varchar(label);
121: }
122:
123: public SqlExprBuilder columnIn(String alias, String column,
124: Number[] ids) {
125: if (ids.length == 1) {
126: return columnEquals(alias, column, ids[0]);
127: }
128: SqlBracketBuilder open = open();
129: for (int i = 0; i < ids.length; i++) {
130: if (i > 0) {
131: open.or();
132: }
133: open.column(alias, column);
134: open.eq();
135: open.number(ids[i]);
136: }
137: open.close();
138: return this ;
139: }
140:
141: public SqlExprBuilder columnsEqual(String al1, String col1,
142: String al2, String col2) {
143: return column(al1, col1).eq().column(al2, col2);
144: }
145:
146: public void concat() {
147: append(" || ");
148: }
149:
150: public SqlExprBuilder eq() {
151: where.append(" = ");
152: return this ;
153: }
154:
155: public List<Object> getParameters() {
156: return parameters;
157: }
158:
159: public boolean isEmpty() {
160: return where.length() == 0;
161: }
162:
163: public SqlExprBuilder isNotNull() {
164: where.append(" IS NOT NULL ");
165: return this ;
166: }
167:
168: public SqlExprBuilder isNull() {
169: where.append(" IS NULL ");
170: return this ;
171: }
172:
173: public void like() {
174: where.append(" LIKE ");
175: }
176:
177: public SqlBracketBuilder lowerCase() {
178: where.append(" lower");
179: return open();
180: }
181:
182: public void math(MathExpr.MathOp op) {
183: append(" ").append(op.getSymbol()).append(" ");
184: }
185:
186: public SqlBracketBuilder mod(int value) {
187: where.append(" MOD");
188: SqlBracketBuilder open = open();
189: open.setClosing("," + value + open.getClosing());
190: return open;
191: }
192:
193: public SqlBracketBuilder not() {
194: where.append(" NOT");
195: return open();
196: }
197:
198: public SqlExprBuilder notEqual() {
199: where.append(" != ");
200: return this ;
201: }
202:
203: public SqlBracketBuilder open() {
204: return factory.createSqlBracketBuilder(this );
205: }
206:
207: public SqlExprBuilder or() {
208: append(" OR ");
209: return this ;
210: }
211:
212: public void plus(int range) {
213: where.append(" + " + range);
214: }
215:
216: public SqlRegexBuilder regex() {
217: return factory.createSqlRegexBuilder(this );
218: }
219:
220: public void rightShift(int rightShift) {
221: where.append(" >> " + rightShift);
222: }
223:
224: public CharSequence toSql() {
225: return where;
226: }
227:
228: @Override
229: public String toString() {
230: return where.toString();
231: }
232:
233: public SqlExprBuilder varchar(String stringValue) {
234: if (stringValue == null) {
235: appendNull();
236: } else {
237: where.append(" ? ");
238: parameters.add(stringValue);
239: }
240: return this ;
241: }
242:
243: protected void addParameters(List<Object> params) {
244: parameters.addAll(params);
245: }
246:
247: protected String getSqlNull() {
248: return NULL;
249: }
250:
251: protected Mark mark() {
252: return new Mark(where.length(), parameters.size());
253: }
254:
255: protected void reset(Mark mark) {
256: where.delete(mark.length, where.length());
257: for (int i = parameters.size() - 1; i >= mark.size; i--) {
258: parameters.remove(i);
259: }
260: }
261:
262: }
|