1 29 30 package com.caucho.db.table; 31 32 import com.caucho.db.index.BTree; 33 import com.caucho.db.index.KeyCompare; 34 import com.caucho.db.sql.Expr; 35 import com.caucho.db.sql.QueryContext; 36 import com.caucho.db.sql.SelectResult; 37 import com.caucho.db.store.Transaction; 38 import com.caucho.log.Log; 39 40 import java.sql.SQLException ; 41 import java.util.logging.Logger ; 42 43 abstract public class Column { 44 protected final static Logger log = Log.open(Column.class); 45 46 public final static int NONE = 0; 47 public final static int VARCHAR = 1; 48 public final static int INT = 2; 49 public final static int LONG = 3; 50 public final static int DOUBLE = 4; 51 public final static int DATE = 5; 52 public final static int BLOB = 6; 53 public final static int NUMERIC = 7; 54 55 private final Row _row; 56 private final String _name; 57 58 protected final int _columnOffset; 59 protected final int _nullOffset; 60 protected final byte _nullMask; 61 62 private Table _table; 63 64 private boolean _isPrimaryKey; 65 private boolean _isUnique; 66 private boolean _isNotNull; 67 private int _autoIncrementMin = -1; 68 private Expr _defaultExpr; 69 70 private BTree _index; 71 72 Column(Row row, String name) 73 { 74 _row = row; 75 _name = name; 76 77 _columnOffset = _row.getLength(); 78 _nullOffset = _row.getNullOffset(); 79 _nullMask = _row.getNullMask(); 80 } 81 82 85 public String getName() 86 { 87 return _name; 88 } 89 90 93 void setTable(Table table) 94 { 95 _table = table; 96 } 97 98 101 public Table getTable() 102 { 103 return _table; 104 } 105 106 109 int getColumnOffset() 110 { 111 return _columnOffset; 112 } 113 114 117 abstract public int getTypeCode(); 118 119 122 public Class getJavaType() 123 { 124 return Object .class; 125 } 126 127 130 public boolean isPrimaryKey() 131 { 132 return _isPrimaryKey; 133 } 134 135 138 public void setPrimaryKey(boolean primaryKey) 139 { 140 _isPrimaryKey = primaryKey; 141 } 142 143 146 public boolean isUnique() 147 { 148 return _isUnique; 149 } 150 151 154 public void setUnique() 155 { 156 _isUnique = true; 157 } 158 159 162 public BTree getIndex() 163 { 164 return _index; 165 } 166 167 170 public void setIndex(BTree index) 171 { 172 _index = index; 173 } 174 175 178 public KeyCompare getIndexKeyCompare() 179 { 180 return null; 181 } 182 183 186 public void setNotNull() 187 { 188 _isNotNull = true; 189 } 190 191 194 public boolean isNotNull() 195 { 196 return _isNotNull; 197 } 198 199 202 public void setDefault(Expr expr) 203 { 204 _defaultExpr = expr; 205 } 206 207 210 public Expr getDefault() 211 { 212 return _defaultExpr; 213 } 214 215 218 public void setAutoIncrement(int min) 219 { 220 _autoIncrementMin = min; 221 } 222 223 226 public int getAutoIncrement() 227 { 228 return _autoIncrementMin; 229 } 230 231 234 abstract public int getDeclarationSize(); 235 236 abstract int getLength(); 237 238 244 public final boolean isNull(byte []block, int rowOffset) 245 { 246 return (block[rowOffset + _nullOffset] & _nullMask) == 0; 247 } 248 249 255 public final void setNull(byte []block, int rowOffset) 256 { 257 block[rowOffset + _nullOffset] &= ~_nullMask; 258 } 259 260 266 protected final void setNonNull(byte []block, int rowOffset) 267 { 268 block[rowOffset + _nullOffset] |= _nullMask; 269 } 270 271 277 public abstract String getString(byte []block, int rowOffset) 278 throws SQLException ; 279 280 287 abstract void setString(Transaction xa, 288 byte []block, int rowOffset, String value) 289 throws SQLException ; 290 291 298 public int getInteger(byte []block, int rowOffset) 299 throws SQLException 300 { 301 String str = getString(block, rowOffset); 302 303 if (str == null) 304 return 0; 305 306 return Integer.parseInt(str); 307 } 308 309 316 void setInteger(Transaction xa, 317 byte []block, int rowOffset, int value) 318 throws SQLException 319 { 320 setString(xa, block, rowOffset, String.valueOf(value)); 321 } 322 323 330 public long getLong(byte []block, int rowOffset) 331 throws SQLException 332 { 333 String str = getString(block, rowOffset); 334 335 if (str == null) 336 return 0; 337 338 return Long.parseLong(str); 339 } 340 341 348 void setLong(Transaction xa, 349 byte []block, int rowOffset, long value) 350 throws SQLException 351 { 352 setString(xa, block, rowOffset, String.valueOf(value)); 353 } 354 355 362 public double getDouble(byte []block, int rowOffset) 363 throws SQLException 364 { 365 String str = getString(block, rowOffset); 366 367 if (str == null) 368 return 0; 369 370 return Double.parseDouble(str); 371 } 372 373 380 void setDouble(Transaction xa, 381 byte []block, int rowOffset, double value) 382 throws SQLException 383 { 384 setString(xa, block, rowOffset, String.valueOf(value)); 385 } 386 387 394 void setExpr(Transaction xa, 395 byte []block, int rowOffset, 396 Expr expr, QueryContext context) 397 throws SQLException 398 { 399 if (expr.isNull(context)) 400 setNull(block, rowOffset); 401 else 402 setString(xa, block, rowOffset, expr.evalString(context)); 403 } 404 405 411 public long getDate(byte []block, int rowOffset) 412 throws SQLException 413 { 414 throw new UnsupportedOperationException (); 415 } 416 417 424 void setDate(Transaction xa, byte []block, int rowOffset, double value) 425 throws SQLException 426 { 427 throw new UnsupportedOperationException (); 428 } 429 430 440 int evalToBuffer(byte []block, int rowOffset, 441 byte []buffer, int bufferOffset) 442 throws SQLException 443 { 444 throw new UnsupportedOperationException (getClass().getName()); 445 } 446 447 450 public boolean isEqual(byte []block, int rowOffset, 451 byte []buffer, int offset, int length) 452 { 453 return false; 454 } 455 456 459 public boolean isEqual(byte []buffer1, int rowOffset1, 460 byte []buffer2, int rowOffset2) 461 { 462 throw new UnsupportedOperationException (); 463 } 464 465 468 public boolean isEqual(byte []block, int rowOffset, String string) 469 { 470 return false; 471 } 472 473 476 public void evalToResult(byte []block, int rowOffset, SelectResult result) 477 throws SQLException 478 { 479 throw new UnsupportedOperationException (getClass().getName()); 480 } 481 482 485 public void set(Transaction xa, 486 TableIterator iter, Expr expr, QueryContext context) 487 throws SQLException 488 { 489 setString(xa, iter.getBuffer(), iter.getRowOffset(), 490 expr.evalString(context)); 491 492 iter.setDirty(); 493 } 494 495 502 void setIndex(Transaction xa, 503 byte []block, int rowOffset, 504 long rowAddr, QueryContext context) 505 throws SQLException 506 { 507 } 508 509 516 void delete(Transaction xa, byte []block, int rowOffset) 517 throws SQLException 518 { 519 } 520 521 public String toString() 522 { 523 if (getIndex() != null) 524 return getClass().getName() + "[" + _name + ",index]"; 525 else 526 return getClass().getName() + "[" + _name + "]"; 527 } 528 } 529 | Popular Tags |