1 package com.daffodilwoods.daffodildb.server.sql99.expression. 2 stringvalueexpression; 3 4 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 5 import com.daffodilwoods.daffodildb.server.serversystem.*; 6 import com.daffodilwoods.daffodildb.server.sql99.common.*; 7 import com.daffodilwoods.daffodildb.server.sql99.expression.numericvalueexpression.*; 8 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*; 9 import com.daffodilwoods.daffodildb.server.sql99.token.*; 10 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 11 import com.daffodilwoods.daffodildb.utils.*; 12 import com.daffodilwoods.daffodildb.utils.field.*; 13 import com.daffodilwoods.database.resource.*; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 17 public class locatefunction extends AbstractNumericValueFunction implements numericvaluefunction { 18 19 public Srightparen_1874859514 _Srightparen_18748595140; 20 public numericvalueexpression _OptScomma94843605numericvalueexpression1; 21 public charactervalueexpression _charactervalueexpression2; 22 public Scomma94843605 _Scomma948436053; 23 public charactervalueexpression _charactervalueexpression4; 24 public Sleftparen653880241 _Sleftparen6538802415; 25 public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442556; 26 27 public Object run(Object object) throws com.daffodilwoods.database.resource. 28 DException { 29 FieldBase result1 = (FieldBase) _charactervalueexpression4.run(object); 30 if (result1.isNull()) { 31 return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, INTEGER); 32 } 33 FieldBase result2 = (FieldBase) _charactervalueexpression2.run(object); 34 if (result2.isNull()) { 35 return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, INTEGER); 36 } 37 FieldBase result3 = null; 38 if (_OptScomma94843605numericvalueexpression1 != null) { 39 result3 = (FieldBase) _OptScomma94843605numericvalueexpression1.run(object); 40 } 41 int start = 0; 42 if (result3 != null && !result3.isNull()) { 43 Object obj3 = result3.getObject(); 44 int type3 = getDataType(result3); 45 start = getCount(obj3, type3) - 1; 46 } 47 return getResult(result1, result2, start); 48 } 49 50 public ColumnDetails[] getChildColumnDetails() throws DException { 51 return columnDetails; 52 } 53 54 public ParameterInfo[] getParameterInfo() throws DException { 55 ArrayList list =new ArrayList (); 56 ParameterInfo[] param1 = _charactervalueexpression4.getParameterInfo(); 57 for (int i = 0; i < param1.length; i++) { 58 if (param1[i].getQuestionMark()) { 59 param1[i].setDataType(Datatypes.CHARACTER); 60 param1[i].setName("LOCATE Arg1"); 61 list.addAll(Arrays.asList(param1)); 62 } 63 } 64 ParameterInfo[] param2 = _charactervalueexpression2.getParameterInfo(); 65 for (int i = 0; i < param2.length; i++) { 66 if (param2[i].getQuestionMark()) { 67 param2[i].setDataType(Datatypes.CHARACTER); 68 param2[i].setName("LOCATE Arg2"); 69 list.addAll(Arrays.asList(param2)); 70 } 71 } 72 ParameterInfo[] param4 = null; 73 if (_OptScomma94843605numericvalueexpression1 != null) { 74 param4 = _OptScomma94843605numericvalueexpression1.getParameterInfo(); 75 for (int i = 0; i < param4.length; i++) { 76 if (param4[i].getQuestionMark()) { 77 param4[i].setDataType(Datatypes.INTEGER); 78 param4[i].setName("LOCATE Arg3"); 79 list.addAll(Arrays.asList(param4)); 80 } 81 } 82 } 83 84 ParameterInfo[] param3 = new ParameterInfo[list.size()]; 85 list.toArray(param3); 86 for (int i = 0; i < param3.length; i++) { 87 if(param3[i].getQuestionMark()) 88 return param3; 89 } 90 return getThisParameterInfo(); 91 } 92 93 protected ParameterInfo[] getThisParameterInfo() throws DException { 94 ParameterInfo parameterInfo = new ParameterInfo(); 95 parameterInfo.setName(toString()); 96 parameterInfo.setDataType(Datatypes.INTEGER); 97 return new ParameterInfo[] { 98 parameterInfo}; 99 } 100 101 protected String getNameOfColumn(ColumnDetails[] columnDetails) throws 102 DException { 103 if (columnDetails == null) { 104 return ""; 105 } 106 String nameOfColumn = ""; 107 for (int i = 0; i < columnDetails.length; ++i) { 108 nameOfColumn += columnDetails[i].getColumn(); 109 } 110 return nameOfColumn; 111 } 112 113 133 134 private int getCount(Object object, int type) throws DException { 135 switch (type) { 136 case BYTE: 137 case TINYINT: 138 case SHORT: 139 case SMALLINT: 140 case INTEGER: 141 case INT: 142 case REAL: 143 case LONG: 144 case BIGINT: 145 case BIGDECIMAL: 146 case DEC: 147 case DECIMAL: 148 case NUMERIC: 149 case DOUBLE: 150 case FLOAT: 151 case DOUBLEPRECISION: 152 return ( (Number ) object).intValue(); 153 case CHARACTER: 154 case VARCHAR: 155 case CHAR: 156 case CHARACTERVARYING: 157 String string = (String ) object; 158 Double d = null; 159 try { 160 d = new Double (string); 161 } catch (Exception e) { 162 throw new DException("DSE4104", null); 163 } 164 return d.intValue(); 165 default: 166 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "locate"}); 167 } 168 } 169 170 private Object getResult(FieldBase result1, FieldBase result2, 171 int start) throws DException { 172 int type1 = getDataType(result1); 173 int type2 = getDataType(result2); 174 switch (type1) { 175 case CHARACTER: 176 case VARCHAR: 177 case CHAR: 178 case CHARACTERVARYING: 179 switch (type2) { 180 case CHARACTER: 181 case VARCHAR: 182 case CHAR: 183 case CHARACTERVARYING: 184 return new FieldLiteral(locate( ( (String ) result2.getObject()).toString(), 185 ( (String ) result1.getObject()).toString(), start), Datatype.INTEGER); 186 default: 187 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type2), "locate"}); 188 } 189 default: 190 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type1), "locate"}); 191 } 192 } 193 194 private Object locate(String string1, String string2, int start) throws 195 DException { 196 if (start < 0) { 197 throw new DException("DSE1160", null); 198 } 199 int i = string1.indexOf(string2, start); 200 if (i == -1) { 201 return new Integer (0); 202 } 203 return new Integer ( (i + 1) - start); 204 } 205 206 public AbstractRowValueExpression[] getChilds() { 207 AbstractRowValueExpression[] childs = _OptScomma94843605numericvalueexpression1 == null ? new AbstractRowValueExpression[] { 208 (AbstractRowValueExpression) (_charactervalueexpression4), 209 (AbstractRowValueExpression) (_charactervalueexpression2)} 210 : new AbstractRowValueExpression[] { 211 (AbstractRowValueExpression) (_charactervalueexpression4), 212 (AbstractRowValueExpression) (_charactervalueexpression2), 213 (AbstractRowValueExpression) _OptScomma94843605numericvalueexpression1}; 214 return childs; 215 216 } 217 218 public String getType() throws DException { 219 return (String ) _SNONRESERVEDWORD1364442556.run(null); 220 } 221 222 public String toString() { 223 StringBuffer sb = new StringBuffer (); 224 sb.append(" "); 225 sb.append(_SNONRESERVEDWORD1364442556); 226 sb.append(" "); 227 sb.append(_Sleftparen6538802415); 228 sb.append(" "); 229 sb.append(_charactervalueexpression4); 230 sb.append(" "); 231 sb.append(_Scomma948436053); 232 sb.append(" "); 233 sb.append(_charactervalueexpression2); 234 if (_OptScomma94843605numericvalueexpression1 != null) { 235 sb.append(","); 236 sb.append(_OptScomma94843605numericvalueexpression1); 237 } 238 sb.append(" "); 239 sb.append(_Srightparen_18748595140); 240 return sb.toString(); 241 } 242 243 public Object clone() throws CloneNotSupportedException { 244 locatefunction tempClass = new locatefunction(); 245 tempClass._Srightparen_18748595140 = (Srightparen_1874859514) 246 _Srightparen_18748595140.clone(); 247 if (_OptScomma94843605numericvalueexpression1 != null) { 248 tempClass._OptScomma94843605numericvalueexpression1 = ( 249 numericvalueexpression) _OptScomma94843605numericvalueexpression1. 250 clone(); 251 } 252 tempClass._charactervalueexpression2 = (charactervalueexpression) 253 _charactervalueexpression2.clone(); 254 tempClass._Scomma948436053 = (Scomma94843605) _Scomma948436053.clone(); 255 tempClass._charactervalueexpression4 = (charactervalueexpression) 256 _charactervalueexpression4.clone(); 257 tempClass._Sleftparen6538802415 = (Sleftparen653880241) 258 _Sleftparen6538802415.clone(); 259 tempClass._SNONRESERVEDWORD1364442556 = (SNONRESERVEDWORD136444255) 260 _SNONRESERVEDWORD1364442556.clone(); 261 return tempClass; 262 } 263 264 public ByteComparison getByteComparison(Object object) throws DException { 265 ByteComparison byteComparison = new ByteComparison(false, new int[] {INTEGER}); 266 byteComparison.setSize(getColumnSize(object)); 267 return byteComparison; 268 } 269 270 public _Reference[] checkSemantic(_ServerSession parent) throws DException { 271 _Reference[] ref = super.checkSemantic(parent); 272 if(ref!=null) { 273 return ref; 274 } 275 int type1 = _charactervalueexpression4.getByteComparison(parent).getDataTypes()[0]; 276 int type2 = _charactervalueexpression2.getByteComparison(parent).getDataTypes()[0]; 277 switch (type1) { 278 case -1: 279 case CHARACTER: 280 case VARCHAR: 281 case CHAR: 282 case CHARACTERVARYING: 283 switch (type2) { 284 case -1: 285 case CHARACTER: 286 case VARCHAR: 287 case CHAR: 288 case CHARACTERVARYING: 289 return ref; 290 default: 291 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type2), "locate"}); 292 } 293 default: 294 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type1), "locate"}); 295 } 296 297 } 298 public int getColumnSize(Object object) throws DException { 299 return Datatypes.INTSIZE; 300 } 301 302 public ColumnDetails[] getColumnDetails() throws DException { 303 initializeChildren(); 304 int len = childs.length; 305 column = new ColumnDetails(); 306 String type = getType(); StringBuffer str = new StringBuffer (); str.append(type); 309 str.append("("); 310 ColumnDetails[][] cds = new ColumnDetails[len][]; 311 String [] namesOfColumn = new String [len]; 312 for (int i = 0; i < childs.length; i++) { 313 cds[i] = childs[i].getColumnDetails(); 314 namesOfColumn[i] = getNameOfColumn(cds[i]); 315 this.columnDetails = StaticClass.mergeColumnDetails(this.columnDetails, cds[i]); 316 str.append(namesOfColumn[i]); 317 str.append(","); 318 } 319 str.deleteCharAt(str.length() - 1); 320 str.append(")"); 321 322 column.setFunctionType(type); 323 column.setType(SCALARFUNCTION); 324 column.setColumnName(new String [] {str.toString()}); 325 column.setObject(this); 326 return new ColumnDetails[] {column}; 327 } 328 329 330 } 331 | Popular Tags |