1 5 package org.h2.value; 6 7 import java.math.BigDecimal ; 8 import java.sql.PreparedStatement ; 9 import java.sql.SQLException ; 10 import java.sql.Timestamp ; 11 12 import org.h2.message.Message; 13 import org.h2.util.DateTimeUtils; 14 import org.h2.util.MathUtils; 15 16 public class ValueTimestamp extends Value { 17 public static final int PRECISION = 23; 18 public static final int DEFAULT_SCALE = 10; 19 private Timestamp value; 20 21 private ValueTimestamp(Timestamp value) { 22 this.value = value; 23 } 24 25 public Timestamp getTimestamp() { 26 return (Timestamp )value.clone(); 27 } 28 29 public Timestamp getTimestampNoCopy() { 30 return value; 31 } 32 33 public String getSQL() { 34 return "TIMESTAMP '" + getString() + "'"; 35 } 36 37 public static Timestamp parseTimestamp(String s) throws SQLException { 38 return (Timestamp ) DateTimeUtils.parseDateTime(s, Value.TIMESTAMP, Message.TIMESTAMP_CONSTANT_1); 39 } 40 41 public int getType() { 42 return Value.TIMESTAMP; 43 } 44 45 protected int compareSecure(Value o, CompareMode mode) { 46 ValueTimestamp v = (ValueTimestamp) o; 47 int c = value.compareTo(v.value); 48 return c == 0 ? 0 : (c < 0 ? -1 : 1); 49 } 50 51 public String getString() { 52 return value.toString(); 53 } 54 55 public long getPrecision() { 56 return PRECISION; 57 } 58 59 public int getScale() { 60 return DEFAULT_SCALE; 61 } 62 63 public int hashCode() { 64 return value.hashCode(); 65 } 66 67 public Object getObject() { 68 return getTimestamp(); 70 } 71 72 public void set(PreparedStatement prep, int parameterIndex) throws SQLException { 73 prep.setTimestamp(parameterIndex, value); 74 } 75 76 public static ValueTimestamp get(Timestamp timestamp) { 77 timestamp = (Timestamp ) timestamp.clone(); 78 return getNoCopy(timestamp); 79 } 80 81 public static ValueTimestamp getNoCopy(Timestamp timestamp) { 82 return (ValueTimestamp) Value.cache(new ValueTimestamp(timestamp)); 83 } 84 85 public Value convertScale(boolean onlyToSmallerScale, int targetScale) throws SQLException { 86 if (targetScale < 0 || targetScale > DEFAULT_SCALE) { 87 throw Message.getInvalidValueException(""+targetScale, "scale"); 89 } 90 int nanos = value.getNanos(); 91 BigDecimal bd = new BigDecimal ("" + nanos); 92 bd = bd.movePointLeft(9); 93 bd = MathUtils.setScale(bd, targetScale); 94 bd = bd.movePointRight(9); 95 int n2 = bd.intValue(); 96 if (n2 == nanos) { 97 return this; 98 } 99 long t = value.getTime(); 100 while (n2 >= 1000000000) { 101 t += 1000; 102 n2 -= 1000000000; 103 } 104 Timestamp t2 = new Timestamp (t); 105 t2.setNanos(n2); 106 return ValueTimestamp.getNoCopy(t2); 107 } 108 109 113 public int getDisplaySize() { 114 return "2001-01-01 23:59:59".length(); 115 } 116 117 protected boolean isEqual(Value v) { 118 return v instanceof ValueTimestamp && value.equals(((ValueTimestamp)v).value); 119 } 120 121 } 122 | Popular Tags |