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;
007:
008: import org.openrdf.model.Resource;
009: import org.openrdf.model.URI;
010: import org.openrdf.model.Value;
011: import org.openrdf.query.algebra.Var;
012: import org.openrdf.sail.rdbms.model.RdbmsResource;
013: import org.openrdf.sail.rdbms.model.RdbmsURI;
014: import org.openrdf.sail.rdbms.schema.ValueTypes;
015:
016: /**
017: * Represents a variable in an SQL expression.
018: *
019: * @author James Leigh
020: *
021: */
022: public class ColumnVar implements Cloneable {
023: private int index;
024: private boolean anonymous;
025: private boolean hidden;
026: private boolean implied;
027: private String name;
028: private Value value;
029: private String alias;
030: private String column;
031: private boolean nullable;
032: private ValueTypes types;
033:
034: private ColumnVar() {
035: }
036:
037: public static ColumnVar createSubj(String alias, Var v,
038: Resource resource) {
039: ColumnVar var = new ColumnVar();
040: var.alias = alias;
041: var.column = "subj";
042: var.name = v.getName();
043: var.anonymous = v.isAnonymous();
044: var.value = resource;
045: var.types = ValueTypes.RESOURCE;
046: if (resource instanceof RdbmsURI) {
047: var.types = ValueTypes.URI;
048: }
049: return var;
050: }
051:
052: public static ColumnVar createPred(String alias, Var v, URI uri,
053: boolean implied) {
054: ColumnVar var = createSubj(alias, v, uri);
055: var.column = "pred";
056: var.implied = uri != null && implied;
057: var.types = ValueTypes.URI;
058: return var;
059: }
060:
061: public static ColumnVar createObj(String alias, Var v, Value value) {
062: ColumnVar var = new ColumnVar();
063: var.alias = alias;
064: var.column = "obj";
065: var.name = v.getName();
066: var.anonymous = v.isAnonymous();
067: var.value = value;
068: var.types = ValueTypes.UNKNOWN;
069: if (value instanceof RdbmsURI) {
070: var.types = ValueTypes.URI;
071: } else if (value instanceof RdbmsResource) {
072: var.types = ValueTypes.RESOURCE;
073: }
074: return var;
075: }
076:
077: public static ColumnVar createCtx(String alias, Var v,
078: Resource resource) {
079: ColumnVar var = new ColumnVar();
080: var.alias = alias;
081: var.column = "ctx";
082: if (v == null) {
083: var.name = "__ctx"
084: + Integer.toHexString(System.identityHashCode(var));
085: var.anonymous = true;
086: var.hidden = true;
087: } else {
088: var.name = v.getName();
089: var.anonymous = v.isAnonymous();
090: }
091: var.value = resource;
092: var.types = ValueTypes.RESOURCE;
093: if (resource instanceof RdbmsURI) {
094: var.types = ValueTypes.URI;
095: }
096: return var;
097: }
098:
099: public ValueTypes getTypes() {
100: return types;
101: }
102:
103: public void setTypes(ValueTypes types) {
104: this .types = types;
105: }
106:
107: public boolean isAnonymous() {
108: return anonymous;
109: }
110:
111: public boolean isHidden() {
112: return hidden || value != null;
113: }
114:
115: public boolean isImplied() {
116: return implied;
117: }
118:
119: public boolean isResource() {
120: return !types.isLiterals();
121: }
122:
123: public boolean isURI() {
124: return !types.isLiterals() && !types.isBNodes();
125: }
126:
127: public boolean isNullable() {
128: return nullable;
129: }
130:
131: public int getIndex() {
132: return index;
133: }
134:
135: public void setIndex(int index) {
136: this .index = index;
137: }
138:
139: public String getName() {
140: return name;
141: }
142:
143: public Value getValue() {
144: return value;
145: }
146:
147: public void setValue(Value value) {
148: this .value = value;
149: }
150:
151: public String getColumn() {
152: return column;
153: }
154:
155: public boolean isPredicate() {
156: return "pred".equals(column);
157: }
158:
159: public String getAlias() {
160: return alias;
161: }
162:
163: public ColumnVar as(String name) {
164: try {
165: ColumnVar clone = (ColumnVar) super .clone();
166: clone.name = name;
167: return clone;
168: } catch (CloneNotSupportedException e) {
169: throw new AssertionError(e);
170: }
171: }
172:
173: public ColumnVar as(String alias, String column) {
174: try {
175: ColumnVar clone = (ColumnVar) super .clone();
176: clone.alias = alias;
177: clone.column = column;
178: clone.nullable = true;
179: return clone;
180: } catch (CloneNotSupportedException e) {
181: throw new AssertionError(e);
182: }
183: }
184:
185: @Override
186: public boolean equals(Object other) {
187: if (other instanceof ColumnVar) {
188: return name.equals(((ColumnVar) other).name);
189: }
190:
191: return false;
192: }
193:
194: @Override
195: public int hashCode() {
196: return name.hashCode();
197: }
198:
199: @Override
200: public String toString() {
201: StringBuilder sb = new StringBuilder(128);
202:
203: sb.append(alias).append(".").append(column);
204:
205: sb.append(" (name=").append(name);
206:
207: if (value != null) {
208: sb.append(", value=").append(value.toString());
209: }
210:
211: sb.append(")");
212: if (index > 0) {
213: sb.append("#").append(index);
214: }
215:
216: return sb.toString();
217: }
218:
219: }
|