| 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.ResultSet ; 49 import java.sql.SQLException ; 50 import java.sql.Types ; 51 52 import com.quadcap.util.Debug; 53 import com.quadcap.util.Util; 54 55 60 public class TypeInterval implements Type, Externalizable { 61 public static final TypeInterval typeInterval = new TypeInterval(); 62 63 public static final int YEAR = 0; 64 public static final int MONTH = 1; 65 public static final int DAY = 2; 66 public static final int HOUR = 3; 67 public static final int MINUTE = 4; 68 public static final int SECOND = 5; 69 public static final int NANO = 6; 70 71 static final String [] fieldNames = { 72 "YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND", "NANO" 73 }; 74 75 static final long[] fieldNanos = { 76 365*24*3600*1000000000L, 30*24*3600*1000000000L, 24*3600*1000000000L, 3600*1000000000L, 60*1000000000L, 1000000000L, 1L }; 84 85 static final long[] fieldMon = { 86 12, 1, 0, 0, 0, 0, 0 87 }; 88 89 static final long[] pow10 = { 90 1L, 10L, 100L, 91 1000L, 10000L, 100000L, 92 1000000L, 10000000L, 100000000L, 93 1000000000L 94 }; 95 96 int start = -1; 97 int end = -1; 98 int startPrecision = 2; 99 int secPrecision = 6; 100 101 public static int convertCalendarField(int field) throws ValueException { 102 switch (field) { 103 case Calendar.YEAR: 104 return YEAR; 105 case Calendar.MONTH: 106 return MONTH; 107 case Calendar.DAY_OF_MONTH: 108 return DAY; 109 case Calendar.HOUR_OF_DAY: 110 return HOUR; 111 case Calendar.MINUTE: 112 return MINUTE; 113 case Calendar.SECOND: 114 return SECOND; 115 } 116 throw new ValueException("Bad time component: " + field); 117 } 118 119 public TypeInterval() { 120 } 121 122 public TypeInterval(int start, int startPrecision, int end, 123 int secPrecision) 124 throws antlr.RecognitionException 125 { 126 if (start == MONTH) { 127 throw new antlr.RecognitionException( 128 "first interval field can't be MONTH"); 129 } 130 this.start = start; 131 this.startPrecision = startPrecision; 132 this.end = end; 133 this.secPrecision = secPrecision; 134 } 135 136 public TypeInterval(int start, int startPrecision, 137 int secPrecision) { 138 this.start = start; 139 this.end = start; 140 this.startPrecision = startPrecision; 141 this.secPrecision = secPrecision; 142 } 143 144 int getStart() { return start; } 145 int getEnd() { return end; } 146 147 int getSecPrecision() { return secPrecision; } 148 149 public String fieldName(int type) { return fieldNames[type]; } 150 151 static final long getNanos(int type) { return fieldNanos[type]; } 152 static final long getMon(int type) { return fieldMon[type]; } 153 154 158 final long getMult() { 159 if (end == NANO) { 160 return 1000000000L / pow10[secPrecision]; 161 } else { 162 return getNanos(end); 163 } 164 } 165 166 172 long units(int type, boolean ym) { 173 if (ym) { 174 return getMon(type); 175 } else if (end == NANO && type == NANO) { 176 return 1; 177 } else { 178 return getNanos(type) / getMult(); 179 } 180 } 181 182 public String getTypeName() { 183 return toString(); 184 } 185 186 public int getJDBCType() { return Types.OTHER; } 187 188 public String getJDBCClassName() { return "java.lang.String"; } 189 190 public int getPrecision() { return 32; } 191 192 public int getScale() { return 0; } 193 194 public int getMaxPrecision() { return 32; } 195 196 public int getMinScale() { return -1; } 197 198 public int getMaxScale() { return -1; } 199 200 public boolean isCharType() { return false; } 201 202 public boolean isCaseSensitive() { return false; } 203 204 public boolean isCurrency() { return false; } 205 206 public boolean isSigned() { return true; } 207 208 public String toString() { 209 StringBuffer sb = new StringBuffer ("INTERVAL"); 210 if (start >= 0) { 211 sb.append(' '); 212 sb.append(fieldNames[start]); 213 if (start == SECOND) { 214 if (startPrecision != 2 || secPrecision != 6) { 215 sb.append('('); 216 sb.append(Integer.toString(startPrecision)); 217 sb.append(','); 218 sb.append(Integer.toString(secPrecision)); 219 sb.append(')'); 220 } 221 } else if (startPrecision != 2) { 222 sb.append('('); 223 sb.append(Integer.toString(startPrecision)); 224 sb.append(')'); 225 } 226 int fld = start; 227 if (end >= 0 && end != NANO && end != start) { 228 sb.append(" TO "); 229 sb.append(fieldNames[end]); 230 fld = end; 231 } 232 } 233 return sb.toString(); 239 } 240 241 public int getDisplayWidth() { return 10; } 242 243 public void readExternal(ObjectInput in) throws IOException { 244 start = in.readInt(); 245 end = in.readInt(); 246 startPrecision = in.readInt(); 247 secPrecision = in.readInt(); 248 } 249 250 public void writeExternal(ObjectOutput out) throws IOException { 251 out.writeInt(start); 252 out.writeInt(end); 253 out.writeInt(startPrecision); 254 out.writeInt(secPrecision); 255 } 256 257 public Value convert(Value v) throws ValueException { 258 return v.convert(this); 259 } 260 261 public String getCreateParams() { 262 return null; 263 } 264 } 265 | Popular Tags |