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.sqlNull;
009: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.unsupported;
010: import static org.openrdf.sail.rdbms.managers.LiteralManager.getCalendarValue;
011:
012: import org.openrdf.model.Literal;
013: import org.openrdf.model.URI;
014: import org.openrdf.model.Value;
015: import org.openrdf.model.datatypes.XMLDatatypeUtil;
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.DateTimeColumn;
026: import org.openrdf.sail.rdbms.algebra.NumberValue;
027: import org.openrdf.sail.rdbms.algebra.SqlNull;
028: import org.openrdf.sail.rdbms.algebra.base.SqlExpr;
029: import org.openrdf.sail.rdbms.exceptions.UnsupportedRdbmsOperatorException;
030:
031: /**
032: * Creates an SQL expression for a literal's time value.
033: *
034: * @author James Leigh
035: *
036: */
037: public class TimeExprFactory extends
038: QueryModelVisitorBase<UnsupportedRdbmsOperatorException> {
039: protected SqlExpr result;
040:
041: public SqlExpr createTimeExpr(ValueExpr expr)
042: throws UnsupportedRdbmsOperatorException {
043: result = null;
044: if (expr == null)
045: return new SqlNull();
046: expr.visit(this );
047: if (result == null)
048: return new SqlNull();
049: return result;
050: }
051:
052: @Override
053: public void meet(Datatype node) {
054: result = sqlNull();
055: }
056:
057: @Override
058: public void meet(Lang node)
059: throws UnsupportedRdbmsOperatorException {
060: result = sqlNull();
061: }
062:
063: @Override
064: public void meet(MathExpr node)
065: throws UnsupportedRdbmsOperatorException {
066: result = sqlNull();
067: }
068:
069: @Override
070: public void meet(Str node) {
071: result = sqlNull();
072: }
073:
074: @Override
075: public void meet(ValueConstant vc) {
076: result = valueOf(vc.getValue());
077: }
078:
079: @Override
080: public void meet(Var node) {
081: if (node.getValue() == null) {
082: result = new DateTimeColumn(node);
083: } else {
084: result = valueOf(node.getValue());
085: }
086: }
087:
088: @Override
089: protected void meetNode(QueryModelNode arg)
090: throws UnsupportedRdbmsOperatorException {
091: throw unsupported(arg);
092: }
093:
094: private SqlExpr valueOf(Value value) {
095: if (value instanceof Literal) {
096: Literal lit = (Literal) value;
097: URI dt = lit.getDatatype();
098: if (dt != null && XMLDatatypeUtil.isCalendarDatatype(dt)) {
099: try {
100: return new NumberValue(getCalendarValue(lit
101: .calendarValue()));
102: } catch (IllegalArgumentException e) {
103: return null;
104: }
105: }
106: }
107: return null;
108: }
109: }
|