1 package com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary; 2 3 import java.math.*; 4 import java.sql.*; 5 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.expression.*; 9 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*; 10 import com.daffodilwoods.daffodildb.server.sql99.expression.stringvalueexpression.*; 11 import com.daffodilwoods.daffodildb.server.sql99.token.*; 12 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 13 import com.daffodilwoods.daffodildb.utils.field.*; 14 import com.daffodilwoods.database.resource.*; 15 import java.util.*; 16 import com.daffodilwoods.daffodildb.utils.*; 17 18 public class ifnullfunction extends AbstractGeneralFunction implements generalvaluespecification, charactervaluefunction { 19 public Srightparen_1874859514 _Srightparen_18748595140 ; 20 public valueexpression[] _OptRepScomma94843605valueexpression1 ; 21 public valueexpression _valueexpression2 ; 22 public Scomma94843605 _Scomma948436053 ; 23 public valueexpression _valueexpression4 ; 24 public Sleftparen653880241 _Sleftparen6538802415 ; 25 public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442556 ; 26 27 28 private int resultType = -1; 29 private int type1 = -1; 30 private int type2 = -1; 31 32 public Object run(Object object) throws com.daffodilwoods.database.resource.DException { 33 if (resultType == -1) { 34 resultType = getAppropriateDatatype(object); 35 } 36 FieldBase result1 = (FieldBase) _valueexpression4.run(object); 37 if (!result1.isNull()) { 38 if (type1 >= type2) { 39 return result1; 40 } 41 return getObject(result1.getObject(), type2); 42 } 43 FieldBase result2 = (FieldBase) _valueexpression2.run(object); 44 if (result2.isNull()) { 45 if(_OptRepScomma94843605valueexpression1!=null){ 46 int len =_OptRepScomma94843605valueexpression1.length; 47 FieldBase[] optionalFieldBase= new FieldBase[len]; 48 for (int i = 0; i <len; i++) { 49 optionalFieldBase[i] =(FieldBase)_OptRepScomma94843605valueexpression1[i].run(object); 50 } 51 result2 =(FieldBase)getObjectFromOptionArray(optionalFieldBase); 52 } 53 } 54 if (result2.isNull()) { 55 return type2 >= type1 ? result2 : result1; 56 } 57 if (type2 >= type1) { 58 return result2; 59 } 60 return getObject(result2.getObject(), type1); 61 } 62 63 69 private Object getObjectFromOptionArray(FieldBase[] field) throws DException { 70 for (int i = 0; i <field.length; i++) { 71 if(!field[i].isNull()){ 72 return field[i]; 73 } 74 } 75 return new FieldLiteral(FieldUtility.NULLBUFFERRANGE,resultType); 76 } 77 78 public ColumnDetails[] getChildColumnDetails() throws DException { 79 ArrayList list =new ArrayList(); 80 list.addAll(Arrays.asList(_valueexpression4.getColumnDetails())); 81 list.addAll(Arrays.asList(_valueexpression2.getColumnDetails())); 82 if(_OptRepScomma94843605valueexpression1!=null) { 83 for (int i = 0; i < _OptRepScomma94843605valueexpression1.length; i++) { 84 list.addAll(Arrays.asList(_OptRepScomma94843605valueexpression1[i].getColumnDetails())) ; 85 } 86 } 87 ColumnDetails[] cd =new ColumnDetails[list.size()]; 88 list.toArray(cd); 89 return cd; 90 } 91 92 private boolean checkForVariableColumn() throws DException { 93 for (int i = 0; i < columnDetails.length; ++i) { 94 if (columnDetails[i].getColumn().equals("?")) { 95 return true; 96 } 97 } 98 return false; 99 } 100 101 private String getNameOfColumn(ColumnDetails[] columnDetails) throws DException { 102 String nameOfColumn = ""; 103 for (int i = 0; i < columnDetails.length; ++i) { 104 nameOfColumn += columnDetails[i].getColumn(); 105 } 106 return nameOfColumn; 107 } 108 109 private void syntaxCheckingForColumn(ColumnDetails[] columnDetails) throws DException { 110 if (columnDetails.length > 1) { 111 int i = 0; 112 for (; i < columnDetails.length; ++i) { 113 if (columnDetails[i].getType() == CONSTANT) { 114 break; 115 } 116 } 117 if (i == columnDetails.length) { 118 throw new DException("DSE526", null); 119 } 120 } 121 } 122 123 private ColumnDetails[] mergeColumnDetails(ColumnDetails[] columnDetails1, ColumnDetails[] columnDetails2) throws DException { 124 ColumnDetails[] objectTarget = new ColumnDetails[columnDetails1.length + columnDetails2.length]; 125 int i = 0; 126 for (; i < columnDetails1.length; ++i) { 127 objectTarget[i] = columnDetails1[i]; 128 } 129 for (int j = 0; j < columnDetails2.length; ++i, ++j) { 130 objectTarget[i] = columnDetails2[j]; 131 } 132 return objectTarget; 133 } 134 135 public AbstractRowValueExpression[] getChilds() { 136 ArrayList list =new ArrayList(); 137 list.add((AbstractRowValueExpression) (_valueexpression4)); 138 list.add((AbstractRowValueExpression) (_valueexpression2)); 139 if(_OptRepScomma94843605valueexpression1!=null) { 140 for (int i = 0; i < _OptRepScomma94843605valueexpression1.length; i++) { 141 list.add((AbstractRowValueExpression) _OptRepScomma94843605valueexpression1[i]); 142 } 143 } 144 AbstractRowValueExpression[] childs = new AbstractRowValueExpression[list.size()]; 145 list.toArray(childs); 146 return childs; 147 } 148 149 public String toString() { 150 StringBuffer sb = new StringBuffer (); 151 sb.append(" "); 152 sb.append(_SNONRESERVEDWORD1364442556); 153 sb.append(" "); 154 sb.append(_Sleftparen6538802415); 155 sb.append(" "); 156 sb.append(_valueexpression4); 157 sb.append(" "); 158 if(_OptRepScomma94843605valueexpression1 != null ) { 159 for(int i = 0 ; i < _OptRepScomma94843605valueexpression1.length ; i++ ) 160 sb.append(",").append(_OptRepScomma94843605valueexpression1[i]); 161 } 162 sb.append(" "); 163 sb.append(_Scomma948436053); 164 sb.append(" "); 165 sb.append(_valueexpression2); 166 sb.append(" "); 167 sb.append(_Srightparen_18748595140); 168 return sb.toString(); 169 } 170 171 public Object clone() throws CloneNotSupportedException { 172 ifnullfunction tempClass = new ifnullfunction(); 173 tempClass._Srightparen_18748595140 = (Srightparen_1874859514) _Srightparen_18748595140.clone(); 174 tempClass._valueexpression2 = (valueexpression) _valueexpression2.clone(); 175 tempClass._Scomma948436053 = (Scomma94843605) _Scomma948436053.clone(); 176 tempClass._valueexpression4 = (valueexpression) _valueexpression4.clone(); 177 if(_OptRepScomma94843605valueexpression1 != null ) { 178 for (int i = 0; i < _OptRepScomma94843605valueexpression1.length; i++) { 179 tempClass._OptRepScomma94843605valueexpression1 =(valueexpression[])_OptRepScomma94843605valueexpression1[i].clone(); 180 } 181 } 182 tempClass._Sleftparen6538802415 = (Sleftparen653880241) _Sleftparen6538802415.clone(); 183 tempClass._SNONRESERVEDWORD1364442556 = (SNONRESERVEDWORD136444255) _SNONRESERVEDWORD1364442556.clone(); 184 return tempClass; 185 } 186 187 public ParameterInfo[] getParameterInfo() throws DException { 188 ArrayList list =new ArrayList(); 189 list.addAll(Arrays.asList(_valueexpression4.getParameterInfo())); 190 list.addAll(Arrays.asList(_valueexpression2.getParameterInfo())); 191 if(_OptRepScomma94843605valueexpression1 != null ) { 192 for (int i = 0; i < _OptRepScomma94843605valueexpression1.length; i++) { 193 list.addAll(Arrays.asList(_OptRepScomma94843605valueexpression1[i].getParameterInfo())); 194 } 195 } 196 ParameterInfo[] param = new ParameterInfo[list.size()]; 197 list.toArray(param); 198 209 return param; 210 } 211 212 public ByteComparison getByteComparison(Object object) throws DException { 213 int dt = resultType == -1 ? getAppropriateDatatype(object) : resultType; 214 ByteComparison byteComparison= new ByteComparison(false, new int[] {dt}); 215 byteComparison.setSize(getColumnSize(object)); 216 return byteComparison; 217 } 218 219 private int getAppropriateDatatype(Object object) throws DException { 220 type1 = getDataTypeForByte(_valueexpression4.getByteComparison(object)); 221 type2 = getDataTypeForByte(_valueexpression2.getByteComparison(object)); 222 checkForDataType(type1, type2); 223 if(_OptRepScomma94843605valueexpression1!=null) { 224 int len =_OptRepScomma94843605valueexpression1.length; 225 for (int i = 0; i < len; i++) { 226 type2 = getDataTypeForByte(_OptRepScomma94843605valueexpression1[i].getByteComparison(object)); 227 checkForDataType(type1, type2); 228 type1 = type1 > type2 ? type1 : type2; 229 } 230 231 } 232 return (type1 > type2) ? type1 : type2; 233 } 234 235 private void checkForDataType(int type1,int type2) throws DException { 236 try { 237 Check.checkForDatatype(type1, type2); 238 } catch (DException ex) { 239 if (ex.getDseCode().equalsIgnoreCase("DSE514") || 240 ex.getDseCode().equalsIgnoreCase("DSE6001")) { throw new DException("DSE8100", new Object [] {StaticClass.getDataTypeName(type1), 242 StaticClass.getDataTypeName(type2)}); 243 } 244 throw ex; 245 } 246 247 } 248 249 private FieldBase getObject(Object object, int type) throws DException { 250 switch (type) { 251 case BYTE: 252 case TINYINT: 253 return new FieldLiteral(new Byte ("" + object), BYTE); 254 case SHORT: 255 case SMALLINT: 256 return new FieldLiteral(new Short ("" + object), SHORT); 257 case INTEGER: 258 case INT: 259 return new FieldLiteral(new Integer ("" + object), INTEGER); 260 case LONG: 261 case BIGINT: 262 return new FieldLiteral(new Long ("" + object), LONG); 263 case REAL: 264 return new FieldLiteral(new Float ("" + object), FLOAT); 265 case DOUBLE: 266 case FLOAT: 267 case DOUBLEPRECISION: 268 return new FieldLiteral(new Double ("" + object), DOUBLE); 269 case BIGDECIMAL: 270 case DEC: 271 case DECIMAL: 272 case NUMERIC: 273 return new FieldLiteral(new BigDecimal("" + object), BIGDECIMAL); 274 case CHARACTER: 275 case VARCHAR: 276 case CHAR: 277 case CHARACTERVARYING: 278 return new FieldStringLiteral(object.toString(), CHARACTER); 279 case DATE: 280 return new FieldLiteral(java.sql.Date.valueOf(object.toString()), DATE); 281 case TIME: 282 return new FieldLiteral(Time.valueOf(object.toString()), TIME); 283 case TIMESTAMP: 284 return new FieldLiteral(Timestamp.valueOf(object.toString()), TIMESTAMP); 285 default: 286 throw new DException("DSE514", new Object [] {new Integer (type)}); 287 } 288 } 289 290 public _Reference[] checkSemantic(_ServerSession parent) throws DException { 291 _Reference[] ref = super.checkSemantic(parent); 292 getAppropriateDatatype(parent); 293 return ref; 294 } 295 296 int getColumnSize(Object object) throws DException { 297 ColumnDetails[] columnDetails = getChildColumnDetails(); 298 int type =columnDetails[0].getType(); 299 if(type!=TypeConstants.CONSTANT) { 300 int datatype = columnDetails[0].getDatatype(); 301 switch (datatype) { 302 case BYTE: 303 case TINYINT: 304 case INTEGER: 305 case INT: 306 return INTSIZE; 307 case REAL: 308 return FLOATSIZE; 309 case DOUBLE: 310 case FLOAT: 311 case DOUBLEPRECISION: 312 return DOUBLESIZE; 313 case LONG: 314 case BIGINT: 315 return LONGSIZE; 316 case SHORT: 317 case SMALLINT: 318 return SHORTSIZE; 319 case BIGDECIMAL: 320 case DEC: 321 case DECIMAL: 322 case NUMERIC: 323 if (columnDetails[0].getSize() != -5) { 324 return columnDetails[0].getSize(); 325 } 326 case DATE : 327 return DATESIZE; 328 case TIME : 329 return TIMESIZE; 330 case TIMESTAMP : 331 return TIMESTAMPSIZE; 332 default : 333 if (columnDetails[0].getSize() != -5) { 334 return columnDetails[0].getSize(); 335 } 336 } 337 } else if(columnDetails[0].getQuestion()) { 338 return 255; 339 } else { 340 341 if (_valueexpression4.checkForSubQuery()) { _Reference[] refs = _valueexpression4.getReferences(null); 343 if (refs[0] instanceof subquery) 344 return ( (subquery) refs[0]).getByteComparison(object).getSize(); 345 } 346 else { 347 348 FieldBase field = (FieldBase) _valueexpression4.run(object); 349 field.setDatatype(columnDetails[0].getDatatype()); 350 return field.getLength(); 351 352 } 353 } 354 return -1; 355 } 356 357 } 358 | Popular Tags |