1 29 30 package com.caucho.db.table; 31 32 import com.caucho.db.index.BTree; 33 import com.caucho.db.sql.Expr; 34 import com.caucho.db.sql.QueryContext; 35 import com.caucho.db.sql.SelectResult; 36 import com.caucho.db.store.Transaction; 37 import com.caucho.sql.SQLExceptionWrapper; 38 import com.caucho.util.QDate; 39 40 import java.sql.SQLException ; 41 42 45 class DateColumn extends Column { 46 private static QDate _gmtDate = new QDate(); 47 53 DateColumn(Row row, String name) 54 { 55 super(row, name); 56 } 57 58 61 public int getTypeCode() 62 { 63 return DATE; 64 } 65 66 69 public Class getJavaType() 70 { 71 return java.sql.Date .class; 72 } 73 74 77 public int getDeclarationSize() 78 { 79 return 8; 80 } 81 82 85 public int getLength() 86 { 87 return 8; 88 } 89 90 97 void setString(Transaction xa, byte []block, int rowOffset, String str) 98 throws SQLException 99 { 100 if (str == null) 101 setNull(block, rowOffset); 102 else { 103 long date = 0; 104 105 try { 106 synchronized (_gmtDate) { 107 date = _gmtDate.parseDate(str); 108 } 109 } catch (Exception e) { 110 throw new SQLExceptionWrapper(e); 111 } 112 113 setDate(xa, block, rowOffset, date); 114 } 115 } 116 117 123 public String getString(byte []block, int rowOffset) 124 { 125 if (isNull(block, rowOffset)) 126 return null; 127 else 128 return QDate.formatISO8601(getDate(block, rowOffset)); 129 } 130 131 138 void setDate(Transaction xa, byte []block, int rowOffset, long value) 139 { 140 int offset = rowOffset + _columnOffset; 141 142 block[offset++] = (byte) (value >> 56); 143 block[offset++] = (byte) (value >> 48); 144 block[offset++] = (byte) (value >> 40); 145 block[offset++] = (byte) (value >> 32); 146 block[offset++] = (byte) (value >> 24); 147 block[offset++] = (byte) (value >> 16); 148 block[offset++] = (byte) (value >> 8); 149 block[offset++] = (byte) (value); 150 151 setNonNull(block, rowOffset); 152 } 153 154 160 public long getDate(byte []block, int rowOffset) 161 { 162 if (isNull(block, rowOffset)) 163 return 0; 164 165 int offset = rowOffset + _columnOffset; 166 long value = 0; 167 168 value = (block[offset++] & 0xffL) << 56; 169 value |= (block[offset++] & 0xffL) << 48; 170 value |= (block[offset++] & 0xffL) << 40; 171 value |= (block[offset++] & 0xffL) << 32; 172 value |= (block[offset++] & 0xffL) << 24; 173 value |= (block[offset++] & 0xffL) << 16; 174 value |= (block[offset++] & 0xffL) << 8; 175 value |= (block[offset++] & 0xffL); 176 177 return value; 178 } 179 180 187 void setLong(Transaction xa, byte []block, int rowOffset, long value) 188 { 189 setDate(xa, block, rowOffset, value); 190 } 191 192 198 public long getLong(byte []block, int rowOffset) 199 { 200 return getDate(block, rowOffset); 201 } 202 203 210 void setInteger(Transaction xa, byte []block, int rowOffset, int value) 211 { 212 setDate(xa, block, rowOffset, value); 213 } 214 215 222 public int getInteger(byte []block, int rowOffset) 223 { 224 return (int) getDate(block, rowOffset); 225 } 226 227 230 public void evalToResult(byte []block, int rowOffset, SelectResult result) 231 { 232 if (isNull(block, rowOffset)) { 233 result.writeNull(); 234 return; 235 } 236 237 result.writeDate(getDate(block, rowOffset)); 238 } 239 240 250 int evalToBuffer(byte []block, int rowOffset, 251 byte []buffer, int bufferOffset) 252 throws SQLException 253 { 254 if (isNull(block, rowOffset)) 255 return 0; 256 257 int startOffset = rowOffset + _columnOffset; 258 int len = 8; 259 260 System.arraycopy(block, startOffset, buffer, bufferOffset, len); 261 262 return len; 263 } 264 265 272 void setExpr(Transaction xa, 273 byte []block, int rowOffset, 274 Expr expr, QueryContext context) 275 throws SQLException 276 { 277 if (expr.isNull(null)) 278 setNull(block, rowOffset); 279 else 280 setDate(xa, block, rowOffset, expr.evalDate(context)); 281 } 282 283 286 public boolean isEqual(byte []block1, int rowOffset1, 287 byte []block2, int rowOffset2) 288 { 289 if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2)) 291 return false; 292 293 int startOffset1 = rowOffset1 + _columnOffset; 294 int startOffset2 = rowOffset2 + _columnOffset; 295 296 return (block1[startOffset1 + 0] == block2[startOffset2 + 0] && 297 block1[startOffset1 + 1] == block2[startOffset2 + 1] && 298 block1[startOffset1 + 2] == block2[startOffset2 + 2] && 299 block1[startOffset1 + 3] == block2[startOffset2 + 3] && 300 block1[startOffset1 + 4] == block2[startOffset2 + 4] && 301 block1[startOffset1 + 5] == block2[startOffset2 + 5] && 302 block1[startOffset1 + 6] == block2[startOffset2 + 6] && 303 block1[startOffset1 + 7] == block2[startOffset2 + 7]); 304 } 305 306 313 void setIndex(Transaction xa, 314 byte []block, int rowOffset, 315 long rowAddr, QueryContext context) 316 throws SQLException 317 { 318 BTree index = getIndex(); 319 320 if (index == null) 321 return; 322 323 index.insert(block, rowOffset + _columnOffset, 8, rowAddr, xa, false); 324 } 325 326 329 public void set(TableIterator iter, Expr expr, QueryContext context) 330 throws SQLException 331 { 332 iter.setDirty(); 333 setDate(iter.getTransaction(), iter.getBuffer(), iter.getRowOffset(), 334 expr.evalDate(context)); 335 } 336 337 344 void delete(Transaction xa, byte []block, int rowOffset) 345 throws SQLException 346 { 347 BTree index = getIndex(); 348 349 if (index != null) 350 index.remove(block, rowOffset + _columnOffset, 8, xa); 351 } 352 } 353 | Popular Tags |