|Use SQuirreL SQL During RPG Development with Embedded SQL|
|Programming - SQL|
|Written by Thomas Snyder|
|Wednesday, 16 March 2011 01:00|
Any OS, any programming language, any database—SQuirreL is a perfect fit for almost any situation involving SQL!
In a previous article, I showed you how to install the SQuirreL SQL client to run interactive SQL on your IBM i. With all the new stuff to learn about and all the options that are out there, sometimes a little introduction to the basics will be all it takes to get rolling. In this article, I'll cover some of my favorite features that come with it.
A few months ago, I wrote an article called "Format and Compile RPG to Use Embedded SQL," which showed how you could evolve an RPG program to use embedded SQL. I will be using the content of that article to demonstrate how SQuirreL would be a good companion during testing and development of that code.
As a refresher, the code was written to illustrate how to retrieve the data from two files:
Our goal will be to write a SQL statement that will retrieve the content from both files using a JOIN on the primary account number key.
The DDS for the files are available at the link above, but if you're using SQuirreL, you could easily retrieve the field names a couple of different ways. One quick and easy way is to run a SELECT * over the table.
Figure 1: Use SELECT * to get the field names within a table. (Click images to enlarge.)
Here, you can see the field names in the header of the results grid displayed at the bottom.
Suppose you forgot the name of the file or you want to browse all the files in your library and do not want to switch back and forth between your SQuirreL and green-screen. You can browse the library using the Objects tab.
Figure 2: Browse the physical files in a library using the Objects tab.
Using the Objects tab, you can see all the files in the AIRLIB library by clicking on the TABLE node in the navigation tree. By selecting the EMPLOYEE table, you could click on the Columns tab to view the fields that are available in the EMPLOYEE table.
Using Automatic Completion (CTRL+Space Bar)
Once you have the field names, you could click on the SQL tab to return to the SQL entry screen to build your query and run it in SQuirreL.
While entering your SQL statement, you could take advantage of the auto completion option that is available by clicking CTRL+space bar. This will create a pop-up window to assist with completing the code that you are entering.
Figure 3: This screen shows automatic completion, running man. and limit rows in SQuirreL.
Executing the SQL (Running Man and CTRL+Enter)
The CTRL+ENTER shortcut is one of my favorites and was also a favorite of one of my readers. Once you have your SQL statement entered, you can select the statement by clicking and dragging over it to select it. Then you can run it by clicking on the running man button in the tool bar, or you can use CTRL+ENTER.
Changing the Number of Records in your Results
By default, the results will contain only 100 records. When your results have more than 100 records, you will not see them unless you increase this number. Above the SQL entry text area, you can see the "Limit rows" checkbox on the top right.
Selecting the "Limit rows" checkbox will limit the number of rows displayed in the grid at the bottom to be no more than the number of rows specified to the right of the checkbox. Unselecting the checkbox will display all the records.
Copying and Pasting with IBM Rational Developer
When you have the SQL query just the way you want it, you can easily copy and paste your code into RDi. And if you need to perform further analysis on preexisting SQL that is embedded in RPG, you can easily lift the code from an RPG program and paste it into SQuirreL for troubleshooting.
Figure 4: Easily copy and paste code between SQuirreL and IBM Rational Developer.
Copying and Pasting with Headers
Another one of my favorite features is copy and paste with headers. When you run a query in SQuirreL, it will display your results in a grid at the bottom of the screen. You can easily copy and paste the results from SQuirreL into Excel. I've used this many times to modify an existing query to list all of the detail records that make up a summary value.
Suppose the results of your sum(MHNET) are in question. You can copy and paste the code from your RPG program into SQuirreL, remove the sum() from around MHNET, and remove the GROUP BY segment to list all of the detail records. Then, you can copy with headers into Excel, put a sum formula cell at the bottom of your MHNET column, and forward to the questioning party to display all of the detail records that make up the total.
Figure 5: Use Copy with headers to copy data into an Excel spreadsheet.
To select all of the results, right-click on any of the records in the results and click on Select All. Then right-click on a record again and select Copy with headers. With an open Excel spreadsheet, paste your results and perform any calculations.
You can save your queries into a file to be recalled at a later date. This is useful for SQL statements that you may be using to create a patch program or simply for SQL statements that you run on a regular basis.
Figure 6: Save SQL files and create new SQL worksheets.
To save your SQL to a file on your hard disc, select Session on your menu bar, then File, and then your option, such as Save As.
Using Multiple Worksheets
You can have multiple worksheets open to keep your queries organized. Go to New Worksheet or hit CTRL+N. I typically keep one worksheet clean, with my single statement that I am working with, and have a second worksheet open as a scratch pad area to run SQL segments that would be used to create the single SQL statement that I am building.
Using Multiple Aliases
Along with multiple worksheets, you can also have multiple aliases. Multiple aliases are good for different environments. You could have a test library on one and production on another. Once you have your SQL tested out, you can copy and paste over to your production alias and execute it.
Figure 7: Create multiple aliases with SQuirreL.
Using Graphical Development Tools and Open Source
In development environments where you are supporting multiple databases from different vendors, it would be undesirable to have a different specialized software application for each separate database. Not only would you be required to become familiar with each of the tools and the different methods to do the same thing in each application, you would also need to maintain each one with installs and updates, and if you're paying for each one, that would require the cost of each tool multiplied by the number of databases that you are supporting.
If you use SQuirreL to access your databases, you can use SQuirreL SQL as one standardized application that accesses them all. You would be able to use one tool for every database, with no additional dependencies or costs. And you could run it on any operating system that supports Java.
So Many Possibilities
These are exciting times for open source on the IBM i. Just think of all the compatibility angles. You could be developing on a Windows, Linux, or Mac client for an IBM i, Linux, Windows, or Mac server, using RPG, Java, PHP, or whatever language, and your SQL code could be interactively tested in SQuirreL for DB2, Microsoft SQL, Sybase, Oracle, MySQL, or whatever database you chose! This makes SQuirreL a perfect fit for almost any situation involving SQL!
In my next article, I will be closing the SQuirreL series by discussing schemas and library lists. So keep an eye out for it next month. Happy St. Patrick’s Day!
JTOpen: The Open Source version of the IBM Toolbox for Java
Some of the graphics in this article were found at http://www.openclipart.org/.
as/400, os/400, iseries, system i, i5/os, ibm i, power systems, 6.1, 7.1, V7,
|Last Updated on Wednesday, 16 March 2011 01:00|