Programming Server Applications
Total Page:16
File Type:pdf, Size:1020Kb
IBM® DB2 Universal Database™ Application Development Guide: Programming Server Applications Ve r s i o n 8 SC09-4827-00 IBM® DB2 Universal Database™ Application Development Guide: Programming Server Applications Ve r s i o n 8 SC09-4827-00 Before using this information and the product it supports, be sure to read the general information under Notices. This document contains proprietary information of IBM. It is provided under a license agreement and is protected by copyright law. The information contained in this publication does not include any product warranties, and any statements provided in this manual should not be interpreted as such. You can order IBM publications online or through your local IBM representative. v To order publications online, go to the IBM Publications Center at www.ibm.com/shop/publications/order v To find your local IBM representative, go to the IBM Directory of Worldwide Contacts at www.ibm.com/planetwide To order DB2 publications from DB2 Marketing and Sales in the United States or Canada, call 1-800-IBM-4YOU (426-4968). When you send information to IBM, you grant IBM a nonexclusive right to use or distribute the information in any way it believes appropriate without incurring any obligation to you. © Copyright International Business Machines Corporation 1993 - 2002. All rights reserved. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents About This Book ..........vii Chapter 3. SQL-Bodied Routines ....59 CREATE Statements for SQL-Bodied Routines 59 Part 1. Routines (Stored Dynamic SQL in SQL-Bodied Routines . 60 Procedures, UDFs, and Methods) . 1 Displaying Error Messages for SQL Procedures............62 Condition Handlers in SQL Procedures . 63 Chapter 1. Introducing Routines .....3 Condition Handlers in SQL Procedures . 63 Routines (Stored Procedures, UDFs, Methods) 3 Condition Handler Declarations ....64 Benefits of Routines ..........5 SIGNAL and RESIGNAL Statements in TypesofRoutines..........6 Condition Handlers ........67 Routines:StoredProcedures......7 SQLCODE and SQLSTATE Variables in Routines: Scalar User-Defined Functions . 8 SQLProcedures..........68 Routines: Table User-Defined Functions . 9 Routines: Methods .........10 Chapter 4. External Routines......71 Parameter Styles for External Routines . 71 Chapter 2. Developing Routines ....13 Syntax for Passing Arguments to Routines Supported Routine Programming Languages 13 Written in C/C++, OLE, or COBOL ....74 Best Practices for Developing Routines . 16 SQLinExternalRoutines.......89 Performance Considerations for Authorizations and Binding for External DevelopingRoutines........16 Routines that Contain SQL .......92 Security Considerations for Routines . 20 Effects of DYNAMICRULES on Dynamic SQL 95 Library and Class Management C/C++ Routines ..........97 Considerations for Developing Routines . 23 C/C++ Routines .........97 Restrictions for Routines .......24 Include File for C/C++ Routines Registering Routines .........27 (sqludf.h)...........102 Writing Routines ..........29 Supported SQL Data Types in C/C++ . 103 Debugging Routines .........31 SQL Data Type Handling in C/C++ Conflicts When Reading and Writing Tables Routines............106 FromRoutines...........33 Graphic Host Variables in C/C++ StoredProcedureFeatures.......35 Routines............115 Stored Procedure Parameter Modes . 35 C++TypeDecoration.......116 StoredProcedureResultSets.....36 JavaRoutines...........118 Parameter Handling in PROGRAM TYPE JavaRoutines..........118 MAIN or PROGRAM TYPE SUB Stored JAR File Administration on the Database Procedures...........47 Server.............122 UDFandMethodFeatures.......49 Supported SQL Data Types in Java . 123 Scratchpads for UDFs and Methods . 49 Debugging Stored Procedures in Java . 125 Scratchpads on 32-bit and 64-bit Platforms 52 OLEAutomationRoutines.......129 Method and Scalar Function Processing OLE Automation Routine Design . 130 Model.............53 Creating and Registering OLE User-Defined Table Functions ......54 AutomationRoutines.......130 User-Defined Table Functions .....54 Object Instance and Scratchpad Table Function Processing Model ....55 Considerations and OLE Routines . 132 Table Function Execution Model for Java 57 Supported SQL Data Types in OLE Automation...........133 © Copyright IBM Corp. 1993 - 2002 iii OLE Automation Routines in BASIC and Dropping User-Defined Types......185 C++.............134 Defining Currency-Based Distinct Types . 186 OLE DB User-Defined Table Functions . 138 Defining a Distinct Type for Completed Job OLE DB User-Defined Table Functions 138 Application Forms .........187 CreatinganOLEDBTableUDF....139 Creating Tables to Track International Sales 188 Fully Qualified Rowset Names ....142 Creating a Table to Store Completed Job Supported SQL Data Types in OLE DB 143 Application Forms .........189 Manipulating Distinct Types ......189 Chapter 5. Invoking/Calling Routines . 145 Manipulating Distinct Types .....189 Invoking Routines .........145 Casting between Distinct Types ....191 Invoking Stored Procedures ......146 Performing Comparisons Involving Invoking UDFs ..........148 Distinct Types ..........192 Invoking User-Defined Table Functions . 149 Performing Comparisons between Routine Nesting ..........151 Distinct Types and Constants .....193 Invoking 32-bit Routines on a 64-bit Performing Assignments Involving Database Server ..........151 Distinct Types in Embedded SQL . 193 Code Pages and Routines .......152 Performing Assignments Involving ReferencingRoutines.........154 Distinct Types in Dynamic SQL ....194 RoutineNamesandPaths......154 Performing Assignments Involving References to Functions .......156 Different Distinct Types .......195 Function Selection ........157 Performing UNION Operations on Distinct Types as Routine Parameters . 159 Distinctly Typed Columns ......196 LOB Values as UDF Parameters ....160 Defining Sourced UDFs for Distinct Types 196 References to Stored Procedures ....161 Stored Procedure Selection ......162 Chapter 8. User-Defined Structured Types 199 User-Defined Structured Types .....200 Part 2. Large Objects, Defining Structured Types .......201 User-Defined Distinct Types, and Storing Instances of Structured Types . 202 Instantiability in Structured Types ....203 Triggers ...........163 Structured Type Hierarchies ......203 Creating a Structured Type Hierarchy . 204 Chapter 6. Large Objects.......165 Defining Behavior for Structured Types . 206 Large Object Usage .........165 Dynamic Dispatch of Methods .....207 Large Object Locators ........166 System-Generated Routines for Structured Retrieving a LOB Value with a LOB Locator 168 Types..............209 Deferring the Evaluation of LOB Expressions 170 Comparison and Casting Functions for Large Object File Reference Variables . 173 StructuredTypes.........209 Writing Data from a CLOB Column to a Text Constructor Functions for Structured File..............175 Types.............210 Inserting Data from a Text File into a CLOB Mutator Methods for Structured Types 210 Column.............176 Observer Methods for Structured Types 211 TypedTables...........211 Chapter 7. User-Defined Distinct Types 179 TypedTables..........211 User-Defined Types .........179 CreatingTypedTables.......212 User-Defined Distinct Types ......179 Dropping Typed Tables .......216 Strong Typing in User-Defined Distinct Substitutability in Typed Tables ....217 Types..............181 Storing Objects in Typed Table Rows . 218 Defining Distinct Types ........182 Defining System-Generated Object Creating Tables with Columns Based on Identifiers ...........220 Distinct Types ...........184 iv Programming Server Applications Defining Constraints on Object Identifier Data Conversion Considerations ....263 Columns............222 Transform Function Requirements . 264 Reference Types .........223 Retrieving Subtype Data from DB2 . 266 TypedViews...........228 Returning Subtype Data to DB2 ....269 TypedViews..........228 StructuredTypeHostVariables.....273 CreatingTypedViews.......229 Declaring Structured Type Host Variables 273 Altering Typed Views .......231 Describing a Structured Type .....274 Dropping Typed Views .......232 Querying Typed Tables and Typed Views 232 Chapter 9. Triggers.........275 Issuing Queries to Dereference References 232 Triggers in Application Development . 275 Returning Objects of a Particular Type INSERT, UPDATE, and DELETE Triggers 278 UsingONLY..........234 Trigger Interactions with Referential Restricting Returned Types Using a TYPE Constraints ............279 Predicate............235 INSTEAD OF Triggers ........279 Returning All Possible Types Using Trigger Creation Guidelines ......281 OUTER ............236 CreatingTriggers..........282 StructuredTypesasColumnTypes....237 Trigger Granularity .........283 Storing Structured Type Objects in Table TriggerActivationTime........284 Columns............237 Transition Variables .........286 Inserting Structured Type Attributes Into Transition Tables ..........288 Columns............240 Triggered Action ..........289 Defining and Altering Tables with Triggered Action .........289 StructuredTypeColumns......240 Triggered Action: Conditions .....290 Defining Types with Structured Type Triggered Action: SQL Statements . 291 Attributes ...........241 Triggered Action: Functions .....292 Inserting Rows That Contain Structured Multiple Triggers ..........293 TypeValues...........242 Synergy Between Triggers, Constraints, and Modifying Structured Type Values in Routines.............294