KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Enumeration JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 import java.sql.ResultSetMetaData JavaDoc;
50 import java.sql.SQLException JavaDoc;
51
52 import com.quadcap.sql.types.Type;
53
54 import com.quadcap.util.Debug;
55
56 /**
57  * A column lives in a tuple, has a name, a type, and a default value.
58  *
59  * @author Stan Bailes
60  */

61 public class Column implements Externalizable JavaDoc {
62     transient Tuple table;
63     transient Vector JavaDoc constraints = null;
64
65     String JavaDoc name;
66     String JavaDoc shortName;
67     int column;
68     boolean joinColumn = false;
69     Type type;
70     Expression defaultExpr;
71     boolean isAutoIncr = false;
72     int nullable
73     = ResultSetMetaData.columnNullable;
74
75     /**
76      * Default constructor
77      */

78     public Column() {}
79
80     /**
81      * Basic column construction: name + type
82      */

83     public Column(String JavaDoc name, Type type) {
84     this.name = name;
85     this.type = type;
86     }
87
88     /**
89      * Copy (rename) column construction: name + old col
90      */

91     public Column(String JavaDoc name, Column col) {
92     this.name = name;
93     this.type = col.getType();
94         this.isAutoIncr = col.isAutoIncr;
95     }
96
97     /**
98      * Read me from a stream
99      *
100      * @exception IOException may be thrown
101      */

102     public void readExternal(ObjectInput JavaDoc in)
103     throws IOException JavaDoc, ClassNotFoundException JavaDoc
104     {
105     name = (String JavaDoc)in.readObject();
106     shortName = (String JavaDoc)in.readObject();
107     type = (Type)in.readObject();
108     column = in.readInt();
109     defaultExpr = (Expression)in.readObject();
110     nullable = in.readInt();
111         isAutoIncr = (in.read() == 1);
112     }
113
114     /**
115      * Write me to a stream
116      *
117      * @exception IOException may be thrown
118      */

119     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
120     out.writeObject(name);
121     out.writeObject(shortName);
122     out.writeObject(type);
123     out.writeInt(column);
124     out.writeObject(defaultExpr);
125     out.writeInt(nullable);
126         out.write(isAutoIncr ? 1 : 0);
127     }
128
129     /**
130      * Add a new column constraint. Keep the constraints in order by
131      * priority.
132      */

133     public void addConstraint(Constraint con) {
134         if (con instanceof AutoNumberConstraint) {
135             isAutoIncr = true;
136         }
137         
138     con.setColumn(this);
139     if (constraints == null) constraints = new Vector JavaDoc();
140         boolean added = false;
141         for (int i = 0; i < constraints.size(); i++) {
142             Constraint c1 = (Constraint)constraints.elementAt(i);
143             if (con.getPriority() < c1.getPriority()) {
144                 constraints.insertElementAt(con, i);
145                 added = true;
146                 break;
147             }
148         }
149         if (!added) constraints.addElement(con);
150     }
151
152     /**
153      * Return all of this column's constraints, as a vector
154      */

155     Vector JavaDoc getConstraints() {
156     return constraints;
157     }
158
159     /**
160      * Return this column's type
161      */

162     public Type getType() { return type; }
163
164     /**
165      * Set the column's type
166      */

167     void setType(Type type) { this.type = type; }
168
169     /**
170      * Set the column's position (one-based) in the table
171      */

172     void setColumn(int column) { this.column = column; }
173
174     /**
175      * Get the column's position (one-based) in the table
176      */

177     public int getColumn() { return column; }
178
179     /**
180      * Get the column's name
181      */

182     public String JavaDoc getName() { return name; }
183
184     /**
185      * Set the column's name
186      */

187     void setName(String JavaDoc name) { this.name = name; }
188
189     /**
190      * Set the column's short name
191      */

192     void setShortName(String JavaDoc name) { this.shortName = name; }
193
194     /**
195      * Get the column's short name
196      */

197     public String JavaDoc getShortName() {
198     if (shortName == null) shortName = name;
199     return shortName;
200     }
201
202     /**
203      * Return true if this column has the 'joinColumn' flag set.
204      */

205     public boolean isJoinColumn() {
206     return joinColumn;
207     }
208
209     /**
210      * Set the 'joinColumn' flag.
211      */

212     public void setJoinColumn(boolean b) {
213     joinColumn = b;
214     }
215
216     /**
217      * Set the column's table
218      */

219     void setTable(Tuple table) { this.table = table; }
220
221     /**
222      * Get the tuple in which this column is contained.
223      */

224     public Tuple getRelation() { return table; }
225
226     /**
227      * Return the JDBC 'nullable' state of this column.
228      */

229     public int getNullable() {
230     return nullable;
231     }
232
233     /**
234      * Return true if this column allows nulls.
235      */

236     public boolean isNullable() {
237         return nullable == ResultSetMetaData.columnNullable;
238     }
239
240     /**
241      * Set the JDBC 'nullable' state of this column.
242      */

243     public void setNullable(int nullable) {
244     this.nullable = nullable;
245     }
246
247     /**
248      * Set the column's default value
249      */

250     public void setDefault(Expression expr) {
251     defaultExpr = expr;
252     }
253
254     /**
255      * Get the column's default value
256      */

257     public Expression getDefault() {
258     return defaultExpr;
259     }
260
261     /**
262      * Return <code><b>true</b></code> if this column is an auto-increment
263      * type.
264      */

265     public boolean isAutoIncrement() {
266         return isAutoIncr;
267     }
268
269     //#ifdef DEBUG
270
/**
271      * Return a string for debugging purposes
272      */

273     public String JavaDoc toString() {
274     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Column ");
275     if (joinColumn) sb.append("*");
276     sb.append(name);
277     sb.append(", type = ");
278     sb.append(type);
279     sb.append(", col = ");
280     sb.append(column);
281         if (isAutoIncr) sb.append(", with identity");
282     return sb.toString();
283     }
284     //#endif
285
}
286
Popular Tags