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