1 package com.daffodilwoods.daffodildb.server.sql99.expression.datetimevalueexpression; 2 3 import java.sql.*; 4 import java.util.*; 5 6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 7 import com.daffodilwoods.daffodildb.server.serversystem.*; 8 import com.daffodilwoods.daffodildb.server.sql99.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*; 10 import com.daffodilwoods.daffodildb.server.sql99.token.*; 11 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 12 import com.daffodilwoods.daffodildb.utils.*; 13 import com.daffodilwoods.daffodildb.utils.field.*; 14 import com.daffodilwoods.database.resource.*; 15 import com.daffodilwoods.database.utility.P; 16 import com.daffodilwoods.daffodildb.server.sql99.expression.numericvalueexpression.numericvaluefunction; 17 import com.daffodilwoods.daffodildb.server.sql99.expression.numericvalueexpression.AbstractNumericValueFunction; 18 19 public class timestampdifffunction 20 extends AbstractNumericValueFunction 21 implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter, 22 numericvaluefunction , TypeConstants, Datatypes, 23 TimeStampDenometersInterface { 24 25 public Srightparen_1874859514 _Srightparen_18748595140; 26 public datetimevalueexpression _datetimevalueexpression1; 27 public Scomma94843605 _Scomma948436052; 28 public datetimevalueexpression _datetimevalueexpression3; 29 public Scomma94843605 _Scomma948436054; 30 public timestampdenometers _timestampdenometers5; 31 public Sleftparen653880241 _Sleftparen6538802416; 32 public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442557; 33 34 35 public Object run(Object object) throws com.daffodilwoods.database.resource. 36 DException { 37 FieldBase result1 = (FieldBase) _datetimevalueexpression3.run(object); FieldBase result2 = (FieldBase) _datetimevalueexpression1.run(object); if (result1.isNull() || result2.isNull()) { 40 return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, INTEGER); 41 } 42 int type1 = result1.getDatatype(); 43 int type2 = result2.getDatatype(); 44 if (type1 == -1) { 45 result1.setDatatype( -1); 46 type1 = result1.getDatatype(); 47 } 48 if (type2 == -1) { 49 result2.setDatatype( -1); 50 type2 = result2.getDatatype(); 51 } 52 Object result3 = _timestampdenometers5.run(object); int type3 = getType(result3); 54 return getResult(type1, type2, result1.getObject(), result2.getObject(), 55 type3); 56 } 57 58 public ParameterInfo[] getParameterInfo() throws DException { 59 ArrayList list =new ArrayList(); 60 ParameterInfo[] parameterInfo2 = _datetimevalueexpression3.getParameterInfo(); 61 for (int i = 0; i <parameterInfo2.length; i++) { 62 if (parameterInfo2[i].getQuestionMark()) { 63 parameterInfo2[i].setDataType(Datatypes.TIMESTAMP); 64 parameterInfo2[i].setName("TIMESTAMPDIFF Arg1"); 65 list.addAll(Arrays.asList(parameterInfo2)); 66 } 67 } 68 ParameterInfo[] parameterInfo3 = _datetimevalueexpression1.getParameterInfo(); 69 for (int i = 0; i < parameterInfo3.length; i++) { 70 if (parameterInfo3[i].getQuestionMark()) { 71 parameterInfo3[i].setDataType(Datatypes.TIMESTAMP); 72 parameterInfo3[i].setName("TIMESTAMPDIFF Arg2"); 73 list.addAll(Arrays.asList(parameterInfo3)); 74 } 75 } 76 ParameterInfo[] param4 = new ParameterInfo[list.size()]; 77 list.toArray(param4); 78 for (int i = 0; i < param4.length; i++) { 79 if(param4[i].getQuestionMark()) { 80 return param4; 81 } 82 83 } 84 return getThisParameterInfo(); 85 } 86 87 protected ParameterInfo[] getThisParameterInfo() throws DException { 88 ParameterInfo parameterInfo = new ParameterInfo(); 89 parameterInfo.setName(toString()); 90 parameterInfo.setDataType(INTEGER); 91 return new ParameterInfo[] { 92 parameterInfo}; 93 } 94 95 96 protected String getNameOfColumn(ColumnDetails[] columnDetails) throws 97 DException { 98 String nameOfColumn = ""; 99 for (int i = 0; i < columnDetails.length; ++i) { 100 nameOfColumn += columnDetails[i].getColumn(); 101 } 102 return nameOfColumn; 103 } 104 105 129 130 public ColumnDetails[] getChildColumnDetails() throws DException { 131 return columnDetails; 132 } 133 134 private Object getResult(int type1, int type2, Object result1, Object result2, 135 int field) throws DException { 136 switch (type1) { 137 case CHARACTER: 138 case VARCHAR: 139 case CHAR: 140 case CHARACTERVARYING: 141 String string = (String ) result1; 142 Timestamp timestamp = null; 143 try { 144 timestamp = TypeValidityHandler.getTimestamp(string); 145 } 146 catch (DException ex) { 147 throw new DException("DSE419", new Object [] {"TIMESTAMPDIFF"}); 148 } 149 switch (type2) { 150 case CHARACTER: 151 case VARCHAR: 152 case CHAR: 153 case CHARACTERVARYING: 154 String string1 = (String ) result2; 155 Timestamp timestamp1 = null; 156 try { 157 timestamp1 = TypeValidityHandler.getTimestamp(string1); 158 } 159 catch (DException ex1) { 160 throw new DException("DSE419", new Object [] {"TIMESTAMPDIFF"}); 161 } 162 return new FieldLiteral( 163 new Integer (timeStampDiff(field, (Timestamp) timestamp, 164 (Timestamp) timestamp1)), 165 Datatype.INTEGER); 166 case TIMESTAMP: 167 return new FieldLiteral( 168 new Integer (timeStampDiff(field, (Timestamp) timestamp, 169 (Timestamp) result2)), 170 Datatype.INTEGER); 171 default: 172 throw new DException("DSE419", new Object [] {"TIMESTAMPDIFF"}); 173 } 174 case TIMESTAMP: 175 switch (type2) { 176 case CHARACTER: 177 case VARCHAR: 178 case CHAR: 179 case CHARACTERVARYING: 180 String string1 = (String ) result2; 181 Timestamp timestamp1 = null; 182 try { 183 timestamp1 = TypeValidityHandler.getTimestamp(string1); 184 } 185 catch (DException ex1) { 186 throw new DException("DSE419", new Object [] {"TIMESTAMPDIFF"}); 187 } 188 return new FieldLiteral( 189 new Integer (timeStampDiff(field, (Timestamp) result1, 190 (Timestamp) timestamp1)), 191 Datatype.INTEGER); 192 case TIMESTAMP: 193 return new FieldLiteral( 194 new Integer (timeStampDiff(field, (Timestamp) result1, 195 (Timestamp) result2)), 196 Datatype.INTEGER); 197 default: 198 throw new DException("DSE419", new Object [] {"TIMESTAMPDIFF"}); 199 } 200 default: 201 throw new DException("DSE4108", 202 new Object [] {StaticClass.getDataTypeName(type2), 203 "timestampdifffunction"}); 204 205 } 206 } 207 208 private int getType(Object result3) throws DException { 209 String fieldName = (String ) result3; 210 if (fieldName.equalsIgnoreCase("SQL_TSI_FRAC_SECOND")) { 211 return SQL_TSI_FRAC_SECOND; 212 } 213 if (fieldName.equalsIgnoreCase("SQL_TSI_SECOND")) { 214 return SQL_TSI_SECOND; 215 } 216 if (fieldName.equalsIgnoreCase("SQL_TSI_MINUTE")) { 217 return SQL_TSI_MINUTE; 218 } 219 if (fieldName.equalsIgnoreCase("SQL_TSI_HOUR")) { 220 return SQL_TSI_HOUR; 221 } 222 if (fieldName.equalsIgnoreCase("SQL_TSI_DAY")) { 223 return SQL_TSI_DAY; 224 } 225 if (fieldName.equalsIgnoreCase("SQL_TSI_WEEK")) { 226 return SQL_TSI_WEEK; 227 } 228 if (fieldName.equalsIgnoreCase("SQL_TSI_MONTH")) { 229 return SQL_TSI_MONTH; 230 } 231 if (fieldName.equalsIgnoreCase("SQL_TSI_YEAR")) { 232 return SQL_TSI_YEAR; 233 } 234 if (fieldName.equalsIgnoreCase("SQL_TSI_QUARTER")) { 235 return SQL_TSI_QUARTER; 236 } 237 throw new DException("DSE413", new Object [] {fieldName}); 238 } 239 240 public int timeStampDiff(int interval, Timestamp timestampFrom, 241 Timestamp timestampTo) throws DException { 242 switch (interval) { 243 case SQL_TSI_FRAC_SECOND: 244 return getDifferenceInFracSeconds(timestampFrom, timestampTo); 245 case SQL_TSI_SECOND: 246 return getDifferenceInSeconds(timestampFrom, timestampTo); 247 case SQL_TSI_MINUTE: 248 return getDifferenceInMinutes(timestampFrom, timestampTo); 249 case SQL_TSI_HOUR: 250 return getDifferenceInHours(timestampFrom, timestampTo); 251 case SQL_TSI_DAY: 252 return getDifferenceInDays(timestampFrom, timestampTo); 253 case SQL_TSI_WEEK: 254 return getDifferenceInWeeks(timestampFrom, timestampTo); 255 case SQL_TSI_MONTH: 256 return getDifferenceInMonths(timestampFrom, timestampTo); 257 case SQL_TSI_QUARTER: 258 return getDifferenceInQuarters(timestampFrom, timestampTo); 259 case SQL_TSI_YEAR: 260 return getDifferenceInYears(timestampFrom, timestampTo); 261 default: 262 throw new DException("DSE7063", new Object [] {new Integer (interval)}); 263 } 264 } 265 266 private int getDifferenceInFracSeconds(Timestamp timestampFrom, 267 Timestamp timestampTo) throws 268 DException { 269 return (int) (timestampTo.getNanos() - timestampFrom.getNanos()); 270 } 271 272 private int getDifferenceInSeconds(Timestamp timestampFrom, 273 Timestamp timestampTo) throws DException { 274 return (int) ( (timestampTo.getTime() - timestampFrom.getTime()) / 275 NO_OF_MILLIS_IN_SECOND); 276 } 277 278 private int getDifferenceInMinutes(Timestamp timestampFrom, 279 Timestamp timestampTo) throws DException { 280 return getDifferenceInSeconds(timestampFrom, timestampTo) / 281 NO_OF_SECONDS_IN_MINUTE; 282 } 283 284 private int getDifferenceInHours(Timestamp timestampFrom, 285 Timestamp timestampTo) throws DException { 286 return getDifferenceInMinutes(timestampFrom, timestampTo) / 287 NO_OF_SECONDS_IN_MINUTE; 288 } 289 290 private int getDifferenceInDays(Timestamp timestampFrom, 291 Timestamp timestampTo) throws DException { 292 return getDifferenceInHours(timestampFrom, timestampTo) / 293 NO_OF_HOURS_IN_DAY; 294 } 295 296 private int getDifferenceInWeeks(Timestamp timestampFrom, 297 Timestamp timestampTo) throws DException { 298 return getDifferenceInDays(timestampFrom, timestampTo) / NO_OF_DAYS_IN_WEEK; 299 } 300 301 private int getDifferenceInMonths(Timestamp timestampFrom, 302 Timestamp timestampTo) throws DException { 303 Calendar cal = Calendar.getInstance(); 304 cal.setTime(new com.daffodilwoods.daffodildb.utils.DBDate(timestampFrom. 305 getTime())); 306 int yearFrom = cal.get(Calendar.YEAR); 307 int monthFrom = cal.get(Calendar.MONTH); 308 int dayFrom = cal.get(Calendar.DAY_OF_MONTH); 309 int hourFrom = cal.get(Calendar.HOUR); 310 int minuteFrom = cal.get(Calendar.MINUTE); 311 int secondFrom = cal.get(Calendar.SECOND); 312 int milliSecondFrom = cal.get(Calendar.MILLISECOND); 313 314 cal.setTime(new com.daffodilwoods.daffodildb.utils.DBDate(timestampTo. 315 getTime())); 316 int yearTo = cal.get(Calendar.YEAR); 317 int monthTo = cal.get(Calendar.MONTH); 318 319 int months = (yearTo - yearFrom) * 12 + (monthTo - monthFrom); 320 if (monthFrom == monthTo) { 321 int dayTo = cal.get(Calendar.DAY_OF_MONTH); 322 if (dayFrom > dayTo) { 323 months--; 324 } 325 else if (dayFrom == dayTo) { 326 int hourTo = cal.get(Calendar.HOUR); 327 if (hourFrom > hourTo) { 328 months--; 329 } 330 else if (hourFrom == hourTo) { 331 int minuteTo = cal.get(Calendar.MINUTE); 332 if (minuteFrom > minuteTo) { 333 months--; 334 } 335 else if (minuteFrom == minuteTo) { 336 int secondTo = cal.get(Calendar.SECOND); 337 if (secondFrom > secondTo) { 338 months--; 339 } 340 else if (secondFrom == secondTo) { 341 int milliSecondTo = cal.get(Calendar.MILLISECOND); 342 if (milliSecondFrom > milliSecondTo) { 343 months--; 344 } 345 } 346 } 347 } 348 } 349 } 350 return months; 351 } 352 353 private int getDifferenceInQuarters(Timestamp timestampFrom, 354 Timestamp timestampTo) throws DException { 355 return getDifferenceInMonths(timestampFrom, timestampTo) / 356 NO_OF_MONTHS_IN_QUARTER; 357 } 358 359 private int getDifferenceInYears(Timestamp timestampFrom, 360 Timestamp timestampTo) throws DException { 361 return getDifferenceInMonths(timestampFrom, timestampTo) / 362 NO_OF_MONTHS_IN_YEAR; 363 } 364 365 public AbstractRowValueExpression[] getChilds() { 366 AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] { 367 (AbstractRowValueExpression) (_datetimevalueexpression1), 368 (AbstractRowValueExpression) (_datetimevalueexpression3)}; 369 return childs; 370 } 371 372 public String getType() throws DException { 373 return (String )_SNONRESERVEDWORD1364442557.run(null); 374 } 375 376 public String toString() { 377 StringBuffer sb = new StringBuffer (); 378 sb.append(" "); 379 sb.append(_SNONRESERVEDWORD1364442557); 380 sb.append(" "); 381 sb.append(_Sleftparen6538802416); 382 sb.append(" "); 383 sb.append(_timestampdenometers5); 384 sb.append(" "); 385 sb.append(_Scomma948436054); 386 sb.append(" "); 387 sb.append(_datetimevalueexpression3); 388 sb.append(" "); 389 sb.append(_Scomma948436052); 390 sb.append(" "); 391 sb.append(_datetimevalueexpression1); 392 sb.append(" "); 393 sb.append(_Srightparen_18748595140); 394 return sb.toString(); 395 } 396 397 public Object clone() throws CloneNotSupportedException { 398 timestampdifffunction tempClass = new timestampdifffunction(); 399 tempClass._Srightparen_18748595140 = (Srightparen_1874859514) 400 _Srightparen_18748595140.clone(); 401 tempClass._datetimevalueexpression1 = (datetimevalueexpression) 402 _datetimevalueexpression1.clone(); 403 tempClass._Scomma948436052 = (Scomma94843605) _Scomma948436052.clone(); 404 tempClass._datetimevalueexpression3 = (datetimevalueexpression) 405 _datetimevalueexpression3.clone(); 406 tempClass._Scomma948436054 = (Scomma94843605) _Scomma948436054.clone(); 407 tempClass._timestampdenometers5 = (timestampdenometers) 408 _timestampdenometers5.clone(); 409 tempClass._Sleftparen6538802416 = (Sleftparen653880241) 410 _Sleftparen6538802416.clone(); 411 tempClass._SNONRESERVEDWORD1364442557 = (SNONRESERVEDWORD136444255) 412 _SNONRESERVEDWORD1364442557.clone(); 413 return tempClass; 414 } 415 416 public ByteComparison getByteComparison(Object object) throws DException { 417 ByteComparison byteComparison = new ByteComparison(false, new int[] {INTEGER}); 418 byteComparison.setSize(getColumnSize(object)); 419 return byteComparison; 420 } 421 422 public _Reference[] checkSemantic(_ServerSession parent) throws DException { 423 _Reference[] ref = super.checkSemantic(parent); 424 if(ref!=null) { 425 return ref; 426 } 427 int type1 = _datetimevalueexpression3.getByteComparison(parent). 428 getDataTypes()[0]; 429 int type2 = _datetimevalueexpression1.getByteComparison(parent). 430 getDataTypes()[0]; 431 switch (type1) { 432 case -1: 433 case TIMESTAMP: 434 switch (type2) { 435 case -1: 436 case TIMESTAMP: 437 return ref; 438 default: 439 throw new DException("DSE4108", 440 new Object [] {StaticClass. 441 getDataTypeName(type2), 442 "timestampdifffunction"}); 443 } 444 default: 445 throw new DException("DSE4108", 446 new Object [] {StaticClass.getDataTypeName(type1), 447 "timestampdifffunction"}); 448 } 449 450 } 451 452 474 public ColumnDetails[] getColumnDetails() throws DException { 475 initializeChildren(); 476 int len = childs.length; 477 column = new ColumnDetails(); 478 String type = getType(); 479 StringBuffer str = new StringBuffer (); 480 str.append(type); 481 str.append("("); 482 ColumnDetails[][] cds = new ColumnDetails[len][]; 483 String [] namesOfColumn = new String [len]; 484 for (int i = 0; i < childs.length; i++) { 485 cds[i] = childs[i].getColumnDetails(); 486 namesOfColumn[i] = getNameOfColumn(cds[i]); 487 this.columnDetails = StaticClass.mergeColumnDetails(this.columnDetails, cds[i]); 488 str.append(namesOfColumn[i]); 489 str.append(","); 490 } 491 str.deleteCharAt(str.length() - 1); 492 str.append(")"); 493 494 column.setFunctionType(type); 495 column.setType(SCALARFUNCTION); 496 column.setColumnName(new String [] {str.toString()}); 497 column.setObject(this); 498 return new ColumnDetails[] {column}; 499 } 500 501 public int getColumnSize( Object object) throws DException { 502 return Datatypes.INTSIZE; 503 } 504 505 } 506 | Popular Tags |