001: /**
002: * com.mckoi.database.CorrelatedVariable 08 Nov 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: /**
026: * A wrapper for a variable in a sub-query that references a column outside
027: * of the current query. A correlated variable differs from a regular
028: * variable because its value is constant in an operation, but may vary over
029: * future iterations of the operation.
030: * <p>
031: * This object is NOT immutable.
032: *
033: * @author Tobias Downer
034: */
035:
036: public class CorrelatedVariable implements Cloneable,
037: java.io.Serializable {
038:
039: static final long serialVersionUID = -607848111230634419L;
040:
041: /**
042: * The Variable reference itself.
043: */
044: private Variable variable;
045:
046: /**
047: * The number of sub-query branches back that the reference for this
048: * variable can be found.
049: */
050: private int query_level_offset;
051:
052: /**
053: * The temporary value this variable has been set to evaluate to.
054: */
055: private transient TObject eval_result;
056:
057: /**
058: * Constructs the CorrelatedVariable.
059: */
060: public CorrelatedVariable(Variable variable, int level_offset) {
061: this .variable = variable;
062: this .query_level_offset = level_offset;
063: }
064:
065: /**
066: * Returns the wrapped Variable.
067: */
068: public Variable getVariable() {
069: return variable;
070: }
071:
072: /**
073: * Returns the number of sub-query branches back that the reference for this
074: * variable can be found. For example, if the correlated variable references
075: * the direct descendant this will return 1.
076: */
077: public int getQueryLevelOffset() {
078: return query_level_offset;
079: }
080:
081: /**
082: * Sets the value this correlated variable evaluates to.
083: */
084: public void setEvalResult(TObject ob) {
085: this .eval_result = ob;
086: }
087:
088: /**
089: * Given a VariableResolver this will set the value of the correlated
090: * variable.
091: */
092: public void setFromResolver(VariableResolver resolver) {
093: Variable v = getVariable();
094: setEvalResult(resolver.resolve(v));
095: }
096:
097: /**
098: * Returns the value this correlated variable evaluates to.
099: */
100: public TObject getEvalResult() {
101: return eval_result;
102: }
103:
104: /**
105: * Returns the TType this correlated variable evaluates to.
106: */
107: public TType returnTType() {
108: return eval_result.getTType();
109: }
110:
111: /**
112: * Clones the object.
113: */
114: public Object clone() throws CloneNotSupportedException {
115: CorrelatedVariable v = (CorrelatedVariable) super .clone();
116: v.variable = (Variable) variable.clone();
117: return v;
118: }
119:
120: public String toString() {
121: return "CORRELATED: " + getVariable() + " = " + getEvalResult();
122: }
123:
124: }
|