[Appendix A] Appendix: PL/SQL Exercises [Appendix A] Appendix: PL/SQL Exercises Table of Contents A Appendix: PL/SQL Exercises .2 A.1 Exercises A.1.1 Conditional Logic A.1.2 Loops .3 A.1.3 Exception Handling A.1.4 Cursors A.1.5 Builtin Functions 11 A.1.6 Builtin Packages 12 A.1.7 Modules 13 A.1.8 Module Evaluation: Foreign Key Lookup 14 .17 A.2 Solutions .17 A.2.1 Conditional Logic 17 A.2.2 Loops .18 A.2.3 Exception Handling 20 A.2.4 Cursors 21 A.2.5 Builtin Functions 23 A.2.6 Builtin Packages 25 A.2.7 Modules 26 A.2.8 Module Evaluation: Foreign Key Lookup 28 1.2.1 The Iceberg Approach to Coding 31 1.2.2 The Client−Side Layers 32 .32 PL/SQL Packages 34 1.1 What Is a PL/SQL Package? 34 .35 1.2 What Are the Types and Layers of Packages? .35 .37 1.3 What Are the Benefits of Packages? 37 1.3.1 Enforced Information Hiding 37 1.3.2 Object−Oriented Design 37 1.3.3 Top−Down Design 37 1.3.4 Object Persistence 37 1.3.5 Guaranteeing Transaction Integrity .38 1.3.6 Performance Improvement 38 .40 1.4 Using Packages 40 1.4.1 The Package Specification .40 1.4.2 Referencing Package Elements 41 1.4.3 The Memory−Resident Architecture of Packages 42 1.4.4 Access to Package Elements 45 .48 1.5 Types of Packages 48 1.5.1 Builtin Packages 48 1.5.2 Prebuilt Packages 50 1.5.3 Build−Your−Own Packages 50 .51 1.6 Building Packages 51 1.6.1 When Should You Build a Package? .51 1.6.2 The Package Body 56 1.6.3 The Initialization Section .57 i [Appendix A] Appendix: PL/SQL Exercises Table of Contents 1.6.4 Working with Large Packages .60 1.6.5 Calling Packaged Functions in SQL 62 2.2.1 Choosing the Order of Elements 66 .67 Best Practices for Packages 68 2.1 Starting With Packages 70 .70 2.2 Using Effective Coding Style for Packages 72 .74 2.3 Selecting Package Names 74 2.3.1 Choosing Appropriate and Accurate Names 74 2.3.2 Avoiding Redundancy 75 2.3.3 Avoiding Superfluous Naming Elements 76 .78 2.4 Organizing Package Source Code 78 2.4.1 Creating Codependent Packages 79 .81 2.5 Constructing the Optimal Interface to Your Package 81 2.5.1 Seeing Developers as Users 81 2.5.2 Making Your Programs Case−Insensitive .81 2.5.3 Avoiding Need for User to Know and Pass Literals 82 .85 2.6 Building Flexibility Into Your Packages .85 2.6.1 Toggling Package Behavior 86 2.6.2 Toggles for Code Generation 87 2.6.3 Changing Package Behavior Without Changing the Application 88 .91 2.7 Building Windows Into Your Packages 91 2.7.1 Centralizing the View Mechanism 92 2.7.2 Designing the Window Interface 92 2.7.3 Implementing the Window 94 2.7.4 Summarizing the Window Technique 95 .97 2.8 Overloading for Smart Packages 97 2.8.1 When to Overload 97 2.8.2 Developing an Appreciation of Overloading .102 2.9.1 Implementing Overloading with Private Programs .103 2.9.2 Lava Lamp Code Consolidation 103 104 2.9 Modularizing for Maintainable Packages 107 110 2.10 Hiding Package Data 110 2.10.1 Gaining Control of Your Data 110 2.10.2 Tracing Variable Reads and Writes 112 2.10.3 Simplifying Package Interfaces 113 2.10.4 When to Make Data Public 114 2.10.5 Anchoring to Public Variables 115 3.8.1 When the num_in Argument Is 117 3.8.2 When string_in Is NULL .118 ii [Appendix A] Appendix: PL/SQL Exercises Table of Contents Not Found 119 119 The PL/SQL Development Spiral 122 3.1 The Basic Problem .122 124 3.2 Adding Value .124 126 3.3 Supplying Backward Compatibility .126 128 3.4 Improving the User Interface .128 130 3.5 Rough Waters Ahead 130 132 3.6 Building a Structured Function 132 135 3.7 Handling Program Assumptions 135 137 3.8 Broadening the Scope 137 139 3.9 Considering Implementation Options 139 141 3.10 Choosing the Best Performer .141 144 3.11 Don't Forget Backward Compatibility .144 145 3.12 Obliterating the Literals .145 148 3.13 Glancing Backward, Looking Upward 148 149 Getting Started with PL/Vision .150 4.1 What Is PL/Vision? 150 4.1.1 The Benefits of PL/Vision 150 4.1.2 The Origins of PL/Vision 151 153 4.2 PL/Vision Package Bundles 153 4.2.1 Building Blocks 153 4.2.2 Developer Utilities .154 4.2.3 Plug−and−Play Components .155 156 4.3 Installation Instructions 156 4.3.1 What's On the Disk? 156 4.3.2 Storage Requirements 156 4.3.3 Beginning the Installation 157 4.3.4 Using the PL/Vision Lite Online Reference 158 4.3.5 Creating the PL/Vision Packages 159 4.3.6 Granting Access to PL/Vision .160 162 4.4 Installing Online Help for PL/Vision 162 4.4.1 Special Handling for PLVdyn 162 4.4.2 A PL/Vision Initialization Script for SQL*Plus 163 iii [Appendix A] Appendix: PL/SQL Exercises Table of Contents 4.4.3 Converting Scripts to PL/SQL Programs .163 4.4.4 A Warning About Partial Installation 164 4.4.5 Uninstalling PL/Vision 164 166 4.5 Using Online Help .166 4.5.1 Zooming in on help text .166 168 4.6 Summary of Files on Disk 168 4.6.1 Contents of the install Subdirectory .168 4.6.2 Contents of the test Subdirectory 168 4.6.3 Contents of the use Subdirectory 169 172 PL/Vision Package Specifications 173 5.1 Common Package Elements 173 175 5.2 p: a DBMS_OUTPUT Substitute 175 5.2.1 Toggling output from the p package 175 5.2.2 Setting the line separator 175 5.2.3 Setting the line prefix 175 5.2.4 The overloadings of the l procedure 175 177 5.3 PLV: Top−Level Constants and Functions 177 5.3.1 PL/Vision constants .177 5.3.2 Anchoring datatypes 177 5.3.3 Setting the date format mask .177 5.3.4 Setting the NULL substitution value 177 5.3.5 Assertion routines 178 5.3.6 Miscellaneous programs 178 180 5.4 PLVcase: PL/SQL Code Conversion 180 5.4.1 Package constants 180 5.4.2 Case−converting programs 180 182 5.5 PLVcat: PL/SQL Code Cataloguing 182 5.5.1 Cataloguing package contents .182 5.5.2 Identifying references in stored code 182 183 5.6 PLVchr: Operations on Single Characters 183 5.6.1 PLVchr constants 183 5.6.2 Character type functions 183 5.6.3 Other functions and procedures 184 185 5.7 PLVcmt: Commit Processing 185 5.7.1 Controlling commit activity 185 5.7.2 Logging commit activity 185 5.7.3 Performing commits 185 5.7.4 Managing the commit counter .185 187 5.8 PLVddd: DDL Syntax Dump 187 5.8.1 Including the schema 187 5.8.2 Including the storage parameter 187 iv Preface Acknowledgments This book is very much a product of my imagination, grounded in the realities and possibilities of the PL/SQL language Vague technical fantasies can be translated into packages only with a freedom of time and spirit Many people helped in many ways to provide me with this freedom Bill Hinman, President and CEO of SSC, has supported me fully and encouraged me strenuously from the day I joined his consulting company I am especially grateful for the weeks of straight writing time I was able to enjoy in the final stages of writing this book Barrie Hinman, CFO of SSC, has been invaluable in managing the many details of our growing organization and my scattered activities Hugo Toledo, author of Oracle Networking, made many things possible for me over the past year, from helping me with the installation of Oracle Server 7.3 for Windows NT to directing me to the executable which made it possible to print my training materials in "pure black and white." Hugo is my "answer man" and he'll never understand how deeply he is appreciated My technical reviewers played a critical role in turning my many hundreds of pages of somewhat raw text into a useful book (I hope) Brian Shelden of Symantec has been an enthusiastic booster and, much more importantly, tester of my concepts and code Thomas Dunbar of WebCys again provided many excellent criticisms of my text and approach, forcing me to clarify concepts and justify techniques Bert Scalzo showed an early and steady willingness to point out my mistakes which makes him a very valuable asset in my technical life John Beresniewicz of Wynnsoft appeared late on the scene, but jumped in with both hands typing His ideas and experience have enriched both the text and my understanding of my own code Chuck Sisk of SSC was ready to take on whatever I asked of him even if that changed every week Finally, there is David Thompson of SSC David is a meticulous, principled, and very smart Oracle developer Both in my first book and in this text, he went beyond critiquing my work to providing well thought−out additions to the text David built the PLVddd package, initially in a version independent of PL/Vision He then converted it to use the PL/Vision packages, a process that served as an excellent test of the usefulness and usability of my software Many of my reviewers took time during their summer vacations to read my text, and for that I am deeply grateful I thank Kasu Sista and Ken Wiegle of Links Technology, Inc., for their generosity in providing a development machine on which I could test PL/Vision code for Release 2.3 of PL/SQL at a critical stage in this book I thank Petra Smulders of the European Oracle User Group, Lex de Haan of Oracle EMEA Education, and Ineke Werkman of Oracle Netherlands for their parts in my two wonderful trips to the Netherlands in 1996, where I trained Oracle developers and enjoyed Amsterdam I thank Peter Vasterd of Oracle Corporation for keeping a wide line of communication open with me, answering my sometimes, well, brainless questions, helping me get around technical and bureaucratic obstacles, and specifically making sure that I was able to build and test my software on an Oracle Server 7.3 database I thank Tom White and Steve Hilker of RevealNet for their enthusiastic support of my work and their help in making PL/Vision a living and breathing product 653 [Appendix A] Appendix: PL/SQL Exercises I thank the various editors who have published my articles over the last year: Tony Ziemba of Oracle Developer (portions of this book appeared originally as articles in that publication), Jerry Coffee of Oracle Informant, Kathleen O'Connor of Oracle Technical Journal, Rich Niemiec of Select Magazine, and the folks at the Oracle Integrator Writing these articles helped me focus my thinking and coding in ways that clarified the contents of this book I thank Ernie Martinez and Mark A Ebel of COM.sortium LLC in Orlando for their help in sorting out my difficulties with UTL_FILE on my Windows NT Oracle 7.3 Server I thank Fred King, most able, industrious, and downright amiable computer serviceperson, who went beyond the call of duty to cannibalize a "spare" Toshiba Portege in order to retrieve the text of this book from my dead laptop and keep me writing I thank the good people at O'Reilly & Associates for another fine publishing experience I thank Debby Russell, my editor, for another outstanding effort at transforming lots of interesting ideas and too much text into a text of manageable size and cohesiveness I not really understand how she manages to juggle all of her editing and book development responsibilities so effectively I know, however, that this book stands as yet another testimony to her skill and dedication Thanks as well to David Futato, the production manager for the book; Kismet McDonough−Chan, the copyeditor; Mike Sierra, who converted the Microsoft Word files to FrameMaker; Edie Freedman, who designed the cover; Nancy Priest and Mary Jane Walsh, who designed the interior layout; Chris Reilley, who prepared the diagrams; Eden Reiner, who handled the advertising material; and Seth Maislin, who prepared the index I thank my wife, Veva Silva Feuerstein, for taking on a bigger−than−usual share (more than big enough to begin with) of "the family thing" and "the house thing" as I buried myself in my computers I am lucky to have her and I hope she feels the same way about me I thank my son, Eli Silva Feuerstein, for never giving up on asking me to play with him, even as I was buried in inches of paper doing final edits The games of horse and one−on−one in our tiny backyard basketball court definitely improved the text you read in this book I thank my son, Chris Silva, for helping us get away from our pets now and then, and for being such a fine big brother to Eli I thank my Uncle Dave Gventer for making clear to me early in life that just because most people believe one thing, that doesn't make it true He opened my eyes to a different way of looking at the world Finally, I thank my mother and father, Joan Lee and Sheldon Feuerstein, for their support and love Comments and Questions I Working With Packages Copyright (c) 2000 O'Reilly Associates All rights reserved .1 654 ... .32 PL/ SQL Packages 34 1.1 What Is a PL/ SQL Package? 34 .35 1.2 What Are the Types and Layers of Packages? .35 ... Online Help for PL/ Vision 162 4.4.1 Special Handling for PLVdyn 162 4.4.2 A PL/ Vision Initialization Script for SQL* Plus 163 iii [Appendix A] Appendix: PL/ SQL Exercises... 180 5.4 PLVcase: PL/ SQL Code Conversion 180 5.4.1 Package constants 180 5.4.2 Case−converting programs 180 182 5.5 PLVcat: PL/ SQL Code