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.Enumeration;
047: import java.util.Vector;
048:
049: import java.sql.ResultSetMetaData;
050: import java.sql.SQLException;
051:
052: import com.quadcap.sql.types.Type;
053:
054: import com.quadcap.util.Debug;
055:
056: /**
057: * A column lives in a tuple, has a name, a type, and a default value.
058: *
059: * @author Stan Bailes
060: */
061: public class Column implements Externalizable {
062: transient Tuple table;
063: transient Vector constraints = null;
064:
065: String name;
066: String shortName;
067: int column;
068: boolean joinColumn = false;
069: Type type;
070: Expression defaultExpr;
071: boolean isAutoIncr = false;
072: int nullable = ResultSetMetaData.columnNullable;
073:
074: /**
075: * Default constructor
076: */
077: public Column() {
078: }
079:
080: /**
081: * Basic column construction: name + type
082: */
083: public Column(String name, Type type) {
084: this .name = name;
085: this .type = type;
086: }
087:
088: /**
089: * Copy (rename) column construction: name + old col
090: */
091: public Column(String name, Column col) {
092: this .name = name;
093: this .type = col.getType();
094: this .isAutoIncr = col.isAutoIncr;
095: }
096:
097: /**
098: * Read me from a stream
099: *
100: * @exception IOException may be thrown
101: */
102: public void readExternal(ObjectInput in) throws IOException,
103: ClassNotFoundException {
104: name = (String) in.readObject();
105: shortName = (String) in.readObject();
106: type = (Type) in.readObject();
107: column = in.readInt();
108: defaultExpr = (Expression) in.readObject();
109: nullable = in.readInt();
110: isAutoIncr = (in.read() == 1);
111: }
112:
113: /**
114: * Write me to a stream
115: *
116: * @exception IOException may be thrown
117: */
118: public void writeExternal(ObjectOutput out) throws IOException {
119: out.writeObject(name);
120: out.writeObject(shortName);
121: out.writeObject(type);
122: out.writeInt(column);
123: out.writeObject(defaultExpr);
124: out.writeInt(nullable);
125: out.write(isAutoIncr ? 1 : 0);
126: }
127:
128: /**
129: * Add a new column constraint. Keep the constraints in order by
130: * priority.
131: */
132: public void addConstraint(Constraint con) {
133: if (con instanceof AutoNumberConstraint) {
134: isAutoIncr = true;
135: }
136:
137: con.setColumn(this );
138: if (constraints == null)
139: constraints = new Vector();
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)
150: constraints.addElement(con);
151: }
152:
153: /**
154: * Return all of this column's constraints, as a vector
155: */
156: Vector getConstraints() {
157: return constraints;
158: }
159:
160: /**
161: * Return this column's type
162: */
163: public Type getType() {
164: return type;
165: }
166:
167: /**
168: * Set the column's type
169: */
170: void setType(Type type) {
171: this .type = type;
172: }
173:
174: /**
175: * Set the column's position (one-based) in the table
176: */
177: void setColumn(int column) {
178: this .column = column;
179: }
180:
181: /**
182: * Get the column's position (one-based) in the table
183: */
184: public int getColumn() {
185: return column;
186: }
187:
188: /**
189: * Get the column's name
190: */
191: public String getName() {
192: return name;
193: }
194:
195: /**
196: * Set the column's name
197: */
198: void setName(String name) {
199: this .name = name;
200: }
201:
202: /**
203: * Set the column's short name
204: */
205: void setShortName(String name) {
206: this .shortName = name;
207: }
208:
209: /**
210: * Get the column's short name
211: */
212: public String getShortName() {
213: if (shortName == null)
214: shortName = name;
215: return shortName;
216: }
217:
218: /**
219: * Return true if this column has the 'joinColumn' flag set.
220: */
221: public boolean isJoinColumn() {
222: return joinColumn;
223: }
224:
225: /**
226: * Set the 'joinColumn' flag.
227: */
228: public void setJoinColumn(boolean b) {
229: joinColumn = b;
230: }
231:
232: /**
233: * Set the column's table
234: */
235: void setTable(Tuple table) {
236: this .table = table;
237: }
238:
239: /**
240: * Get the tuple in which this column is contained.
241: */
242: public Tuple getRelation() {
243: return table;
244: }
245:
246: /**
247: * Return the JDBC 'nullable' state of this column.
248: */
249: public int getNullable() {
250: return nullable;
251: }
252:
253: /**
254: * Return true if this column allows nulls.
255: */
256: public boolean isNullable() {
257: return nullable == ResultSetMetaData.columnNullable;
258: }
259:
260: /**
261: * Set the JDBC 'nullable' state of this column.
262: */
263: public void setNullable(int nullable) {
264: this .nullable = nullable;
265: }
266:
267: /**
268: * Set the column's default value
269: */
270: public void setDefault(Expression expr) {
271: defaultExpr = expr;
272: }
273:
274: /**
275: * Get the column's default value
276: */
277: public Expression getDefault() {
278: return defaultExpr;
279: }
280:
281: /**
282: * Return <code><b>true</b></code> if this column is an auto-increment
283: * type.
284: */
285: public boolean isAutoIncrement() {
286: return isAutoIncr;
287: }
288:
289: //#ifdef DEBUG
290: /**
291: * Return a string for debugging purposes
292: */
293: public String toString() {
294: StringBuffer sb = new StringBuffer("Column ");
295: if (joinColumn)
296: sb.append("*");
297: sb.append(name);
298: sb.append(", type = ");
299: sb.append(type);
300: sb.append(", col = ");
301: sb.append(column);
302: if (isAutoIncr)
303: sb.append(", with identity");
304: return sb.toString();
305: }
306: //#endif
307: }
|