001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.util.ArrayList;
047: import java.util.Vector;
048:
049: import java.sql.SQLException;
050:
051: import com.quadcap.sql.types.Op;
052: import com.quadcap.sql.types.Type;
053: import com.quadcap.sql.types.Value;
054:
055: import com.quadcap.util.Debug;
056: import com.quadcap.util.Util;
057:
058: /**
059: * Expression class for names (typically column or function names).
060: *
061: * @author Stan Bailes
062: */
063: public class NameExpression extends Expression implements
064: Externalizable {
065: String name;
066: boolean not = false;
067:
068: /**
069: * Default constructor
070: */
071: public NameExpression() {
072: }
073:
074: public NameExpression(String name) {
075: this .name = name;
076: }
077:
078: /**
079: * I'm a scalar, basically
080: */
081: public int rank() {
082: return 0;
083: }
084:
085: public Type getType(Session session, Cursor cursor)
086: throws SQLException {
087: String rname = session.getConnection().resolveColname(name,
088: cursor);
089: Column c = cursor.getColumn(rname);
090: return c.getType();
091: }
092:
093: public Value getValue(Session session, Cursor cursor)
094: throws SQLException {
095: Cursor effectiveCursor = cursor;
096: if (cursor == null) {
097: throw new SQLException("No cursor for name expression: "
098: + name, "42000");
099: }
100: String rname = session.getConnection().resolveColname(name,
101: cursor);
102: Column c = cursor.getColumn(rname);
103: if (c == null) {
104: throw new SQLException("Bad column: " + name, "Q000S");
105: }
106: Tuple tc = c.getRelation();
107: if (tc instanceof Cursor && !(cursor instanceof StaticCursor)) {
108: effectiveCursor = (Cursor) tc;
109: }
110:
111: int col = c.getColumn();
112: Row row = effectiveCursor.getRow();
113: if (row == null) {
114: throw new SQLException("no cursor", "Q000T");
115: }
116: Value v = row.item(col);
117: if (not)
118: v = v.unop(Op.NOT);
119: return v;
120: }
121:
122: public Row getValues(Session session, Cursor cursor)
123: throws SQLException {
124: ArrayList v = new ArrayList();
125: v.add(getValue(session, cursor));
126: return new Row(v);
127: }
128:
129: public String getName() {
130: return name;
131: }
132:
133: public void invert() {
134: not = !not;
135: }
136:
137: public void visitSubExpressions(ExpressionVisitor ev) {
138: }
139:
140: public void readExternal(ObjectInput in) throws IOException,
141: ClassNotFoundException {
142: name = (String) in.readObject();
143: }
144:
145: public void writeExternal(ObjectOutput out) throws IOException {
146: out.writeObject(name);
147: }
148:
149: public String toString() {
150: return name;
151: }
152: }
|