IBM DB2 for i and IBM Data Studio

76 downloads 11978 Views 2MB Size Report
Using IBM Data Studio to create, test, and deploy your DB2 for i SQL and Java stored procedures. Kent Milligan and Gene Cobb. ISV Business Strategy and ...
IBM DB2 for i and IBM Data Studio Using IBM Data Studio to create, test, and deploy your DB2 for i SQL and Java stored procedures

Kent Milligan and Gene Cobb ISV Business Strategy and Enablement July 2013

© Copyright IBM Corporation, 2013

Table of contents Abstract........................................................................................................................................1 Introduction .................................................................................................................................1 Creating a project........................................................................................................................2 Creating a new procedure ..........................................................................................................7 Creating an SQL procedure..................................................................................................................... 7 Creating a Java procedure .................................................................................................................... 13

Importing procedures ...............................................................................................................18 Running a procedure ................................................................................................................19 Exporting and deploying procedures......................................................................................20 Exporting to a deployment script ........................................................................................................... 20 Deploying to target database................................................................................................................. 22

Accessing the physical data model ........................................................................................23 Summary....................................................................................................................................32 Resources..................................................................................................................................33 About the authors .....................................................................................................................34 Trademarks and special notices..............................................................................................35

IBM DB2 for i and IBM Data Studio

Abstract This white paper helps you understand the IBM Data Studio (formerly known as IBM DB2 Developer Workbench and IBM DB2 Development Center) capabilities that help you in the creation, testing, and deployment of SQL and Java stored procedures as well as user-defined functions on IBM DB2 for i.

Introduction IBM® Data Studio provides an integrated development environment that enables the development of data-centric application. This eclipse-based set of development tools supports the entire IBM DB2® family of database offerings; therefore, it is an especially useful tool if you are developing and deploying procedures and functions on multiple DB2 server platforms. IBM Data Studio is the replacement product for the IBM DB2 Developer Workbench that was first introduced with DB2 Version 9. Data Studio supports the development of SQL and Java™ stored procedures as well as user-defined functions when used with DB2 for i servers or any other DB2 server. Even though DB2 for i supports user-defined functions and external stored procedures that are written in high-level languages such as RPG and COBOL, Data Studio does not support the creation of these objects on the IBM i platform. You can also use the Data Studio debugger to debug SQL stored procedures and user-defined functions that were created on a DB2 for i server. For more information about using the Data Studio debugger, refer: https://ibm.biz/BdxpniIBM i Developers can download Data Studio from the following website: ibm.com/developerworks/downloads/im/data/ The following website has a complete listing of the Data Studio features that are supported with DB2 for i databases. ibm.com/support/docview.wss?uid=swg27022150

IBM DB2 for i and IBM Data Studio

1

Creating a project IBM Data Studio manages the development of stored procedures and function objects through the use of projects. Projects keep track of the database connections and associated procedure objects. For example, you can use a single project to group and manage all of the stored procedures that are associated with a single application (for example, payroll application). To launch Data Studio and create a new project, perform the following steps: 1. In the Microsoft® Windows® Start menu, click All Programs → Data Studio → Data Studio Client. You are given a choice to create a new project or to open an existing project. The New Data Development Project wizard appears (refer to Figure 1) when creating a new project.

Figure 1. Data Studio Data Development Project creation wizard

2. Click Next to continue the project creation process.

IBM DB2 for i and IBM Data Studio

2

3. The next step is identifying the connection to use for the new project from the Select Connection page (refer to Figure 2). You can select a previously created database connection by clicking the connection name or by creating a new connection.

Figure 2. Select Connection page

4. You can define a new connection on the Connection Parameters page (shown in Figure 3). The default naming convention results in the generation of a connection name from the database or host name for your IBM i server. From the no driver drop-down list, you can select any of the three options: 

Option 1: The majority of IBM i programmers select AS/400 Toolbox for Java because this JDBC driver is available at no charge. The IBM Toolbox for Java is automatically included with the IBM i operating system software. You can also download this JDBC driver from the

