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.algebra.base;
007:
008: import org.openrdf.model.URI;
009: import org.openrdf.model.vocabulary.XMLSchema;
010: import org.openrdf.query.algebra.MathExpr;
011: import org.openrdf.query.algebra.Compare.CompareOp;
012: import org.openrdf.sail.rdbms.algebra.DoubleValue;
013: import org.openrdf.sail.rdbms.algebra.FalseValue;
014: import org.openrdf.sail.rdbms.algebra.SqlAbs;
015: import org.openrdf.sail.rdbms.algebra.SqlAnd;
016: import org.openrdf.sail.rdbms.algebra.SqlCase;
017: import org.openrdf.sail.rdbms.algebra.SqlCompare;
018: import org.openrdf.sail.rdbms.algebra.SqlConcat;
019: import org.openrdf.sail.rdbms.algebra.SqlEq;
020: import org.openrdf.sail.rdbms.algebra.SqlIsNull;
021: import org.openrdf.sail.rdbms.algebra.SqlLike;
022: import org.openrdf.sail.rdbms.algebra.SqlLowerCase;
023: import org.openrdf.sail.rdbms.algebra.SqlMathExpr;
024: import org.openrdf.sail.rdbms.algebra.SqlNot;
025: import org.openrdf.sail.rdbms.algebra.SqlNull;
026: import org.openrdf.sail.rdbms.algebra.SqlOr;
027: import org.openrdf.sail.rdbms.algebra.SqlRegex;
028: import org.openrdf.sail.rdbms.algebra.StringValue;
029: import org.openrdf.sail.rdbms.exceptions.UnsupportedRdbmsOperatorException;
030:
031: /**
032: * Support method to create SQL expressions.
033: *
034: * @author James Leigh
035: *
036: */
037: public class SqlExprSupport {
038:
039: public static SqlExpr abs(SqlExpr arg) {
040: return new SqlAbs(arg);
041: }
042:
043: public static SqlExpr and(SqlExpr left, SqlExpr right) {
044: return new SqlAnd(left, right);
045: }
046:
047: public static SqlExpr cmp(SqlExpr left, CompareOp op, SqlExpr right) {
048: return new SqlCompare(left, op, right);
049: }
050:
051: public static SqlExpr coalesce(SqlExpr... exprs) {
052: SqlCase sqlCase = new SqlCase();
053: for (SqlExpr expr : exprs) {
054: sqlCase.when(isNotNull(expr.clone()), expr);
055: }
056: return sqlCase;
057: }
058:
059: public static SqlExpr concat(SqlExpr left, SqlExpr right) {
060: return new SqlConcat(left, right);
061: }
062:
063: public static SqlExpr eq(SqlExpr left, SqlExpr right) {
064: return new SqlEq(left, right);
065: }
066:
067: public static SqlExpr eqComparingNull(SqlExpr left, SqlExpr right) {
068: SqlExpr leftIsNull = isNull(left.clone());
069: SqlExpr rightIsNull = isNull(right.clone());
070: SqlExpr bothNull = and(leftIsNull, rightIsNull);
071: SqlExpr bothNotNull = and(not(leftIsNull), not(rightIsNull));
072: return or(bothNull, and(bothNotNull, eq(left, right)));
073: }
074:
075: public static SqlExpr eqIfNotNull(SqlExpr left, SqlExpr right) {
076: SqlExpr leftIsNotNull = isNotNull(left.clone());
077: SqlExpr rightIsNotNull = isNotNull(right.clone());
078: SqlExpr bothNotNull = and(leftIsNotNull, rightIsNotNull);
079: return and(bothNotNull, eq(left, right));
080: }
081:
082: public static SqlExpr eqOrBothNull(SqlExpr left, SqlExpr right) {
083: SqlExpr leftIsNull = isNull(left.clone());
084: SqlExpr rightIsNull = isNull(right.clone());
085: SqlExpr bothNull = and(leftIsNull, rightIsNull);
086: return or(bothNull, eq(left, right));
087: }
088:
089: public static SqlExpr eqOrSimpleType(SqlExpr left, SqlExpr right) {
090: SqlExpr bothSimple = and(simple(left), simple(right));
091: return or(eq(left.clone(), right.clone()), bothSimple);
092: }
093:
094: public static SqlExpr ge(SqlExpr left, SqlExpr right) {
095: return new SqlCompare(left, CompareOp.GE, right);
096: }
097:
098: public static SqlExpr gt(SqlExpr left, SqlExpr right) {
099: return new SqlCompare(left, CompareOp.GT, right);
100: }
101:
102: public static SqlExpr in(SqlExpr compare, SqlExpr... values) {
103: SqlExpr expr = null;
104: for (SqlExpr value : values) {
105: if (expr == null) {
106: expr = new SqlEq(compare, value);
107: } else {
108: expr = or(expr, new SqlEq(compare.clone(), value));
109: }
110: }
111: if (expr == null)
112: return new FalseValue();
113: return expr;
114: }
115:
116: public static SqlExpr isNotNull(SqlExpr arg) {
117: return not(isNull(arg));
118: }
119:
120: public static SqlExpr isNull(SqlExpr arg) {
121: return new SqlIsNull(arg);
122: }
123:
124: public static SqlExpr le(SqlExpr left, SqlExpr right) {
125: return new SqlCompare(left, CompareOp.LE, right);
126: }
127:
128: public static SqlExpr like(SqlExpr left, SqlExpr right) {
129: return new SqlLike(left, right);
130: }
131:
132: public static SqlExpr lowercase(SqlExpr arg) {
133: return new SqlLowerCase(arg);
134: }
135:
136: public static SqlExpr lt(SqlExpr left, SqlExpr right) {
137: return new SqlCompare(left, CompareOp.LT, right);
138: }
139:
140: public static SqlExpr neq(SqlExpr left, SqlExpr right) {
141: return new SqlNot(new SqlEq(left, right));
142: }
143:
144: public static SqlExpr neqComparingNull(SqlExpr left, SqlExpr right) {
145: SqlExpr leftIsNull = isNull(left.clone());
146: SqlExpr rightIsNull = isNull(right.clone());
147: SqlExpr onlyLeftIsNull = and(not(leftIsNull), rightIsNull
148: .clone());
149: SqlExpr onlyRightIsNull = and(leftIsNull.clone(),
150: not(rightIsNull));
151: SqlExpr compareNull = or(onlyRightIsNull, onlyLeftIsNull);
152: return or(not(eq(left, right)), compareNull);
153: }
154:
155: public static SqlExpr not(SqlExpr arg) {
156: return new SqlNot(arg);
157: }
158:
159: public static SqlExpr num(double value) {
160: return new DoubleValue(value);
161: }
162:
163: public static SqlExpr or(SqlExpr left, SqlExpr right) {
164: return new SqlOr(left, right);
165: }
166:
167: public static SqlExpr regex(SqlExpr value, SqlExpr pattern) {
168: return new SqlRegex(value, pattern);
169: }
170:
171: public static SqlExpr regex(SqlExpr value, SqlExpr pattern,
172: SqlExpr flags) {
173: return new SqlRegex(value, pattern, flags);
174: }
175:
176: public static SqlExpr simple(SqlExpr arg) {
177: SqlExpr isString = eq(arg.clone(), str(XMLSchema.STRING));
178: return or(isNull(arg.clone()), isString);
179: }
180:
181: public static SqlExpr sqlNull() {
182: return new SqlNull();
183: }
184:
185: public static SqlExpr str(String str) {
186: if (str == null)
187: return sqlNull();
188: return new StringValue(str);
189: }
190:
191: public static SqlExpr str(URI uri) {
192: return new StringValue(uri.stringValue());
193: }
194:
195: public static SqlExpr sub(SqlExpr left, SqlExpr right) {
196: return new SqlMathExpr(left, MathExpr.MathOp.MINUS, right);
197: }
198:
199: public static UnsupportedRdbmsOperatorException unsupported(
200: Object arg) {
201: return new UnsupportedRdbmsOperatorException(arg.toString());
202: }
203:
204: private SqlExprSupport() {
205: // no constructor
206: }
207:
208: }
|