Quadcap Embeddable Database

com/quadcap/sql/Column.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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.Enumeration; 00047 import java.util.Vector; 00048 00049 import java.sql.ResultSetMetaData; 00050 import java.sql.SQLException; 00051 00052 import com.quadcap.sql.types.Type; 00053 00054 import com.quadcap.util.Debug; 00055 00056 /** 00057 * A column lives in a tuple, has a name, a type, and a default value. 00058 * 00059 * @author Stan Bailes 00060 */ 00061 public class Column implements Externalizable { 00062 transient Tuple table; 00063 transient Vector constraints = null; 00064 00065 String name; 00066 String shortName; 00067 int column; 00068 boolean joinColumn = false; 00069 Type type; 00070 Expression defaultExpr; 00071 boolean isAutoIncr = false; 00072 int nullable 00073 = ResultSetMetaData.columnNullable; 00074 00075 /** 00076 * Default constructor 00077 */ 00078 public Column() {} 00079 00080 /** 00081 * Basic column construction: name + type 00082 */ 00083 public Column(String name, Type type) { 00084 this.name = name; 00085 this.type = type; 00086 } 00087 00088 /** 00089 * Copy (rename) column construction: name + old col 00090 */ 00091 public Column(String name, Column col) { 00092 this.name = name; 00093 this.type = col.getType(); 00094 this.isAutoIncr = col.isAutoIncr; 00095 } 00096 00097 /** 00098 * Read me from a stream 00099 * 00100 * @exception IOException may be thrown 00101 */ 00102 public void readExternal(ObjectInput in) 00103 throws IOException, ClassNotFoundException 00104 { 00105 name = (String)in.readObject(); 00106 shortName = (String)in.readObject(); 00107 type = (Type)in.readObject(); 00108 column = in.readInt(); 00109 defaultExpr = (Expression)in.readObject(); 00110 nullable = in.readInt(); 00111 isAutoIncr = (in.read() == 1); 00112 } 00113 00114 /** 00115 * Write me to a stream 00116 * 00117 * @exception IOException may be thrown 00118 */ 00119 public void writeExternal(ObjectOutput out) throws IOException { 00120 out.writeObject(name); 00121 out.writeObject(shortName); 00122 out.writeObject(type); 00123 out.writeInt(column); 00124 out.writeObject(defaultExpr); 00125 out.writeInt(nullable); 00126 out.write(isAutoIncr ? 1 : 0); 00127 } 00128 00129 /** 00130 * Add a new column constraint. Keep the constraints in order by 00131 * priority. 00132 */ 00133 public void addConstraint(Constraint con) { 00134 if (con instanceof AutoNumberConstraint) { 00135 isAutoIncr = true; 00136 } 00137 00138 con.setColumn(this); 00139 if (constraints == null) constraints = new Vector(); 00140 boolean added = false; 00141 for (int i = 0; i < constraints.size(); i++) { 00142 Constraint c1 = (Constraint)constraints.elementAt(i); 00143 if (con.getPriority() < c1.getPriority()) { 00144 constraints.insertElementAt(con, i); 00145 added = true; 00146 break; 00147 } 00148 } 00149 if (!added) constraints.addElement(con); 00150 } 00151 00152 /** 00153 * Return all of this column's constraints, as a vector 00154 */ 00155 Vector getConstraints() { 00156 return constraints; 00157 } 00158 00159 /** 00160 * Return this column's type 00161 */ 00162 public Type getType() { return type; } 00163 00164 /** 00165 * Set the column's type 00166 */ 00167 void setType(Type type) { this.type = type; } 00168 00169 /** 00170 * Set the column's position (one-based) in the table 00171 */ 00172 void setColumn(int column) { this.column = column; } 00173 00174 /** 00175 * Get the column's position (one-based) in the table 00176 */ 00177 public int getColumn() { return column; } 00178 00179 /** 00180 * Get the column's name 00181 */ 00182 public String getName() { return name; } 00183 00184 /** 00185 * Set the column's name 00186 */ 00187 void setName(String name) { this.name = name; } 00188 00189 /** 00190 * Set the column's short name 00191 */ 00192 void setShortName(String name) { this.shortName = name; } 00193 00194 /** 00195 * Get the column's short name 00196 */ 00197 public String getShortName() { 00198 if (shortName == null) shortName = name; 00199 return shortName; 00200 } 00201 00202 /** 00203 * Return true if this column has the 'joinColumn' flag set. 00204 */ 00205 public boolean isJoinColumn() { 00206 return joinColumn; 00207 } 00208 00209 /** 00210 * Set the 'joinColumn' flag. 00211 */ 00212 public void setJoinColumn(boolean b) { 00213 joinColumn = b; 00214 } 00215 00216 /** 00217 * Set the column's table 00218 */ 00219 void setTable(Tuple table) { this.table = table; } 00220 00221 /** 00222 * Get the tuple in which this column is contained. 00223 */ 00224 public Tuple getRelation() { return table; } 00225 00226 /** 00227 * Return the JDBC 'nullable' state of this column. 00228 */ 00229 public int getNullable() { 00230 return nullable; 00231 } 00232 00233 /** 00234 * Return true if this column allows nulls. 00235 */ 00236 public boolean isNullable() { 00237 return nullable == ResultSetMetaData.columnNullable; 00238 } 00239 00240 /** 00241 * Set the JDBC 'nullable' state of this column. 00242 */ 00243 public void setNullable(int nullable) { 00244 this.nullable = nullable; 00245 } 00246 00247 /** 00248 * Set the column's default value 00249 */ 00250 public void setDefault(Expression expr) { 00251 defaultExpr = expr; 00252 } 00253 00254 /** 00255 * Get the column's default value 00256 */ 00257 public Expression getDefault() { 00258 return defaultExpr; 00259 } 00260 00261 /** 00262 * Return <code><b>true</b></code> if this column is an auto-increment 00263 * type. 00264 */ 00265 public boolean isAutoIncrement() { 00266 return isAutoIncr; 00267 } 00268 00269 //#ifdef DEBUG 00270 /** 00271 * Return a string for debugging purposes 00272 */ 00273 public String toString() { 00274 StringBuffer sb = new StringBuffer("Column "); 00275 if (joinColumn) sb.append("*"); 00276 sb.append(name); 00277 sb.append(", type = "); 00278 sb.append(type); 00279 sb.append(", col = "); 00280 sb.append(column); 00281 if (isAutoIncr) sb.append(", with identity"); 00282 return sb.toString(); 00283 } 00284 //#endif 00285 }