00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.Externalizable;
00042
import java.io.IOException;
00043
import java.io.ObjectInput;
00044
import java.io.ObjectOutput;
00045
00046
import java.util.ArrayList;
00047
import java.util.Vector;
00048
00049
import java.sql.SQLException;
00050
00051
import com.quadcap.sql.types.Op;
00052
import com.quadcap.sql.types.Type;
00053
import com.quadcap.sql.types.Value;
00054
00055
import com.quadcap.util.Debug;
00056
import com.quadcap.util.Util;
00057
00058
00059
00060
00061
00062
00063 public class NameExpression extends Expression implements
Externalizable {
00064 String
name;
00065 boolean not =
false;
00066
00067
00068
00069
00070 public NameExpression() {}
00071
00072 public NameExpression(String name) {
00073
this.name = name;
00074 }
00075
00076
00077
00078
00079 public int rank() {
return 0; }
00080
00081 public Type getType(
Session session,
Cursor cursor)
throws SQLException {
00082 String rname = session.getConnection().resolveColname(
name, cursor);
00083
Column c = cursor.getColumn(rname);
00084
return c.
getType();
00085 }
00086
00087 public Value getValue(
Session session,
Cursor cursor)
throws SQLException {
00088
Cursor effectiveCursor = cursor;
00089
if (cursor == null) {
00090
throw new SQLException(
"No cursor for name expression: " +
name,
"42000");
00091 }
00092 String rname = session.getConnection().resolveColname(
name, cursor);
00093
Column c = cursor.
getColumn(rname);
00094
if (c == null) {
00095
throw new SQLException(
"Bad column: " +
name,
"Q000S");
00096 }
00097
Tuple tc = c.
getRelation();
00098
if (tc instanceof
Cursor && !(cursor instanceof
StaticCursor)) {
00099 effectiveCursor = (
Cursor)tc;
00100 }
00101
00102
int col = c.
getColumn();
00103
Row row = effectiveCursor.
getRow();
00104
if (row == null) {
00105
throw new SQLException(
"no cursor",
"Q000T");
00106 }
00107
Value v = row.
item(col);
00108
if (
not) v = v.
unop(
Op.NOT);
00109
return v;
00110 }
00111
00112 public Row getValues(
Session session,
Cursor cursor)
throws SQLException {
00113 ArrayList v =
new ArrayList();
00114 v.add(getValue(session, cursor));
00115
return new Row(v);
00116 }
00117
00118 public String
getName() {
return name; }
00119
00120 public void invert() {
00121
not = !
not;
00122 }
00123
00124 public void visitSubExpressions(
ExpressionVisitor ev) {
00125 }
00126
00127 public void readExternal(ObjectInput in)
00128
throws IOException, ClassNotFoundException
00129 {
00130
name = (String)in.readObject();
00131 }
00132
00133 public void writeExternal(ObjectOutput out)
throws IOException {
00134 out.writeObject(
name);
00135 }
00136
00137 public String
toString() {
00138
return name;
00139 }
00140 }