1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.catalog.UUID; 25 26 import org.apache.derby.iapi.sql.conn.LanguageConnectionFactory; 27 28 import org.apache.derby.iapi.sql.depend.ProviderList; 29 import org.apache.derby.iapi.sql.compile.CompilerContext; 30 import org.apache.derby.iapi.sql.compile.NodeFactory; 31 import org.apache.derby.iapi.sql.compile.Parser; 32 33 import org.apache.derby.iapi.sql.conn.Authorizer; 34 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 35 36 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 37 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 38 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 39 import org.apache.derby.iapi.sql.dictionary.AliasDescriptor; 40 import org.apache.derby.iapi.sql.dictionary.StatementTablePermission; 41 import org.apache.derby.iapi.sql.dictionary.StatementSchemaPermission; 42 import org.apache.derby.iapi.sql.dictionary.StatementColumnPermission; 43 import org.apache.derby.iapi.sql.dictionary.StatementRoutinePermission; 44 45 import org.apache.derby.iapi.types.DataTypeDescriptor; 46 47 import org.apache.derby.iapi.sql.compile.TypeCompilerFactory; 48 49 import org.apache.derby.iapi.sql.depend.Dependent; 50 import org.apache.derby.iapi.sql.depend.Provider; 51 import org.apache.derby.iapi.sql.depend.DependencyManager; 52 import org.apache.derby.iapi.error.ExceptionSeverity; 53 import org.apache.derby.iapi.sql.execute.ExecutionContext; 54 55 import org.apache.derby.iapi.types.DataTypeDescriptor; 56 import org.apache.derby.iapi.sql.ParameterValueSet; 57 58 import org.apache.derby.iapi.store.access.StoreCostController; 59 import org.apache.derby.iapi.store.access.SortCostController; 60 61 import org.apache.derby.iapi.services.context.ContextManager; 62 import org.apache.derby.iapi.services.loader.ClassFactory; 63 import org.apache.derby.iapi.services.compiler.JavaFactory; 64 import org.apache.derby.iapi.services.uuid.UUIDFactory; 65 import org.apache.derby.iapi.services.monitor.Monitor; 66 import org.apache.derby.iapi.services.io.FormatableBitSet; 67 68 import org.apache.derby.iapi.error.StandardException; 69 70 import org.apache.derby.iapi.reference.SQLState; 71 72 import org.apache.derby.iapi.services.sanity.SanityManager; 73 74 import org.apache.derby.iapi.services.context.ContextImpl; 75 import org.apache.derby.iapi.util.ReuseFactory; 76 77 import java.sql.SQLWarning ; 78 import java.util.Vector ; 79 import java.util.Properties ; 80 import java.util.HashMap ; 81 import java.util.Iterator ; 82 import java.util.Map.Entry; 83 import java.util.BitSet ; 84 import java.util.List ; 85 import java.util.Stack ; 86 import java.util.ArrayList ; 87 88 94 public class CompilerContextImpl extends ContextImpl 95 implements CompilerContext { 96 97 101 104 public void cleanupOnError(Throwable error) throws StandardException { 105 106 setInUse(false); 107 resetContext(); 108 109 if (error instanceof StandardException) { 110 111 StandardException se = (StandardException) error; 112 118 int severity = se.getSeverity(); 119 120 if (severity < ExceptionSeverity.SYSTEM_SEVERITY) 121 { 122 if (currentDependent != null) 123 { 124 currentDependent.makeInvalid(DependencyManager.COMPILE_FAILED, 125 lcc); 126 } 127 closeStoreCostControllers(); 128 closeSortCostControllers(); 129 } 130 133 if (severity >= ExceptionSeverity.SESSION_SEVERITY) 134 popMe(); 135 } 136 137 } 138 139 143 public void resetContext() 144 { 145 nextColumnNumber = 1; 146 nextTableNumber = 0; 147 nextSubqueryNumber = 0; 148 resetNextResultSetNumber(); 149 nextEquivalenceClass = -1; 150 compilationSchema = null; 151 parameterList = null; 152 parameterDescriptors = null; 153 scanIsolationLevel = ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL; 154 warnings = null; 155 savedObjects = null; 156 reliability = CompilerContext.SQL_LEGAL; 157 returnParameterFlag = false; 158 initRequiredPriv(); 159 } 160 161 public Parser getParser() { 169 return parser; 170 } 171 172 177 public NodeFactory getNodeFactory() 178 { return lcf.getNodeFactory(); } 179 180 181 public int getNextColumnNumber() 182 { 183 return nextColumnNumber++; 184 } 185 186 public int getNextTableNumber() 187 { 188 return nextTableNumber++; 189 } 190 191 public int getNumTables() 192 { 193 return nextTableNumber; 194 } 195 196 202 203 public int getNextSubqueryNumber() 204 { 205 return nextSubqueryNumber++; 206 } 207 208 214 215 public int getNumSubquerys() 216 { 217 return nextSubqueryNumber; 218 } 219 220 public int getNextResultSetNumber() 221 { 222 return nextResultSetNumber++; 223 } 224 225 public void resetNextResultSetNumber() 226 { 227 nextResultSetNumber = 0; 228 } 229 230 public int getNumResultSets() 231 { 232 return nextResultSetNumber; 233 } 234 235 public String getUniqueClassName() 236 { 237 if (SanityManager.DEBUG) 239 { 240 SanityManager.ASSERT(nextClassName <= Long.MAX_VALUE); 241 } 242 return classPrefix.concat(Long.toHexString(nextClassName++)); 243 } 244 245 250 public int getNextEquivalenceClass() 251 { 252 return ++nextEquivalenceClass; 253 } 254 255 public ClassFactory getClassFactory() 256 { 257 return lcf.getClassFactory(); 258 } 259 260 public JavaFactory getJavaFactory() 261 { 262 return lcf.getJavaFactory(); 263 } 264 265 public void setCurrentDependent(Dependent d) { 266 currentDependent = d; 267 } 268 269 275 276 public ProviderList getCurrentAuxiliaryProviderList() 277 { 278 return currentAPL; 279 } 280 281 286 287 public void setCurrentAuxiliaryProviderList(ProviderList apl) 288 { 289 currentAPL = apl; 290 } 291 292 public void createDependency(Provider p) throws StandardException { 293 if (SanityManager.DEBUG) 294 SanityManager.ASSERT(currentDependent != null, 295 "no current dependent for compilation"); 296 297 if (dm == null) 298 dm = lcc.getDataDictionary().getDependencyManager(); 299 dm.addDependency(currentDependent, p, getContextManager()); 300 addProviderToAuxiliaryList(p); 301 } 302 303 311 public void createDependency(Dependent d, Provider p) throws StandardException 312 { 313 if (dm == null) 314 dm = lcc.getDataDictionary().getDependencyManager(); 315 316 dm.addDependency(d, p, getContextManager()); 317 addProviderToAuxiliaryList(p); 318 } 319 320 325 private void addProviderToAuxiliaryList(Provider p) 326 { 327 if (currentAPL != null) 328 { 329 currentAPL.addProvider(p); 330 } 331 } 332 333 public int addSavedObject(Object obj) { 334 if (savedObjects == null) savedObjects = new Vector (); 335 336 savedObjects.addElement(obj); 337 return savedObjects.size()-1; 338 } 339 340 public Object [] getSavedObjects() { 341 if (savedObjects == null) return null; 342 343 Object [] retVal = new Object [savedObjects.size()]; 344 savedObjects.copyInto(retVal); 345 savedObjects = null; return retVal; 347 } 348 349 350 public void setSavedObjects(Object [] objs) 351 { 352 if (objs == null) 353 { 354 return; 355 } 356 357 for (int i = 0; i < objs.length; i++) 358 { 359 addSavedObject(objs[i]); 360 } 361 } 362 363 364 public void setCursorInfo(Object cursorInfo) 365 { 366 this.cursorInfo = cursorInfo; 367 } 368 369 370 public Object getCursorInfo() 371 { 372 return cursorInfo; 373 } 374 375 376 377 public void firstOnStack() 378 { 379 firstOnStack = true; 380 } 381 382 383 public boolean isFirstOnStack() 384 { 385 return firstOnStack; 386 } 387 388 393 public void setInUse(boolean inUse) 394 { 395 this.inUse = inUse; 396 397 401 if ( ! inUse) 402 { 403 closeStoreCostControllers(); 404 closeSortCostControllers(); 405 } 406 } 407 408 413 public boolean getInUse() 414 { 415 return inUse; 416 } 417 418 427 public void setReliability(int reliability) { this.reliability = reliability; } 428 429 435 public int getReliability() { return reliability; } 436 437 442 public StoreCostController getStoreCostController(long conglomerateNumber) 443 throws StandardException 444 { 445 449 for (int i = 0; i < storeCostConglomIds.size(); i++) 450 { 451 Long conglomId = (Long ) storeCostConglomIds.elementAt(i); 452 if (conglomId.longValue() == conglomerateNumber) 453 return (StoreCostController) storeCostControllers.elementAt(i); 454 } 455 456 459 StoreCostController retval = 460 lcc.getTransactionCompile().openStoreCost(conglomerateNumber); 461 462 463 storeCostControllers.insertElementAt(retval, 464 storeCostControllers.size()); 465 466 467 storeCostConglomIds.insertElementAt( 468 new Long (conglomerateNumber), 469 storeCostConglomIds.size()); 470 471 return retval; 472 } 473 474 477 private void closeStoreCostControllers() 478 { 479 for (int i = 0; i < storeCostControllers.size(); i++) 480 { 481 StoreCostController scc = 482 (StoreCostController) storeCostControllers.elementAt(i); 483 try { 484 scc.close(); 485 } catch (StandardException se) { 486 } 487 } 488 489 storeCostControllers.removeAllElements(); 490 storeCostConglomIds.removeAllElements(); 491 } 492 493 498 public SortCostController getSortCostController() throws StandardException 499 { 500 503 if (sortCostController == null) 504 { 505 508 509 sortCostController = 510 lcc.getTransactionCompile().openSortCostController((Properties ) null); 511 } 512 513 return sortCostController; 514 } 515 516 520 private void closeSortCostControllers() 521 { 522 if (sortCostController != null) 523 { 524 sortCostController.close(); 525 sortCostController = null; 526 } 527 } 528 529 536 public SchemaDescriptor getCompilationSchema() 537 { 538 return compilationSchema; 539 } 540 541 548 public SchemaDescriptor setCompilationSchema(SchemaDescriptor newDefault) 549 { 550 SchemaDescriptor tmpSchema = compilationSchema; 551 compilationSchema = newDefault; 552 return tmpSchema; 553 } 554 555 558 public void setParameterList(Vector parameterList) 559 { 560 this.parameterList = parameterList; 561 562 563 int numberOfParameters = (parameterList == null) ? 0 : parameterList.size(); 564 565 if (numberOfParameters > 0) 566 { 567 parameterDescriptors = new DataTypeDescriptor[numberOfParameters]; 568 } 569 } 570 571 574 public Vector getParameterList() 575 { 576 return parameterList; 577 } 578 579 582 public void setReturnParameterFlag() 583 { 584 returnParameterFlag = true; 585 } 586 587 590 public boolean getReturnParameterFlag() 591 { 592 return returnParameterFlag; 593 } 594 595 598 public DataTypeDescriptor[] getParameterTypes() 599 { 600 return parameterDescriptors; 601 } 602 603 606 public void setScanIsolationLevel(int isolationLevel) 607 { 608 scanIsolationLevel = isolationLevel; 609 } 610 611 614 public int getScanIsolationLevel() 615 { 616 return scanIsolationLevel; 617 } 618 619 622 public void setEntryIsolationLevel(int isolationLevel) 623 { 624 this.entryIsolationLevel = isolationLevel; 625 } 626 627 630 public int getEntryIsolationLevel() 631 { 632 return entryIsolationLevel; 633 } 634 635 638 public TypeCompilerFactory getTypeCompilerFactory() 639 { 640 return typeCompilerFactory; 641 } 642 643 644 647 public void addWarning(SQLWarning warning) { 648 if (warnings == null) 649 warnings = warning; 650 else 651 warnings.setNextWarning(warning); 652 } 653 654 657 public SQLWarning getWarnings() { 658 return warnings; 659 } 660 661 671 public CompilerContextImpl(ContextManager cm, 672 LanguageConnectionContext lcc, 673 TypeCompilerFactory typeCompilerFactory ) 674 { 675 super(cm, CompilerContext.CONTEXT_ID); 676 677 this.lcc = lcc; 678 lcf = lcc.getLanguageConnectionFactory(); 679 this.parser = lcf.newParser(this); 680 this.typeCompilerFactory = typeCompilerFactory; 681 682 classPrefix = "ac"+lcf.getUUIDFactory().createUUID().toString().replace('-','x'); 684 685 initRequiredPriv(); 686 } 687 688 private void initRequiredPriv() 689 { 690 currPrivType = Authorizer.NULL_PRIV; 691 privTypeStack.clear(); 692 requiredColumnPrivileges = null; 693 requiredTablePrivileges = null; 694 requiredSchemaPrivileges = null; 695 requiredRoutinePrivileges = null; 696 LanguageConnectionContext lcc = (LanguageConnectionContext) 697 getContextManager().getContext(LanguageConnectionContext.CONTEXT_ID); 698 if( lcc.usesSqlAuthorization()) 699 { 700 requiredColumnPrivileges = new HashMap (); 701 requiredTablePrivileges = new HashMap (); 702 requiredSchemaPrivileges = new HashMap (); 703 requiredRoutinePrivileges = new HashMap (); 704 } 705 } 707 715 public void pushCurrentPrivType( int privType) 716 { 717 privTypeStack.push( ReuseFactory.getInteger( currPrivType)); 718 currPrivType = privType; 719 } 720 721 public void popCurrentPrivType( ) 722 { 723 currPrivType = ((Integer ) privTypeStack.pop()).intValue(); 724 } 725 726 731 public void addRequiredColumnPriv( ColumnDescriptor column) 732 { 733 if( requiredColumnPrivileges == null || currPrivType == Authorizer.NULL_PRIV 735 || currPrivType == Authorizer.DELETE_PRIV || currPrivType == Authorizer.INSERT_PRIV || currPrivType == Authorizer.TRIGGER_PRIV || currPrivType == Authorizer.EXECUTE_PRIV 739 || column == null) 740 return; 741 755 TableDescriptor td = column.getTableDescriptor(); 756 if (td == null) 757 return; 758 UUID tableUUID = td.getUUID(); 759 StatementTablePermission key = new StatementTablePermission( tableUUID, currPrivType); 760 StatementColumnPermission tableColumnPrivileges 761 = (StatementColumnPermission) requiredColumnPrivileges.get( key); 762 if( tableColumnPrivileges == null) 763 { 764 tableColumnPrivileges = new StatementColumnPermission( tableUUID, 765 currPrivType, 766 new FormatableBitSet( td.getNumberOfColumns())); 767 requiredColumnPrivileges.put(key, tableColumnPrivileges); 768 } 769 tableColumnPrivileges.getColumns().set(column.getPosition() - 1); 770 } 772 777 public void addRequiredTablePriv( TableDescriptor table) 778 { 779 if( requiredTablePrivileges == null || table == null) 780 return; 781 782 StatementTablePermission key = new StatementTablePermission( table.getUUID(), currPrivType); 783 requiredTablePrivileges.put(key, key); 784 } 785 786 791 public void addRequiredRoutinePriv( AliasDescriptor routine) 792 { 793 if( requiredRoutinePrivileges == null || routine == null) 795 return; 796 797 if (routine.getSchemaUUID().toString().equals(SchemaDescriptor.SYSFUN_SCHEMA_UUID)) 799 return; 800 801 if (requiredRoutinePrivileges.get(routine.getUUID()) == null) 802 requiredRoutinePrivileges.put(routine.getUUID(), ReuseFactory.getInteger(1)); 803 } 804 805 810 public void addRequiredSchemaPriv(String schemaName, String aid, int privType) 811 { 812 if( requiredSchemaPrivileges == null || schemaName == null) 813 return; 814 815 StatementSchemaPermission key = new 816 StatementSchemaPermission(schemaName, aid, privType); 817 818 requiredSchemaPrivileges.put(key, key); 819 } 820 821 824 public List getRequiredPermissionsList() 825 { 826 int size = 0; 827 if( requiredRoutinePrivileges != null) 828 size += requiredRoutinePrivileges.size(); 829 if( requiredTablePrivileges != null) 830 size += requiredTablePrivileges.size(); 831 if( requiredSchemaPrivileges != null) 832 size += requiredSchemaPrivileges.size(); 833 if( requiredColumnPrivileges != null) 834 size += requiredColumnPrivileges.size(); 835 836 ArrayList list = new ArrayList ( size); 837 if( requiredRoutinePrivileges != null) 838 { 839 for( Iterator itr = requiredRoutinePrivileges.keySet().iterator(); itr.hasNext();) 840 { 841 UUID routineUUID = (UUID) itr.next(); 842 843 list.add( new StatementRoutinePermission( routineUUID)); 844 } 845 } 846 if( requiredTablePrivileges != null) 847 { 848 for( Iterator itr = requiredTablePrivileges.values().iterator(); itr.hasNext();) 849 { 850 list.add( itr.next()); 851 } 852 } 853 if( requiredSchemaPrivileges != null) 854 { 855 for( Iterator itr = requiredSchemaPrivileges.values().iterator(); itr.hasNext();) 856 { 857 list.add( itr.next()); 858 } 859 } 860 if( requiredColumnPrivileges != null) 861 { 862 for( Iterator itr = requiredColumnPrivileges.values().iterator(); itr.hasNext();) 863 { 864 list.add( itr.next()); 865 } 866 } 867 return list; 868 } 870 873 874 private final Parser parser; 875 private final LanguageConnectionContext lcc; 876 private final LanguageConnectionFactory lcf; 877 private TypeCompilerFactory typeCompilerFactory; 878 private Dependent currentDependent; 879 private DependencyManager dm; 880 private boolean firstOnStack; 881 private boolean inUse; 882 private int reliability = CompilerContext.SQL_LEGAL; 883 private int nextColumnNumber = 1; 884 private int nextTableNumber; 885 private int nextSubqueryNumber; 886 private int nextResultSetNumber; 887 private int entryIsolationLevel; 888 private int scanIsolationLevel; 889 private int nextEquivalenceClass = -1; 890 private long nextClassName; 891 private Vector savedObjects; 892 private String classPrefix; 893 private SchemaDescriptor compilationSchema; 894 private ProviderList currentAPL; 895 private boolean returnParameterFlag; 896 897 private Vector storeCostControllers = new Vector (); 898 private Vector storeCostConglomIds = new Vector (); 899 900 private SortCostController sortCostController; 901 902 private Vector parameterList; 903 904 905 private DataTypeDescriptor[] parameterDescriptors; 906 907 private Object cursorInfo; 908 909 private SQLWarning warnings; 910 911 private Stack privTypeStack = new Stack (); 912 private int currPrivType = Authorizer.NULL_PRIV; 913 private HashMap requiredColumnPrivileges; 914 private HashMap requiredTablePrivileges; 915 private HashMap requiredSchemaPrivileges; 916 private HashMap requiredRoutinePrivileges; 917 } | Popular Tags |