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.factories;
007:
008: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.and;
009: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.coalesce;
010: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.isNotNull;
011: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.isNull;
012: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.sqlNull;
013: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.str;
014: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.unsupported;
015:
016: import org.openrdf.model.URI;
017: import org.openrdf.model.Value;
018: import org.openrdf.model.vocabulary.XMLSchema;
019: import org.openrdf.query.algebra.Datatype;
020: import org.openrdf.query.algebra.Lang;
021: import org.openrdf.query.algebra.MathExpr;
022: import org.openrdf.query.algebra.QueryModelNode;
023: import org.openrdf.query.algebra.Str;
024: import org.openrdf.query.algebra.ValueConstant;
025: import org.openrdf.query.algebra.ValueExpr;
026: import org.openrdf.query.algebra.Var;
027: import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
028: import org.openrdf.sail.rdbms.algebra.LongURIColumn;
029: import org.openrdf.sail.rdbms.algebra.SqlCase;
030: import org.openrdf.sail.rdbms.algebra.SqlNull;
031: import org.openrdf.sail.rdbms.algebra.URIColumn;
032: import org.openrdf.sail.rdbms.algebra.base.SqlExpr;
033: import org.openrdf.sail.rdbms.exceptions.UnsupportedRdbmsOperatorException;
034:
035: /**
036: * Creates an SQL expression for a URI's string value.
037: *
038: * @author James Leigh
039: *
040: */
041: public class URIExprFactory extends
042: QueryModelVisitorBase<UnsupportedRdbmsOperatorException> {
043: protected SqlExpr result;
044: private SqlExprFactory sql;
045:
046: public void setSqlExprFactory(SqlExprFactory sql) {
047: this .sql = sql;
048: }
049:
050: public SqlExpr createUriExpr(ValueExpr expr)
051: throws UnsupportedRdbmsOperatorException {
052: result = null;
053: if (expr == null)
054: return new SqlNull();
055: expr.visit(this );
056: if (result == null)
057: return new SqlNull();
058: return result;
059: }
060:
061: @Override
062: public void meet(Datatype node)
063: throws UnsupportedRdbmsOperatorException {
064: SqlCase sqlCase = new SqlCase();
065: sqlCase.when(isNotNull(type(node.getArg())),
066: type(node.getArg()));
067: sqlCase.when(and(isNull(lang(node.getArg())),
068: isNotNull(label(node.getArg()))), str(XMLSchema.STRING
069: .stringValue()));
070: result = sqlCase;
071: }
072:
073: @Override
074: public void meet(Lang node)
075: throws UnsupportedRdbmsOperatorException {
076: result = sqlNull();
077: }
078:
079: @Override
080: public void meet(MathExpr node)
081: throws UnsupportedRdbmsOperatorException {
082: result = sqlNull();
083: }
084:
085: @Override
086: public void meet(Str node) {
087: result = sqlNull();
088: }
089:
090: @Override
091: public void meet(ValueConstant vc) {
092: result = valueOf(vc.getValue());
093: }
094:
095: @Override
096: public void meet(Var var) {
097: if (var.getValue() == null) {
098: result = coalesce(new URIColumn(var),
099: new LongURIColumn(var));
100: } else {
101: result = valueOf(var.getValue());
102: }
103: }
104:
105: @Override
106: protected void meetNode(QueryModelNode arg)
107: throws UnsupportedRdbmsOperatorException {
108: throw unsupported(arg);
109: }
110:
111: private SqlExpr label(ValueExpr arg)
112: throws UnsupportedRdbmsOperatorException {
113: return sql.createLabelExpr(arg);
114: }
115:
116: private SqlExpr lang(ValueExpr arg)
117: throws UnsupportedRdbmsOperatorException {
118: return sql.createLanguageExpr(arg);
119: }
120:
121: private SqlExpr type(ValueExpr arg)
122: throws UnsupportedRdbmsOperatorException {
123: return sql.createDatatypeExpr(arg);
124: }
125:
126: private SqlExpr valueOf(Value value) {
127: if (value instanceof URI) {
128: return str(((URI) value).stringValue());
129: }
130: return sqlNull();
131: }
132:
133: }
|