Session F08 DB2 Stored Procedure and UDF Support in Rational Application Developer V6.01 Marichu Scanlon marichu@us.ibm.com Wed, May 10, 2006 08:30 a.m. 09:40 a.m. Platform: Cross Platform Audience: -DBAs / Application Developers who are looking into stored procedures and UDFs -DBAs / Application Developers who have started to use Development Center for developing stored procedures and UDFs and Want to incorporate Java and/or Web development -Application developers who are currently developing in Java and want to find a way to independently develop and test Java stored procedures and UDFs -NOT for experienced users of Development Center Platform discussed: -Primarily supports development of SPs and UDFs against a DB2 for Windows, Linux and Unix server; when you see Windows, I mean Linux and Unix as well. -While RAD supports connection to multiple databases, the SP / UDF development support is only for DB2 servers. -Mention differences when dealing with DB2 for z/os and DB2 for iseries 1
Objectives of the presentation Overview of RAD v6.01 Support for DB2 SP and UDF development Connectivity to DB2 servers Stored Procedure and UDF Builder (SUBuilder) for developing and deploying SPs and UDFs Developing SQL Statements Export, Import, Copy / Paste features Additional Features 2 Overview of RAD V6.01 workbench and how SUBuilder exploits the various capabilities of Eclipse in RAD >> should this be Eclipse??? 2
RAD V6.01 Overview An Eclipse based IDE for developing Java and Web applications Successor to Websphere Studio Application Developer Connectivity to DB2 and non-db2 databases Quickly create, test and deploy various application artifacts (e.g. Web Services, J2EE and portal apps); and in particular, Stored Procedures, SQL Statements and User Defined Functions. 3 Quickly design, develop, analyze, test, profile and deploy Web, Web services, Java, J2EE and portal applications with this comprehensive IDE. Optimized for IBM WebSphere software, and supporting multi-vendor runtime environments, IBM Rational Application Developer for WebSphere Software is powered by the Eclipse open source platform so developers can adapt and extend their development environment to match their needs and increase their productivity. When used with the IBM Software Development Platform, developers can access a broad range of requirements and change management functions directly from Rational Application Developer for WebSphere Software. Accelerate portal, SOA and J2EE using RAD tools and wizards. Leverage existing skills and shorten the Java learning curve with drag-and-drop UI components and point-and-click database connectivity. Improve code quality with automated tools for applying coding standard reviews, component and Web Service unit testing and multi-tier runtime analysis. Integrate your business applications with interoperable Web services and service-oriented architectures. Visualize and graphically edit code through the UML Visual Editor for Java and EJB. Collaborate and share assets across the team using built-in IBM Rational ClearCase LT version control. Adapt and extend your development environment with Eclipse-based plug-ins to match your needs. Quickly build and deploy interactive reports using drag-and-drop UI components and Crystal Reports. 3
Database Developer Capability Database Developer capability supports development of database objects. Supports connectivity to DB2 and non-db2 servers Organizes database objects needed for a application in a project Facilitates movement of the objects from project to project; or server to server 4 To launch RAD V6: Start->Programs-> IBM Rational > IBM Rational Application Developer > Rational Application Developer To enable Database capability: click on Window->Preferences > > Workbench > Capabilities > click on Database Developer To Open Data Perspective: click on Window > Open Perspective > Other > Data 4
The Data Perspective Data Definition - Contains db objects copied from the server Editor View -Shows single object view Database Explorer Output View results, msgs, parms 5 Some of the functions supported in the Data Perspective: Create and Filter Database connections Browse and Edit database objects Work online or off-line Browse or edit database schemas Create Databases objects (e.g. tables) Generate DDL Create Java or SQL SPs Create SQL UDFs on Distributed DB2 Build and Run SQL statements 5
A typical development scenario Create a connection to a DB Server Create a SQL Stored Procedure Deploy the Stored Procedure Run the Stored Procedure Debug the Stored Procedure Export and Deploy to another server Import or Copy and Paste a Stored Procedure to a different project 6 6
Connectivity to a server Connect to DB2 or non-db2 servers Support for DB2 Universal driver or other JDBC driver DB2 database Aliases defined in DB2 UDB s Configuration Assistant Configuring location of the driver If using DB2 Universal Driver, Connection URL is built as you enter information. Test Connection option Connection Wizard 7 The Database Explorer is the view in the Data Perspective that allows the user to create and manage their database server connections. If a DB2 Alias is already defined in Client Configuration Assistant, these connections will be displayed. You may create a new connection using the connection wizard. The Class Location may be different. You need the DB2 Universal Driver (com.ibm.db2.jcc.db2driver) to connect. This is a Type 4 connection. Ability to configure location allows one driver for various clients point to a file server, for example. When using Type 4 JDBC driver, you need to have a DB2 Connect License. That license is located in the DB2 Connect install code when it is extracted into directories. That's the only way to obtain the license jar files. DB2 Connect can be connected 'on some machine', then the jar copied over; or the license jars be available on a server that is being used. 7
The Database Explorer Hierarchical organization of databases and objects in it. Supports creation and modification of Connections through the Connection wizard. Connections can be refreshed with newly created objects. Supports filtering before loading objects from database catalogs. Database model objects created for each entry in the catalog. ƒ Called Reverse Engineering or Catalog Loading Objects are Copied to Project 8 Right-click on the connection to see list of actions Refresh can be done at schema or folder level 8
Data Definition View Project folder contains objects copied from the Database Explorer. Target Connection is DB2 UDB for z/os v8 Launch Create SP Wizard from here Select SQL or Java 9 Right click on Folder to show list of items that can be done on the object. Actions are dependent on the folder. Launch SP wizard to create a new SP or UDF Create SP wizard also launchable from: File > New > SQL Stored Procedure Select Stored Procedure folder, then right-click on white space Within the Wizard, can create a new connection in the Database Explorer. 9
A note about the Target Connection If the wizard is launched from the File menu, the user is prompted for the connection Tool needs to know about the DB2 server in order to build the SP correctly. Select a project then database, then schema 10 The wizard needs to know the Target Connection in order to be able to correctly build the Stored procedure. 10
Create Stored Procedure Wizard Guides user in creating a SP or UDF Fields/options specific to: SQL, JDBC or SQLJ SP SQL scalar or table UDF; Websphere MQ UDF Creates other artifacts (e.g. jar files, packages, etc) Allows embedding SQL statements Option to enable SQL or Java SP to be used in a DADX Web Service Create SP Wizard 11 Expand the Project Folder to Stored Procedures folder Right-click on folder > New > SQL Stored Procedure. This launches the Stored Procedure Wizard Also can invoke the New SP wizard from the File > New menu item Create SP Wizard enables: Creation of SQL and Java Stored Procedure DDL and other artifacts (e.g. jar files, packages, etc.) Creation of SQL Statements to embed in the SP / UDF, and persisting these statements for use in other SPs or UDFs Note there is also the ability to Export and Import a Stored Procedure from a file or a project. More on this later. Scalar and Table UDF allowed for LUW only 11
Build SQL Statements Named stmts Be guided in building the statement Type statement then Parse Number of queries = number of result sets 12 1). Unlike DC, the statements are NAMED. The statements can be ADDed, REMOVEd, and IMPORTed from the file system. 2) Display one or all statements to be embedded in the stored procedure Note: Statement details only shows the highlighted statement. 3) Launch SQL Wizard by clicking on the SQLAssist button to build a Select, Insert, Update, or Delete Statement. 4) Can simply type SQL statement in details text box, then click Parse to validate the SQL Syntax 5) For z/os, calculate actual cost of the query Note: Actual cost of the query' output is relative to the server you are going to. So, if you are going to a small 'test server' the info may not be as meaningful as when going against a production server for the same query. A solution would be to connect to a production server, assuming the proper authorization is available, and run the query on a production server and tune as appropriate. 6) For LUW, launch Visual Explain against the query* Note: Visual Explain capability must be installed in client 12
SQL Wizard Multiple tabs for constructing the stmt Select elements from available list then Click > Deselect or remove all 13 Button is SQL Assist in order to be consistent with Development Center Code is wizard like - Navigate through Next and Back. SQL Wizard is a multi-tab wizard for constructing the SQL Statement to embed in the SP Tables and columns are selected and manipulated to create the statement SQL Assist in DC navigates through the Statement Outline; more dynamic; not sequential Statements created using SQL Wizard are stored in the Scripts folder and can later on be manipulated using the SQL Builder. 13
SQL Wizard, continued Launch the Expression Builder Graphically join 2 tables 14 Launch Expression Builder for constructing expressions from the Columns tab, Conditions tab, and Group tab Graphically join tables by clicking on one column and dragging to the 2 nd column. Note: column data types must be compatible, or you will get a parser error later on. 14
Execute and Parse within SQL Wizard Execute the built statement Results shown after execution Edit and parse; limited round trip engineering 15 Round Trip Engineering reverse engineer SQL Statement from a string to SQL model and from SQL model to SQL String. Need to click Execute twice bug. Will contrast this with the SQL Builder later on. 15
Parameters and Error Handling Error Handling for SQLState and SQLCode auto-generates output parms for each. Input parms created for host variables in the SQL Statement. 16 Create or Modify IN, OUT, INOUT parameters Host Variables in SQL Statement are automatically converted to INput variables of the SP SQL Types converted to Java Types for Java SPs Declare Exit Handler section for SQL SPs and catch / try block for Java SPs generated for Error Handling option selected Result set entry generates a RESULT SETS option If Result set is set to 1, but there are multiple statements, which query INPUT variable is also generated 16
Options for SQL Stored Procedure This page is skipped when server is DB2 LUW v8.2 Can globally set this for all SQL SPs in Preferences. Advanced Options enabled for z/os servers only 17 Options can be globally set in Preferences for SQL and Java SP, or specified in the wizard Go to Window > Preferences > Data > Stored Procedures > Build Options > Java and SQL For SQL SP: (DB2 LUW v8.1 and below), specify Collection ID Starting with DB2 UDB for LUW v8.2, collection id is no longer needed, because the SQL procedures are native. That is, they are not converted to C SPs. Java SP: Auto-generated Jar ID and Java Package Name For z/os, Collection ID specified is the one specified when binding the JDBC driver packages Select between Dynamic SQL using JDBC or Static SQL using SQLJ For SQLJ, specify the SQLJ Translator class and location SQLJ Translator information shipped with DB2 UDB is in db2java.zip for Customization job DSNTJJCL from <hlq>.sdsnsamp is run to bind the four DSNJDBC packages. This job binds the packages into a collection named DSNJDBC and also binds a plan DSNJDBC. These packages are required to be bound in order to run both JDBC and SQLJ Java stored procedures making JDBC calls: 17
Code Fragments and Source Code Launches the File Browser to point to code snipets. Inserted at various portions of the SP 18 Import from the File System code snippets to be inserted into the generated SP / UDF code For Java Stored Procedure Copyright info (header) Import statements Global Data Variables Common methods For SQL Procedures, insert code for: Copyright info (header) Variable declarations Exception handling Pre-return code 18
Summary Page Summarizes the user s entries or input Displays the Create Procedure DDL that will be used to catalog the SP 19 Back button to revisit input Finish button to exit the wizard. Workbench is refreshed; and Stored Procedure Code is displayed in the Editor View. Show SQL button is used to display the Create Procedure DDL that will be used to catalog the Stored Procedure. For SQL SP, this is the same code that shows up in the Editor. 19
Editor view of the Generated Code Build catalogs the SP; in z/os, calls DSNTPSMP Tabbed panels show properties of the SP Changes to source reflected in properties 20 Tool shows the generated code in the Editor View Tabs show properties of the SP/UDF Changes made to the source in the Editor View are reflected in the Properties Action Open displays the SP source in the Editor 20
Build and Build for Debug Build for Debug and Debug not Available for z/os Build messages 21 Build - deploys the SP to the current server using the options set during the creation. For z/os: SQL SPs, are built using the DSNTPSMP utility Java SPs are built using either the DSNTJSPP utility, when connected using the Legacy Driver; on the client side and jars are sent to the server, when connected using the DB2 Universal Driver. There are setup needed to enable DSNTPSMP and DSNTJSPP. When using the Java build utility, DSNTJSPP, the source is sent to the server and processed by DSNTJSPP. When NOT using DSNTJSPP, all processing is done on the client; and the files are just sent to the server. Can have source > 32 KB. For LUW: LUW v8.1 and below, need a C compiler to build the SQL SP Build for Debug Available for LUW SQL SP only Build successfully at least once first before Build for Debug and Debug is enabled. Process for building and running is the same as in Development Center. Right click on the Stored Procedure > Build If build is successful, then you can Run the SP Can also specify certain conditions before and after running the SP Generate a Java Bean from this Stored Procedure for use someplace else 21
Run and Debug 22 Run Executes the SP against the current server. Prompts user for INPUT variable values Reports any messages or errors in Messages tab of Output View Displays result sets in Results tab Displays values of all parameters in Parameters tab Status of build and run kept for each object Status lines can be removed for clarity Results for multiple result sets are shown one result set at a time. Messages can be Copied to the clipboard or Saved to a file Debug Launches the Debug Perspective Debugger is not covered in this presentation 22
Debug Perspective Debugging actions Shows threads running and the line number Variables used in the SP and their values Highlighted code is next line to be executed. 23 Debugger actions: Step into, Step Over, Step Return, Resume debug, 23
Run Settings, Delete, Copy/Paste Removes SP from Project; does not issue a Drop from the Server Copy SP from one project to another 24 Run Settings: Add queries before and after execution; Enter INput parameter values Delete: Remove SP or UDF from Project through the Delete menu item Does not issue a SQL Drop to the server Because SP is persisted as a file in the project, we can use Copy / Cut / Paste to move SP to another project. Still have to Deploy to execute 24
Export and Deploy Export wizard allows selecting one or more SPs to export and later deploy to another server. 25 Why export? Deploy several routines at once to a single target Deploy using binaries Deploy to another like server (i.e. same OS) Export options Export to Project Export to File System produces portable files Issue Export from the Stored Procedures or UDF folder 25
Export and Deploy to Project 26 Export creates files to facilitate deploying routines to a target server Files Generated: <rtn_name>.sql for SQL SPs and SQL UDFs. <rtn_name>.xml,.ddl files, jar, and Java Source folders <file name>.properties enter target server information DeployInstructions.txt Deploy from client command line For.sql files, use the db2 command line For.xml files, use the Ant Build utility ant -buildfile [filename].xml Requires ANT to be installed. Get from http://www.apache.org Deploy of exported script is not supported for z/os 26
Import a Routine from the File System or another project Import.sql or.java files (i.e. source) Import wizard panels similar to Create SP wizard Panels pre-populated with routine s data 27 Import routine from file system or another project Import.java or.sql files (i.e. source) Auto-generate jar id for Java SPs Option to deploy / build to the current server imported routine Advantage no need to go through wizard 27
Additional Features Connection Wizard: Filtering Specify the JDK and/or default Schema For z/os, Package and/or Build Owner Create UDF Wizard Create SP Wizard Java SP Options Advanced options for z/os when building SQL and Java SPs Generate a Java Bean from a Java SP Team Support 28 28
Connection Wizard - Filtering LIKE wildcard % expr filter Filter Schema, Table, Stored Procedure and UDFs Multiple filters Ordered; logical AND 29 Useful when connected databases is large (e.g. z/os) 29
Connection Wizard - options Tool gets this from JAVA_HOME; but is modifiable If blank, default to logon id For z/os connections, can set these to RACF group ids 30 SUBuilder picks the current JAVA_HOME environment on the client side as the initial value for the Java Home directory But user has ability to specify a different JDK level Specify a default schema to be used when objects created are not qualified. QUALIFIER bind option is generated when this is specified. Default value for SQL ID is the login id used to connect to the server. The connection login id is the DEFAULT SQL ID. When connecting to z/os, there are additional connection fields that can be specified when developing SPs. Package Owner is the authorization id that will be set as the OWNER in the BIND options Build Owner is the authorization id that will be used when the Create Procedure statement is executed; i.e. the owner of the SP If these fields are not specified, the logon id is used. RACF group ids that can be specified for either or both fields. 30
User Defined Functions Support for LUW only Create UDF Wizard guides user in creating a TABLE or SCALAR UDF Support for creating a Websphere MQ UDF Same features as SP No debugger support 31 Support for developing UDFs is available for DB2 on LUW only New UDF Wizard is similar to New SP wizard Support for Table and Scalar UDFs Support for Websphere MQ UDFs No Debugger support 31
Java Stored Procedure Options Jar ID and Java Package name are auto generated For SQLJ, root package is also auto generated Specify where the SQLJ Translator location 32 32
Advanced Options for z/os SQL SP Stored Procedure Options Runtime options WLM Environment Stay Resident External Security Build Options Compile Options Bind Options 33 External Security for DSNTPSMP is DB2 on the server. 33
Advanced Options for z/os Java SPs Build using DSNTJSPP if connected using the DB2 JDBC Driver (a.k.a. Legacy Driver) WLM used at runtime determines if using Legacy or JCC driver For SQLJ SPs, auto generate the root package name. Built packages will be named as: <root pkgname>1/2/3/4 34 34
Generate Java Bean Generate Java Bean Convert the Java SP into a Java Bean. Specify a Java project Specify a Java package (or default package) Can generate helper class to handle result sets Need to respecify the table and columns used for the result set. 35 35
Team Support Projects can be shared; then Checked in and Checked out 36 Can use either CVS or ClearCase. A CVS client is available with Eclipse. Using ClearCase requires setup with Rational ClearCase. For CVS, specify the file server used as the repository of your project. 36
Neat Stuff Not Found in Development Center Ability to work Offline Support for FullSelect and With Statement with SQL Builder Support for Comments within the Statement SQL Scrapbook features: Freestyle typing of a DML (limited) and DDL statements Open DDL statements in editor Execute statements Save statements into a project 37 For non-z/os, can work OFFLINE. SQL Scrapbook: From Database Explorer, click on Connection folder > Open SQL Scrapbook 37
References: Redbook: Through the Call and Beyond, http://www.redbooks.ibm.com/redpieces/pdfs/sg24 7083.pdf Debra Eaton s tutorial in developerworks, DB2 and Rational: Working Together, Part 1: Introduction to Rational Application Developer, http://www-128.ibm.com/developerworks/edu/dmdw-dm-0512eaton-i.html 38 38
Session F08 Session DB2 Stored Procedure and UDF Support in RAD V6 Marichu Scanlon IBM Corporation marichu@us.ibm.com 39 39