1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.serversystem.*; 6 import com.daffodilwoods.daffodildb.server.sql99.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dcl.sqlcontrolstatement.*; 9 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 10 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dml.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 13 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 14 import com.daffodilwoods.daffodildb.server.sql99.token.*; 15 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 16 import com.daffodilwoods.database.resource.*; 17 18 public class SQL_invokedprocedure { 19 public routinebody _routinebody0; 20 public routinecharacteristics _routinecharacteristics1; 21 public SQLparameterdeclarationlist _SQLparameterdeclarationlist2; 22 public schemaqualifiedroutinename _schemaqualifiedroutinename3; 23 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439224; 24 25 private SchemaDescriptor schemaDescriptor; 26 private DataTypeDescriptor[] dtd = null; 27 28 public void setSchemaDescriptor(SchemaDescriptor schemaDes) { 29 schemaDescriptor = schemaDes; 30 } 31 32 private Object [] routineCharacteristics; 33 34 43 public Object run(Object object) throws DException { 44 _ServerSession currentSession = (_ServerSession) object; 45 boolean isIndependentStt = schemaDescriptor == null; 46 RoutineDescriptor routineDescriptor = new RoutineDescriptor(); 47 setQualifiedRotuineName(currentSession, routineDescriptor); 48 if (isIndependentStt) { 49 validateUserPrivilege(currentSession); 50 } 51 52 checkNoOfParameters(currentSession, routineDescriptor); 53 checkSetRoutineCharacterstics(currentSession, routineDescriptor); 54 setParametersAndDtdDescriptor(currentSession, routineDescriptor); 55 if (_routinebody0 instanceof externaljavareference) { 56 if (_Server.ISONEDOLLARDB) 57 throw new DException("DSE2054", new Object [] {"External java procedure"}); 58 externaljavareference dataStt = (externaljavareference) _routinebody0; 59 dataStt.setRoutineDescriptor(routineDescriptor); 60 dataStt.run(currentSession); 61 checkForParameters(dataStt, routineDescriptor); 62 } else { 63 checkSatements(currentSession, routineDescriptor); 64 } 65 setRoutineDescriptor(currentSession, routineDescriptor); 66 createPrivileges(currentSession, routineDescriptor); 67 if (currentSession.getDbVersion() >= 4.0) { 68 if (! (_routinebody0 instanceof externaljavareference)) 69 storeTableIncludedColumnIncluded(routineDescriptor); 70 } 71 return null; 72 } 73 74 private void checkForParameters(externaljavareference dataStt, RoutineDescriptor routineDescriptor) throws DException { 75 int noOfParameters = _SQLparameterdeclarationlist2.getNumberOfParameters(); 76 77 int noOfJavaParameters = dataStt.getJavaParameters(); 78 79 if (noOfParameters != noOfJavaParameters) 80 throw new DException("DSE8123", null); 81 82 String [] parameterModes = _SQLparameterdeclarationlist2.getModeOfParameters(); 83 84 if (noOfJavaParameters > 0) { 85 Class [] param = dataStt.getJavaParametersClass(); 86 87 if (param != null) { 88 String external = routineDescriptor.EXTERNAL_NAME; 89 String methodIdentifier = external.substring(external.indexOf("(") + 1, external.indexOf(")")); 90 StringTokenizer token = new StringTokenizer(methodIdentifier, ","); 91 String str[] = new String [param.length]; 92 int k = 0; 93 while (token.hasMoreTokens()) { 94 str[k++] = token.nextToken(); 95 } 96 97 for (int i = 0; i < param.length; i++) { 98 if (parameterModes[i].equalsIgnoreCase("IN")) { 99 Object javaMethodParametersType = (Object ) param[i]; 100 Object parametersTyp = null; 101 if ( (dtd[i].data_Type.equalsIgnoreCase("char") && dtd[i].getPrecision() > 1) || 102 (dtd[i].data_Type.equalsIgnoreCase("character") && dtd[i].getPrecision() > 1)|| 103 (dtd[i].data_Type.equalsIgnoreCase("varchar") && dtd[i].getPrecision() > 1)|| 104 (dtd[i].data_Type.equalsIgnoreCase("varchar2") && dtd[i].getPrecision() > 1)) { 105 parametersTyp = String .class; 106 } else { 107 parametersTyp = GeneralUtility.getJavaDataType(dtd[i].data_Type); 108 } 109 if (! (javaMethodParametersType.equals(parametersTyp))) 110 throw new DException("DSE8124", new Object [] {new Integer (i + 1)}); 111 } else if (parameterModes[i].equalsIgnoreCase("INOUT") || 112 parameterModes[i].equalsIgnoreCase("OUT")) { 113 if (! (str[i].equalsIgnoreCase("JavaParameter"))) 114 throw new DException("DSE8124", new Object [] {new Integer (i + 1)}); 115 } 116 } 117 } 118 } 119 } 120 121 private void setQualifiedRotuineName(_ServerSession currentSession, 122 RoutineDescriptor routineDescriptor) throws 123 DException { 124 routineDescriptor.ROUTINE_CATALOG = _schemaqualifiedroutinename3.getCatalogName(); 125 routineDescriptor.ROUTINE_SCHEMA = _schemaqualifiedroutinename3.getSchemaName(); 126 routineDescriptor.ROUTINE_NAME = _schemaqualifiedroutinename3.getIdentifierName(); 127 128 131 if (schemaDescriptor != null) { 132 if (routineDescriptor.ROUTINE_SCHEMA == null) { 133 routineDescriptor.ROUTINE_SCHEMA = schemaDescriptor.schema_name; 134 } else 135 if (! (routineDescriptor.ROUTINE_SCHEMA.equalsIgnoreCase(schemaDescriptor.schema_name))) { 136 throw new DException("DSE895", null); 137 } 138 if (routineDescriptor.ROUTINE_CATALOG == null) { 139 routineDescriptor.ROUTINE_CATALOG = schemaDescriptor.catalog_name; 140 } else 141 if (! (routineDescriptor.ROUTINE_CATALOG.equalsIgnoreCase(schemaDescriptor.catalog_name))) { 142 throw new DException("DSE895", null); 143 } 144 } else { 145 if (routineDescriptor.ROUTINE_CATALOG == null) { 146 routineDescriptor.ROUTINE_CATALOG = currentSession.getCurrentCatalog(); 147 } 148 if (routineDescriptor.ROUTINE_SCHEMA == null) { 149 routineDescriptor.ROUTINE_SCHEMA = currentSession.getCurrentSchema(); 150 } 151 schemaDescriptor = new SchemaDescriptor(); 152 schemaDescriptor.catalog_name = routineDescriptor.ROUTINE_CATALOG; 153 schemaDescriptor.schema_name = routineDescriptor.ROUTINE_SCHEMA; 154 schemaDescriptor.load(currentSession); 155 } 156 } 157 158 private void checkSetRoutineCharacterstics(_ServerSession serverSession, 159 RoutineDescriptor routineDescriptor) throws DException { 160 routineCharacteristics = _routinecharacteristics1.getAllRoutineCharacteristics(); 161 if (routineCharacteristics[2] == null) { 162 routineDescriptor.SPECIFIC_NAME = serverSession.getDataDictionary().generateSpecificName(); 163 routineDescriptor.SPECIFIC_CATALOG = routineDescriptor.ROUTINE_CATALOG; 164 routineDescriptor.SPECIFIC_SCHEMA = routineDescriptor.ROUTINE_SCHEMA; 165 } else { 166 SRESERVEDWORD1206543922specificname temp = (SRESERVEDWORD1206543922specificname) routineCharacteristics[2]; 167 String tempSchema = temp.getSchemaName(); 168 String tempCatalog = temp.getCatalogName(); 169 routineDescriptor.SPECIFIC_CATALOG = tempCatalog == null ? 170 routineDescriptor.ROUTINE_CATALOG : tempCatalog; 171 routineDescriptor.SPECIFIC_SCHEMA = tempSchema == null ? 172 routineDescriptor.ROUTINE_SCHEMA : tempSchema; 173 routineDescriptor.SPECIFIC_NAME = temp.getIdentifierName(); 174 checkCatalogSchemaOfRoutineAndSpecificName(routineDescriptor); 175 } 176 } 177 178 private void setRoutineDescriptor(_ServerSession serverSession, 179 RoutineDescriptor routineDescriptor) throws DException { 180 182 routineDescriptor.ROUTINE_NAME = _schemaqualifiedroutinename3.getIdentifierName(); 183 routineDescriptor.ROUTINE_TYPE = SqlKeywords.PROCEDURE; 184 routineDescriptor.ROUTINE_DEFINITION = "CREATE " + toString(); 185 routineDescriptor.SCHEMA_LEVEL_ROUTINE = SqlSchemaConstants.YES; 186 routineDescriptor.IS_USER_DEFINED_CAST = SqlKeywords.NO; 187 routineDescriptor.SECURITY_TYPE = SqlKeywords.IMPLEMENTATION + " " + SqlKeywords.DEFINED; 188 routineDescriptor.AS_LOCATOR = SqlSchemaConstants.NO; 189 java.sql.Timestamp time = new java.sql.Timestamp (System.currentTimeMillis()); 190 routineDescriptor.CREATED = time; 191 routineDescriptor.LAST_ALTERED = time; 192 if (routineCharacteristics != null) { 193 if (! (_routinebody0 instanceof SQLroutinebody)) { 194 routineDescriptor.PARAMETER_STYLE = routineCharacteristics[1].toString(); 195 } 196 routineDescriptor.IS_DETERMINISTIC = routineCharacteristics[3].toString(); 197 routineDescriptor.SQL_DATA_ACCESS = routineCharacteristics[4].toString(); 198 } 199 if (_routinebody0 instanceof SQLroutinebody) 200 routineDescriptor.ROUTINE_BODY = SqlKeywords.SQL; 201 else if (_routinebody0 instanceof externaljavareference) 202 routineDescriptor.ROUTINE_BODY = SqlKeywords.EXTERNAL; 203 routineDescriptor.save(serverSession); 204 } 205 206 private void setParametersAndDtdDescriptor(_ServerSession serverSession, RoutineDescriptor routineDescriptor) throws 207 DException { 208 dtd = _SQLparameterdeclarationlist2.storeParameteresForProcedure( 209 new Object [] {routineDescriptor.SPECIFIC_CATALOG, 210 routineDescriptor.SPECIFIC_SCHEMA, 211 routineDescriptor.SPECIFIC_NAME} 212 , serverSession); 213 } 214 215 private void checkSatements(_ServerSession serverSession, RoutineDescriptor routineDescriptor) throws DException { 216 SQLprocedurestatement[] sqlstatements = _routinebody0.getStatements(); 217 String [] parameterNames = _SQLparameterdeclarationlist2.getNameOfParameters(); 218 219 int noOfParameters = _SQLparameterdeclarationlist2.getNumberOfParameters(); 220 Object [] parameterTypes = _SQLparameterdeclarationlist2.getDataTypeOfParameters(); 221 RoutineServerSession routineServerSession = new RoutineServerSession(SqlKeywords.PROCEDURE, routineDescriptor.ROUTINE_CATALOG, 222 routineDescriptor.ROUTINE_SCHEMA, routineDescriptor.ROUTINE_NAME, noOfParameters, parameterTypes, serverSession, true); 223 224 ColumnDetails[] statementCD = null; 225 ArrayList statementsCD = new ArrayList(); 226 for (int i = 0; i < sqlstatements.length; i++) { 227 SQLprocedurestatement sqlp = (SQLprocedurestatement) sqlstatements[i]; 228 SQLexecutablestatement sqlexe = sqlp.getExecutableStatement(); 229 if (sqlexe instanceof SQLcontrolstatement) { 230 SQLcontrolstatement contStt = (SQLcontrolstatement) sqlexe; 231 statementCD = contStt.getColumnDetails(); 232 if (statementCD != null) 233 statementsCD.addAll(Arrays.asList(statementCD)); 234 } 235 } 236 237 for (int i = 0; i < sqlstatements.length; i++) { 238 SQLexecutablestatement sql_exec = sqlstatements[i].getExecutableStatement(); 239 if (sql_exec instanceof SQLdatastatement) { 240 SQLdatastatement dataStt = (SQLdatastatement) sql_exec; 241 _Reference[] refs = dataStt.checkSemantic(serverSession); 242 243 Object [] parameterRefs = dataStt.getParameters(serverSession); 244 if (parameterRefs != null && parameterRefs.length != 0) { 245 throw new DException("DSE8164", null); 246 } 247 248 boolean refMatchWithParameter; 249 if (refs != null && refs.length != 0) { 250 for (int j = 0; j < refs.length; j++) { 251 refMatchWithParameter = false; 252 if (parameterNames != null) { 253 for (int k = 0; k < parameterNames.length; k++) { 254 if ( (refs[j].getColumn().equalsIgnoreCase(parameterNames[k]))) { 255 refMatchWithParameter = true; 256 break; 257 } 258 } 259 } 260 if (!refMatchWithParameter) { 261 throw new DException("DSE8175", new Object [] {refs[j].getColumn()}); 262 } 263 } 264 } } else if (sql_exec instanceof SQLcontrolstatement) { 266 if (_Server.ISONEDOLLARDB) 267 throw new DException("DSE2054", new Object [] {"Psm in procedure"}); 268 SQLcontrolstatement dataStt = (SQLcontrolstatement) sql_exec; 269 270 ArrayList refList = new ArrayList(); 271 ColumnDetails[] statementCDs = (ColumnDetails[]) statementsCD.toArray(new ColumnDetails[0]); 272 GeneralPurposeStaticClass.addRecursively(statementCDs, refList); 273 for (int k = 0; k < refList.size(); k++) { 274 ColumnDetails cd = (ColumnDetails) refList.get(k); 275 if (parameterNames != null) { 276 for (int j = 0; j < parameterNames.length; j++) { 277 if (cd.getQualifiedColumnName().equalsIgnoreCase(parameterNames[j])) { 278 cd.setDatatype(dtd[j].getType()); 279 cd.setSize(dtd[j].getPrecision()); 280 break; 281 } 282 } 283 } 284 } 285 286 if (dataStt instanceof compoundstatement) { 287 if (noOfParameters != 0) { 288 ArrayList parameterList = new ArrayList(); 289 for (int j = 0; j < parameterNames.length; j++) { 290 parameterList.add(parameterNames[j]); 291 } 292 compoundstatement compStatement = (compoundstatement) dataStt; 293 compStatement.setParameterInDeclarationList(parameterList); 294 } 295 } 296 297 _Reference[] refs = dataStt.checkSemantic(routineServerSession); 298 if (refs != null) { 299 for (int k = 0; k < refs.length; k++) { 300 boolean flag = false; 301 if (parameterNames != null) { 302 for (int j = 0; j < parameterNames.length; j++) { 303 if (refs[k].getQualifiedColumnName().equalsIgnoreCase(parameterNames[j])) { 304 refs[k].setDatatype(dtd[j].getType()); 305 refs[k].setSize(dtd[j].getPrecision()); 306 flag = true; 307 } 308 if (flag) 309 break; 310 } 311 } 312 } 313 } 314 315 boolean refMatchWithParameter; 316 if (refs != null && refs.length != 0) { 317 for (int j = 0; j < refs.length; j++) { 318 refMatchWithParameter = false; 319 if (parameterNames != null) { 320 for (int k = 0; k < parameterNames.length; k++) { 321 if ( (refs[j].getColumn().equalsIgnoreCase(parameterNames[k]))) { 322 refMatchWithParameter = true; 323 break; 324 } 325 } 326 } 327 if (!refMatchWithParameter) { 328 throw new DException("DSE8175", new Object [] {refs[j].getQualifiedColumnName()}); 329 } 330 } 331 } 333 Object [] parameterRefs = dataStt.getParameters(serverSession); 334 if (parameterRefs != null && parameterRefs.length != 0) { 335 throw new DException("DSE8164", null); 336 } 337 338 } else { 339 throw new DException("DSE8189", new Object [] {sql_exec.toString()}); 340 } 341 } 342 } 343 344 360 private void createPrivileges(_ServerSession serverSession, 361 RoutineDescriptor routineDescriptor) throws 362 DException { 363 String grantee = schemaDescriptor.schema_owner; 364 String grantQuery = QueryMaker.getRoutineDefinitionGrantQuery( 365 routineDescriptor.getQualifiedSpecificName(), 366 grantee, isPrivilegeGrantable(serverSession)); 367 grantprivilegestatement _grantprivilegestatement = (grantprivilegestatement) 368 com.daffodilwoods.daffodildb.utils.parser.Parser.parseQuery(grantQuery); 369 _grantprivilegestatement.setObjectDescriptor(routineDescriptor); 370 _grantprivilegestatement.run(serverSession.getSystemServerSession()); 371 } 372 373 public SQLprocedurestatement[] getStatements() throws DException { 374 return _routinebody0.getStatements(); 375 } 376 377 private void checkNoOfParameters(_ServerSession serverSession, 378 RoutineDescriptor routineDescriptor) throws 379 DException { 380 int noOfParameters = _SQLparameterdeclarationlist2.getNumberOfParameters(); 381 382 ArrayList list = QueryMaker.getSpecificCorrespondingToProcedureName( 383 serverSession, routineDescriptor.ROUTINE_CATALOG, 384 routineDescriptor.ROUTINE_SCHEMA, routineDescriptor.ROUTINE_NAME); 385 _Executer executer = SqlSchemaConstants.getExecuter(serverSession, 386 QueryMaker.getParameterCountQuery()); 387 if (list != null) { 388 for (int i = 0, size = list.size(); i < size; i++) { 389 Object [] obj = (Object []) list.get(i); 390 _SelectQueryIterator iter = (_SelectQueryIterator) executer.executeForFresh(obj); 391 if (iter.first()) { 392 if ( ( ( (Object []) iter.getObject())[0]).hashCode() == 393 noOfParameters) { 394 throw new DException("DSE5024", 395 new Object [] {routineDescriptor.ROUTINE_CATALOG + 396 "." + routineDescriptor.ROUTINE_SCHEMA + "." + 397 routineDescriptor.ROUTINE_NAME, 398 routineDescriptor.ROUTINE_CATALOG + "." + 399 routineDescriptor.ROUTINE_SCHEMA + "." + 400 routineDescriptor.SPECIFIC_NAME}); 401 } 402 } else { 403 if (noOfParameters == 0) { 404 throw new DException("DSE5024", 405 new Object [] {routineDescriptor.ROUTINE_CATALOG + 406 "." + routineDescriptor.ROUTINE_SCHEMA + "." + 407 routineDescriptor.ROUTINE_NAME, 408 routineDescriptor.ROUTINE_CATALOG + "." + 409 routineDescriptor.ROUTINE_SCHEMA + "." + 410 routineDescriptor.SPECIFIC_NAME}); 411 } 412 } 413 } 414 } 415 } 416 417 private void checkCatalogSchemaOfRoutineAndSpecificName(RoutineDescriptor routDes) throws DException { 418 if (!routDes.SPECIFIC_CATALOG.equalsIgnoreCase(routDes.ROUTINE_CATALOG)) { 419 throw new DException("DSE5025", null); 420 } 421 if (!routDes.SPECIFIC_SCHEMA.equalsIgnoreCase(routDes.ROUTINE_SCHEMA)) { 422 throw new DException("DSE5025", null); 423 } 424 } 425 426 private void validateUserPrivilege(_ServerSession currentSession) throws DException { 427 String schemaOwner = schemaDescriptor.schema_owner; 428 if (!currentSession.isEnabledAuthorizationIdentifier(schemaOwner, true)) { 429 ; throw new DException("DSE12", null); 431 } 432 } 433 434 435 private boolean isPrivilegeGrantable(_ServerSession user_session) throws 436 DException { 437 return true; 438 } 439 440 441 private void storeTableIncludedColumnIncluded(RoutineDescriptor routineDescriptor) throws DException { 442 SQLprocedurestatement[] sqlstatements = _routinebody0.getStatements(); 443 storeTablesIncluded(sqlstatements, routineDescriptor); 444 storeColumnsIncluded(sqlstatements, routineDescriptor); 445 } 446 447 private void storeTablesIncluded(SQLprocedurestatement[] sqlstatements, RoutineDescriptor routineDescriptor) throws 448 DException { 449 ArrayList tablesIncluded = new ArrayList(); 450 if (sqlstatements != null && sqlstatements.length > 0) { 451 for (int i = 0; i < sqlstatements.length; i++) { 452 SQLexecutablestatement sql_exec = sqlstatements[i].getExecutableStatement(); 453 if (sql_exec instanceof SQLdatastatement || sql_exec instanceof SQLcontrolstatement) { 454 455 } 456 } 457 } 458 ArrayList tablesList = GeneralUtility.removeRedundentTables(routineDescriptor.SPECIFIC_CATALOG, 459 routineDescriptor.SPECIFIC_SCHEMA, tablesIncluded); 460 routineDescriptor.tablesUsed = tablesList; 461 } 462 463 private void storeColumnsIncluded(SQLprocedurestatement[] sqlstatements, RoutineDescriptor routineDescriptor) throws 464 DException { 465 ArrayList columnsIncluded = new ArrayList(); 466 if (sqlstatements != null && sqlstatements.length > 0) { 467 for (int i = 0; i < sqlstatements.length; i++) { 468 SQLexecutablestatement sql_exec = sqlstatements[i].getExecutableStatement(); 469 if (sql_exec instanceof SQLdatastatement || sql_exec instanceof SQLcontrolstatement) { 470 471 } 472 } 473 } 474 ArrayList columnsList = GeneralUtility.removeRedundentColumns( 475 routineDescriptor.SPECIFIC_CATALOG, routineDescriptor.SPECIFIC_SCHEMA, null, 476 columnsIncluded); 477 routineDescriptor.columnsUsed = columnsList; 478 } 479 480 public Object clone() throws CloneNotSupportedException { 481 return this; 482 } 483 484 public String toString() { 485 StringBuffer sb = new StringBuffer (); 486 sb.append(" "); 487 sb.append(_SRESERVEDWORD12065439224); 488 sb.append(" "); 489 sb.append(_schemaqualifiedroutinename3); 490 sb.append(" "); 491 sb.append(_SQLparameterdeclarationlist2); 492 sb.append(" "); 493 sb.append(_routinecharacteristics1); 494 sb.append(" "); 495 sb.append(_routinebody0); 496 return sb.toString(); 497 } 498 } 499 | Popular Tags |