February 16, 2002 Douglas Clark is a program management consultant who first started using OS/2 version 1.3. He's married, with 2 girls, and is old enough to remember when 4 color mainframe terminals were a big thing. If you have a comment about the content of this article, please feel free to vent in the OS/2 eZine discussion forums. There is also a Printer Friendly version of this page. |
|
ODBC on OS/2 Part 11
| ||||||||||||||
OverviewIn designing the classes my goals were:
My intention was not to replace the ODBC API but rather to simplify the portion of the ODBC API that I worked with most often. PODBC is made up of 7 VisualAge version 4 classes which handle different aspects of working with ODBC data. While the classes are compiled with version 4 IOC components, I tried to not use any version 4 specific classes so that the classes could be compiled with VAC version 3. Those 7 classes are:
The first two classes, PEnvironment and PConnect, are always required; you can't do any real work without them. The rest of the classes are only required if you need the function provided by the class. The sequence used in a program that is accessing ODBC data is:
In the spirit of quick and dirty programing I am going to jump right to examples of programs that use the classes. Example 1 - Listing Data Sources Defined in ODBCThe first example is a very simple program that lists the data sources defined to ODBC. This program only uses the PEnvironment class because it does not actually connect to any data source. The list of data sources - name and description - are provided by the ODBC Driver Manager so you don't need any of the other classes to just list the data source names and descriptions
Line 6 is where the environmental object is created. The default constructor creates a environment that is linked to the local ODBC Driver Manager. This works fine for VIO type programs, i.e. non-PM type programs; or PM type programs that are going to connect to a non-Intersolv ODBC database driver, such as DB2/2 or PostgreSQL. For PM type programs that are going to connect to Intersolv drivers you need to use a different constructor. (This is because PM type programs that access Intersolv ODBC drivers bundled with an application, such as Lotus SmartSuite, throw up an annoy-ware type of dialog box every time you open a cursor. This alternate constructor opens a VIO session that handles the actual ODBC interface and handles the communications between the class methods and that session.) There are also alternate constructors for creating an environment that is linked to an ODBC Driver Manager on a remote machine, say a Windows NT machine for example. Line 8 opens the Data Source Name (DSN) list. Line 10 is a loop that fetches each row from the DSN list. Each row contains two columns: a name and a description. The Notice that the guts of the program are enclosed within a try block of a try-catch set. Any errors encountered by the classes result in an exception, which is picked up by the catch block. Line 18 outputs the PODBC error number and line 19 outputs the error message; if the error comes from the ODBC Driver Manager or database driver the entire ODBC error will appear in the error text. The output from the program is shown below.
Compare the output from the example program with the data sources lists in the ODBC Administrator.
To build the example the only files you need are the header file for the PEnvironment class and the library file needed for linking to the PODBC DLL. The figure below shows the VisualAge C++ version 4 configuration file used to build the example.
Line 8 identifies the target of the "build process," in this case xmp01.exe. Line 10 specifies the header file for the PEnvironment class. Line 11 specifies the library needed for the PODBC classes. Line 12 specifies the source code file that contains the example. The rest of the configuration file is pretty much standard. The settings shown in the rest of the file can be changed either in the IDE or by editing the configuration file. I prefer to edit the file because it is quicker. To run the program you need the following files somewhere in your LIBPATH
Example 2 - List Rows from a TableExample 2 connects to a data source and lists the rows in a table. The figure below shows the source code for the example.
Line 5 creates an environment object. Line 7 creates a connection object. Notice that the environment object is passed in as the first parameter to the connection object's constructor. The second parameter is the data source name (DSN) that we want to connect to. In this case we are connecting to a dBase database driver named dbase_xmp. In OS/2 the name specified for the DSN must exactly match, upper and lower case, the name as it is specified in ODBC. This can be a problem if you don't already know the name or if you cannot guarantee the form of the name as it is configured in ODBC. The There are 8 different constructors for the PConnect class. Most, like the constructor used in this example, automatically connect to the data source. Line 9 creates a cursor object. The PCursor object also has multiple constructors, most of which open the cursor when the object is constructed. The first parameter to the constructor is the connection object, which links the cursor to the connection. The second parameter is the SQL Line 11 starts a loop that fetches each row of data from the cursor's result set. The Line 13 sets up a loop that goes through each column of the result set. The Line 15 outputs the data for each column. The The results of running this example are show below.
The configuration file for building this example is show below.
Lines 11 and 12 add the headers for the PConnect and PCursor classes. Line 8 changes the name of the target. Line 14 specifies the name of the source file. Example 3 - Creating a Table, Displaying the Table Structure, Listing the DataExample 3 shows how to send non-select SQL statements to the data source. In this example we are creating a dBase table and inserting rows in the table. Then we use the PTableDef class to show the structure of the table. Finally we list the data from the table.
Line 5 creates an environment object. Line 7 creates a connect object. Once again we are connecting to a dBase ODBC driver. Since dBase is what ODBC calls a "file type" of database, a directory in the OS/2 file system represents the database, with each table being a file in that directory. The setup screen for the dBase driver, accessed from the ODBC Administrator, contains an entry where you specify what file system directory is to be the "database". You can use the Line 10 uses the to create the table in the C:\MYDATA directory instead of creating it in f:\temp. Line 13 creates a PTableDef object, which is used to list the columns that make up a table. Line 15 outputs the number of columns in the bdays table with the PTableDef method Line 17 starts a loop that runs through the columns on the table. Line 19 outputs the name and type of each column in the table. The Lines 22 - 26 insert rows into our new table. We are using an ODBC escape clause to enter date values. This is because each database has its own format for entering date values. For example some require the date string to be in the form mm/dd/yyy, others require dd-month-yyyy with the month spelled out. The ODBC escape clause lets us enter dates in one known format and the ODBC database driver is responsible for translating that into the format required by the database. There are ODBC escape clauses for specifying date, time, and timestamp values; the three most troublesome data types. On the other hand, dates are always returned by ODBC in the same format, YYY-MM-DD, regardless of how the database stores the data (we will see how to format the output value below.) While this helps greatly in dealing with dates from different databases there are still problems caused by some databases, Oracle and Microsoft Access to name two, that add a time value to date columns. So for those databases the date part of the value is returned by ODBC as YYYY-MMM-DD but some time value is also added to the end. Line 29 opens a cursor for the bdays table. Lines 31 - 33 output a heading that appears above the data using one of the PCursor methods that return characteristics of the result set. Once the cursor is open you can use various PCursor methods for finding out: how many columns are in the result set, the name of each column, the data type of each column, it size and precision, and if the database driver supports it, the table that column came from. Line 35 creates a FormatData object that we will use to format the date strings that are output in line 41. One of the ways the PODBC classes achieve simplicity is by converting all data output to IStrings - the Line 41 uses the The following figure shows the output for XMP03.EXE
In order to run the program you must have the FrmData.DLL file somewhere on your LIBPATH, in addition to the PODBC.DLL and PODBCDET.DLL files. The configuration file for building XMP03.EXE is shown below.
Line 13 adds the header for the FormatData class. Line 14 specifies the FormatData library. Line 15 adds the header for the PTableDef class. Technical DetailsThe PODBC classes use various IOC classes, such as IString and IException. The IBM license for VisualAge requires the those DLL files be renamed when they are distributed. Since the PODBC classes are useless without VisualAge C++ I do not include any of the IOC DLL files in the package - if you have the compiler you already have those files. I do not rename any of the IOC DLLs used in the PODBC package because there is a good chance you will use some of those same IOC DLLs in other parts of your program. This allows you to rename all instances of a IOC DLL at once for your program and the PODBC DLLs when you distribute it.The code for these examples is included in the PODBC classes. The PODBC classes appear on hobbes.nmsu.edu in three files: PODBC2vv.ZIP (where vv is a version number) contains the library and DLL files needed to use PODBC on OS/ 2 PODBCWvv.ZIP (where vv is a version number) contains the library and DLL files for Win32 ConclusionHopefully this has given you the "flavor" of the PODBC classes. While the PODBC classes are not the only C++ classes that exist for OS/2 I believe they are one of the simplest to use.Next month we will cover a visual version of the POBDC classes which turns the classes into parts for use in the Visual Builder.
|
||||||||||||||||
|