1 28 29 package com.caucho.db.table; 30 31 import com.caucho.db.index.BTree; 32 import com.caucho.db.index.IntKeyCompare; 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 39 import java.sql.SQLException ; 40 41 44 class IntColumn extends Column { 45 51 IntColumn(Row row, String name) 52 { 53 super(row, name); 54 } 55 56 59 public int getTypeCode() 60 { 61 return INT; 62 } 63 64 67 public Class getJavaType() 68 { 69 return int.class; 70 } 71 72 75 public int getDeclarationSize() 76 { 77 return 4; 78 } 79 80 83 public int getLength() 84 { 85 return 4; 86 } 87 88 91 public KeyCompare getIndexKeyCompare() 92 { 93 return new IntKeyCompare(); 94 } 95 96 102 public String getString(byte []block, int rowOffset) 103 { 104 if (isNull(block, rowOffset)) 105 return null; 106 else 107 return String.valueOf(getInteger(block, rowOffset)); 108 } 109 110 117 void setString(Transaction xa, byte []block, int rowOffset, String str) 118 { 119 if (str == null) 120 setNull(block, rowOffset); 121 else 122 setInteger(xa, block, rowOffset, (int) Long.parseLong(str)); 123 } 124 125 131 public int getInteger(byte []block, int rowOffset) 132 { 133 if (isNull(block, rowOffset)) 134 return 0; 135 136 int offset = rowOffset + _columnOffset; 137 int value = 0; 138 139 value = (block[offset++] & 0xff) << 24; 140 value |= (block[offset++] & 0xff) << 16; 141 value |= (block[offset++] & 0xff) << 8; 142 value |= (block[offset++] & 0xff); 143 144 return value; 145 } 146 147 154 void setInteger(Transaction xa, byte []block, int rowOffset, int value) 155 { 156 int offset = rowOffset + _columnOffset; 157 158 block[offset++] = (byte) (value >> 24); 159 block[offset++] = (byte) (value >> 16); 160 block[offset++] = (byte) (value >> 8); 161 block[offset++] = (byte) (value); 162 163 setNonNull(block, rowOffset); 164 } 165 166 173 void setLong(Transaction xa, byte []block, int rowOffset, long value) 174 { 175 setInteger(xa, block, rowOffset, (int) value); 176 } 177 178 184 public long getLong(byte []block, int rowOffset) 185 { 186 return getInteger(block, rowOffset); 187 } 188 189 196 void setExpr(Transaction xa, 197 byte []block, int rowOffset, 198 Expr expr, QueryContext context) 199 throws SQLException 200 { 201 if (expr.isNull(null)) 202 setNull(block, rowOffset); 203 else 204 setInteger(xa, block, rowOffset, (int) expr.evalLong(context)); 205 } 206 207 210 public void evalToResult(byte []block, int rowOffset, SelectResult result) 211 { 212 if (isNull(block, rowOffset)) { 213 result.writeNull(); 214 return; 215 } 216 217 int startOffset = rowOffset + _columnOffset; 218 219 result.write(Column.INT); 220 result.write(block, startOffset, 4); 221 } 222 223 233 int evalToBuffer(byte []block, int rowOffset, 234 byte []buffer, int bufferOffset) 235 throws SQLException 236 { 237 if (isNull(block, rowOffset)) 238 return 0; 239 240 int startOffset = rowOffset + _columnOffset; 241 int len = 4; 242 243 System.arraycopy(block, startOffset, buffer, bufferOffset, len); 244 245 return len; 246 } 247 248 251 public boolean isEqual(byte []block1, int rowOffset1, 252 byte []block2, int rowOffset2) 253 { 254 if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2)) 255 return false; 256 257 int startOffset1 = rowOffset1 + _columnOffset; 258 int startOffset2 = rowOffset2 + _columnOffset; 259 260 return (block1[startOffset1 + 0] == block2[startOffset2 + 0] && 261 block1[startOffset1 + 1] == block2[startOffset2 + 1] && 262 block1[startOffset1 + 2] == block2[startOffset2 + 2] && 263 block1[startOffset1 + 3] == block2[startOffset2 + 3]); 264 } 265 266 273 void setIndex(Transaction xa, 274 byte []block, int rowOffset, 275 long rowAddr, QueryContext context) 276 throws SQLException 277 { 278 BTree index = getIndex(); 279 280 if (index == null) 281 return; 282 283 index.insert(block, rowOffset + _columnOffset, 4, rowAddr, xa, false); 284 } 285 286 289 public void set(TableIterator iter, Expr expr, QueryContext context) 290 throws SQLException 291 { 292 iter.setDirty(); 293 setInteger(iter.getTransaction(), 294 iter.getBuffer(), iter.getRowOffset(), 295 (int) expr.evalLong(context)); 296 } 297 298 305 void delete(Transaction xa, byte []block, int rowOffset) 306 throws SQLException 307 { 308 BTree index = getIndex(); 309 310 if (index != null) 311 index.remove(block, rowOffset + _columnOffset, 4, xa); 312 } 313 } 314 | Popular Tags |