1 package com.quadcap.sql.types; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.util.Calendar ; 47 48 import java.sql.Timestamp ; 49 50 import java.text.SimpleDateFormat ; 51 52 import com.quadcap.sql.io.Extern; 53 import com.quadcap.sql.io.Externable; 54 55 60 public class ValueTimestamp extends ValueDateTime 61 implements Externalizable , Externable { 62 int nanos = 0; 63 64 static SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 65 66 public ValueTimestamp() {} 67 68 public ValueTimestamp(Timestamp val) { 69 super(val.getTime()); 70 nanos = val.getNanos(); 71 } 72 73 public ValueTimestamp(long val) { 74 super(val); 75 nanos = (int)((val % 1000) * 1000); 76 } 77 78 public ValueTimestamp(String str) throws antlr.RecognitionException { 79 try { 80 synchronized (df) { 81 val = df.parse(str).getTime(); 82 int idx = str.indexOf('.'); 83 if (idx > 0) { 84 String frac = str.substring(idx+1); 85 long fi = Integer.parseInt(frac); 86 int digits = frac.length(); 87 if (digits < 9) { 88 fi *= (long)Math.pow(10, 9 - digits); 89 } else if (digits > 9) { 90 fi /= (long)Math.pow(10, digits - 9); 91 } 92 nanos = (int)fi; 93 } else { 94 nanos = 0; 95 } 96 } 97 98 } catch (Throwable e) { 99 throw new antlr.RecognitionException(e.toString()); 100 } 101 } 102 103 public String toString() { 104 synchronized (df) { 105 return df.format(new java.sql.Timestamp (val)); 106 } 107 } 108 109 public Value unop(int op) throws ValueException { 110 switch (op) { 111 case Op.NULL: 112 return ValueBoolean.falseBoolean; 113 default: 114 throw new ValueException("Unary op: " + Op.toString(op) + 115 " not implemented for this type"); 116 } 117 } 118 119 public Value binop(int op, Value l) throws ValueException { 120 return l.binop(op, this); 121 } 122 123 public Value binop(int op, ValueString r) throws ValueException { 124 try { 125 return ValueDateTime.binop(op, this, new ValueTimestamp(r.val)); 126 } catch (antlr.RecognitionException e) { 127 throw new ValueException("Invalid timestamp format: " + val); 128 } 129 } 130 131 public Value binop(int op, ValueInterval r) throws ValueException { 132 switch (op) { 133 case Op.PLUS: 134 if (r.ym) { 135 Calendar c = getCal(); 136 c.add(Calendar.MONTH, (int)r.val); 137 return new ValueTimestamp(c.getTime().getTime()); 138 } else { 139 return new ValueTimestamp(val + r.val); 140 } 141 case Op.MINUS: 142 if (r.ym) { 143 Calendar c = getCal(); 144 c.add(Calendar.MONTH, 0 - (int)r.val); 145 return new ValueTimestamp(c.getTime().getTime()); 146 } else { 147 return new ValueTimestamp(val - r.val); 148 } 149 default: 150 throw badBinop(op, r); 151 } 152 } 153 154 public void readExternal(ObjectInput in) 155 throws IOException , ClassNotFoundException 156 { 157 super.readExternal(in); 158 nanos = in.readInt(); 159 } 160 161 public void writeExternal(ObjectOutput out) 162 throws IOException 163 { 164 super.writeExternal(out); 165 out.writeInt(nanos); 166 } 167 168 public Object asJavaObject() { 169 Timestamp t = new Timestamp (val); 170 t.setNanos(nanos); 171 return t; 172 } 173 174 public void fromJavaObject(Object obj) throws ValueException { 175 if (obj instanceof Timestamp ) { 176 Timestamp t = (Timestamp )obj; 177 val = t.getTime(); 178 nanos = t.getNanos(); 179 } else if (obj instanceof java.util.Date ) { 180 val = ((java.util.Date )obj).getTime(); 181 nanos = 0; 182 } else { 183 throw new ValueException("bad type: " + obj); 184 } 185 } 186 187 public Type getType() { 188 return TypeTimestamp.typeTimestamp; 189 } 190 191 public Value convert(TypeTimestamp type) { 192 return this; 193 } 194 195 public Value convert(TypeDate type) { 196 return new ValueDate(val); 197 } 198 199 public Value convert(TypeTime type) { 200 return new ValueTime(val); 201 } 202 203 public void serializeKey(KeyStream out) throws IOException { 204 out.writeLength(2, KeyStream.COMPOUND); 205 out.writeLong(val); 206 out.writeInt(nanos); 207 } 208 209 static Extern extern = null; 210 public Extern getExtern() { return extern; } 211 public void setExtern(Extern extern) { ValueTimestamp.extern = extern; } 212 } 213 | Popular Tags |