1 package com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression; 2 3 import java.lang.reflect.*; 4 import java.sql.*; 5 import java.util.*; 6 7 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 8 import com.daffodilwoods.daffodildb.server.serversystem.*; 9 import com.daffodilwoods.daffodildb.server.sql99.*; 10 import com.daffodilwoods.daffodildb.server.sql99.common.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dcl.sqlcontrolstatement.*; 12 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 13 import com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition.*; 14 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*; 15 import com.daffodilwoods.daffodildb.server.sql99.dml.*; 16 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 17 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 18 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.*; 19 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*; 20 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 21 import com.daffodilwoods.daffodildb.utils.*; 22 import com.daffodilwoods.daffodildb.utils.field.*; 23 import com.daffodilwoods.daffodildb.utils.parser.*; 24 import com.daffodilwoods.database.resource.*; 25 import com.daffodilwoods.database.utility.TypeConverter; 26 import in.co.daffodil.db.general.*; 27 import in.co.daffodil.db.jdbc.*; 28 29 public class routineinvocation extends AbstractBooleanValueExpression implements nonparenthesizedvalueexpressionprimary, valueexpressionprimary, _Executer, _sortspecificationAndselectSubList, valueexpressionwithoutrowvalueconstructorwithoutboolean { 30 public SQLargumentlist _SQLargumentlist0; 31 public routinename _routinename1; 32 33 ParametersDescriptor[] parameters; 34 DataTypeDescriptor[] dtdDescriptors; 35 SQLprocedurestatement[] statements; 36 Method external_method; 37 Class external_java_class; 38 Constructor construct; 39 String external_methodName; 40 _Executer[] executers; 41 _Reference[][] statementReferences; 42 _ServerSession currentSession; 43 _Reference[] refs; 44 45 boolean isSelfInitiated = true; 46 int numberOfParametersInParameterList; 47 boolean checkRights = true; 48 49 private String ROUTINE_CATALOG; 50 private String ROUTINE_SCHEMA; 51 private String ROUTINE_NAME; 52 private RoutineDescriptor routineDescriptor; 53 private UserFunctionColumnDetails functionCD; 54 private boolean isRoutineInRecursion; 55 private _Executer recursiveExecuters; 56 private _Executer parentExecuter; 57 58 public void setIsSelfInitiated(boolean temp) { 59 isSelfInitiated = temp; 60 } 61 62 public Object run(Object object) throws DException { 63 if (object instanceof _VariableValues) { 64 if (routineDescriptor == null) { 65 checkSemantic( ( (VariableValues) object).getServerSession()); 66 } 67 return functionCD.run(object); 68 } 69 70 _StatementExecutionContext sec = (_StatementExecutionContext) object; 71 checkRights = sec.getUserRight(); 72 currentSession = sec.getServerSession(); 73 if (isRoutineInRecursion) { 74 return new RecursiveRoutineExecuter(currentSession, _SQLargumentlist0, routineDescriptor, parameters, parentExecuter); 75 } 76 setQualifiedRotuineName(currentSession); 77 numberOfParametersInParameterList = _SQLargumentlist0.getNumberOfParametersInArgumentList(); 78 RoutineDescriptor subjectRoutine = getSubjectRoutine(currentSession); 79 parameters = getParameters(currentSession, subjectRoutine); 80 dtdDescriptors = getDTDdescriptorForParameters(currentSession, subjectRoutine); 81 if (subjectRoutine.ROUTINE_BODY.equalsIgnoreCase(SqlKeywords.SQL)) { 82 initialiseExecutersAndReferences(sec); 83 } else if (subjectRoutine.ROUTINE_BODY.equalsIgnoreCase(SqlKeywords.EXTERNAL)) { 84 initializeExternalJavaMethod(currentSession, subjectRoutine); 85 } 86 return this; 87 } 88 89 private void initializeExternalJavaMethod(_ServerSession currentSession, RoutineDescriptor subjectRoutine) throws DException { 90 String external = subjectRoutine.EXTERNAL_NAME; 91 int index2 = external.indexOf("::"); 92 String jar_plus_className = external.substring(0, index2); 93 94 int index = jar_plus_className.indexOf(":"); 95 String package_plus_className = jar_plus_className.substring(index + 1, jar_plus_className.length()); 96 97 102 103 try { 104 external_java_class = Class.forName(package_plus_className.trim()); 105 } catch (NoClassDefFoundError ex) { 106 throw new DException("DSE8120", new Object [] {package_plus_className}); 107 } catch (ClassNotFoundException ex) { 108 throw new DException("DSE8121", new Object [] {package_plus_className}); 109 } 110 int index3 = external.indexOf("("); 111 external_methodName = external.substring(index2 + 2, index3 > 0 ? index3 : external.length()).trim(); 112 113 122 123 124 Class [] params = null; 125 if (parameters != null) { 126 params = new Class [parameters.length]; 127 for (int i = 0; i < parameters.length; i++) { 128 if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.IN)) { 129 if ( (dtdDescriptors[i].data_Type.equalsIgnoreCase("char") && dtdDescriptors[i].getPrecision() > 1) || 130 (dtdDescriptors[i].data_Type.equalsIgnoreCase("character") && dtdDescriptors[i].getPrecision() > 1) || 131 (dtdDescriptors[i].data_Type.equalsIgnoreCase("varchar") && dtdDescriptors[i].getPrecision() > 1) || 132 (dtdDescriptors[i].data_Type.equalsIgnoreCase("varchar2") && dtdDescriptors[i].getPrecision() > 1)) { 133 params[i] = String .class; 134 } else { 135 params[i] = GeneralUtility.getJavaDataType(dtdDescriptors[i].data_Type); 136 } 137 } else if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.OUT) || 138 parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.INOUT)) { 139 params[i] = javaParameter.class; 140 } 141 } 142 } 143 try { 144 external_method = external_java_class.getDeclaredMethod(external_methodName, params); 145 } catch (SecurityException ex1) { 146 throw new DException("DSE8126", new Object [] {external_methodName, external_java_class}); 147 } catch (NoSuchMethodException ex2) { 148 throw new DException("DSE8126", new Object [] {external_methodName, external_java_class}); 149 } 150 151 152 153 try { 154 construct = external_java_class.getConstructor(new Class [] {java.sql.Connection .class}); 155 } catch (SecurityException ex) { 156 throw new DException("DSE8127", null); 157 } catch (NoSuchMethodException ex1) { 158 throw new DException("DSE8127", null); 159 } 160 161 } 162 163 private Object newInstanceOfClass(_ServerSession currentSession, Constructor construct) throws DException { 164 _Connection con = (_Connection) currentSession; 165 Object instance = null; 166 try { 167 DaffodilDBConnection dConn = new DaffodilDBConnection(con); 168 dConn.setAutoCommit(false); 169 instance = construct.newInstance(new Object [] {dConn}); 170 } catch (SQLException ex2) { 171 throw new DException("DSE8127", null); 172 } catch (InvocationTargetException ex3) { 173 throw new DException("DSE8127", null); 174 } catch (IllegalArgumentException ex4) { 175 throw new DException("DSE8127", null); 176 } catch (IllegalAccessException ex5) { 177 throw new DException("DSE8127", null); 178 } catch (InstantiationException ex6) { 179 throw new DException("DSE8127", null); 180 } 181 return instance; 182 } 183 184 private ParametersDescriptor[] getParameters(_ServerSession currentSession, RoutineDescriptor subjectRoutine) throws DException { 185 if (numberOfParametersInParameterList <= 0) { 186 return null; 187 } else { 188 ParametersDescriptor[] parameterDes = new ParametersDescriptor[numberOfParametersInParameterList]; 189 String clause = "select * from " + SystemTables.parameters_TableName + " where specific_catalog = ? " 190 + " and specific_schema = ? and specific_name = ? "; 191 _SelectQueryIterator iter = SqlSchemaConstants.getIterator(currentSession.getSystemServerSession(), clause, new Object [] {subjectRoutine.SPECIFIC_CATALOG, subjectRoutine.SPECIFIC_SCHEMA, subjectRoutine.SPECIFIC_NAME}); 192 ArrayList list = new ArrayList(); 193 if (iter.first()) { 194 do { 195 ParametersDescriptor para = new ParametersDescriptor(); 196 para.loadDataFromRecord(iter); 197 list.add(para); 198 } while (iter.next()); 199 } 200 if (list.size() == numberOfParametersInParameterList) { 201 parameterDes = (ParametersDescriptor[]) list.toArray(new ParametersDescriptor[0]); 202 } else { 203 throw new DException("DSE753", null); 204 } 205 return parameterDes; 206 } 207 } 208 209 private DataTypeDescriptor[] getDTDdescriptorForParameters(_ServerSession currentSession, RoutineDescriptor subjectRoutine) throws DException { 210 if (numberOfParametersInParameterList <= 0) { 211 return null; 212 } else { 213 DataTypeDescriptor[] dtdDes = new DataTypeDescriptor[numberOfParametersInParameterList]; 214 String clause = "select * from " + SystemTables.dataTypeDescriptor_TableName 215 + " where object_catalog = ? and object_schema = ? and object_name = ? " 216 + " and object_type='ROUTINE'"; 217 _SelectQueryIterator iter = SqlSchemaConstants.getIterator(currentSession.getSystemServerSession(), clause, new Object [] {subjectRoutine.SPECIFIC_CATALOG, subjectRoutine.SPECIFIC_SCHEMA, subjectRoutine.SPECIFIC_NAME}); 218 ArrayList list = new ArrayList(); 219 220 if (iter.first()) { 221 do { 222 DataTypeDescriptor dtd = new DataTypeDescriptor(); 223 dtd.loadDataFromRecord(iter); 224 list.add(dtd); 225 } while (iter.next()); 226 } 227 if (subjectRoutine.ROUTINE_TYPE.equalsIgnoreCase(SqlKeywords.PROCEDURE)) { 228 if (list.size() == numberOfParametersInParameterList) { 229 dtdDes = (DataTypeDescriptor[]) list.toArray(new DataTypeDescriptor[0]); 230 } else { 231 throw new DException("DSE753", null); 232 } 233 } else { if (list.size() == numberOfParametersInParameterList + 1) { 235 dtdDes = (DataTypeDescriptor[]) list.toArray(new DataTypeDescriptor[0]); 236 } else { 237 throw new DException("DSE753", null); 238 } 239 } 240 ArrayList arr = new ArrayList(); 241 for (int i = 0; i < parameters.length; i++) { 242 for (int j = 0; j < dtdDes.length; j++) { 243 if (parameters[i].DTD_IDENTIFIER.equalsIgnoreCase(dtdDes[j].dtd_identifier)) { 244 arr.add(dtdDes[j]); 245 } 246 } 247 } 248 if (arr.size() == numberOfParametersInParameterList) { 249 dtdDes = (DataTypeDescriptor[]) arr.toArray(new DataTypeDescriptor[0]); 250 } else { 251 throw new DException("DSE753", null); 252 } 253 return dtdDes; 254 } 255 } 256 257 private void initialiseExecutersAndReferences(_StatementExecutionContext sec) throws DException { 258 for (int i = 0; i < statements.length; i++) { 259 SQLprocedurestatement sqlp = (SQLprocedurestatement) statements[i]; 260 SQLexecutablestatement sqlexe = sqlp.getExecutableStatement(); 261 sec.setUserRight(false); 262 if ( (sqlexe instanceof SQLcontrolstatement)) { 263 if ( (sqlexe instanceof callstatement)) { 264 try { 265 ( (_StatementExecutionContext) sec).addProcedureCount(); 266 if ( ( (_StatementExecutionContext) sec).getProcedureCount() > 32) 267 throw new DException("DSE8160", null); 268 executers[i] = (_Executer) sqlexe.run(sec); 269 } finally { 270 ( (_StatementExecutionContext) sec).subProcedureCount(); 271 } 272 } 273 } executers[i] = (_Executer) sqlexe.run(sec); 275 } 276 277 } 278 279 private RoutineDescriptor getSubjectRoutine(_ServerSession currentSession) throws DException { 280 ArrayList routineToCheck = getAllPossibleCandidateRoutines(currentSession); 281 RoutineDescriptor temp = numberOfParametersInParameterList == 0 ? 282 getFromWithoutParameters(currentSession, routineToCheck) : 283 getFromWithParameters(currentSession, routineToCheck); 284 285 if ( (! (!checkRights || checkForExecutable(currentSession, temp))) || temp == null) 286 throw new DException("DSE8133", new Object [] {_routinename1.getQualifiedRoutineName(currentSession).getIdentifier()}); 287 return temp; 288 } 289 290 private ArrayList getAllPossibleCandidateRoutines(_ServerSession currentSession) throws DException { 291 ArrayList possibleCandidateRoutines = new ArrayList(); 292 String clause = "select * from " + SqlSchemaConstants.routines_TableName + " where routine_catalog ='" 293 + ROUTINE_CATALOG + "'" + " and routine_schema='" + ROUTINE_SCHEMA + "'" + " and routine_name='" + ROUTINE_NAME + "'"; 294 _SelectQueryIterator iter = SqlSchemaConstants.getIterator(currentSession, clause, null); 295 if (iter.first()) { 296 do { 297 RoutineDescriptor routine_descriptor = new RoutineDescriptor(); 298 routine_descriptor.loadDataFromRecord(iter); 299 possibleCandidateRoutines.add(routine_descriptor); 300 } while (iter.next()); 301 } else { 302 throw new DException("DSE728", new Object [] {_routinename1.getQualifiedRoutineName(currentSession).getIdentifier(), new Integer (numberOfParametersInParameterList)}); 303 } 304 return possibleCandidateRoutines; 305 } 306 307 308 private RoutineDescriptor getFromWithoutParameters(_ServerSession currentSession, ArrayList executableRoutines) throws DException { 309 for (int i = 0; i < executableRoutines.size(); i++) { 310 RoutineDescriptor routine = (RoutineDescriptor) executableRoutines.get(i); 311 ParametersDescriptor par = new ParametersDescriptor(); 312 String clause = "select * from " + SystemTables.parameters_TableName + " where " 313 + " specific_catalog = ? and specific_schema = ? and specific_name = ?"; 314 _Iterator iterator = SqlSchemaConstants.getIterator(currentSession.getSystemServerSession(), clause, new Object [] {routine.SPECIFIC_CATALOG, routine.SPECIFIC_SCHEMA, routine.SPECIFIC_NAME}); 315 if (!iterator.first()) { 316 return routine; 317 } 318 } 319 throw new DException("DSE728", new Object [] {_routinename1.getQualifiedRoutineName(currentSession).getIdentifier(), new Integer (numberOfParametersInParameterList)}); 320 } 321 322 private RoutineDescriptor getFromWithParameters(_ServerSession currentSession, ArrayList executableRoutines) throws DException { 323 for (int i = 0; i < executableRoutines.size(); i++) { 324 RoutineDescriptor routine = (RoutineDescriptor) executableRoutines.get(i); 325 String clause = "select count(*) from " + SystemTables.parameters_TableName + " where " 326 + " specific_catalog = '" + routine.SPECIFIC_CATALOG + "' and specific_schema = '" 327 + routine.SPECIFIC_SCHEMA + "' and specific_name ='" + routine.SPECIFIC_NAME + "'"; 328 _SelectQueryIterator iterator = SqlSchemaConstants.getIterator(currentSession.getSystemServerSession(), clause, null); 329 330 if (iterator.first()) { 331 do { 332 Object [] values = (Object []) iterator.getObject(); 333 if (values[0].hashCode() == numberOfParametersInParameterList) 334 return routine; } while (iterator.next()); 336 } 337 } 338 throw new DException("DSE728", new Object [] {_routinename1.getQualifiedRoutineName(currentSession).getIdentifier(), new Integer (numberOfParametersInParameterList)}); 339 } 340 341 private boolean checkForExecutable(_ServerSession currentSession, RoutineDescriptor routine_descriptor) throws DException { 342 _PrivilegeCharacteristics pc = currentSession.getUserSession().getPrivilegeCharacteristics(); 343 _PrivilegeTable pt = pc.getRoutinePrivilegeTable(); 344 return pt.hasExecutionRights(routine_descriptor.SPECIFIC_CATALOG, routine_descriptor.SPECIFIC_SCHEMA, routine_descriptor.SPECIFIC_NAME); 345 } 346 347 public Object execute(_VariableValues variableValues) throws DException { 348 if (numberOfParametersInParameterList != 0) { 349 Object [] values = new Object [numberOfParametersInParameterList]; 350 if (variableValues instanceof ProcedureVariableValue) { 351 ProcedureVariableValue vv = (ProcedureVariableValue) variableValues; 352 HashMap map = vv.getHashMap(); 353 for (int i = 0; i < parameters.length; i++) { 354 String param = parameters[i].PARAMETER_NAME; 355 if (map.containsKey(param) == false) { 356 throw new DException("DSE8041", new Object [] {param}); 357 } 358 values[i] = map.get(param); 359 } 360 } else { 361 362 if (_SQLargumentlist0.getParameterReferences() == null) { 363 values = (Object []) _SQLargumentlist0.run(variableValues == null ? new VariableValues(currentSession) : variableValues); 364 } else { 365 values = (Object []) _SQLargumentlist0.run(variableValues == null ? new VariableValues(_SQLargumentlist0.getParameterReferences(), currentSession) : variableValues); 366 } 367 } 368 Object [] obj = (Object []) executeRoutine(values); 369 if (parameters != null) { 370 if (variableValues != null) { 371 Object [][] afterExecuteRefValuePair = variableValues. 372 getReferenceAndValuePair(); 373 for (int i = 0; i < parameters.length; i++) { 374 if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.OUT) || 375 parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.INOUT)) { 376 if (afterExecuteRefValuePair != null) { 377 ColumnDetails[] parameterColumnDetails = _SQLargumentlist0. 378 _OptSQLargumentOptRepScomma94843605SQLargument0. 379 _OptRepScomma94843605SQLargument0[i].getColumnDetails(); 380 if (parameterColumnDetails == null || 381 parameterColumnDetails[0].getReferenceType() != 382 ColumnDetails.COLUMNDETAIL) { 383 throw new DException("DSE8194", null); 384 } 385 ColumnDetails cd = parameterColumnDetails[0]; 386 String parameterReference = cd.getColumn(); 387 for (int j = 0; j < afterExecuteRefValuePair.length; j++) { 388 _Reference vvRef = (_Reference) afterExecuteRefValuePair[j][0]; 389 if (parameterReference.equalsIgnoreCase(vvRef. 390 getQualifiedColumnName())) { 391 FieldBase fb = external_method == null ? 392 (FieldBase) values[i] : 393 FieldUtility.getFieldForVariable( ( (javaParameter) 394 values[i]).getObject()); 395 fb = FieldUtility.convertToAppropriateType(fb, vvRef.getDatatype(), vvRef.getSize(), null); 396 variableValues.setConditionVariableValue(new _Reference[] { 397 vvRef} 398 , new Object [] {fb} 399 , 1); 400 } 401 } 402 } 403 } 404 } 405 } 406 } 407 return makeResultObject(values, obj); 408 } else { 409 return execute( (Object [])null); 410 } 411 } 412 413 public Object execute(Object [] values) throws DException { 414 _Reference[] ref = _SQLargumentlist0.getParameterReferences(); 415 int refCount = ref == null ? 0 : ref.length; 416 int valueCount = values == null ? 0 : values.length; 417 if (refCount != valueCount) { 418 throw new DException("DSE753", null); 419 } 420 VariableValues vv = new VariableValues(ref, currentSession); 421 if (values != null) { 422 FieldBase[] base = FieldUtility.getFields(values); 423 vv.setConditionVariableValue(ref, base, 1); 424 } 425 Object [] fullValues = (Object []) _SQLargumentlist0.run(vv); 426 Object [] result = (Object []) executeRoutine(fullValues); 427 return makeResultObject(fullValues, result); 428 } 429 430 public Object executeRoutine(Object [] values) throws DException { 431 int valueLen = values == null ? 0 : values.length; 432 if (valueLen != numberOfParametersInParameterList) { 433 throw new DException("DSE753", null); 434 } 435 436 if (values != null) { 437 for (int i = 0; i < values.length; i++) { 438 try { 439 values[i] = FieldUtility.convertToAppropriateType( (FieldBase) values[i], dtdDescriptors[i].getType(), dtdDescriptors[i].getPrecision(), dtdDescriptors[i].getScale(), null); 440 } catch (DException ex) { 441 if (ex.getDseCode().equalsIgnoreCase("DSE5546")) { 442 throw new DException("DSE8148", new Object [] {dtdDescriptors[i].dtd_identifier}); 443 } else if (ex.getDseCode().equalsIgnoreCase("DSE773")) { 444 throw new DException("DSE8177", null); 445 } 446 throw ex; 447 } 448 } 449 450 if (external_method == null) { 451 Object [] result = new Object [statements.length]; 452 try { 453 result = callPrecedure(values); 454 } catch (DException ex) { 455 if (ex.getDseCode().equalsIgnoreCase("DSE8193")) { 456 Object [] rs = ex.getParameters(); 457 result[result.length - 1] = rs; 458 } else { 459 throw ex; 460 } 461 } 462 return result; 463 } else { 464 for (int i = 0; i < parameters.length; i++) { 465 if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.IN)) { 466 if (dtdDescriptors[i].data_Type.equalsIgnoreCase("char") && ! (dtdDescriptors[i].getPrecision() > 1)) { 467 Object ss = (Object ) values[i]; 468 char as = ss.toString().charAt(0); 469 values[i] = new java.lang.Character (as); 470 } else 471 values[i] = ( (FieldBase) values[i]).getObject(); 472 } else if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.OUT)) { 473 values[i] = new javaParameterImplemention(); 474 } else if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.INOUT)) { 475 values[i] = new javaParameterImplemention( ( (FieldBase) values[i]).getObject()); 476 } 477 } 478 try { 479 external_method.invoke(newInstanceOfClass(currentSession, construct), values); 480 } catch (InvocationTargetException ex2) { 481 ex2.printStackTrace(); 482 throw new DException("DSE8145", new Object [] {external_methodName}); 483 } catch (IllegalArgumentException ex3) { 484 throw new DException("DSE8145", new Object [] {external_methodName}); 485 } catch (IllegalAccessException ex4) { 486 throw new DException("DSE8145", new Object [] {external_methodName}); 487 } 488 return null; 489 } 490 } else { 491 if (external_method == null) { 492 Object [] result = new Object [statements.length]; 493 try { 494 result = callPrecedure(null); 495 } catch (DException ex) { 496 if (ex.getDseCode().equalsIgnoreCase("DSE8193")) { 497 Object [] rs = ex.getParameters(); 498 result[result.length - 1] = rs; 499 } else { 500 throw ex; 501 } 502 } 503 return result; 504 } else { 505 try { 506 external_method.invoke(newInstanceOfClass(currentSession, construct), null); 507 } catch (InvocationTargetException ex1) { 508 throw new DException("DSE8145", new Object [] {external_methodName}); 509 } catch (IllegalArgumentException ex2) { 510 throw new DException("DSE8145", new Object [] {external_methodName}); 511 } catch (IllegalAccessException ex3) { 512 throw new DException("DSE8145", new Object [] {external_methodName}); 513 } 514 } 515 return null; 516 } 517 } 518 519 public Object [] getParameters(Object object) throws DException { 520 return _SQLargumentlist0.getParameterReferences(); 521 } 522 523 public Object [] callPrecedure(Object [] values) throws DException { 524 Object [] resultOfStatements = new Object [executers.length]; 525 for (int i = 0; i < statements.length; i++) { 526 resultOfStatements[i] = executeSingleProcedureStatement(statements[i], 527 executers[i], values, statementReferences[i]); 528 529 } 530 return resultOfStatements; 531 } 532 533 private Object executeSingleProcedureStatement(SQLprocedurestatement statement, 534 _Executer sttExecuter, Object [] parametersValues, 535 _Reference[] refs) throws DException { 536 Object returnValue = null; 537 SQLexecutablestatement sqlexe = statement.getExecutableStatement(); 538 if ( (sqlexe instanceof SQLdatastatement) || 539 (sqlexe instanceof SQLcontrolstatement)) { 540 if (refs != null) { 541 if (parameters == null && refs.length > 0) { 542 throw new DException("DSE8047", new Object [] {refs[0].getQualifiedColumnName(), _routinename1.getQualifiedRoutineName(currentSession).getIdentifier()}); 543 } 544 Object [] values = new Object [refs.length]; 545 for (int i = 0; i < refs.length; i++) { 546 boolean flag = false; 547 for (int j = 0; j < parameters.length; j++) { 548 if (parameters[j].PARAMETER_NAME.equalsIgnoreCase(refs[i].getQualifiedColumnName())) { 549 refs[i].setDatatype(dtdDescriptors[j].getType()); 550 refs[i].setSize(dtdDescriptors[j].getPrecision()); 551 values[i] = parametersValues[j]; 552 flag = true; 553 } 554 } 555 if (flag == false) { 556 throw new DException("DSE8047", new Object [] {refs[i].getQualifiedColumnName(), _routinename1.getQualifiedRoutineName(currentSession).getIdentifier()}); 557 } 558 } 559 VariableValues vv = new VariableValues(refs, currentSession); 560 vv.setConditionVariableValue(refs, values, 1); 561 returnValue = sttExecuter.execute(vv); 562 564 Object [][] refValuePair = vv.getReferenceAndValuePair(); 565 if (parameters != null) { 566 for (int i = 0; i < parameters.length; i++) { 567 if (refValuePair != null) { 568 for (int j = 0; j < refValuePair.length; j++) { 569 _Reference reference = (_Reference) refValuePair[j][0]; 570 if (reference.getQualifiedColumnName().equalsIgnoreCase(parameters[i]. 571 PARAMETER_NAME)) { 572 parametersValues[i] = refValuePair[j][1]; 573 } 574 } 575 } 576 } 577 } 578 } else { 579 VariableValues vv = new VariableValues(refs, currentSession); 580 returnValue = sttExecuter.execute(vv); 581 } 582 } else { 583 } 584 return returnValue; 585 } 586 587 645 public _Reference[] checkSemantic(_ServerSession object) throws DException { 646 _ServerSession rss; 647 boolean isCreationTime = false; 648 if (! (object instanceof RoutineServerSession)) { 649 rss = new RoutineServerSession(object); 650 } else { 651 rss = (_ServerSession) object; 652 } 653 656 setQualifiedRotuineName(rss); 657 numberOfParametersInParameterList = _SQLargumentlist0.getNumberOfParametersInArgumentList(); 658 ColumnDetails[] routineColumnDetails = getChildColumnDetails(); 659 isCreationTime = ( (RoutineServerSession) rss).isCreationTimeRoutine(); 660 try { 661 if (isCreationTime) { 662 if (!isSameRoutine(rss, numberOfParametersInParameterList, ROUTINE_CATALOG, ROUTINE_SCHEMA, ROUTINE_NAME, routineColumnDetails)) { 663 routineDescriptor = getSubjectRoutine(rss); 664 parameters = getParameters(rss, routineDescriptor); 665 dtdDescriptors = getDTDdescriptorForParameters(rss, routineDescriptor); 666 if (routineDescriptor != null && routineDescriptor.ROUTINE_TYPE.equalsIgnoreCase(SqlKeywords.FUNCTION)) { 667 DataTypeDescriptor returnTypeDTD = getDataTypeDescriptor(rss); 668 getColumnDetails(); 669 functionCD.setDatatype(TypeConverter.getDataBaseType(returnTypeDTD.data_Type)); 670 functionCD.setSize(GeneralUtility.getBytesCount(functionCD.getDatatype(), returnTypeDTD.getPrecision())); 671 } 672 } 673 } else { 674 routineDescriptor = getSubjectRoutine(rss); 675 parameters = getParameters(rss, routineDescriptor); 676 dtdDescriptors = getDTDdescriptorForParameters(rss, routineDescriptor); 677 isRoutineInRecursion = ( (RoutineServerSession) rss).isSameRoutine(routineDescriptor.SPECIFIC_NAME); 678 parentExecuter = ( (RoutineServerSession) rss).getRecursiveRoutineExecuter(routineDescriptor.SPECIFIC_NAME); 679 recursiveExecuters = this; 680 if (routineDescriptor != null && routineDescriptor.ROUTINE_TYPE.equalsIgnoreCase(SqlKeywords.FUNCTION)) { 681 DataTypeDescriptor returnTypeDTD = getDataTypeDescriptor(rss); 682 684 getColumnDetails(); 685 functionCD.setDatatype(TypeConverter.getDataBaseType(returnTypeDTD.data_Type)); 686 functionCD.setSize(GeneralUtility.getBytesCount(functionCD.getDatatype(), returnTypeDTD.getPrecision())); 687 } 688 if (!isRoutineInRecursion) { 689 ( (RoutineServerSession) rss).setRoutineCharactersticsInRoutineMap(routineDescriptor.SPECIFIC_NAME, recursiveExecuters); 690 if (!routineDescriptor.ROUTINE_BODY.equalsIgnoreCase(SqlKeywords.EXTERNAL)) { 691 intializeExecuterAndReferences(rss, routineDescriptor); 692 } 693 } 694 } 695 ( (RoutineServerSession) rss).reinitializeMap(); 696 refs = _SQLargumentlist0.checkSemantic(rss); 697 if (!isRoutineInRecursion && routineDescriptor != null) { 698 ( (RoutineServerSession) rss).setRoutineCharactersticsInRoutineMap(routineDescriptor.SPECIFIC_NAME, recursiveExecuters); 699 } 700 return refs; 701 } finally { 702 if (!isCreationTime && routineDescriptor != null) { 703 ( (RoutineServerSession) rss).removeRoutineCharactersticsFromRoutineMap(routineDescriptor.SPECIFIC_NAME); 704 } 705 } 706 } 707 708 public String toString() { 709 StringBuffer sb = new StringBuffer (); 710 sb.append(" "); 711 sb.append(_routinename1); 712 sb.append(" "); 713 sb.append(_SQLargumentlist0); 714 return sb.toString(); 715 } 716 717 public Object clone() throws CloneNotSupportedException { 718 routineinvocation tempClass = new routineinvocation(); 719 tempClass._SQLargumentlist0 = (SQLargumentlist) _SQLargumentlist0.clone(); 720 tempClass._routinename1 = (routinename) _routinename1.clone(); 721 return tempClass; 722 } 723 724 public Object executeForFresh(_VariableValues parm1) throws com.daffodilwoods.database.resource.DException { 725 throw new java.lang.UnsupportedOperationException ("Method executeForFresh() not yet implemented."); 726 } 727 728 public Object executeForFresh(Object [] parm1) throws com.daffodilwoods.database.resource.DException { 729 throw new java.lang.UnsupportedOperationException ("Method executeForFresh() not yet implemented."); 730 } 731 732 public AbstractRowValueExpression[] getChilds() { 733 AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {}; 734 return childs; 735 } 736 737 public _Reference[] getReferences(TableDetails tableDetails) throws DException { 738 throw new DException("DSE565", new Object [] {"getReferences()"}); 739 } 740 741 public void setColumnPredicates(_AllColumnPredicates allColumnPredicates) throws DException { 742 throw new DException("DSE565", new Object [] {"setColumnPredicates()"}); 743 } 744 745 764 public ParameterInfo[] getParameterInfo() throws DException { 765 if (_SQLargumentlist0 != null && _SQLargumentlist0.getNumberOfParametersInArgumentList() > 0) { 766 SQLargument[] obj = (SQLargument[]) _SQLargumentlist0.getSqlArguments().toArray(new SQLargument[0]); 767 if (obj == null || obj.length == 0) { 768 return new ParameterInfo[0]; 769 } 770 ArrayList list = new ArrayList(); 771 for (int i = 0; i < obj.length; i++) { 772 ParameterInfo[] parmInfo = obj[i].getParameterInfo(); 773 for (int j = 0; j < parmInfo.length; j++) { 774 if (parmInfo[j].getQuestionMark()) { 775 if (parmInfo[j].getDataType() == -1) { 776 ParametersDescriptor pd = parameters[i]; 777 DataTypeDescriptor dtd = dtdDescriptors[i]; 778 parmInfo[j].setDataType(TypeConverter.getDataBaseType(dtd.data_Type)); 779 parmInfo[j].setName(pd.PARAMETER_NAME); 780 parmInfo[j].setParameterMode(pd.PARAMETER_MODE); 781 } 782 list.add(parmInfo[j]); 783 } 784 } 785 } 786 return (ParameterInfo[]) list.toArray(new ParameterInfo[0]); 787 } 788 return new ParameterInfo[0]; 789 } 790 791 public byte[][] getByte(_VariableValues variableValues) throws DException { 792 throw new UnsupportedOperationException ("Not Supported yet"); 793 } 794 795 public Object getObject(_VariableValues variableValues) throws DException { 796 throw new UnsupportedOperationException ("Not Supported yet"); 797 } 798 799 private void setQualifiedRotuineName(_ServerSession currentSession) throws DException { 800 ROUTINE_CATALOG = _routinename1.getCatalogName(); 801 ROUTINE_SCHEMA = _routinename1.getSchemaName(); 802 ROUTINE_NAME = _routinename1.getRoutineName(); 803 804 if (ROUTINE_CATALOG == null) { 805 ROUTINE_CATALOG = currentSession.getCurrentCatalog(); 806 } 807 if (ROUTINE_SCHEMA == null) { 808 ROUTINE_SCHEMA = currentSession.getCurrentSchema(); 809 } 810 } 811 812 894 895 public void setDefaultValues(_VariableValueOperations variableValueOperations) throws DException { 896 throw new UnsupportedOperationException ("Method Not Implemented Yet"); 897 } 898 899 public boolean checkForSubQuery() throws DException { 900 return false; 901 } 902 903 public int getCardinality() throws DException { 904 return 1; 905 } 906 907 public ByteComparison getByteComparison(Object object) throws DException { 908 int dataType = functionCD.getDatatype(); 909 ByteComparison byteComparison = new ByteComparison(false, new int[] {dataType}); 910 byteComparison.setSize(functionCD.getSize()); 911 return byteComparison; 912 } 913 914 915 917 public _Reference[] getReferences(TableDetails[] tableDetails) throws DException { 918 if (refs != null) { 919 ArrayList argumentsList = new ArrayList(); 920 SQLargument[] args = _SQLargumentlist0._OptSQLargumentOptRepScomma94843605SQLargument0._OptRepScomma94843605SQLargument0; 921 for (int i = 0; i < args.length; i++) { 922 _Reference[] argRef = args[i].getReferences(tableDetails); 923 if (argRef != null) 924 argumentsList.addAll(Arrays.asList(argRef)); 925 } 926 _Reference[] ref = new _Reference[argumentsList.size()]; 927 argumentsList.toArray(ref); 928 return ref; 929 } 930 return null; 931 } 932 933 public ColumnDetails[] getColumnDetails() throws DException { 934 if (columnDetails != null) 935 return columnDetails; 936 functionCD = new UserFunctionColumnDetails(); 937 functionCD.setObject(this); 938 functionCD.setType(USERFUNCTION); 939 functionCD.setColumnName(new String [] {toString()}); 940 columnDetails = new ColumnDetails[] {functionCD}; 941 return new ColumnDetails[] {functionCD}; 942 } 943 944 public ColumnDetails[] getChildColumnDetails() throws DException { 945 return _SQLargumentlist0.getColumnDetails(); 946 } 947 948 950 951 public void getColumnsIncluded(ArrayList aList) throws DException { 952 } 953 954 956 957 public void getTablesIncluded(ArrayList aList) throws DException { 958 } 959 960 public String getUserFunctionReturnType() throws DException { 961 return getFunctionReturnType(currentSession); 962 } 963 964 public String getFunctionReturnType(_ServerSession currentSession) throws DException { 965 if (routineDescriptor.ROUTINE_TYPE.equalsIgnoreCase(SqlKeywords.FUNCTION)) { 966 String queryClause = " SELECT data_type from " + 967 SystemTables.dataTypeDescriptor_TableName + " where dtd_identifier='dummy' and object_catalog= '" + 968 routineDescriptor.SPECIFIC_CATALOG + "' and object_schema= '" + routineDescriptor.SPECIFIC_SCHEMA 969 + "' and object_name= '" + routineDescriptor.SPECIFIC_NAME + "'"; 970 _SelectQueryIterator retIter = SqlSchemaConstants.getIterator(currentSession, queryClause, null); 971 if (retIter.first()) { 972 Object [] obj = (Object []) retIter.getColumnValues(); 973 return obj[0].toString(); 974 } 975 } 976 return null; 977 } 978 979 private DataTypeDescriptor getDataTypeDescriptor(_ServerSession currentSession) throws DException { 980 DataTypeDescriptor dt = new DataTypeDescriptor(); 981 dt.object_catalog = routineDescriptor.SPECIFIC_CATALOG; 982 dt.object_schema = routineDescriptor.SPECIFIC_SCHEMA; 983 dt.object_name = routineDescriptor.SPECIFIC_NAME; 984 dt.object_type = SqlKeywords.ROUTINE; 985 dt.dtd_identifier = "dummy"; 986 dt.load(currentSession); 987 return dt; 988 } 989 990 public String getRoutineType() throws DException { 991 return routineDescriptor.ROUTINE_TYPE; 992 } 993 994 public Object makeResultObject(Object [] values, Object [] results) throws DException { 995 Object [] result = getResult(results); 996 if (parameters != null) { 997 if (external_method == null) { 998 Object [] outputValues = new Object [parameters.length]; 999 for (int i = 0; i < parameters.length; i++) { 1000 if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.IN)) { 1001 outputValues[i] = null; 1002 } else { 1003 outputValues[i] = ( (FieldBase) values[i]).getObject(); 1004 } 1005 } 1006 if (routineDescriptor.ROUTINE_TYPE.equalsIgnoreCase(SqlKeywords.FUNCTION)) { 1007 Object [] rs = (Object []) results[results.length - 1]; 1008 return new CallResult( ( (FieldBase) rs[rs.length - 1]).getObject(), null); 1009 } else { 1010 return new CallResult(result, outputValues); 1011 } 1012 } else { 1013 Object [] outputValuesEx = new Object [parameters.length]; 1014 for (int i = 0; i < parameters.length; i++) { 1015 if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.IN)) { 1016 outputValuesEx[i] = null; 1017 } else if (parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.OUT) || parameters[i].PARAMETER_MODE.equalsIgnoreCase(SqlKeywords.INOUT)) { 1018 outputValuesEx[i] = ( (javaParameter) values[i]).getObject(); 1019 } 1020 } 1021 return new CallResult(null, outputValuesEx); 1022 } 1023 } else { 1024 if (external_method == null) { 1025 if (routineDescriptor.ROUTINE_TYPE.equalsIgnoreCase(SqlKeywords.FUNCTION)) { 1026 Object [] rs = (Object []) results[results.length - 1]; 1027 return new CallResult( ( (FieldBase) rs[rs.length - 1]).getObject(), null); 1028 } else { 1029 return new CallResult(result, null); 1030 } 1031 } else { 1032 return new CallResult(null, null); 1033 } 1034 } 1035 } 1036 1037 private boolean isSameRoutine(_ServerSession object, int numberOfParametersInParameterList, String routine_catalog, String routine_schema, String routine_name, ColumnDetails[] routineColumnDetails) throws DException { 1038 RoutineServerSession routineServerSession = (RoutineServerSession) object; 1039 String routineCatalog = routineServerSession.getRoutineCatalog(); 1040 String routineSchema = routineServerSession.getRoutineSchema(); 1041 String routineName = routineServerSession.getRoutineName(); 1042 int noOfParameters = routineServerSession.noOfParameters; 1043 Object [] parameterTypes = routineServerSession.parameterTypes; 1044 if (noOfParameters != numberOfParametersInParameterList) { 1045 return false; 1046 } else { 1047 if (routineCatalog.equalsIgnoreCase(routine_catalog) && routineSchema.equalsIgnoreCase(routine_schema) && routineName.equalsIgnoreCase(routine_name)) { 1048 if (numberOfParametersInParameterList > 0) 1049 checkDatatype(routineColumnDetails, parameterTypes, routine_name); 1050 return true; 1051 } 1052 return false; 1053 } 1054 } 1055 1056 private boolean checkDatatype(ColumnDetails[] routineColumnDetails, Object [] parameterTypes, String routine_name) throws DException { 1057 ArrayList dataTypes = new ArrayList(); 1058 int[] callParameters = new int[routineColumnDetails.length]; 1059 int[] createParameters = new int[parameterTypes.length]; 1060 for (int i = 0; i < routineColumnDetails.length; i++) { 1061 callParameters[i] = routineColumnDetails[i].getDatatype(); 1062 } 1063 for (int j = 0; j < parameterTypes.length; j++) { 1064 createParameters[j] = TypeConverter.getDataBaseType(parameterTypes[j].toString().trim()); 1065 } 1066 int m = 0; 1067 for (int k = 0; k < createParameters.length; k++) { 1068 while (m < callParameters.length) { 1069 try { 1070 dataTypes.add(TypeConverter.getDataBaseTypeName(callParameters[m])); 1071 Check.checkForUserDefinedFunctionDatatype(createParameters[k], callParameters[m]); 1072 } catch (DException ex) { 1073 if (ex.getDseCode().equalsIgnoreCase("DSE87")) { 1074 throw new DException("DSE728", new Object [] {routine_name, dataTypes.toString()}); 1075 } 1076 throw ex; 1077 } 1078 m++; 1079 break; 1080 } 1081 } 1082 return true; 1083 } 1084 1085 private void intializeExecuterAndReferences(_ServerSession currentSession, RoutineDescriptor subjectRoutine) throws DException { 1086 String mainquery = subjectRoutine.ROUTINE_DEFINITION; 1087 SQL_invokedroutine queryObject = (SQL_invokedroutine) Parser.parseQuery(mainquery); 1088 statements = queryObject.getStatements(); 1089 executers = new _Executer[statements.length]; 1090 statementReferences = new _Reference[statements.length][]; 1091 1092 ColumnDetails[] statementCD = null; 1093 ArrayList statementsCD = new ArrayList(); 1094 1095 for (int i = 0; i < statements.length; i++) { 1096 SQLprocedurestatement sqlp = (SQLprocedurestatement) statements[i]; 1097 SQLexecutablestatement sqlexe = sqlp.getExecutableStatement(); 1098 if (sqlexe instanceof SQLdatastatement) { 1099 if (currentSession.getUserSession().getTransactionAccessMode().toString().equalsIgnoreCase("Read Only")) 1100 throw new DException("DSE1184", (Object [])null); 1101 SQLdatastatement dataStt = (SQLdatastatement) sqlexe; 1102 _Reference[] refs = dataStt.checkSemantic(currentSession); 1103 statementReferences[i] = refs; 1104 } else if (sqlexe instanceof SQLcontrolstatement) { 1105 SQLcontrolstatement contStt = (SQLcontrolstatement) sqlexe; 1106 if (contStt instanceof compoundstatement) 1107 ( (compoundstatement) contStt).setServerSession(currentSession); 1108 statementCD = contStt.getColumnDetails(); 1109 if (statementCD != null) 1110 statementsCD.addAll(Arrays.asList(statementCD)); 1111 1112 ArrayList refList = new ArrayList(); 1113 ColumnDetails[] statementCDs = (ColumnDetails[]) statementsCD.toArray(new ColumnDetails[0]); 1114 GeneralPurposeStaticClass.addRecursively(statementCDs, refList); 1115 for (int k = 0; k < refList.size(); k++) { 1116 ColumnDetails cd = (ColumnDetails) refList.get(k); 1117 if (parameters != null) { 1118 for (int j = 0; j < parameters.length; j++) { 1119 if (cd.getQualifiedColumnName().equalsIgnoreCase(parameters[j].PARAMETER_NAME)) { 1120 cd.setDatatype(dtdDescriptors[j].getType()); 1121 cd.setSize(dtdDescriptors[j].getPrecision()); 1122 break; 1123 } 1124 } 1125 } 1126 } 1127 1128 _Reference[] refs = contStt.checkSemantic(currentSession); 1129 if (refs != null) { 1130 for (int k = 0; k < refs.length; k++) { 1131 boolean flag = false; 1132 if (parameters != null) { 1133 for (int j = 0; j < parameters.length; j++) { 1134 if (refs[k].getQualifiedColumnName().equalsIgnoreCase(parameters[j].PARAMETER_NAME)) { 1135 refs[k].setDatatype(dtdDescriptors[j].getType()); 1136 refs[k].setSize(dtdDescriptors[j].getPrecision()); 1137 flag = true; 1138 } 1139 if (flag) 1140 break; 1141 } 1142 } 1143 } 1144 } 1145 statementReferences[i] = refs; 1146 } 1147 } 1148 } 1149 1150 private Object [] getResult(Object fieldbase) throws DException { 1151 if (fieldbase != null) { 1152 Object [] result = (Object []) fieldbase; 1153 Object [] newResult = new Object [result.length]; 1154 for (int i = 0; i < result.length; i++) { 1155 if (result[i] instanceof Object []) { 1156 newResult[i] = getResult( (Object []) result[i]); 1157 } else { 1158 newResult[i] = (result[i] instanceof FieldBase ? ( (FieldBase) result[i]).getObject() : result[i]); 1159 } 1160 } 1161 return newResult; 1162 } 1163 return null; 1164 } 1165} 1166 | Popular Tags |