1 package com.daffodilwoods.daffodildb.server.sql99.expression.datetimevalueexpression; 2 3 import com.daffodilwoods.database.resource.*; 4 import com.daffodilwoods.daffodildb.server.sql99.token.*; 5 import com.daffodilwoods.daffodildb.server.sql99.expression.stringvalueexpression.charactervalueexpression; 6 import com.daffodilwoods.daffodildb.server.sql99.common.TypeConstants; 7 import com.daffodilwoods.daffodildb.server.sql99.common.Datatypes; 8 import java.sql.Timestamp ; 9 import com.daffodilwoods.daffodildb.utils.TypeValidityHandler; 10 import com.daffodilwoods.daffodildb.utils.field.FieldTimeStampLiteral; 11 import com.daffodilwoods.daffodildb.server.sql99.common.StaticClass; 12 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession; 13 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference; 14 import com.daffodilwoods.daffodildb.utils.field.FieldBase; 15 import com.daffodilwoods.daffodildb.utils.FieldUtility; 16 import com.daffodilwoods.daffodildb.server.sql99.utils.ByteComparison; 17 import com.daffodilwoods.daffodildb.server.sql99.common.ColumnDetails; 18 import com.daffodilwoods.daffodildb.server.sql99.common.ParameterInfo; 19 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.AbstractRowValueExpression; 20 import java.text.SimpleDateFormat ; 21 import com.daffodilwoods.daffodildb.utils.DTimeStamp; 22 import java.text.*; 23 import com.daffodilwoods.daffodildb.server.sql99.expression.stringvalueexpression.*; 24 import java.text.DateFormat ; 25 import java.sql.Date ; 26 import com.daffodilwoods.database.resource.DException; 27 28 public class timestampfunction extends AbstractDateTimeValueFunction implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter ,datetimevaluefunction , TypeConstants, Datatypes{ 29 30 31 public Srightparen_1874859514 _Srightparen_18748595140 ; 32 public Scomma94843605characterstringliteral _OptScomma94843605characterstringliteral1 ; 33 public Scomma94843605characterstringliteral _OptScomma94843605characterstringliteral2 ; 34 public charactervalueexpression _charactervalueexpression3 ; 35 public Sleftparen653880241 _Sleftparen6538802414 ; 36 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439225 ; 37 38 39 public _Reference[] checkSemantic(_ServerSession parent) throws DException { 40 _Reference[] ref = super.checkSemantic(parent); 41 if(ref!=null) { 42 return ref; 43 } 44 int type = _charactervalueexpression3.getByteComparison(parent).getDataTypes()[0]; 45 switch (type) { 46 case -1: 47 case CHARACTER: 48 case VARCHAR: 49 case CHAR: 50 case CHARACTERVARYING: 51 return ref; 52 default: 53 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "TimestampFunction"}); 54 } 55 56 } 57 58 59 public Object run(Object object) throws com.daffodilwoods.database.resource.DException { 60 61 FieldBase result = (FieldBase) _charactervalueexpression3.run(object); 62 if (result.isNull()) { 63 return new FieldTimeStampLiteral(FieldUtility.NULLBUFFERRANGE); 64 } 65 FieldBase format_mask1 =null ,format_mask2 =null; 66 if (_OptScomma94843605characterstringliteral2 != null) { 67 format_mask1 = (FieldBase) _OptScomma94843605characterstringliteral2.run(object); 68 if (format_mask1 != null && !format_mask1.isNull()) { 69 int type1 = setAndGetDatatype(format_mask1); 70 } 71 } 72 73 if (_OptScomma94843605characterstringliteral1 != null) { 74 format_mask2 = (FieldBase) _OptScomma94843605characterstringliteral1.run(object); 75 if (format_mask2 != null && !format_mask2.isNull()) { 76 int type2 = setAndGetDatatype(format_mask2); 77 } 78 } 79 int type = setAndGetDatatype(result); 80 return getResult( type, result.getObject() ,(format_mask1 == null ? null :(String )format_mask1.getObject()),(format_mask2 == null ? null :(String )format_mask2.getObject())); 81 82 } 83 84 85 private int setAndGetDatatype(FieldBase fieldbase) throws DException{ 86 int type =fieldbase.getDatatype(); 87 if(type==-1) { 88 fieldbase.setDatatype( -1); 89 type = fieldbase.getDatatype(); 90 } 91 return type; 92 } 93 94 private Object getResult(int type, Object result, String format_mask1,String format_mask2) throws DException { 95 switch (type) { 96 case CHARACTER: 97 case VARCHAR: 98 case CHAR: 99 case CHARACTERVARYING: 100 String string = (String )result; 101 if(format_mask1 ==null){ 102 return new FieldTimeStampLiteral(getTimeStampWhenFormatIsNotSpecified(string,type)); 103 } 104 format_mask1 = format_mask1.replaceAll("Y","y"); 105 format_mask1 = format_mask1.replaceAll("MM","M"); 106 format_mask1 = format_mask1.replaceAll("DD","d"); 107 format_mask1 = format_mask1.replaceAll("HH24","H"); 108 format_mask1 = format_mask1.replaceAll("MI","m"); 109 format_mask1 = format_mask1.replaceAll("SS","s"); 110 SimpleDateFormat dateFormat = new SimpleDateFormat (format_mask1); 111 112 Timestamp timestamp = null; 113 try { 114 timestamp = getJDKCompatibleTimestamp(dateFormat.parse(string).getTime()); 115 } 116 catch (ParseException ex) { 117 throw new DException("DSE419",new Object [] {"TimestampFunction"}); 118 } 119 catch (DException ex) { 120 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "TimestampFunction"}); 121 } 122 return new FieldTimeStampLiteral(timestamp); 123 default: 124 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "TimestampFunction"}); 125 } 126 127 } 128 129 130 private Timestamp getJDKCompatibleTimestamp(long timestampWithNanoSeconds) throws DException { 131 Timestamp ts = new Timestamp (timestampWithNanoSeconds); 132 String javaVersion = System.getProperty("java.runtime.version").substring(0, 3); 133 try { 134 float version = Float.parseFloat(javaVersion); 135 if (version >= 1.4f) 136 return ts; 137 DTimeStamp dtimestamp = new DTimeStamp(ts.getTime() + ts.getNanos() / 1000000); 138 return dtimestamp; 139 } catch (NumberFormatException ex) { 140 return ts; 141 } 142 } 143 144 private Object getTimeStampWhenFormatIsNotSpecified(String string, int type) throws DException { 145 Timestamp timestamps; 146 try { 147 timestamps = TypeValidityHandler.getTimestamp(string); 148 } 149 catch (DException exd) { 150 SimpleDateFormat dateFormat = new SimpleDateFormat ("D-MMM-yyyy"); 151 try { 152 timestamps = getJDKCompatibleTimestamp(dateFormat.parse(string).getTime()); 153 } 154 catch (ParseException ex) { 155 throw new DException("DSE8022", null); 156 } 157 catch (DException ex) { 158 throw ex; 159 } 160 161 } 162 return timestamps; 163 } 164 165 216 217 private SimpleDateFormat dateTimeFormat(String format) throws DException { 218 String valid_Format = getValidDateFormat(format); 219 return new SimpleDateFormat (valid_Format); 220 } 221 222 private String getValidDateFormat(String format_mask) { 223 format_mask = format_mask.replaceAll("BC", "G"); 224 format_mask = format_mask.replaceAll("AC", "G"); 225 226 format_mask= format_mask.replaceAll("YYYY", "yyyy"); 227 format_mask = format_mask.replaceAll("IYYY", "yyyy"); 228 format_mask = format_mask.replaceAll("YY", "yy"); 229 format_mask = format_mask.replaceAll("YEAR", "yyyy"); 230 format_mask = format_mask.replaceAll("SYEAR", "yyyy"); 231 232 format_mask = format_mask.replaceAll("MM", "M"); 233 format_mask = format_mask.replaceAll("Mon","MMM"); 234 format_mask = format_mask.replaceAll("MON","MMM"); 235 format_mask = format_mask.replaceAll("FMonth","MMMM"); 236 format_mask = format_mask.replaceAll("FMMonth","MMMM"); 237 format_mask = format_mask.replaceAll("FMmonth","MMMM"); 238 format_mask = format_mask.replaceAll("Month", "MMMM"); 239 format_mask = format_mask.replaceAll("month", "MMMM"); 240 241 242 243 format_mask = format_mask.replaceAll("WW", "w"); 244 format_mask = format_mask.replaceAll("IW", "w"); 245 246 format_mask = format_mask.replaceAll("DDDth", "D"); 247 format_mask = format_mask.replaceAll("DDD", "D"); 248 format_mask = format_mask.replaceAll("DDth", "d"); 249 format_mask = format_mask.replaceAll("ddth", "d"); 250 format_mask = format_mask.replaceAll("DD", "d"); 251 format_mask = format_mask.replaceAll("DAY", "E"); 252 format_mask = format_mask.replaceAll("FMDAY", "E"); 253 254 255 format_mask = format_mask.replaceAll("HH", "h"); 256 format_mask = format_mask.replaceAll("HH12", "h"); 257 format_mask = format_mask.replaceAll("HH24", "H"); 258 format_mask = format_mask.replaceAll("MI", "m"); 259 format_mask = format_mask.replaceAll("SS", "s"); 260 format_mask = format_mask.replaceAll("ss", "s"); 261 format_mask = format_mask.replaceAll("ssss", "s"); 262 263 264 format_mask = format_mask.replaceAll("AM", "a"); 265 format_mask = format_mask.replaceAll("PM", "a"); 266 format_mask = format_mask.replaceAll("A.M.", "a"); 267 format_mask = format_mask.replaceAll("P.M.", "a"); 268 return format_mask; 269 } 270 271 272 public ParameterInfo[] getParameterInfo() throws DException { 273 311 return new ParameterInfo[0]; 312 } 313 314 protected String getNameOfColumn(ColumnDetails[] columnDetails) throws DException { 315 String nameOfColumn = ""; 316 for (int i = 0; i < columnDetails.length; ++i) { 317 nameOfColumn += columnDetails[i].getColumn(); 318 } 319 return nameOfColumn; 320 } 321 322 protected ParameterInfo[] getThisParameterInfo() throws DException { 323 ParameterInfo parameterInfo = new ParameterInfo(); 324 parameterInfo.setName(toString()); 325 parameterInfo.setDataType(TIMESTAMP); 326 return new ParameterInfo[] {parameterInfo}; 327 } 328 329 330 public ByteComparison getByteComparison(Object object) throws DException { 331 ByteComparison byteComparison = new ByteComparison(false, new int[] {TIMESTAMP}); 332 byteComparison.setSize(getColumnSize(object)); 333 return byteComparison; 334 } 335 336 337 public String getType() throws DException { 338 return (String ) _SRESERVEDWORD12065439225.run(null); 339 } 340 341 public int getFunctionType() { 342 return TIMESTAMPFUNCTION; 343 } 344 345 346 347 public AbstractRowValueExpression[] getChilds() { 348 if (_OptScomma94843605characterstringliteral2 == null) { 349 if (_OptScomma94843605characterstringliteral1 == null) { 350 return new AbstractRowValueExpression[] { 351 (AbstractRowValueExpression) (_charactervalueexpression3)}; 352 } 353 else { 354 return new AbstractRowValueExpression[] { 355 (AbstractRowValueExpression) (_charactervalueexpression3), 356 (AbstractRowValueExpression) 357 _OptScomma94843605characterstringliteral1 358 }; 359 360 } 361 } 362 else { 363 if (_OptScomma94843605characterstringliteral1 == null) { 364 return new AbstractRowValueExpression[] { 365 (AbstractRowValueExpression) (_charactervalueexpression3), 366 (AbstractRowValueExpression) _OptScomma94843605characterstringliteral2 367 }; 368 } 369 else { 370 return new AbstractRowValueExpression[] { 371 (AbstractRowValueExpression) (_charactervalueexpression3), 372 (AbstractRowValueExpression) 373 _OptScomma94843605characterstringliteral2, 374 (AbstractRowValueExpression) 375 _OptScomma94843605characterstringliteral1 376 }; 377 378 } 379 } 380 } 381 382 public ColumnDetails[] getChildColumnDetails() throws DException { 383 return columnDetails; 384 } 385 386 387 public String toString() { 388 StringBuffer sb = new StringBuffer (); 389 sb.append(" "); 390 sb.append(_SRESERVEDWORD12065439225); 391 sb.append(" "); 392 sb.append(_Sleftparen6538802414); 393 sb.append(" "); 394 sb.append(_charactervalueexpression3); 395 sb.append(" "); 396 if(_OptScomma94843605characterstringliteral2 != null ) { 397 sb.append(_OptScomma94843605characterstringliteral2); 398 } 399 sb.append(" "); 400 if(_OptScomma94843605characterstringliteral1 != null ) { 401 sb.append(_OptScomma94843605characterstringliteral1); 402 } 403 sb.append(" "); 404 sb.append(_Srightparen_18748595140); 405 return sb.toString(); 406 } 407 408 public Object clone() throws CloneNotSupportedException { 409 timestampfunction tempClass = new timestampfunction(); 410 tempClass._Srightparen_18748595140 = (Srightparen_1874859514) _Srightparen_18748595140.clone(); 411 tempClass._OptScomma94843605characterstringliteral1 = (Scomma94843605characterstringliteral) _OptScomma94843605characterstringliteral1.clone(); 412 tempClass._OptScomma94843605characterstringliteral2 = (Scomma94843605characterstringliteral) _OptScomma94843605characterstringliteral2.clone(); 413 tempClass._charactervalueexpression3 = (charactervalueexpression) _charactervalueexpression3.clone(); 414 tempClass._Sleftparen6538802414 = (Sleftparen653880241) _Sleftparen6538802414.clone(); 415 tempClass._SRESERVEDWORD12065439225 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439225.clone(); 416 return tempClass; 417 } 418 419 420 421 422 public int getColumnSize( Object object) throws DException { 423 return Datatypes.TIMESTAMPSIZE; 424 } 425 426 427 } 428 | Popular Tags |