HPlogo ALLBASE/SQL C Application Programming Guide: HP 9000 Computer Systems > Chapter 2 Using the Preprocessor

Sample Modified Source File

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

 » Index

The following modified source file is the result of preprocessing program cex2 (shown at the end of Chapter 1). In the listing, the parts of the code are shaded for easy reference, as follows:

  • Conditional compiler directives that tell the compiler to ignore the original SQL commands.

  • Begin and end braces that indicate the preprocessor-generated code.

Figure 2-6 Sample Modified Source File



#include "cex2.sqlt"\

#include "cex2.sqlv"\

#include "cex2.sqle"

/* 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;

#endif

sqlca_type sqlca;







                           /* SQL Communication Area */



          /* Begin Host Variable Declarations */

#if 0

    EXEC SQL BEGIN DECLARE SECTION;

#endif



    char       PartNumber[17];

    char       PartName[31];

    double     SalesPrice;

    sqlind     SalesPriceInd;

    char       SQLMessage[133];

    

#if 0

    EXEC SQL END DECLARE SECTION;

#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);

} 

printf("\n");

printf("%s\n",SQLMessage);

} while (sqlca.sqlcode != 0);



if (Abort)

  {

  EndTransaction();

  ReleaseDBE();

  }

}  /* End SQLStatusCheck Function */



boolean ConnectDBE()     /* Function to Connect to ../sampledb/PartsDBE */

{

boolean ConnectDBE;

ConnectDBE = TRUE;

printf("\n Connect to ../sampledb/PartsDBE");

#if 0

EXEC SQL CONNECT TO '../sampledb/PartsDBE';

#endif

 {  

sqlvar1 = "00AE00002E2E2F73616D706C6564622F5061727473444245 

202020202020202020202020202020202020202020202020202020202020 

202020202020202020202020202020202020202020202020202020202020 

202020202020202020202020202020202020202020202020202020202020 

202020202020202020202020202020202020"; 

sqlxconc(&sqlca,sqlvar1); 

} 



if (sqlca.sqlcode != OK)

  {

  ConnectDBE = FALSE;

  SQLStatusCheck();

  }  /* End if */

return (ConnectDBE);

}  /* End of ConnectDBE Function */



int ReleaseDBE()     /* Function to Release ../sampledb/PartsDBE */

{

printf("\n");

printf("\n Release ../sampledb/PartsDBE");

printf("\n");

#if 0

EXEC SQL RELEASE;

#endif

 {   

sqlvar2 = "00B200002020202020202020202020202020202020202020FFFFFFFF"; 

sqlxconc(&sqlca,sqlvar2); 

} 



if (sqlca.sqlcode != OK) SQLStatusCheck();

}  /* End ReleaseDBE Function */



boolean BeginTransaction()     /* Function to Begin Work */

{

boolean BeginTransaction;

BeginTransaction = TRUE;





printf("\n");

printf("\n Begin Work");

#if 0

EXEC SQL BEGIN WORK;

#endif

 {  

sqlvar3 = "00A6007F00110061";

sqlxconc(&sqlca,sqlvar3);

} 



if (sqlca.sqlcode != OK)

  {

  BeginTransaction = FALSE;

  SQLStatusCheck();

  ReleaseDBE();

  }  /* 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);

} 



if (sqlca.sqlcode != OK) SQLStatusCheck();

}  /* End EndTransaction Function */



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 */





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;

  } 

  } 



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 ../sampledb/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 ../sampledb/PartsDBE");

printf("\n");



if (ConnectDBE())  {

  Select();

  ReleaseDBE();

  }

else

  printf("\n Error: Cannot Connect to ../sampledb/PartsDBE!\n");

}  /* End of Program */
Feedback to webmaster