[ Pobierz całość w formacie PDF ]
.So, youcan change the body (implementation) without having to recompile callingprograms.8-4 PL/SQL User s Guide and Reference Advantages of PackagesAdvantages of PackagesPackages offer several advantages: modularity, easier application design,information hiding, added functionality, and better performance.ModularityPackages let you encapsulate logically related types, items, and subprograms in anamed PL/SQL module.Each package is easy to understand, and the interfacesbetween packages are simple, clear, and well defined.This aids applicationdevelopment.Easier Application DesignWhen designing an application, all you need initially is the interface information inthe package specs.You can code and compile a spec without its body.Then, storedsubprograms that reference the package can be compiled as well.You need notdefine the package bodies fully until you are ready to complete the application.Information HidingWith packages, you can specify which types, items, and subprograms are public(visible and accessible) or private (hidden and inaccessible).For example, if apackage contains four subprograms, three might be public and one private.Thepackage hides the implementation of the private subprogram so that only thepackage (not your application) is affected if the implementation changes.Thissimplifies maintenance and enhancement.Also, by hiding implementation detailsfrom users, you protect the integrity of the package.Added FunctionalityPackaged public variables and cursors persist for the duration of a session.So, theycan be shared by all subprograms that execute in the environment.Also, they allowyou to maintain data across transactions without having to store it in the database.Better PerformanceWhen you call a packaged subprogram for the first time, the whole package isloaded into memory.So, later calls to related subprograms in the package require nodisk I/O.Also, packages stop cascading dependencies and thereby avoidunnecessary recompiling.For example, if you change the implementation of apackaged function, Oracle need not recompile the calling subprograms becausethey do not depend on the package body.Packages 8-5 The Package SpecThe Package SpecThe package spec contains public declarations.The scope of these declarations islocal to your database schema and global to the package.So, the declared items areaccessible from your application and from anywhere in the package.Figure 8 2illustrates the scoping.Figure 8 2 Package Scopeprocedurepackage spec package body functionprocedureschemafunctionpackage spec package body functionprocedureother objectsThe spec lists the package resources available to applications.All the informationyour application needs to use the resources is in the spec.For example, thefollowing declaration shows that the function namedfac takes one argument oftypeINTEGER and returns a value of typeINTEGER:FUNCTION fac (n INTEGER) RETURN INTEGER; -- returns n!That is all the information you need to call the function.You need not consider itsunderlying implementation (whether it is iterative or recursive for example).Only subprograms and cursors have an underlying implementation.So, if a specdeclares only types, constants, variables, exceptions, and call specs, the packagebody is unnecessary.Consider the following bodiless package:CREATE PACKAGE trans_data AS -- bodiless packageTYPE TimeRec IS RECORD (minutes SMALLINT,hours SMALLINT);TYPE TransRec IS RECORD (category VARCHAR2,account INT,amount REAL,time_of TimeRec);8-6 PL/SQL User s Guide and Reference The Package Specminimum_balance CONSTANT REAL := 10.00;number_processed INT;insufficient_funds EXCEPTION;END trans_data;The packagetrans_data needs no body because types, constants, variables, andexceptions do not have an underlying implementation.Such packages let youdefine global variables usable by subprograms and database triggers that persistthroughout a session.Referencing Package ContentsTo reference the types, items, subprograms, and call specs declared within apackage spec, use dot notation, as follows:package_name.type_namepackage_name.item_namepackage_name.subprogram_namepackage_name.call_spec_nameYou can reference package contents from database triggers, stored subprograms,3GL application programs, and various Oracle tools.For example, you might callthe packaged procedurehire_employee from SQL*Plus, as follows:SQL> CALL emp_actions.hire_employee( TATE ,  CLERK ,.);In the example below, you call the same procedure from an anonymous PL/SQLblock embedded in a Pro*C program.The actual parametersemp_name andjob_title are host variables (that is, variables declared in a host environment).EXEC SQL EXECUTEBEGINemp_actions.hire_employee(:emp_name, :job_title,.);RestrictionsYou cannot reference remote packaged variables directly or indirectly.For example,you cannot call the following procedure remotely because it references a packagedvariable in a parameter initialization clause:CREATE PACKAGE random ASseed NUMBER;PROCEDURE initialize (starter IN NUMBER := seed,.);Also, inside a package, you cannot reference host variables.Packages 8-7 The Package BodyThe Package BodyThe package body implements the package spec.That is, the package body containsthe implementation of every cursor and subprogram declared in the package spec.Keep in mind that subprograms defined in a package body are accessible outsidethe package only if their specs also appear in the package spec.To match subprogram specs and bodies, PL/SQL does a token-by-tokencomparison of their headers.So, except for white space, the headers must matchword for word.Otherwise, PL/SQL raises an exception, as the following exampleshows:CREATE PACKAGE emp_actions AS.PROCEDURE calc_bonus (date_hired emp.hiredate%TYPE,.);END emp_actions;CREATE PACKAGE BODY emp_actions AS.PROCEDURE calc_bonus (date_hired DATE,.) IS-- parameter declaration raises an exception because  DATE-- does not match  emp.hiredate%TYPE word for wordBEGIN.END;END emp_actions;The package body can also contain private declarations, which define types anditems necessary for the internal workings of the package.The scope of thesedeclarations is local to the package body.Therefore, the declared types and itemsare inaccessible except from within the package body.Unlike a package spec, thedeclarative part of a package body can contain subprogram bodies.Following the declarative part of a package body is the optional initialization part,which typically holds statements that initialize some of the variables previouslydeclared in the package.The initialization part of a package plays a minor role because, unlike subprograms,a package cannot be called or passed parameters.As a result, the initialization partof a package is run only once, the first time you reference the package.Remember, if a package spec declares only types, constants, variables, exceptions,and call specs, the package body is unnecessary [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • czarkowski.pev.pl
  •