IBM DB2 for i and IBM Data Studio

3

JTOpen website (http://jt400.sourceforge.net). JTOpen is the open-source version of the IBM Toolbox for Java. The Host name is the TCP/IP host name of your IBM i server. This interface also allows you to override the default schema value (the authorization ID) that is used while running the SQL routine for any SQL statements that contain unqualified table or view references. If a value is not specified, the default value is Authorization ID. The IBM i equivalent of an authorization ID is a user profile.

Figure 3. Connection parameters page

IBM DB2 for i and IBM Data Studio

4



Option 2: It is also possible to use one of the IBM Data Server Driver for JDBC options. This driver is also known as the DB2 JCC or DB2 Universal JDBC driver. You must select this driver if you want to use the Data Studio debugger. The IBM Data Server Ddriver for JDBC is also a Type-4 JDBC driver (as is the Toolbox JDBC driver). In contrast, the Data Server Driver for JDBC is not included within the IBM i operating systems, but it is included with the Data Studio download along with a DB2 Connect license that enables access of DB2 for i servers from Data Studio. For the Data Server Driver for JDBC, the Database name is obtained by issuing the IBM i WRKRDBDIRE command on the command line. The Host name is again the TCP/IP host name of the IBM i server.



Option 3: You can also select Other to use a driver from a third-party software provider.

5. You can either verify that you have set up the connection correctly by clicking Test Connection or move on by clicking Next. 6. The Data Privacy Modeling step allows you to associate the new connection with a physical data model. This is an optional step. Click Next to continue. 7. The Filter page (refer to Figure 4) allows you to include particular schemas that are accessible or inaccessible to a database connection.

IBM DB2 for i and IBM Data Studio

5

Figure 4. The Filter page to specify a schema

8. Click Finish to complete the project creation.

IBM DB2 for i and IBM Data Studio

6

Creating a new procedure At this point, you have completed all of the required configuration steps. You are now ready to use Data Studio to code your stored procedures.

Creating an SQL procedure To create an SQL procedure, perform the following steps: 1. To begin coding a new procedure, right-click the project you just created in the Data Project Explorer view and click New → Stored Procedure (refer to Figure 5).

Figure 5. Data Studio window

IBM DB2 for i and IBM Data Studio

7

2. The New Stored Procedure window appears. To create either an SQL or Java stored procedure, from the Language drop-down menu (as shown in Figure 6), select SQL. To jump start your stored procedure development efforts, select one of the listed templates. In this example, the IN/OUT template was selected. If the template description does not contain sufficient information, then you click the DDL link or the DDL tab to get a preview of the code template. If you want to create your own SQL procedure templates or modify an existing template to meet your coding standards, then click the Routines > Template link. .

Figure 6. Selecting SQL as the stored procedure language type

IBM DB2 for i and IBM Data Studio

8

3. Click Finish to launch the stored procedure editor (shown in Figure 7). Notice how the SQL keywords are color-highlighted. This highlighting is not supported by the IBM i Navigator SQL editor. From the editor, you can modify and also add to the selected SQL code template. As you add new SQL statements, the editor automatically validates the syntax of the new statements. Any SQL statement containing invalid syntax will be flagged with a red X in the left margin of the editor.

Figure 7: Parameters window for stored procedures

IBM DB2 for i and IBM Data Studio

9

4. After the editing of your SQL procedure source code has been completed and saved, you are ready to create the procedure on a DB2 for i server. To initiate the creation process, you can click the Deploy icon at the upper-right corner of the editor or right-click in the edit window and click Deploy. On the Deploy Options page that is displayed (refer to Figure 8), you can specify that the stored procedure be built (object created) on a different database (or system) that the development project is associated with. You can also control the target schema and the SQL path values used for creating the SQL procedure. The Duplicate handling option controls whether or not Data Studio should automatically try to delete any existing stored procedures with the same name or return an error if that stored procedure already exists on the server.

Figure 8: Initial Deploy Options page for stored procedures

IBM DB2 for i and IBM Data Studio

10

5. Click Next to advance to the remaining set of deployment options (refer to Figure 9). The Enable debugging option is the only option available for DB2 for i SQL procedures and functions. This option enables the usage of the Data Studio debugger with the stored procedure. Precompile and compile options can be specified for DB2 for i objects by including the SET OPTION clause within the source code of your SQL routine.

Figure 9: Specifying additional deployment options

IBM DB2 for i and IBM Data Studio

11

6. Click Finish to complete the creation of the stored procedure on the specified DB2 for i server. In the lower-right corner of the Data Studio window (refer to Figure 10), a message area shows the status of the stored procedure creation process.

Figure 10: Data Studio window

As you can see in the messages area, the stored procedure was successfully created. If an error was identified, you can go into the Source Edit view (at the upper-right corner of the Data Studio window) to make changes to the code. .

IBM DB2 for i and IBM Data Studio

12

Creating a Java procedure Data Studio also has the ability to create Java procedures. This feature generates stub Java code with the appropriate SQL statements; even if your Java skills are limited, you might find it quite useful. To create a new Java procedure, perform the following steps: 1. In the Project View pane, right-click Stored Procedures. Then, click New → Stored Procedure (refer to Figure 11).

Figure 11: Creating a new Java procedure

IBM DB2 for i and IBM Data Studio

13

2. On the Name, Language, and Template page, select Java as the procedure type (see Figure 12). You probably want to specify your own Java package name, instead of using the cryptic package name that Data Studio generates by default. The Java package name is the basic grouping unit within a Java stored-procedures environment; it allows you to associate related stored procedures. Notice how the templates provide two different Java database-access methods on this dialog. The majority of Java programmers who develop applications for the IBM i operating system, use the Dynamic JDBC access method. SQLJ has the same performance signature as Dynamic JDBC — because the internal SQL plan caches are automatically employed by DB2 for i. You have the ability to preview and customize the Java stored procedure templates similar to how you can for SQL stored procedures. After you have selected the required template, click Finish.

Figure 12: Selecting Java as the procedure type

IBM DB2 for i and IBM Data Studio

14

3. Next, you will be presented with the Java source code editor (as shown in Figure 13).

Figure 13: Java code editor window

After the Java source code has been completed and saved, start the deployment process by clicking the Deploy icon at the upper-right corner or right-clicking in the edit window and clicking Deploy. The intial Deployment Options window is identical to the options available for SQL procedures (refer to Figure 9). Click Next to access the final set of deployment options displayed in Figure 14. From here, you can enable debug or change the JDK settings. Click Finish to start the deployment process.

IBM DB2 for i and IBM Data Studio

15

Figure 14: Final deploy options for Java stored procedures

IBM DB2 for i and IBM Data Studio

16

4. Data Studio attempts to create this Java procedure on the specified DB2 for i server. In the lowerright corner of the window (Figure 15), a message area shows the status of the creation process for the stored procedure. If the Java stored procedure creation was successful, the JAR file for the Java stored procedure is placed in the following directory on the server: /QIBM/UserData/OS400/SQLLib/Function

Figure 15: Java stored procedure successfully built

At this point, you can use the visual editor to further modify the Java procedure code.

IBM DB2 for i and IBM Data Studio

17

Importing procedures In addition to creating new procedures, you can import existing procedures into your project. This allows you to organize all of your SQL and Java stored procedures into projects, which simplifies maintenance and deployment. You can import these objects from another database connection, an existing project or a file that contains the SQL procedure source code. From the Data Project Explorer view, you can access an Import function by right-clicking the Stored Procedures folder in the Project view. However, this Import function only supports procedure imports from an existing project or a file system object. Thus, you must use the Database Explorer for importing stored procedures from a DB2 for i server. You can initiate the import process from the Database Explorer view at the lower-left corner of the Data Perspective (refer to Figure 16). 1. Select the database connection for the server (that is, jtdbv7r1) where the stored procedure resides. 2. Select the schema (or library) where the stored procedure is created (in this example, KMILL). 3. With the schema now identified, you can select the procedure from the Stored Procedures folder view under the schema. (Note: The easiest way to import the selected stored procedure into Data Studio is to drag the procedure (follow the arrow) into the Stored Procedures folder in the Data Project Explorer view.)

Figure 16: The import process

IBM DB2 for i and IBM Data Studio

18

Running a procedure After you successfully create or import the procedure, you can use Data Studio to run and test it. Data Studio also allows you to run any existing stored procedure on your IBM i system. To run a stored procedure, perform the following steps: 1. Right-click the procedure object and click Run. If the stored procedure has input parameters, a window similar to the input-parameter window (shown in Figure 17) is displayed to prompt you for the required input values. The Load Values and Save Values options make it easy to repeat testing of stored procedures with a defined set of test values. After the logic in the stored procedure has been changed, the Load Values option can be used to perform regression testing by cycling through a set of saved input parameter values to test with. The only supported option for DB2 for i SQL routines on the Run and Performance Options tab is the Commit changes to the database option. If your DB2 for i database is journaled, this option allows the database changes performed by the stored procedure to be made permanent.

Figure 17: Options to specify the parameter values

IBM DB2 for i and IBM Data Studio

19

2. Specify the input values and click OK to run the procedure. Again, the status and output from running the stored procedure is shown in the lower-right corner of the Data Studio window. On the Parameters and Status tabs, you can view any output parameters or result sets that are returned.

Exporting and deploying procedures Data Studio provides two methods for deploying stored procedures: exporting to a deployment script or deploying directly to a target database.

Exporting to a deployment script The deployment option that involves exporting to a script file essentially puts your stored-procedure source code into a text file. This method is appropriate when your intent is to generate a script that you can later load into a change-management tool or IBM i Navigator. You can deploy stored procedures that are stored in the script file by running the script again. Depending on the target database, you might have to modify the exported script files. 1. Right-click a project’s Stored Procedures folder (refer to Figure 18) and click Export.

Figure 18: Exporting stored procedures

IBM DB2 for i and IBM Data Studio

20

The Export Selection window is displayed (refer to Figure 19).

Figure 19: Export –Selection window

2. Select the procedures to export. When you have made all of the selections, click Next. The export target and export options are displayed (refer to Figure 20).

Figure 20: Export target and export options

IBM DB2 for i and IBM Data Studio

21

3. Specify the path and file name of the export file. The Include DROP statements check box enables Data Studio to include DROP statements in the export file for each procedure. This provides users of the export file with a script that cleans up itself by deleting old versions of the stored procedure before creating a stored procedure. 4. When you have all the appropriate options selected, click Next. 5. A summary that enables you to review your settings is displayed. Click Finish to complete the export request.

Deploying to target database The deploying to target database option lets you deploy objects immediately to a target production database. This method is appropriate if you determine that an intermediate export step is not required and that the objects and production environment are ready for immediate deployment. This requires a connection to the server. To deploy the project’s stored procedures, right-click the project’s Stored Procedures folder or right-click an individual stored procedure and click Deploy. The deployment options are displayed (refer to Figure 21).

Figure 21: Deployment options

After a procedure has been selected for deployment, click Next. The next set of options will match the deployment option already described in the SQL and Java stored procedure sections.

IBM DB2 for i and IBM Data Studio

22

Accessing the physical data model Data Studio also provides easy access to the underlying database that is accessed by the stored procedures you are creating and maintaining with its physical data model support. This support provides a graphical representation of the underlying database to make it easier to understand the relationships within your database. This graphical representation is generated by right-clicking within the Data Project Explorer pane and clicking New  Physical Data Model (as shown in Figure 22).

Figure 22: Creating a new physical data model

The next step is identifying the target database to model (as shown in Figure 23). The first input is specifying the project that will house the data model. The specified project must have been created as a Data Design Project. The next input field allows you to name the data model. In this example, the name is My Database Model. Obviously, the database value will be DB2 for i and then you need to select the IBM DB2 for i and IBM Data Studio

23

operating system version that is installed on your target IBM i system. In this case, an existing database is being accessed by your stored procedures, so select the “Create from reverse engineering check box. After selecting this option, click Next to continue.

Figure 23: Physical Data Model Connection window

IBM DB2 for i and IBM Data Studio

24

On the Select Connection page (as shown in Figure 24), select the connection for the DB2 for i server containing the database that will be reverse engineered. Click Next to advance to the next step of the reverse engineer process.

Figure 24: Specifying the connection for the DB2 for i server

IBM DB2 for i and IBM Data Studio

25

Figure 25 displays the Select Objects page where you can specify the schema (or library) that contains the DB2 objects that you want to reverse engineer. You can select a single schema or multiple schemas. You can select the schema from the list of schemas associated with the schema or type in a filter string to narrow the list to your target schema. After selecting the schema, click Next to advance to the final step in the process.

Figure 25: Selecting the schema

IBM DB2 for i and IBM Data Studio

26

The Database Elements page (shown in Figure 26) allows you to select the type of database objects that can appear in your physical data model. In this example, tables are the only object type that will be included in the diagram. After selecting the required database element types, click Finish to build the physical data model.

Figure 26: Database element type selection

IBM DB2 for i and IBM Data Studio

27

After the physical data model is built, you will be taken to the Physical Data Model Editor view (refer to Figure 27). To generate the visualization diagram, your first step is to expand the data object (ctcdbv7r1.rchland.ibm.com) associated with the physical data model object (My Database Model.dbm). After the data object has been expanded, you need to right-click the Diagrams folder and click New Overview Diagram.

Figure 27: Physical Data Model Editor view

IBM DB2 for i and IBM Data Studio

28

The first input field in the Overview Diagram Selection dialog box (shown in Figure 28) allows you to supply a name for the diagram. The Implicit relationships option enables Data Studio to find relationships between DB2 objects by looking for common column names. Data Studio uses foreign key constraint definitions to find explicit relationships between tables. The Include selected elements section enables you to include a subset of the objects from the data model in the diagram. If a selected DB2 object references an unselected DB2 object, the Automatically select referenced elements check box controls whether or not that object is included in the diagram.

Figure 28: Overview Diagram Selection dialog box

IBM DB2 for i and IBM Data Studio

29

Figure 29 contains the data diagram generated by Data Studio for the selected database objects. The boxes in the diagram (for example, Project) are the tables in the data model’s schema. The identifier’s associated with the connecting lines between tables are the constraint names. Data Studio enables you to rearrange objects in the diagram, change text formatting, and add notes to the diagram to make the diagram easier to read. You can also add database objects that were not selected earlier in the process.

Figure 29: Data diagram output

IBM DB2 for i and IBM Data Studio

30

When you are developing stored procedures, you might need to access the column definition for a table being processed by the stored procedure. This can be done from the model diagram by first clicking the table object in the diagram. After the table is selected, right-click and click Show Properties View (as shown in Figure 30). After that task has been selected, the lower-right pane of the window displays a table properties view. Clicking on the Columns tab provides a list of the columns in the table and the attributes for each column. Data Studio also allows you to print the diagram or save the diagram into an image file, so that the diagram can be easily shared with other team members. The Data Studio physical data model and data diagram tooling does support DB2 for i objects created with data description specifications (DDS). Data Studio categorizes logical file objects as views, so keep that in mind when selecting which database object types to include in your physical data model.

Figure 30: Table properties view

IBM DB2 for i and IBM Data Studio

31

Summary The intent of this white paper is to help you understand the Data Studio capabilities and how you can use them to create, test, and deploy SQL and Java stored procedures on DB2 for i. This iterative development toolset provides an environment that can simplify your efforts to create and encapsulate your business logic inside SQL stored procedures.

IBM DB2 for i and IBM Data Studio

32

Resources The following websites provide useful references to supplement the information contained in this paper: 

IBM i Information Center http://publib.boulder.ibm.com/eserver/ibmi.html



IBM Redbooks® ibm.com/redbooks



IBM i on IBM PartnerWorld® ibm.com/partnerworld/i



DB2 for i online manuals ibm.com/systems/i/db2/books.html



DB2 Information Center http://publib.boulder.ibm.com/infocenter/db2help



IBM Toolbox for Java driver download site ibm.com/systems/i/software/toolbox



IBM white paper: Graphical debugging makes procedural SQL debugging on IBM i even easier ibm.biz/Bdx3Y9



IBM Data Studio ibm.com/software/products/us/en/data-studio/



Data Studio download ibm.com/developerworks/downloads/im/data/



IBM developerWorks forums: -

Data Studio: ibm.com/developerworks/forums/forum.jspa?forumID=1086&start=0

-

IBM i: ibm.com/developerworks/forums/forum.jspa?forumID=2675&start=0

-

DB2 for i: ibm.com/developerworks/forums/forum.jspa?forumID=292&start=0

IBM DB2 for i and IBM Data Studio

33

About the authors Kent Milligan is a DB2 technology specialist in IBM ISV Business Strategy and Enablement. Kent spent many years of his IBM career as a member of the DB2 development group in Rochester, Minnesota. He speaks and writes regularly on various IBM i relational database topics.

Gene Cobb is a DB2 technology specialist in IBM ISV Business Strategy and Enablement. He has worked on IBM midrange systems since 1988, with 10 years in the IBM Client Technology Center (CTC), IBM Rochester. During his tenure at the CTC, he assisted IBM clients with application design and development using RPG, DB2 for i, IBM CallPath/400 and IBM Domino® applications. His current responsibilities include providing consulting services to System i developers, with special emphasis in application and database modernization.

IBM DB2 for i and IBM Data Studio

34

Trademarks and special notices © Copyright IBM Corporation 2013. References in this document to IBM products or services do not imply that IBM intends to make them available in every country. IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol (® or ™), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at "Copyright and trademark information" at www.ibm.com/legal/copytrade.shtml. Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. Java and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or its affiliates. Other company, product, or service names may be trademarks or service marks of others. Information is provided "AS IS" without warranty of any kind. All customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have achieved. Actual environmental costs and performance characteristics may vary by customer. Information concerning non-IBM products was obtained from a supplier of these products, published announcement material, or other publicly available sources and does not constitute an endorsement of such products by IBM. Sources for non-IBM list prices and performance numbers are taken from publicly available information, including vendor announcements and vendor worldwide homepages. IBM has not tested these products and cannot confirm the accuracy of performance, capability, or any other claims related to non-IBM products. Questions on the capability of non-IBM products should be addressed to the supplier of those products. All statements regarding IBM future direction and intent are subject to change or withdrawal without notice, and represent goals and objectives only. Contact your local IBM office or IBM authorized reseller for the full text of the specific Statement of Direction. Some information addresses anticipated future capabilities. Such information is not intended as a definitive statement of a commitment to specific levels of performance, function or delivery schedules with respect to any future products. Such commitments are only made in IBM product announcements. The information is presented here to communicate IBM's current investment and development activities as a good faith effort to help with our customers' future planning. Performance is based on measurements and projections using standard IBM benchmarks in a controlled environment. The actual throughput or performance that any user will experience will vary depending upon considerations such as the amount of multiprogramming in the user's job stream, the I/O configuration, the storage configuration, and the workload processed. Therefore, no assurance can be IBM DB2 for i and IBM Data Studio

35

given that an individual user will achieve throughput or performance improvements equivalent to the ratios stated here. Photographs shown are of engineering prototypes. Changes may be incorporated in production models. Any references in this information to non-IBM websites are provided for convenience only and do not in any manner serve as an endorsement of those websites. The materials at those websites are not part of the materials for this IBM product and use of those websites is at your own risk.

IBM DB2 for i and IBM Data Studio

36