1 24 25 package com.mckoi.database; 26 27 import java.io.*; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 32 39 40 public class DataTableDef { 41 42 45 private TableName table_name; 46 47 51 private String table_type_class; 52 53 57 private ArrayList column_list; 58 59 60 63 private boolean immutable; 64 65 68 public DataTableDef() { 69 column_list = new ArrayList (); 70 table_type_class = ""; 71 immutable = false; 72 } 73 74 77 public DataTableDef(DataTableDef table_def) { 78 table_name = table_def.getTableName(); 79 table_type_class = table_def.table_type_class; 80 column_list = (ArrayList ) table_def.column_list.clone(); 81 82 immutable = false; 84 } 85 86 90 public void setImmutable() { 91 immutable = true; 92 } 93 94 97 public boolean immutable() { 98 return immutable; 99 } 100 101 104 private void checkMutable() { 105 if (immutable()) { 106 throw new Error ("Tried to mutate immutable object."); 107 } 108 } 109 110 113 public void dump(PrintStream out) { 114 for (int i = 0; i < columnCount(); ++i) { 115 columnAt(i).dump(out); 116 out.println(); 117 } 118 } 119 120 124 void resolveColumns(boolean ignore_case, Expression exp) { 125 126 if (exp != null) { 131 List list = exp.allVariables(); 132 for (int i = 0; i < list.size(); ++i) { 133 Variable v = (Variable) list.get(i); 134 String col_name = v.getName(); 135 if (ignore_case) { 137 int size = columnCount(); 138 for (int n = 0; n < size; ++n) { 139 if (columnAt(n).getName().equalsIgnoreCase(col_name)) { 142 v.setColumnName(columnAt(n).getName()); 143 } 144 } 145 } 146 } 147 148 } 149 } 150 151 157 public String resolveColumnName(String col_name, boolean ignore_case) 158 throws DatabaseException { 159 int size = columnCount(); 161 int found = -1; 162 for (int n = 0; n < size; ++n) { 163 String this_col_name = columnAt(n).getName(); 166 if (ignore_case && this_col_name.equalsIgnoreCase(col_name)) { 167 if (found == -1) { 168 found = n; 169 } 170 else { 171 throw new DatabaseException( 172 "Ambiguous reference to column '" + col_name + "'"); 173 } 174 } 175 else if (!ignore_case && this_col_name.equals(col_name)) { 176 found = n; 177 } 178 } 179 if (found != -1) { 180 return columnAt(found).getName(); 181 } 182 else { 183 throw new DatabaseException("Column '" + col_name + "' not found"); 184 } 185 } 186 187 192 public void resolveColumnsInArray(DatabaseConnection connection, 193 ArrayList list) throws DatabaseException { 194 boolean ignore_case = connection.isInCaseInsensitiveMode(); 195 for (int i = 0; i < list.size(); ++i) { 196 String col_name = (String ) list.get(i); 197 list.set(i, resolveColumnName((String ) list.get(i), ignore_case)); 198 } 199 } 200 201 203 public void setTableName(TableName name) { 204 this.table_name = name; 205 } 206 207 public void setTableClass(String clazz) { 208 checkMutable(); 209 if (clazz.equals("com.mckoi.database.VariableSizeDataTableFile")) { 210 table_type_class = clazz; 211 } 212 else { 213 throw new Error ("Unrecognised table class: " + clazz); 214 } 215 } 216 217 public void addColumn(DataTableColumnDef col_def) { 218 checkMutable(); 219 for (int i = 0; i < column_list.size(); ++i) { 221 DataTableColumnDef cd = (DataTableColumnDef) column_list.get(i); 222 if (cd.getName().equals(col_def.getName())) { 223 throw new Error ("Duplicated columns found."); 224 } 225 } 226 column_list.add(col_def); 227 } 228 229 233 public void addVirtualColumn(DataTableColumnDef col_def) { 234 checkMutable(); 235 column_list.add(col_def); 236 } 237 238 239 241 public String getSchema() { 242 String schema_name = table_name.getSchema(); 243 return schema_name == null ? "" : schema_name; 244 } 245 246 public String getName() { 247 return table_name.getName(); 248 } 249 250 public TableName getTableName() { 251 return table_name; 252 } 253 254 public String getTableClass() { 255 return table_type_class; 256 } 257 258 public int columnCount() { 259 return column_list.size(); 260 } 261 262 public DataTableColumnDef columnAt(int column) { 263 return (DataTableColumnDef) column_list.get(column); 264 } 265 266 public int findColumnName(String column_name) { 267 int size = columnCount(); 268 for (int i = 0; i < size; ++i) { 269 if (columnAt(i).getName().equals(column_name)) { 270 return i; 271 } 272 } 273 return -1; 274 } 275 276 private transient HashMap col_name_lookup; 278 private transient Object COL_LOOKUP_LOCK = new Object (); 279 283 public final int fastFindColumnName(String col) { 284 synchronized (COL_LOOKUP_LOCK) { 285 if (col_name_lookup == null) { 286 col_name_lookup = new HashMap (30); 287 } 288 Object ob = col_name_lookup.get(col); 289 if (ob == null) { 290 int ci = findColumnName(col); 291 col_name_lookup.put(col, new Integer (ci)); 292 return ci; 293 } 294 else { 295 return ((Integer ) ob).intValue(); 296 } 297 } 298 } 299 300 301 304 public DataTableDef noColumnCopy() { 305 DataTableDef def = new DataTableDef(); 306 def.setTableName(getTableName()); 307 310 def.table_type_class = table_type_class; 311 312 return def; 313 } 314 315 316 318 321 void write(DataOutput out) throws IOException { 322 out.writeInt(2); 324 out.writeUTF(getName()); 325 out.writeUTF(getSchema()); out.writeUTF(table_type_class); 327 out.writeInt(column_list.size()); 328 for (int i = 0; i < column_list.size(); ++i) { 329 ((DataTableColumnDef) column_list.get(i)).write(out); 330 } 331 332 339 349 } 350 351 354 static DataTableDef read(DataInput in) throws IOException { 355 DataTableDef dtf = new DataTableDef(); 356 int ver = in.readInt(); 357 if (ver == 1) { 358 359 throw new IOException("Version 1 DataTableDef no longer supported."); 360 361 } 362 else if (ver == 2) { 363 364 String rname = in.readUTF(); 365 String rschema = in.readUTF(); 366 dtf.setTableName(new TableName(rschema, rname)); 367 dtf.table_type_class = in.readUTF(); 368 int size = in.readInt(); 369 for (int i = 0; i < size; ++i) { 370 DataTableColumnDef col_def = DataTableColumnDef.read(in); 371 dtf.column_list.add(col_def); 372 } 373 374 } 375 else { 376 throw new Error ("Unrecognized DataTableDef version (" + ver + ")"); 377 } 378 379 dtf.setImmutable(); 380 return dtf; 381 } 382 383 } 384 | Popular Tags |