home *** CD-ROM | disk | FTP | other *** search
/ Datatid 1999 #6 / Datatid_1999-06.iso / internet / Tango352Promo / P.SQL / PTKPKG.1 / CBSMAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-17  |  8.5 KB  |  271 lines

  1. /****************************************************************************
  2. **
  3. **  Copyright 1997 Pervasive Software Inc. All Rights Reserved
  4. **
  5. ****************************************************************************/
  6. /****************************************************************************
  7.    CBSMAIN.CPP
  8.      This is a simple sample designed to allow you to confirm your
  9.      ability to compile, link, and execute a Scalable SQL application for
  10.      your target environment using Borland's C++ Builder.
  11.  
  12.      This program demonstrates the C++ Builder interface for Scalable SQL
  13.      for MS Windows NT and Windows 95.  It uses SQL-level functions to
  14.      fetch records from the 'university' database that is included
  15.      with Scalable SQL.  See the prologue to sqlapi.h for more info.
  16.  
  17.      This program does the following operations on the sample database:
  18.      - logs into the database
  19.      - gets a cursor
  20.      - compiles a select statement
  21.      - gets a record
  22.      - displays selected portions of the retrieved record
  23.      - frees resources
  24.      - logs out of the database
  25.  
  26.      IMPORTANT: Be sure to provide the complete path to the sample
  27.      database location, as shown below for a particular case.
  28.      See 'IMPORTANT', below.
  29. ****************************************************************************/
  30. #include <vcl\vcl.h>
  31. #pragma hdrstop
  32.  
  33. #include "CBSMain.h"
  34.  
  35. #include "btitypes.h"              /* BTI file with platform definitions */
  36. #include "sqlapi.h"                /* SQL function prototypes */
  37. #include "sqlconst.h"              /* SQL constants */
  38.  
  39. #pragma resource "*.dfm"
  40. TForm1 *Form1;
  41.  
  42. #define FETCH_FIRST      1
  43. #define FAILURE          1
  44. #define INTERNAL_FORMAT  0
  45.  
  46. /* structure packing - one byte */
  47. #if defined(__BORLANDC__)
  48.   #pragma option -a-
  49. #else
  50.   #if defined(_MSC_VER) || defined(__WATCOMC__)
  51.     #pragma pack(1)
  52.   #endif
  53. #endif
  54.  
  55. typedef struct
  56.    {
  57.       BTI_SINT  RecLen;
  58.       BTI_LONG  ID;
  59.       BTI_LONG  Dummy;
  60.       BTI_CHAR  FirstName[ 16 ];
  61.       BTI_CHAR  LastName[ 26 ];
  62.       BTI_CHAR  PermStreet[ 31 ];
  63.       BTI_CHAR  PermCity[ 31 ];
  64.       BTI_CHAR  PermState[ 3 ];
  65.       BTI_CHAR  PermZip[ 11 ];
  66.       BTI_CHAR  PermCountry[ 21 ];
  67.       BTI_CHAR  Street[ 31 ];
  68.       BTI_CHAR  City[ 31 ];
  69.       BTI_CHAR  State[ 3 ];
  70.       BTI_CHAR  Zip[ 11 ];
  71.       BTI_CHAR  Phone[ 10 ];
  72.       BTI_CHAR  EmergencyPhone[ 20 ];
  73.       BTI_CHAR  Unlisted;
  74.       BTI_CHAR  DateOfBirth[ 4 ];
  75.       BTI_CHAR  EmailAddress[ 31 ];
  76.       BTI_CHAR  Sex;
  77.       BTI_CHAR  Citizenship[ 21 ];
  78.       BTI_CHAR  Survey;
  79.       BTI_CHAR  Smoker;
  80.       BTI_CHAR  Married;
  81.       BTI_CHAR  Children;
  82.       BTI_CHAR  Disability;
  83.       BTI_CHAR  Scholarship;
  84.       BTI_CHAR  Comments[ 200 ];
  85.    } PERSON_STRUCT, BTI_FAR* PERSON_STRUCT_PTR;
  86.  
  87. /* restore structure packing to previous setting */
  88. #if defined(__BORLANDC__)
  89.   #pragma option -a.
  90. #else
  91.   #if defined(_MSC_VER) || defined(__WATCOMC__)
  92.     #pragma pack()
  93.   #endif
  94. #endif
  95.  
  96. //---------------------------------------------------------------------------
  97. __fastcall TForm1::TForm1(TComponent* Owner)
  98.     : TForm(Owner)
  99. {
  100. }
  101. //---------------------------------------------------------------------------
  102. void __fastcall TForm1::exitbuttonClick(TObject *Sender)
  103. {
  104.     Form1->Close();
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TForm1::testbuttonClick(TObject *Sender)
  108. {
  109.     runTest();
  110. }
  111. //---------------------------------------------------------------------------
  112.  
  113. void printLB(TListBox *LB, char *msg)
  114. {
  115.     LB->Items->Add(msg);
  116. }
  117.  
  118. void runTest(void)
  119. {
  120.     #define STATEMENT_BUFFER_SIZE 1024
  121.     #define BYTE_COUNT_SIZE       2
  122.     #define SPACING_NOT_PERTINENT 0
  123.  
  124.     BTI_CHAR userid  [] = "";
  125.     BTI_CHAR password[] = "";
  126.  
  127.     /********************************************************************
  128.        IMPORTANT: You should modify the following to specify the
  129.                   complete path to sample database for your environment.
  130.     ********************************************************************/
  131.     #if defined BTI_NLM
  132.     BTI_CHAR ddpath  [] = "sys:\\pvsw\demodata";
  133.     BTI_CHAR datapath[] = "sys:\\pvsw\demodata";
  134.     #else
  135.     BTI_CHAR ddpath  [] = "c:\\pvsw\\demodata";
  136.     BTI_CHAR datapath[] = "c:\\pvsw\\demodata";
  137.     #endif
  138.     BTI_CHAR reserved[] = "";
  139.  
  140.     const BTI_CHAR_PTR  sampleSelectStatement =
  141.                         "SELECT * from person where ID = 101135758 ";
  142.  
  143.     BTI_SINT      cursorid;
  144.     PERSON_STRUCT personRecord;
  145.     BTI_SIZE      bufferLength;
  146.     BTI_CHAR      statement[ STATEMENT_BUFFER_SIZE ];
  147.     BTI_SIZE      statementLength;
  148.     BTI_SINT      status = 0;
  149.     BTI_LONG      recordsRead = 1;
  150.     BTI_SINT      loggedIn = 0;
  151.     BTI_SINT      cursored = 0;
  152.     BTI_CHAR      tmpBuf[1024];
  153.  
  154.     printLB(Form1->ListBox1,
  155.         "*** Scalable SQL C++ Builder Interface Demo ***");
  156.  
  157.     Form1->Refresh();
  158.     status = XQLLogin(
  159.                 userid,
  160.                 password,
  161.                 ddpath,
  162.                 datapath,
  163.                 reserved,
  164.                 0 );
  165.  
  166.     if ( status == SS_SUCCESS ) {
  167.         loggedIn = 1;
  168.         sprintf(tmpBuf, "XQLLogin status = %d", status);
  169.            printLB(Form1->ListBox1, tmpBuf);
  170.         Form1->Refresh();
  171.            status = XQLCursor( &cursorid );
  172.     }
  173.     else {
  174.         sprintf(tmpBuf, "XQLLogin status = %d", status);
  175.            printLB(Form1->ListBox1, tmpBuf);
  176.         Form1->Refresh();
  177.     }
  178.  
  179.     if ( status == SS_SUCCESS ) {
  180.         cursored = 1;
  181.         sprintf(tmpBuf, "XQLCursor status = %d", status);
  182.            printLB(Form1->ListBox1, tmpBuf);
  183.         Form1->Refresh();
  184.     }
  185.     else {
  186.         sprintf(tmpBuf, "XQLCursor status = %d", status);
  187.            printLB(Form1->ListBox1, tmpBuf);
  188.         Form1->Refresh();
  189.     }
  190.  
  191.    /* Compile SQL select statement */
  192.    /* Negative status codes are informative, not errors. */
  193.    if ( status <= SS_SUCCESS ) {
  194.       strcpy( statement, (const char *)sampleSelectStatement );
  195.       statementLength = strlen( statement );
  196.       sprintf(tmpBuf, "%s", statement);
  197.       printLB(Form1->ListBox1, tmpBuf);
  198.       status = XQLCompile(
  199.                   cursorid,
  200.                   &statementLength,
  201.                   statement );
  202.       sprintf(tmpBuf, "XQLCompile status = %d", status);
  203.       printLB(Form1->ListBox1, tmpBuf);
  204.    }
  205.  
  206.    /* Negative status codes are informative, not errors. */
  207.    if ( status <= SS_SUCCESS ) {
  208.       status = SS_SUCCESS;
  209.    }
  210.  
  211.    /* fetch the record */
  212.    if ( status == SS_SUCCESS ) {
  213.       bufferLength = sizeof( personRecord );
  214.  
  215.       status   = XQLFetch(
  216.                     cursorid,
  217.                     FETCH_FIRST,
  218.                     &bufferLength,
  219.                     (BTI_CHAR_PTR)&personRecord.RecLen,
  220.                     &recordsRead,
  221.                     INTERNAL_FORMAT,
  222.                     SPACING_NOT_PERTINENT );
  223.  
  224.       sprintf(tmpBuf, "XQLFetch status = %d", status);
  225.       printLB(Form1->ListBox1, tmpBuf);
  226.       if ( status <= SS_SUCCESS ) {
  227.           sprintf(tmpBuf, "");
  228.           printLB(Form1->ListBox1, tmpBuf);
  229.           sprintf(tmpBuf, "Selected fields from the retrieved record:" );
  230.           printLB(Form1->ListBox1, tmpBuf);
  231.           sprintf(tmpBuf, "Name:    %s %s",
  232.                     personRecord.FirstName,
  233.                     personRecord.LastName);
  234.           printLB(Form1->ListBox1, tmpBuf);
  235.           sprintf(tmpBuf, "Country: %s", personRecord.PermCountry);
  236.           printLB(Form1->ListBox1, tmpBuf);
  237.           sprintf(tmpBuf, "Street:  %s", personRecord.PermStreet);
  238.           printLB(Form1->ListBox1, tmpBuf);
  239.           sprintf(tmpBuf, "City:    %s", personRecord.PermCity);
  240.           printLB(Form1->ListBox1, tmpBuf);
  241.           sprintf(tmpBuf, "State:   %s", personRecord.PermState);
  242.           printLB(Form1->ListBox1, tmpBuf);
  243.           sprintf(tmpBuf, "Zip:     %s", personRecord.PermZip);
  244.           printLB(Form1->ListBox1, tmpBuf);
  245.           sprintf(tmpBuf, "");
  246.           printLB(Form1->ListBox1, tmpBuf);
  247.       }
  248.    }
  249.  
  250.     if (cursored) {
  251.            status = XQLFree(cursorid);
  252.         sprintf(tmpBuf, "XQLFree status = %d", status);
  253.            printLB(Form1->ListBox1, tmpBuf);
  254.         Form1->Refresh();
  255.         cursored = 0;
  256.     }
  257.     if (loggedIn) {
  258.         status = XQLLogout();
  259.         sprintf(tmpBuf, "XQLLogout status = %d", status);
  260.         printLB(Form1->ListBox1, tmpBuf);
  261.         loggedIn = 0;
  262.     }
  263.  
  264.     XQLStop();
  265.     sprintf(tmpBuf, "XQLStop status = %d", status);
  266.     printLB(Form1->ListBox1, tmpBuf);
  267.  
  268.     return;
  269.  
  270. }
  271.