1 package com.quadcap.sql.types; 2 3 40 41 import java.io.ByteArrayInputStream ; 42 import java.io.ByteArrayOutputStream ; 43 import java.io.IOException ; 44 45 import java.util.Hashtable ; 46 47 import java.sql.SQLException ; 48 import java.sql.Types ; 49 50 import com.quadcap.sql.file.Datafile; 51 52 import com.quadcap.sql.io.ObjectInputStream; 53 import com.quadcap.sql.io.ObjectOutputStream; 54 55 import com.quadcap.util.Debug; 56 57 85 public abstract class Value { 86 public Value() {} 87 88 public static boolean isCaseSensitive = true; 89 90 107 abstract public Value binop(int op, Value l) throws ValueException; 108 109 abstract public Object asJavaObject() throws SQLException ; 110 111 abstract public Type getType() throws SQLException ; 112 113 abstract public void fromJavaObject(Object obj) throws ValueException; 114 115 abstract public void serializeKey(KeyStream out) throws IOException ; 116 117 public static String tn(Value v) { 118 String s = v.getClass().getName(); 119 int idx = s.lastIndexOf('.'); 120 if (idx > 0) s = s.substring(idx+1); 121 return s; 122 } 123 124 public static final Value binop(int op, Value l, Value r) 125 throws ValueException 126 { 127 Value ret = r.binop(op, l); 128 return ret; 136 } 137 138 final ValueException badType(Type type) { 139 return new ValueException("Not " + type + ": " + 140 this.getClass().getName()); 141 } 142 143 public Value convert(TypeBigInt type) throws ValueException { 145 throw badType(type); 146 } 147 148 public Value convert(TypeBinary type) throws ValueException { 149 throw badType(type); 150 } 151 152 public Value convert(TypeBlob type) throws ValueException { 153 throw badType(type); 154 } 155 156 public Value convert(TypeBoolean type) throws ValueException { 157 throw badType(type); 158 } 159 160 public Value convert(TypeChar type) throws ValueException { 161 String s = this.toString(); 162 final int max = type.getMax(); 163 if (max > 0 && s.length() > max) s = s.substring(0, max); 164 if (s.length() < max) { 165 StringBuffer sb = new StringBuffer (s); 166 while (sb.length() < max) sb.append(' '); 167 s = sb.toString(); 168 } 169 return new ValueString(s); 170 } 171 172 public Value convert(TypeClob type) throws ValueException { 173 throw badType(type); 174 } 175 176 public Value convert(TypeDate type) throws ValueException { 177 throw badType(type); 178 } 179 180 public Value convert(TypeDecimal type) throws ValueException { 181 throw badType(type); 182 } 183 184 public Value convert(TypeInt type) throws ValueException { 185 throw badType(type); 186 } 187 188 public Value convert(TypeInterval type) throws ValueException { 189 throw badType(type); 190 } 191 192 public Value convert(TypeReal type) throws ValueException { 193 throw badType(type); 194 } 195 196 public Value convert(TypeSmallInt type) throws ValueException { 197 throw badType(type); 198 } 199 200 public Value convert(TypeVarChar type) throws ValueException { 201 String s = this.toString(); 202 final int max = type.getMax(); 203 if (max > 0 && s.length() > max) s = s.substring(0, max); 204 return new ValueString(s); 205 } 206 207 public Value convert(TypeVarBinary type) throws ValueException { 208 throw badType(type); 209 } 210 211 public Value convert(TypeTime type) throws ValueException { 212 throw badType(type); 213 } 214 215 public Value convert(TypeTimestamp type) throws ValueException { 216 throw badType(type); 217 } 218 219 public Value convert(TypeTinyInt type) throws ValueException { 220 throw badType(type); 221 } 222 223 224 public Value binop(int op, ValueBlob v) throws ValueException { 225 throw badBinop(op, v); 226 } 227 228 public Value binop(int op, ValueBoolean v) throws ValueException { 229 throw badBinop(op, v); 230 } 231 232 public Value binop(int op, ValueByte v) throws ValueException { 233 throw badBinop(op, v); 234 } 235 236 public Value binop(int op, ValueClob v) throws ValueException { 237 throw badBinop(op, v); 238 } 239 240 public Value binop(int op, ValueDate v) throws ValueException { 241 throw badBinop(op, v); 242 } 243 244 public Value binop(int op, ValueDouble v) throws ValueException { 245 throw badBinop(op, v); 246 } 247 248 public Value binop(int op, ValueFloat v) throws ValueException { 249 throw badBinop(op, v); 250 } 251 252 public Value binop(int op, ValueInteger v) throws ValueException { 253 throw badBinop(op, v); 254 } 255 256 public Value binop(int op, ValueInterval v) throws ValueException { 257 throw badBinop(op, v); 258 } 259 260 public Value binop(int op, ValueLong v) throws ValueException { 261 throw badBinop(op, v); 262 } 263 264 public Value binop(int op, ValueNull v) throws ValueException { 265 throw badBinop(op, v); 266 } 267 268 public Value binop(int op, ValueOctets v) throws ValueException { 269 throw badBinop(op, v); 270 } 271 272 public Value binop(int op, ValuePattern v) throws ValueException { 273 throw badBinop(op, v); 274 } 275 276 public Value binop(int op, ValueScaledInteger v) throws ValueException { 277 throw badBinop(op, v); 278 } 279 280 public Value binop(int op, ValueShort v) throws ValueException { 281 throw badBinop(op, v); 282 } 283 284 public Value binop(int op, ValueString l) throws ValueException { 285 return ValueString.binop_convert(op, l, this); 286 } 287 288 public Value binop(int op, ValueTime v) throws ValueException { 289 throw badBinop(op, v); 290 } 291 292 public Value binop(int op, ValueTimestamp v) throws ValueException { 293 throw badBinop(op, v); 294 } 295 296 public Value binop(int op, ValueUnknown v) throws ValueException { 297 return ValueUnknown.valueUnknown; 298 } 299 300 public static final ValueException badBinop(int op, Value l, Value r) { 301 return new ValueException("not implemented: " + 302 l.getClass().getName() + " " + 303 Op.toString(op) + " " + 304 r.getClass().getName()); 305 } 306 307 public final ValueException badBinop(int op, Value v) { 308 return badBinop(op, this, v); 309 } 310 311 public static final boolean isTrue(Value ret) throws ValueException { 312 if (ret instanceof ValueBoolean) { 313 return ((ValueBoolean)ret).isTrue(); 314 } else if (ret instanceof ValueUnknown) { 315 return false; 316 } 317 throw new ValueException("Not boolean: " + ret); 318 } 319 320 public static final boolean boolOp(int op, Value l, Value r) 321 throws ValueException 322 { 323 Value ret = binop(op, l, r); 324 return isTrue(ret); 325 } 326 327 public Value unop(int op) throws ValueException { 328 throw new ValueException("not implemented: " + 329 getClass().getName() + " unary op: " + 330 Op.toString(op)); 331 } 332 333 public static final boolean isNull(Value val) { 334 return val instanceof ValueNull; 335 } 336 337 public static final byte[] bytes(Value val) { 338 try { 339 ObjectOutputStream os = new ObjectOutputStream(null); 340 os.writeObject(val); 341 return os.toByteArray(); 342 } catch (IOException e) { 343 Debug.print(e); 344 throw new RuntimeException (e.toString()); 345 } 346 } 347 348 public static final Value fromBytes(byte[] b) { 349 try { 350 ByteArrayInputStream bis = new ByteArrayInputStream (b); 351 ObjectInputStream is = new ObjectInputStream(bis); 352 return (Value)is.readObject(); 353 } catch (Exception e) { 354 Debug.print(e); 355 throw new RuntimeException (e.toString()); 356 } 357 } 358 359 static final Hashtable javaTypes = new Hashtable (); 360 static final void jc(String javaClass, String valueClass) { 361 try { 362 javaTypes.put(javaClass, Class.forName(valueClass)); 363 } catch (ClassNotFoundException e) { 364 Debug.print(e); 365 } 366 } 367 368 static { 369 jc("[B", "com.quadcap.sql.types.ValueOctets"); 370 jc("java.lang.Boolean", "com.quadcap.sql.types.ValueBoolean"); 371 jc("java.lang.Byte", "com.quadcap.sql.types.ValueByte"); 372 jc("java.lang.Double", "com.quadcap.sql.types.ValueDouble"); 373 jc("java.lang.Float", "com.quadcap.sql.types.ValueFloat"); 374 jc("java.lang.Integer", "com.quadcap.sql.types.ValueInteger"); 375 jc("java.lang.Long", "com.quadcap.sql.types.ValueLong"); 376 jc("java.lang.Short", "com.quadcap.sql.types.ValueShort"); 377 jc("java.lang.String", "com.quadcap.sql.types.ValueString"); 378 jc("java.math.BigDecimal", "com.quadcap.sql.types.ValueScaledInteger"); 379 jc("java.sql.Date", "com.quadcap.sql.types.ValueDate"); 380 jc("java.sql.Time", "com.quadcap.sql.types.ValueTime"); 381 jc("java.sql.Timestamp", "com.quadcap.sql.types.ValueTimestamp"); 382 jc("java.util.Date", "com.quadcap.sql.types.ValueTimestamp"); 383 } 384 385 static final Hashtable jdbcTypes = new Hashtable (); 386 static final void jdbcType(int typeNum, Type type) { 387 jdbcTypes.put(new Integer (typeNum), type); 388 } 389 static { 390 jdbcType(Types.BIT, TypeBoolean.typeBoolean); 391 jdbcType(Types.TINYINT, TypeTinyInt.typeTinyInt); 392 jdbcType(Types.SMALLINT, TypeSmallInt.typeSmallInt); 393 jdbcType(Types.INTEGER, TypeInt.typeInt); 394 jdbcType(Types.BIGINT, TypeBigInt.typeBigInt); 395 jdbcType(Types.FLOAT, TypeReal.typeFloat); 396 jdbcType(Types.REAL, TypeReal.typeReal); 397 jdbcType(Types.DOUBLE, TypeReal.typeDouble); 398 jdbcType(Types.NUMERIC, TypeDecimal.typeDecimal); 399 jdbcType(Types.DECIMAL, TypeDecimal.typeDecimal); 400 jdbcType(Types.CHAR, TypeChar.typeChar); 401 jdbcType(Types.VARCHAR, TypeVarChar.typeVarChar); 402 jdbcType(Types.LONGVARCHAR, TypeVarChar.typeVarChar); 403 jdbcType(Types.DATE, TypeDate.typeDate); 404 jdbcType(Types.TIME, TypeTime.typeTime); 405 jdbcType(Types.TIMESTAMP, TypeTimestamp.typeTimestamp); 406 jdbcType(Types.BINARY, TypeBinary.typeBinary); 407 jdbcType(Types.VARBINARY, TypeVarBinary.typeVarBinary); 408 jdbcType(Types.LONGVARBINARY, TypeVarBinary.typeVarBinary); 409 jdbcType(Types.BLOB, TypeBlob.typeBlob); 415 jdbcType(Types.CLOB, TypeClob.typeClob); 416 } 419 420 public static final Type typeForJdbcType(int type) { 421 Type t = (Type)jdbcTypes.get(new Integer (type)); 422 return t; 423 } 424 425 public void setDatafile(Datafile db) {} 426 427 public static final Value fromObject(Object obj) throws ValueException { 428 if (obj == null) return ValueNull.valueNull; 429 Class valueClass = (Class )javaTypes.get(obj.getClass().getName()); 430 if (valueClass != null) { 431 try { 432 Value v = (Value)(valueClass.newInstance()); 433 v.fromJavaObject(obj); 434 return v; 435 } catch (IllegalAccessException e) { 436 Debug.print(e); 437 throw new ValueException(e.toString()); 438 } catch (InstantiationException e) { 439 Debug.print(e); 440 throw new ValueException(e.toString()); 441 } 442 } 443 throw new ValueException("No mapping for class: " + 444 obj.getClass().getName()); 445 } 446 447 } 448 | Popular Tags |