1 23 24 package org.continuent.sequoia.controller.requests; 25 26 import java.sql.SQLException ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.SortedMap ; 31 import java.util.TreeMap ; 32 import java.util.regex.Matcher ; 33 import java.util.regex.Pattern ; 34 35 import org.continuent.sequoia.common.i18n.Translate; 36 import org.continuent.sequoia.controller.semantic.SemanticBehavior; 37 import org.continuent.sequoia.controller.sql.schema.DatabaseProcedure; 38 import org.continuent.sequoia.controller.sql.schema.DatabaseSchema; 39 40 56 public class StoredProcedure extends AbstractRequest 57 { 58 private static final long serialVersionUID = 5360592917986126504L; 59 60 61 private transient boolean blocking = true; 62 63 64 protected int nbOfParameters; 65 66 69 private HashMap namedParameterValues = null; 70 71 74 private List namedParameterNames = null; 75 76 79 private SortedMap outParameterValues = null; 80 81 84 private List outParameterIndexes = null; 85 86 protected String procedureKey = null; 87 88 private String procedureName = ""; 89 90 protected boolean altersAggregateList = true; 93 protected boolean altersDatabaseCatalog = true; 94 protected boolean altersDatabaseSchema = true; 95 protected boolean altersMetadataCache = true; 96 protected boolean altersQueryResultCache = true; 97 protected boolean altersSomething = true; 98 protected boolean altersStoredProcedureList = true; 99 protected boolean altersUserDefinedTypes = true; 100 protected boolean altersUsers = true; 101 102 private static final String STORED_PROCEDURE_PATTERN_STRING = "^((execute\\s+([^(\\s|\\()]+)(.*)?)|(\\{(\\s*\\?\\s*=)?\\s*call\\s+([^(\\s|\\()]+)(.*)?\\})|(\\s*call\\s+([^(\\s|\\()]+)(.*)?))"; 104 105 private static final Pattern STORED_PROCEDURE_PATTERN = Pattern 106 .compile( 107 STORED_PROCEDURE_PATTERN_STRING, 108 Pattern.CASE_INSENSITIVE 109 | Pattern.DOTALL); 110 111 private static int posSyntax1 = 2; 112 private static int posSyntax2 = 5; 113 private static int posSyntax3 = 10; 114 115 124 public StoredProcedure(String sqlQuery, boolean escapeProcessing, 125 int timeout, String lineSeparator) 126 { 127 super(sqlQuery, escapeProcessing, timeout, lineSeparator, 128 RequestType.STORED_PROCEDURE); 129 } 130 131 134 public boolean altersAggregateList() 135 { 136 return altersAggregateList; 137 } 138 139 142 public boolean altersDatabaseCatalog() 143 { 144 return altersDatabaseCatalog; 145 } 146 147 150 public boolean altersDatabaseSchema() 151 { 152 return (getSemantic() == null ? altersDatabaseSchema : getSemantic() 153 .altersDatabaseSchema()); 154 } 155 156 159 public boolean altersMetadataCache() 160 { 161 return (getSemantic() == null ? altersMetadataCache : getSemantic() 162 .altersMetadataCache()); 163 } 164 165 168 public boolean altersQueryResultCache() 169 { 170 return (getSemantic() == null ? altersQueryResultCache : getSemantic() 171 .altersQueryResultCache()); 172 } 173 174 177 public boolean altersSomething() 178 { 179 return altersSomething; 180 } 181 182 185 public boolean altersStoredProcedureList() 186 { 187 return altersStoredProcedureList; 188 } 189 190 193 public boolean altersUserDefinedTypes() 194 { 195 return altersUserDefinedTypes; 196 } 197 198 201 public boolean altersUsers() 202 { 203 return (getSemantic() == null ? altersUsers : getSemantic().altersUsers()); 204 } 205 206 210 public boolean needsMacroProcessing() 211 { 212 return (getSemantic() == null ? true : getSemantic().needsMacroProcessing()); 213 } 214 215 221 public Object getNamedParameterValue(String name) 222 { 223 return namedParameterValues.get(name); 224 } 225 226 232 public List getNamedParameterNames() 233 { 234 return namedParameterNames; 235 } 236 237 242 public final int getNbOfParameters() 243 { 244 return nbOfParameters; 245 } 246 247 253 public Object getOutParameterValue(Object idx) 254 { 255 return outParameterValues.get(idx); 256 } 257 258 263 public List getOutParameterIndexes() 264 { 265 return outParameterIndexes; 266 } 267 268 274 public String getProcedureKey() 275 { 276 if (procedureKey == null) 277 try 278 { 279 parse(null, 0, true); 280 } 281 catch (SQLException e) 282 { 283 return null; 284 } 285 return procedureKey; 286 } 287 288 293 public String getProcedureName() 294 { 295 return procedureName; 296 } 297 298 303 public boolean mightBlock() 304 { 305 return blocking; 306 } 307 308 313 public void setBlocking(boolean blocking) 314 { 315 this.blocking = blocking; 316 } 317 318 324 public synchronized void setNamedParameterValue(String paramName, Object val) 325 { 326 if (namedParameterValues == null) 327 namedParameterValues = new HashMap (); 328 namedParameterValues.put(paramName, val); 329 } 330 331 336 public synchronized void setNamedParameterName(String paramName) 337 { 338 if (namedParameterNames == null) 339 namedParameterNames = new ArrayList (); 340 namedParameterNames.add(paramName); 341 } 342 343 349 public synchronized void setOutParameterValue(Object paramIdx, Object val) 350 { 351 if (outParameterValues == null) 352 outParameterValues = new TreeMap (); 353 outParameterValues.put(paramIdx, val); 354 } 355 356 361 public synchronized void setOutParameterIndex(int paramIdx) 362 { 363 if (outParameterIndexes == null) 364 outParameterIndexes = new ArrayList (); 365 outParameterIndexes.add(new Integer (paramIdx)); 366 } 367 368 374 public void parse(DatabaseSchema schema, int granularity, 375 boolean isCaseSensitive) throws SQLException 376 { 377 Matcher matcher; 378 sqlQueryOrTemplate = sqlQueryOrTemplate.trim(); 379 380 matcher = STORED_PROCEDURE_PATTERN.matcher(sqlQueryOrTemplate); 381 if (!matcher.matches()) 382 return; 383 384 String parameterList = null; 385 386 if (matcher.group(posSyntax1) != null) 387 { 388 procedureName = matcher.group(posSyntax1 + 1); 389 parameterList = matcher.group(posSyntax1 + 2); 390 } 391 else if (matcher.group(posSyntax2) != null) 392 { 393 procedureName = matcher.group(posSyntax2 + 2); 394 parameterList = matcher.group(posSyntax2 + 3); 395 } 396 else 397 { 398 procedureName = matcher.group(posSyntax3); 399 parameterList = matcher.group(posSyntax3 + 1); 400 } 401 402 int parenthesis = parameterList.indexOf('('); 403 404 if (parenthesis == -1) 405 { 406 int commaIdx = parameterList.indexOf(','); 410 if (commaIdx == -1) 411 { 412 if (parameterList.trim().length() != 0) 413 nbOfParameters = 1; 414 else 415 nbOfParameters = 0; 416 } 417 else 418 { 419 nbOfParameters = 1; 420 while (commaIdx != -1) 421 { 422 nbOfParameters++; 423 commaIdx = parameterList.indexOf(',', commaIdx + 1); 424 } 425 } 426 } 427 else 428 { 429 int commaIdx = parameterList.indexOf(',', parenthesis + 1); 432 433 437 if (commaIdx == -1) 438 { int closingParenthesis = parameterList.indexOf(')', parenthesis + 1); 440 try 441 { 442 if (parameterList.substring(parenthesis + 1, closingParenthesis) 443 .trim().length() == 0) 444 nbOfParameters = 0; 445 else 446 nbOfParameters = 1; 447 } 448 catch (RuntimeException e) 449 { nbOfParameters = -1; 451 } 452 } 453 else 454 { 455 nbOfParameters = 1; 456 while (commaIdx != -1) 457 { 458 nbOfParameters++; 459 commaIdx = parameterList.indexOf(',', commaIdx + 1); 460 } 461 } 462 } 463 464 procedureKey = DatabaseProcedure.buildKey(procedureName, nbOfParameters); 466 467 SemanticBehavior spSemantic = getSemanticManager() 468 .getStoredProcedureSemantic(procedureKey); 469 if (spSemantic != null) 470 setSemantic(spSemantic); 471 472 isParsed = true; 473 } 474 475 478 public void cloneParsing(AbstractRequest request) 479 { 480 if (!request.isParsed) 481 return; 482 cloneParsingCommons(request); 483 StoredProcedure other = (StoredProcedure) request; 484 procedureKey = other.getProcedureKey(); 485 nbOfParameters = other.getNbOfParameters(); 486 writeLockedTables = other.writeLockedTables; 487 } 488 489 498 public void copyNamedAndOutParameters(StoredProcedure otherProc) 499 { 500 this.outParameterValues = otherProc.outParameterValues; 501 this.outParameterIndexes = otherProc.outParameterIndexes; 502 this.namedParameterValues = otherProc.namedParameterValues; 503 this.namedParameterNames = otherProc.namedParameterNames; 504 } 505 506 509 public String getParsingResultsAsString() 510 { 511 StringBuffer sb = new StringBuffer (super.getParsingResultsAsString()); 512 sb.append(Translate.get("request.storedproc.name", procedureName)); 513 sb.append(Translate.get("request.blocking", blocking)); 514 sb.append(Translate.get("request.storedproc.parameters.number", 515 nbOfParameters)); 516 if (namedParameterNames != null && namedParameterNames.size() > 0) 517 { 518 sb.append(Translate.get("request.storedproc.named.parameters.names")); 519 for (int i = 0; i < namedParameterNames.size(); i++) 520 { 521 sb.append(Translate.get("request.storedproc.named.parameters.names", 522 namedParameterNames.get(i))); 523 } 524 } 525 if (namedParameterValues != null && namedParameterValues.size() > 0) 526 { 527 sb.append(Translate.get("request.storedproc.named.parameters.values")); 528 for (int i = 0; i < namedParameterValues.size(); i++) 529 { 530 sb.append(Translate.get("request.storedproc.named.parameters.value", 531 new String []{ 532 namedParameterValues.keySet().toArray()[i].toString(), 533 namedParameterValues.get( 534 namedParameterValues.keySet().toArray()[i]).toString()})); 535 } 536 } 537 if (outParameterIndexes != null && outParameterIndexes.size() > 0) 538 { 539 sb.append(Translate.get("request.storedproc.out.parameters.indexes")); 540 for (int i = 0; i < outParameterIndexes.size(); i++) 541 { 542 sb.append(Translate.get("request.storedproc.out.parameters.index", 543 outParameterIndexes.get(i))); 544 } 545 } 546 if (outParameterValues != null && outParameterValues.size() > 0) 547 { 548 sb.append(Translate.get("request.storedproc.out.parameters.values")); 549 for (int i = 0; i < outParameterValues.size(); i++) 550 { 551 sb 552 .append(Translate.get("request.storedproc.out.parameters.value", 553 new String []{ 554 outParameterValues.keySet().toArray()[i].toString(), 555 outParameterValues.get( 556 outParameterValues.keySet().toArray()[i]).toString()})); 557 } 558 } 559 sb.append(Translate.get("request.alters", 560 new String []{String.valueOf(altersAggregateList()), 561 String.valueOf(altersDatabaseCatalog()), 562 String.valueOf(altersDatabaseSchema()), 563 String.valueOf(altersMetadataCache()), 564 String.valueOf(altersQueryResultCache()), 565 String.valueOf(altersSomething()), 566 String.valueOf(altersStoredProcedureList()), 567 String.valueOf(altersUserDefinedTypes()), 568 String.valueOf(altersUsers())})); 569 return sb.toString(); 570 } 571 572 } | Popular Tags |