1 28 29 package com.caucho.db.table; 30 31 import com.caucho.db.index.BTree; 32 import com.caucho.db.sql.Expr; 33 import com.caucho.db.sql.QueryContext; 34 import com.caucho.db.sql.SelectResult; 35 import com.caucho.db.store.Transaction; 36 37 import java.sql.SQLException ; 38 39 42 class DoubleColumn extends Column { 43 49 DoubleColumn(Row row, String name) 50 { 51 super(row, name); 52 } 53 54 57 public int getTypeCode() 58 { 59 return DOUBLE; 60 } 61 62 65 public Class getJavaType() 66 { 67 return double.class; 68 } 69 70 73 public int getDeclarationSize() 74 { 75 return 8; 76 } 77 78 81 public int getLength() 82 { 83 return 8; 84 } 85 86 93 void setString(Transaction xa, byte []block, int rowOffset, String str) 94 { 95 if (str == null) 96 setNull(block, rowOffset); 97 else 98 setDouble(xa, block, rowOffset, Double.parseDouble(str)); 99 } 100 101 107 public String getString(byte []block, int rowOffset) 108 { 109 if (isNull(block, rowOffset)) 110 return null; 111 else 112 return String.valueOf(getDouble(block, rowOffset)); 113 } 114 115 122 void setInteger(Transaction xa, byte []block, int rowOffset, int value) 123 { 124 setDouble(xa, block, rowOffset, value); 125 } 126 127 133 public int getInteger(byte []block, int rowOffset) 134 { 135 return (int) getDouble(block, rowOffset); 136 } 137 138 145 void setLong(Transaction xa, byte []block, int rowOffset, long value) 146 { 147 setDouble(xa, block, rowOffset, value); 148 } 149 150 156 public long getLong(byte []block, int rowOffset) 157 { 158 return (long) getDouble(block, rowOffset); 159 } 160 161 168 void setDouble(Transaction xa, 169 byte []block, int rowOffset, double value) 170 { 171 int offset = rowOffset + _columnOffset; 172 173 long longValue = Double.doubleToRawLongBits(value); 174 175 block[offset++] = (byte) (longValue >> 56); 176 block[offset++] = (byte) (longValue >> 48); 177 block[offset++] = (byte) (longValue >> 40); 178 block[offset++] = (byte) (longValue >> 32); 179 180 block[offset++] = (byte) (longValue >> 24); 181 block[offset++] = (byte) (longValue >> 16); 182 block[offset++] = (byte) (longValue >> 8); 183 block[offset++] = (byte) (longValue); 184 185 setNonNull(block, rowOffset); 186 } 187 188 194 public double getDouble(byte []block, int rowOffset) 195 { 196 if (isNull(block, rowOffset)) 197 return 0; 198 199 int offset = rowOffset + _columnOffset; 200 long longValue = 0; 201 202 longValue = (block[offset++] & 0xffL) << 56; 203 longValue |= (block[offset++] & 0xffL) << 48; 204 longValue |= (block[offset++] & 0xffL) << 40; 205 longValue |= (block[offset++] & 0xffL) << 32; 206 207 longValue |= (block[offset++] & 0xffL) << 24; 208 longValue |= (block[offset++] & 0xffL) << 16; 209 longValue |= (block[offset++] & 0xffL) << 8; 210 longValue |= (block[offset++] & 0xffL); 211 212 return Double.longBitsToDouble(longValue); 213 } 214 215 222 void setExpr(Transaction xa, byte []block, int rowOffset, 223 Expr expr, QueryContext context) 224 throws SQLException 225 { 226 if (expr.isNull(null)) 227 setNull(block, rowOffset); 228 else 229 setDouble(xa, block, rowOffset, expr.evalDouble(context)); 230 } 231 232 235 public void evalToResult(byte []block, int rowOffset, SelectResult result) 236 { 237 if (isNull(block, rowOffset)) { 238 result.writeNull(); 239 return; 240 } 241 242 result.writeDouble(getDouble(block, rowOffset)); 243 } 244 245 255 int evalToBuffer(byte []block, int rowOffset, 256 byte []buffer, int bufferOffset) 257 throws SQLException 258 { 259 if (isNull(block, rowOffset)) 260 return 0; 261 262 int startOffset = rowOffset + _columnOffset; 263 int len = 8; 264 265 System.arraycopy(block, startOffset, buffer, bufferOffset, len); 266 267 return len; 268 } 269 270 273 public boolean isEqual(byte []block1, int rowOffset1, 274 byte []block2, int rowOffset2) 275 { 276 if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2)) 277 return false; 278 279 int startOffset1 = rowOffset1 + _columnOffset; 280 int startOffset2 = rowOffset2 + _columnOffset; 281 282 return (block1[startOffset1 + 0] == block2[startOffset2 + 0] && 283 block1[startOffset1 + 1] == block2[startOffset2 + 1] && 284 block1[startOffset1 + 2] == block2[startOffset2 + 2] && 285 block1[startOffset1 + 3] == block2[startOffset2 + 3] && 286 block1[startOffset1 + 4] == block2[startOffset2 + 4] && 287 block1[startOffset1 + 5] == block2[startOffset2 + 5] && 288 block1[startOffset1 + 6] == block2[startOffset2 + 6] && 289 block1[startOffset1 + 7] == block2[startOffset2 + 7]); 290 } 291 292 295 public void set(TableIterator iter, Expr expr, QueryContext context) 296 throws SQLException 297 { 298 iter.setDirty(); 299 setDouble(iter.getTransaction(), 300 iter.getBuffer(), iter.getRowOffset(), 301 expr.evalDouble(context)); 302 } 303 304 311 void delete(Transaction xa, byte []block, int rowOffset) 312 throws SQLException 313 { 314 BTree index = getIndex(); 315 316 if (index != null) 317 index.remove(block, rowOffset + _columnOffset, 8, xa); 318 } 319 } 320 | Popular Tags |