KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > UnaryExpression


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.sql.SQLException JavaDoc;
47
48 import com.quadcap.sql.index.BCursor;
49 import com.quadcap.sql.index.Btree;
50
51 import com.quadcap.sql.types.Op;
52 import com.quadcap.sql.types.Type;
53 import com.quadcap.sql.types.TypeBoolean;
54 import com.quadcap.sql.types.Value;
55 import com.quadcap.sql.types.ValueBoolean;
56
57 import com.quadcap.util.Debug;
58
59 /**
60  * Expression implementing unary ops.
61  *
62  * @author Stan Bailes
63  */

64 public class UnaryExpression extends Expression implements Externalizable JavaDoc {
65     Expression e = null;
66     int op = -1;
67     boolean not = false;
68     Value value = null;
69
70     public UnaryExpression() {}
71
72     public UnaryExpression(int op, Expression e) {
73     this.op = op;
74     this.e = e;
75     }
76
77     public void invert() {
78     not = !not;
79     }
80
81     public int rank() { return 0; }
82
83     public Value uniqueValue(Session session, Cursor cursor)
84         throws SQLException JavaDoc, IOException JavaDoc
85     {
86     Cursor c = e.getCursor(session, cursor);
87     try {
88             Btree index = session.makeTempTree();
89             try {
90                 BCursor bc = index.getCursor(true);
91                 try {
92                     while (c.next()) {
93                         Row row = c.getRow();
94                         byte[] key = Key.makeKey(null, row, null, 0, false);
95                         if (bc.seek(key)) {
96                             return ValueBoolean.trueBoolean;
97                         } else {
98                             bc.insert(key, key.length, key, 0, 1);
99                         }
100                     }
101                     return ValueBoolean.falseBoolean;
102                 } finally {
103                     bc.release();
104                 }
105             } finally {
106                 try {
107                     index.free();
108                 } finally {
109                     session.getDatabase().releaseTempFile();
110                 }
111             }
112                 
113     } finally {
114         c.close();
115     }
116     }
117     
118     public Value existsValue(Session session, Cursor cursor)
119         throws SQLException JavaDoc
120     {
121     Cursor c = e.getCursor(session, cursor);
122     try {
123         Value ret = new ValueBoolean(c.next());
124         if (not) ret = ret.unop(Op.NOT);
125             //Debug.println("c = " + c.getClass().getName() + ":" + c);
126
//Debug.println("[" + toString() + "].existsValue(" + cursor.getRow() + ") = " + ret);
127
return ret;
128     } finally {
129         c.close();
130     }
131     }
132     
133     public Type getType(Session session, Cursor cursor) throws SQLException JavaDoc {
134         switch (op) {
135         case Op.EXISTS:
136     case Op.UNIQUE:
137             return TypeBoolean.typeBoolean;
138         default:
139             return e.getType(session, cursor);
140         }
141     }
142     
143     public Value getValue(Session session, Cursor cursor) throws SQLException JavaDoc {
144     switch (op) {
145     case Op.EXISTS:
146         return existsValue(session, cursor);
147     case Op.UNIQUE:
148             try {
149                 return uniqueValue(session, cursor);
150             } catch (IOException JavaDoc ex) {
151                 throw DbException.wrapThrowable(ex);
152             }
153     default:
154         Value v = e.getValue(session, cursor);
155         value = v.unop(op);
156         if (not) {
157         value = value.unop(Op.NOT);
158         }
159         return value;
160     }
161     }
162
163     public void visitSubExpressions(ExpressionVisitor ev) {
164     ev.visit(e);
165     }
166
167     public String JavaDoc toString() {
168     String JavaDoc n = not ? "NOT " : "";
169     return n + Op.toString(op) + " " + e.toString();
170     }
171
172     public void readExternal(ObjectInput JavaDoc in)
173     throws IOException JavaDoc, ClassNotFoundException JavaDoc
174     {
175     e = (Expression)in.readObject();
176     op = in.readInt();
177     not = in.read() == 1;
178     value = (Value)in.readObject();
179     }
180     
181     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
182     out.writeObject(e);
183     out.writeInt(op);
184     out.write(not ? 1 : 0);
185     out.writeObject(value);
186     }
187 }
188
Popular Tags