1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.util.Enumeration ; 47 import java.util.Vector ; 48 49 import java.sql.ResultSetMetaData ; 50 import java.sql.SQLException ; 51 52 import com.quadcap.sql.types.Type; 53 54 import com.quadcap.util.Debug; 55 56 61 public class Column implements Externalizable { 62 transient Tuple table; 63 transient Vector constraints = null; 64 65 String name; 66 String 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 78 public Column() {} 79 80 83 public Column(String name, Type type) { 84 this.name = name; 85 this.type = type; 86 } 87 88 91 public Column(String name, Column col) { 92 this.name = name; 93 this.type = col.getType(); 94 this.isAutoIncr = col.isAutoIncr; 95 } 96 97 102 public void readExternal(ObjectInput in) 103 throws IOException , ClassNotFoundException 104 { 105 name = (String )in.readObject(); 106 shortName = (String )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 119 public void writeExternal(ObjectOutput out) throws IOException { 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 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 (); 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 155 Vector getConstraints() { 156 return constraints; 157 } 158 159 162 public Type getType() { return type; } 163 164 167 void setType(Type type) { this.type = type; } 168 169 172 void setColumn(int column) { this.column = column; } 173 174 177 public int getColumn() { return column; } 178 179 182 public String getName() { return name; } 183 184 187 void setName(String name) { this.name = name; } 188 189 192 void setShortName(String name) { this.shortName = name; } 193 194 197 public String getShortName() { 198 if (shortName == null) shortName = name; 199 return shortName; 200 } 201 202 205 public boolean isJoinColumn() { 206 return joinColumn; 207 } 208 209 212 public void setJoinColumn(boolean b) { 213 joinColumn = b; 214 } 215 216 219 void setTable(Tuple table) { this.table = table; } 220 221 224 public Tuple getRelation() { return table; } 225 226 229 public int getNullable() { 230 return nullable; 231 } 232 233 236 public boolean isNullable() { 237 return nullable == ResultSetMetaData.columnNullable; 238 } 239 240 243 public void setNullable(int nullable) { 244 this.nullable = nullable; 245 } 246 247 250 public void setDefault(Expression expr) { 251 defaultExpr = expr; 252 } 253 254 257 public Expression getDefault() { 258 return defaultExpr; 259 } 260 261 265 public boolean isAutoIncrement() { 266 return isAutoIncr; 267 } 268 269 273 public String toString() { 274 StringBuffer sb = new StringBuffer ("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 } 286 | Popular Tags |