Sample Modified Source File
The following modified source file is the result of preprocessing
program cex2 (shown at the end of Chapter 1). In the listing, the
preprocessor-added code is shaded for easy reference. The numbers in the
figure are the same as the numbers shown for program cex2.
____________________________________________________________________________
| |
| #include "sqltype"\ |
| #include "sqlvar"\ |
| #include "sqlextn" |
| /* Program cex2 */ |
| |
| /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */|
| /* This program illustrates the use of SQL's SELECT command to */|
| /* retrieve one row or tuple of data at a time. */|
| /* BEGIN WORK is executed before the SELECT and a COMMIT WORK */|
| /* is executed after the SELECT. An indicator variable is also */|
| /* used for SalesPrice. */|
| /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */|
| |
| typedef int boolean; |
| |
| char response[2]; |
| boolean Abort; |
| |
| #include <stdio.h> |
| |
| #define OK 0 |
| #define NotFound 100 |
| #define MultipleRows -10002 |
| #define DeadLock -14024 |
| #define FALSE 0 |
| #define TRUE 1 |
| |
| |
| #if 0 |
| EXEC SQL INCLUDE SQLCA; 1 |
| #endif\ |
| sqlca_type sqlca; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
____________________________________________________________________________
Figure 2-9. Modified Source File For Program cex2
________________________________________________________________________
| |
| /* SQL Communication Area */ |
| /* Begin Host Variable Declarations */ |
| |
| #if 0 |
| EXEC SQL BEGIN DECLARE SECTION; 2 |
| #endif |
| |
| char PartNumber[17]; |
| char PartName[31]; |
| double SalesPrice; |
| sqlind SalesPriceInd; |
| char SQLMessage[133]; |
| |
| #if 0 |
| EXEC SQL END DECLARE SECTION; 2 |
| #endif |
| |
| /* End Host Variable Declarations */ |
| |
| int SQLStatusCheck() /* Function to Display Error Messages */|
| { |
| Abort = FALSE; |
| if (sqlca.sqlcode < DeadLock) |
| Abort = TRUE; |
| do { |
| |
| #if 0 |
| EXEC SQL SQLEXPLAIN :SQLMessage; |
| #endif\ |
| {\ |
| sqlxplnc(&sqlca,&sqltempv,133,1);\ |
| sqltempv.rec1.\ |
| SQLREC1_FIELD1[sqltempv.rec1.SQLREC1_FIELD1_LEN] = '\0';\ |
| strcpy(SQLMessage,\ |
| sqltempv.rec1.SQLREC1_FIELD1);\ |
| } 3 |
| |
| printf("\n"); |
| printf("%s\n",SQLMessage); |
| } while (sqlca.sqlcode != 0); |
| |
| if (Abort) |
| { |
| EndTransaction(); |
| ReleaseDBE(); |
| } |
| } /* End SQLStatusCheck Function */ |
________________________________________________________________________
Figure 2-9. Modified Source File For Program cex2 (page 2 of 6)
_________________________________________________________________________________
| |
| boolean ConnectDBE)() /* Function to Connect to PartsDBE */|
| { |
| boolean ConnectDBE; |
| ConnectDBE = TRUE; |
| printf("\n Connect to PartsDBE"); |
| |
| #if 0 |
| EXEC SQL CONNECT TO 'PartsDBE'; |
| #endif\ |
| {\ |
| sqlvar1 = "00AE00005061727473444245202020202020202020202020\\ |
| 202020202020202020202020202020202020202020202020202020202020\\ |
| 202020202020202020202020202020202020202020202020202020202020\\ |
| 202020202020202020202020202020202020202020202020202020202020\\ |
| 202020202020202020202020202020202020";\ |
| sqlxconc(&sqlca,sqlvar1);\ |
| } 4 |
| |
| if (sqlca.sqlcode != OK) |
| { |
| ConnectDBE = FALSE; |
| SQLStatusCheck(); 5 |
| } /* End if */ |
| return (ConnectDBE); |
| } /* End of ConnectDBE Function */ |
| |
| int ReleaseDBE() /* Fundtion to Release PartsDBE */ |
| { |
| printf("\n"); |
| printf("\n Release PartsDBE"); |
| printf("\n"); |
| |
| #if { |
| EXEC SQL RELEASE; |
| #endif\ |
| {\ |
| sqlvar2 = "00B200002020202020202020202020202020202020202020FFFFFFFF";\ |
| sqlxconc(&sqlca,sqlvar2);\ |
| } 6 |
| |
| if (sqlca.sqlcode != OK) SQLStatusCheck(); |
| } /* End ReleaseDBE Function */ |
| |
| boolean BeginTransaction() /* Function to Begin Work */ |
| { |
| boolean BeginTransaction; |
| BeginTransaction = TRUE; |
_________________________________________________________________________________
Figure 2-9. Modified Source File For Program cex2 (page 3 of 6)
______________________________________________________________________
| |
| printf("\n"); |
| printf("\n Begin Work"); |
| |
| #if 0 |
| EXEC SQL BEGIN WORK; |
| #endif\ |
| {\ |
| sqlvar3 = "00A6007F00110061";\ |
| sqlxconc(&sqlca,sqlvar3);\ |
| } 7 |
| |
| |
| if (sqlca.sqlcode != OK) |
| { |
| BeginTransaction = FALSE; |
| SQLStatusCheck(); |
| ReleaseDBE(); 5 |
| } /* End if */ |
| return (BeginTransaction); |
| } /* End BeginTransaction Function */ |
| |
| int EndTransaction() /* Function to Commit Work */ |
| { |
| printf("\n"); |
| printf("\n Commit Work"); |
| |
| #if 0\ |
| EXEC SQL COMMIT WORK; |
| #endif\ |
| {\ |
| sqlvar4 = "00A10000";\ |
| sqlxconc(&sqlca,sqlvar4);\ |
| } 8 |
| |
| if (sqlca.sqlcode != OK) SQLStatusCheck(); |
| } /* End EndTransaction Function */ 5 |
| |
| int DisplayRow() /* Function to Display Parts Table Rows */|
| { |
| printf("\n"); |
| printf("\n Part Number: %s\n", PartNumber); |
| printf(" Part Name: %s\n", PartName); |
| |
| if (SalesPriceInd < 0) { |
| printf(" Sales Price: is NULL \n"); |
| } |
| else |
| printf(" Sales Price: %10.2f\n", SalesPrice); |
| } /* End of DisplayRow */ |
______________________________________________________________________
Figure 2-9. Modified Source File For Program cex2 (page 4 of 6)
_____________________________________________________________________________
| |
| int Select() /* Function to Query Parts Table */ |
| { |
| do { |
| printf("\n"); |
| printf("\n Enter Part Number within Parts Table or '/' to STOP > ");|
| scanf("%s",PartNumber); |
| |
| if (PartNumber[0] != '/') { |
| |
| BeginTransaction(); |
| |
| printf("\n SELECT PartNumber, PartName, SalesPrice"); |
| |
| #if 0 |
| EXEC SQL SELECT PartNumber, PartName, SalesPrice |
| INTO :PartNumber, |
| :PartName, |
| :SalesPrice :SalesPriceInd |
| FROM PurchDB.Parts |
| WHERE PartNumber = :PartNumber; |
| #endif\ |
| {\ |
| sqltempv.rec2.SQLREC2_FIELD1_LEN =\ |
| strlen(PartNumber);\ |
| strcpy(sqltempv.rec2.SQLREC2_FIELD1,\ |
| PartNumber);\ |
| sqlxfetc(&sqlca,sqlowner,sqlmodname,1,&sqltempv,24,80,1);\ |
| if (sqlca.sqlcode == 0)\ |
| {\ |
| sqltempv.rec3.\ |
| SQLREC3_FIELD1[sqltempv.rec3.SQLREC3_FIELD1_LEN] = '\0';\ |
| strcpy(PartNumber,\ |
| sqltempv.rec3.SQLREC3_FIELD1);\ |
| sqltempv.rec3.\ |
| SQLREC3_FIELD2[sqltempv.rec3.SQLREC3_FIELD2_LEN] = '\0';\ |
| strcpy(PartName,\ |
| sqltempv.rec3.SQLREC3_FIELD2);\ |
| SalesPriceInd =\ |
| sqltempv.rec3.SQLREC3_FIELD3_IND;\ |
| if (sqltempv.rec3.SQLREC3_FIELD3_IND >= 0)\ |
| {\ |
| SalesPrice =\ |
| sqltempv.rec3.SQLREC3_FIELD3;\ |
| }\ |
| } 9 |
_____________________________________________________________________________
Figure 2-9. Modified Source File For Program cex2 (page 5 of 6)
________________________________________________________________________
| |
| else |
| { |
| } |
| } |
| if ((sqlca.sqlwarn[0] == 'W') || (sqlca.sqlwarn[0] == 'w')) {|
| printf("\n SQL WARNING has occurred. The following row"); |
| printf("\n of data may not be valid!"); |
| } |
| if (sqlca.sqlcode == OK) { |
| DisplayRow(); |
| } |
| else if (sqlca.sqlcode == NotFound) { |
| printf("\n Row not found!"); |
| } |
| else if (sqlca.sqlcode == MultipleRows) { |
| printf("\n WARNING: More than one row qualifies!"); |
| } |
| else { |
| SQLStatusCheck(); |
| } |
| EndTransaction(); |
| } |
| } /* End do */ |
| while (PartNumber[0] != '/'); |
| |
| }/* End of Select Function */ |
| |
| main() /* Beginning of program */ |
| { |
| |
| printf("\n Program to SELECT specified rows from"); |
| printf("\n the Parts Table - cex2"); |
| printf("\n"); |
| printf("\n Event List:"); |
| printf("\n CONNECT TO PartsDBE"); |
| printf("\n BEGIN WORK"); |
| printf("\n SELECT the specified row from the Parts Table"); |
| printf("\n until the user enters a '/'"); |
| printf("\n COMMIT WORK"); |
| printf("\n RELEASE from PartsDBE"); |
| printf("\n"); |
| |
| if (ConnectDBE()) { |
| Select(); |
| ReleaseDBE(); |
| } |
| else |
| printf("\n Error: Cannot Connect to PartsDBE!\n"); |
| } /* End of Program */ |
________________________________________________________________________
Figure 2-9. Modified Source File For Program cex2 (page 6 of 6)