1. Trang chủ
  2. » Công Nghệ Thông Tin

o'reilly - oracle - advanced programming with packages

671 1K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 671
Dung lượng 2,1 MB

Nội dung

[Appendix A] Appendix: PL/SQL Exercises Table of Contents A. Appendix: PL/SQL Exercises 2 A.1 Exercises 2 A.1.1 Conditional Logic 2 A.1.2 Loops 3 A.1.3 Exception Handling 6 A.1.4 Cursors 8 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 1. 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 [Appendix A] Appendix: PL/SQL Exercises i 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 2. 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 0 117 3.8.2 When string_in Is NULL 118 [Appendix A] Appendix: PL/SQL Exercises ii Table of Contents Not Found 119 119 3. 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 4. 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 [Appendix A] Appendix: PL/SQL Exercises iii 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 5. 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 [Appendix A] Appendix: PL/SQL Exercises iv Table of Contents 5.8.3 Dumping the DDL 187 189 5.9 PLVdyn: Dynamic SQL Operations 189 5.9.1 Tracing PLVdyn activity 189 5.9.2 Controlling execution of dynamic SQL 189 5.9.3 Bundled, low−level operations 189 5.9.4 Data Definition Language operations 190 5.9.5 Data Manipulation Language operations 190 5.9.6 Executing dynamic PL/SQL 191 5.9.7 Miscellaneous programs 191 192 5.10 PLVexc: Exception Handling 192 5.10.1 Package constants 192 5.10.2 Package−based exceptions 192 5.10.3 Logging exception−handling activity 192 5.10.4 Displaying exceptions 193 5.10.5 Rolling back on exception 193 5.10.6 Exception handlers 193 5.10.7 Bailing out program execution 194 5.10.8 Managing the list of bailout errors 194 195 5.11 PLVfile: Operating System I/O Manager 195 5.11.1 Package constants and exceptions 195 5.11.2 Trace PLVfile activity 195 5.11.3 Setting the operating system delimiter 195 5.11.4 Setting the default directory or location 196 5.11.5 Creating files 196 5.11.6 Checking for file existence 196 5.11.7 Opening a file 196 5.11.8 Closing a file 197 5.11.9 Reading from a file 197 5.11.10 Writing to a file 198 5.11.11 Copying a file 198 5.11.12 Displaying the contents of a file 198 5.11.13 Miscellaneous operations 199 200 5.12 PLVfk: Foreign Key Interface 200 5.12.1 Package Constants 200 5.12.2 Setting the PLVfk configuration 200 5.12.3 Looking up the name 200 5.12.4 Looking up the ID 201 202 5.13 PLVgen: PL/SQL Code Generator 202 5.13.1 Package constants 202 5.13.2 Setting the indentation 202 5.13.3 Setting the author 203 5.13.4 Toggles affecting generated code 203 5.13.5 Help generators 204 5.13.6 Generating a package 204 5.13.7 Generating a procedure 204 5.13.8 Generating functions 204 5.13.9 Generating get−and−set routines 205 [Appendix A] Appendix: PL/SQL Exercises v Table of Contents 5.13.10 Miscellaneous code generators 205 207 5.14 PLVhlp: Online Help Architechture 207 5.14.1 Package constants 207 5.14.2 Setting the page size 207 5.14.3 Help text stub generators 207 5.14.4 Displaying online help 207 209 5.15 PLVio: Input/Output Processing 209 5.15.1 Package constants and exceptions 209 5.15.2 Package records 209 5.15.3 Source and target repository type functions 210 5.15.4 Managing the source repository 210 5.15.5 Managing the source WHERE clause 211 5.15.6 Managing the target repository 212 5.15.7 Reading and writing lines 213 5.15.8 Saving and restoring repository settings 213 5.15.9 Miscellaneous PLVio programs 214 5.15.10 Tracing PLVio activity 214 215 5.16 PLVlex: Lexical Analysis 215 5.16.1 Analyzing PL/SQL string content 215 5.16.2 Scanning PL/SQL strings 215 217 5.17 PLVlog: Logging Facility 217 5.17.1 Package constants 217 5.17.2 Controlling logging activity 217 5.17.3 Selecting the log type 217 5.17.4 Writing to the log 218 5.17.5 Reading the log 218 5.17.6 Managing the log 219 5.17.7 Rolling back in PLVlog 219 221 5.18 PLVlst: List Manager 221 5.18.1 Package exceptions 221 5.18.2 Creating and destroying lists 221 5.18.3 Modifying list contents 221 5.18.4 Analyzing list contents 222 223 5.19 PLVmsg: Message Handling 223 5.19.1 Restricting use of text 223 5.19.2 Managing and accessing message text 223 225 5.20 PLVobj: Object Interface 225 5.20.1 Tracing PLVobj activity 225 5.20.2 General constants and exceptions 225 5.20.3 Setting the current object 225 5.20.4 Accessing the current object 226 5.20.5 Interfacing with the PLVobj cursor 226 5.20.6 Programmatic cursor FOR loop elements 226 5.20.7 Saving and restoring PLVobj settings 227 5.20.8 Miscellaneous PLVobj programs 227 229 [Appendix A] Appendix: PL/SQL Exercises vi Table of Contents 5.21 PLVprs: String Parsing 229 5.21.1 Package constants 229 5.21.2 Wrapping long strings into paragraphs 229 5.21.3 Analyzing string contents 230 5.21.4 Parsing strings 230 232 5.22 PLVprsps: PL/SQL Source Code Parsing 232 5.22.1 Package constants 232 5.22.2 Specifying tokens of interest 232 5.22.3 Parsing PL/SQL source code 233 234 5.23 PLVrb: Rollback Processing 234 5.23.1 Controlling rollback activity 234 5.23.2 Logging rollback activity 234 5.23.3 Performing rollbacks 234 5.23.4 Managing savepoints 234 236 5.24 PLVstk: Stack Manager 236 5.24.1 Package constants 236 5.24.2 Creating and destroying stacks 236 5.24.3 Modifying stack contents 236 5.24.4 Analyzing stack contents 236 5.24.5 Tracing Stack Activity 237 238 5.25 PLVtab: Table Interface 238 5.25.1 Predefined table TYPEs 238 5.25.2 The empty PL/SQL tables 238 5.25.3 Toggle for showing header 238 5.25.4 Toggle for showing row numbers 239 5.25.5 Toggle for showing blanks in row 239 5.25.6 Setting the row prefix 239 5.25.7 Saving and restoring settings 239 5.25.8 Displaying a PLVtab table 239 241 5.26 PLVtkn: Token Table Interface 241 5.26.1 Package constants 241 5.26.2 The keyword record TYPE 241 5.26.3 Determining type of token 241 5.26.4 Retrieving keyword information 242 243 5.27 PLVtmr: Program Performance Analyzer 243 5.27.1 Control execution of timings 243 5.27.2 Setting the repetition value 243 5.27.3 Setting the factoring value 243 5.27.4 Capturing the current timestamp 243 5.27.5 Retrieving and displaying elapsed time 243 5.27.6 Calibration and timing scripts 244 245 5.28 PLVtrc: Trace Facility 245 5.28.1 Package constants 245 5.28.2 Controlling trace activity 245 5.28.3 Writing to the PL/Vision log 245 [Appendix A] Appendix: PL/SQL Exercises vii Table of Contents 5.28.4 Displaying current module 245 5.28.5 Accessing the PL/SQL call stack 246 5.28.6 Tracing PL/SQL code execution 246 5.28.7 Displaying an activity trace 246 5.28.8 Accessing the PLVtrc execution call stack (ECS) 247 248 5.29 PLVvu: Code and Error Viewing 248 5.29.1 Package constants 248 5.29.2 Setting the overlap 248 5.29.3 Displaying stored code 248 5.29.4 Displaying compile errors 248 250 6. PLV: Top−Level Constants and Functions 251 6.1 Null Substitution Value 251 253 6.2 Setting the PL/Vision Date Mask 253 255 6.3 Assertion Routines 255 6.3.1 Using the assert Procedure 255 6.3.2 Asserting NOT NULL 256 6.3.3 Asserting "In Range" 257 259 6.4 PLV Utilities 259 6.4.1 Converting Boolean to String 259 6.4.2 Obtaining the Error Message 259 6.4.3 Retrieving Date and Time 260 6.4.4 Pausing Your Program 260 261 6.5 The Predefined Datatypes 261 263 6.6 The Predefined Constants 263 265 7. p: A Powerful Substitute for DBMS_OUTPUT 266 7.1 Using the l Procedure 267 7.1.1 Valid Data Combinations for p.l 268 7.1.2 Displaying Dates 268 270 7.2 The Line Separator 270 272 7.3 The Output Prefix 272 273 7.4 Controlling Output from p 273 275 8. PLVtab: Easy Access to PL/SQL Tables 276 8.1 Using PLVtab−Based PL/SQL Table Types 276 278 8.2 Displaying PLVtab Tables 278 8.2.1 Displaying Wrapped Text 279 8.2.2 Displaying Selected Companies 279 281 [Appendix A] Appendix: PL/SQL Exercises viii Table of Contents 8.3 Showing Header Toggle 281 282 8.4 Showing Row Number Toggle 282 283 8.5 Setting the Display Prefix 283 284 8.6 Emptying Tables with PLVtab 284 8.6.1 Improving the Delete Process 285 287 8.7 Implementing PLVtab.display 287 289 9. PLVmsg: Single−Sourcing PL/SQL Message Text 290 9.1 PLVmsg Data Structures 290 292 9.2 Storing Message Text 292 9.2.1 Adding a Single Message 292 9.2.2 Batch Loading of Message Text 292 9.3.1 Substituting Oracle Messages 294 294 9.3 Retrieving Message Text 295 296 9.4 The Restriction Toggle 296 297 9.5 Integrating PLVmsg with Error Handling 297 9.5.1 Using PLVmsg in PL/Vision 298 300 9.6 Implementing load_ from_dbms 300 303 10. PLVprs, PLVtkn, and PLVprsps: Parsing Strings 304 10.1 PLVprs: Useful String Parsing Extensions 304 10.1.1 Developing a General Solution 305 10.1.2 Customizing the Delimiter Set 305 10.1.3 Parsing Strings into Atomics 306 10.1.4 Wrapping Strings into Paragraphs 311 315 10.2 PLVtkn: Managing PL/SQL Tokens 315 10.2.1 Keeping Track of PL/SQL Keywords 315 10.2.2 Determining Token Type 316 10.2.3 Retrieving Information About a Token 317 319 10.3 PLVprsps: Parsing PL/SQL Strings 319 10.3.1 Selecting Token Types for Parsing 319 10.3.2 Parsing PL/SQL Code 320 10.3.3 Initializing a Table of Tokens 322 10.3.4 Using PLVprsps 323 325 11. PLVobj: A Packaged Interface to ALL_OBJECTS 326 11.1 Why PLVobj? 326 329 [Appendix A] Appendix: PL/SQL Exercises ix [...]... .622 Part I: Working With Packages 622 Part II: PL/Vision Overview 622 Part III: Building Block Packages 622 Part IV: Developer Utility Packages .622 Part V: Plug−and−Play Packages 622 Part VI: Testing Your Knowledge 623 623 624 Part I: Working With Packages 625 ... 600 22.1.8 Rolling Back When an Exception Occurs 602 22.1.9 Halting in PLVexc .602 22.1.10 Bailing Out with PLVexc 603 606 22.2 Application−Specific Exception Packages 606 22.2.1 In the Trenches with PLVexc 606 610 22.3 Implementing PLVexc .610 22.3.1 Analyzing the Need 610... Exercises Table of Contents Part II: PL/Vision Overview 627 628 Part III: Building Block Packages .629 630 Part IV: Developer Utility Packages 631 632 Part V: Plug−and−Play Packages 633 634 Part VI: Testing Your Knowledge 635 636... 518 19 PLVdyn and PLVfk: Dynamic SQL and PL/SQL .519 19.1 About Plug−and−Play 519 521 19.2 Declarative Programming in PL/SQL 521 522 19.3 The Dynamic Packages of PL/Vision 522 524 19.4 PLVdyn: A Code Layer over DBMS_SQL .524 19.4.1 DDL Operations 524 ... Last Record 337 11.4.4 Showing Objects with PLVobj 337 339 11.5 Binding Objects to a Dynamic Cursor .339 11.5.1 Specifying Which Binds Occur 339 11.5.2 Using bindobj 339 11.5.3 Using bindobj in PL/Vision .341 342 11.6 Populating a PL/SQL Table with Object Names .342 ... enhance your ability to write well−structured PL/SQL programs, and also to identify problems with existing code I recommend that you test out your baseline PL/SQL skills on these exercises before you explore Parts III through V of this book, where you will learn how to apply your skills to building robust and reusable packages For solutions to these exercises, see Section A.2, "Solutions" later in this appendix... others fail: UPDATE emp SET empno = 100 WHERE empno > 5000; DELETE FROM dept WHERE deptno = 10; DELETE FROM emp WHERE deptno = 10; 3 Write a PL/SQL block that handles by name the following Oracle error: ORA−1014: ORACLE shutdown in progress The exception handler should, in turn, raise a VALUE_ERROR exception Hint: use the EXCEPTION INIT pragma 4 When the following block is executed, which of the two... lowercase If it is High Noon in New York, what time is it in Calcutta? Remove a certain prefix from a string (for example, change std_company_id to company_id) Replace all instances of _ with a # Return the error message associated with a SQL error code Return the largest integer less than a specified value Review all new hires on the first Wednesday after they'd been working for three months Strip all leading... what would you see when you call the builtin function? 6 You want to build a utility for DBAs that would allow them to create an index from within a PL/SQL program Which package would you use? Which programs inside that package would be needed? 7 A.1.6 Builtin Packages 12 [Appendix A] Appendix: PL/SQL Exercises You need to run a stored procedure named update_data every Sunday at 4 AM to perform a set... for the header is NULL, which means that no header is displayed with the table contents Here is an example of a call to this program: dispdates (birthdays, bday_count, 'List of Birthdays'); Now suppose that you had to use dispdates to satisfy the following requirement: "Display the list of company start dates stored in the date table without any header." I can think of two ways do this: dispdates (company_list, . 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. 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 2. Best Practices for Packages 68 2.1 Starting With Packages 70 70 2.2. 622 Part I: Working With Packages 622 Part II: PL/Vision Overview 622 Part III: Building Block Packages 622 Part IV: Developer Utility Packages 622 Part V: Plug−and−Play Packages 622 Part VI:

Ngày đăng: 07/04/2014, 16:30

TỪ KHÓA LIÊN QUAN