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

  1. /****************************************************************************
  2. **
  3. **  Copyright 1997 Pervasive Software Inc. All Rights Reserved
  4. **
  5. ****************************************************************************/
  6. /****************************************************************************
  7.    SQLSAMP.C
  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 your compiler tools.
  11.  
  12.      This program demonstrates the C/C++ interface for Scalable SQL for DOS,
  13.      Extended DOS, OS2 16-bit, MS Windows, and NLMs.  It uses SQL-level
  14.      functions to fetch records from the 'patients' 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 <stdlib.h>
  31. #include <stdio.h>
  32. #include <stddef.h>
  33. #include <string.h>
  34. #include <btitypes.h>              /* BTI file with platform definitions */
  35. #include <sqlapi.h>                /* SQL function prototypes */
  36. #include <sqlconst.h>              /* SQL constants */
  37.  
  38. /*****************************************************************************
  39.    Constants
  40. *****************************************************************************/
  41. #define DATA_OFFSET        2
  42. #define DATA_LENGTH_OFFSET 0
  43. #define FETCH_FIRST      1
  44. #define FAILURE          1
  45. #define INTERNAL_FORMAT  0
  46.  
  47. /***************************************************************************
  48.    Definition of record from the 'person' table
  49. ****************************************************************************/
  50. /* structure packing - one byte */
  51. #if defined(__BORLANDC__)
  52.   #pragma option -a-
  53. #else
  54.   #if defined(_MSC_VER) || defined(__WATCOMC__)
  55.     #pragma pack(1)
  56.   #endif
  57. #endif
  58.  
  59. typedef struct
  60.    {
  61.       BTI_SINT  RecLen;
  62.       BTI_LONG  ID;
  63.       BTI_LONG  Dummy;
  64.       BTI_CHAR  FirstName[ 16 ];
  65.       BTI_CHAR  LastName[ 26 ];
  66.       BTI_CHAR  PermStreet[ 31 ];
  67.       BTI_CHAR  PermCity[ 31 ];
  68.       BTI_CHAR  PermState[ 3 ];
  69.       BTI_CHAR  PermZip[ 11 ];
  70.       BTI_CHAR  PermCountry[ 21 ];
  71.       BTI_CHAR  Street[ 31 ];
  72.       BTI_CHAR  City[ 31 ];
  73.       BTI_CHAR  State[ 3 ];
  74.       BTI_CHAR  Zip[ 11 ];
  75.       BTI_CHAR  Phone[ 10 ];
  76.       BTI_CHAR  EmergencyPhone[ 20 ];
  77.       BTI_CHAR  Unlisted;
  78.       BTI_CHAR  DateOfBirth[ 4 ];
  79.       BTI_CHAR  EmailAddress[ 31 ];
  80.       BTI_CHAR  Sex;
  81.       BTI_CHAR  Citizenship[ 21 ];
  82.       BTI_CHAR  Survey;
  83.       BTI_CHAR  Smoker;
  84.       BTI_CHAR  Married;
  85.       BTI_CHAR  Children;
  86.       BTI_CHAR  Disability;
  87.       BTI_CHAR  Scholarship;
  88.       BTI_CHAR  Comments[ 200 ];
  89.    } PERSON_STRUCT, BTI_FAR* PERSON_STRUCT_PTR;
  90.  
  91. /* restore structure packing to previous setting */
  92. #if defined(__BORLANDC__)
  93.   #pragma option -a.
  94. #else
  95.   #if defined(_MSC_VER) || defined(__WATCOMC__)
  96.     #pragma pack()
  97.   #endif
  98. #endif
  99.  
  100. /*****************************************************************************
  101.    main
  102. *****************************************************************************/
  103. void main( void )
  104. {
  105.    #define STATEMENT_BUFFER_SIZE 1024
  106.    #define BYTE_COUNT_SIZE       2
  107.    #define SPACING_NOT_PERTINENT 0
  108.  
  109.    BTI_CHAR userid  [] = "";
  110.    BTI_CHAR password[] = "";
  111.    /********************************************************************
  112.       IMPORTANT: You should modify the following to specify the
  113.                  complete path to sample database for your environment.
  114.    ********************************************************************/
  115.    #if defined BTI_NLM
  116.    BTI_CHAR ddpath  [] = "sys:\\pvsw\\demodata";
  117.    BTI_CHAR datapath[] = "sys:\\pvsw\\demodata";
  118.    #else
  119.    BTI_CHAR ddpath  [] = "c:\\pvsw\\demodata";
  120.    BTI_CHAR datapath[] = "c:\\pvsw\\demodata";
  121.    #endif
  122.    BTI_CHAR reserved[] = "";
  123.  
  124.    const BTI_CHAR_PTR  sampleSelectStatement =
  125.                        "SELECT * from person where ID = 101135758 ";
  126.  
  127.    BTI_SINT      cursorid;
  128.    PERSON_STRUCT personRecord;
  129.    BTI_SIZE      bufferLength;
  130.    BTI_CHAR      statement[ STATEMENT_BUFFER_SIZE ];
  131.    BTI_SIZE      statementLength;
  132.    BTI_SINT      status;
  133.    BTI_LONG      recordsRead = 1;
  134.  
  135.    /************************************************************************
  136.       PRINT THE PROGRAM TITLE
  137.    ************************************************************************/
  138.    printf( "**************** Scalable SQL C/C++ Interface Demo ****************\n\n" );
  139.  
  140.    status = XQLLogin(
  141.                userid,
  142.                password,
  143.                ddpath,
  144.                datapath,
  145.                reserved,
  146.                0 );
  147.  
  148.    printf( "XQLLogin status = %d\n", status );
  149.  
  150.    if ( status == SS_SUCCESS )
  151.    {
  152.       status = XQLCursor( &cursorid );
  153.       printf( "XQLCursor status = %d\n", status );
  154.    }
  155.  
  156.    /* COMPILE SQL SELECT STATEMENT */
  157.    /* Negative status codes are informative, not errors. */
  158.    if ( status <= SS_SUCCESS )
  159.    {
  160.       strcpy( statement, (const char *)sampleSelectStatement );
  161.       statementLength = strlen( statement );
  162.       printf( "%s\n", statement );
  163.  
  164.       status = XQLCompile(
  165.                   cursorid,
  166.                   &statementLength,
  167.                   statement );
  168.  
  169.       printf( "XQLCompile status = %d\n", status );
  170.    }
  171.  
  172.    /* Negative status codes are informative, not errors. */
  173.    if ( status <= SS_SUCCESS )
  174.    {
  175.       status = SS_SUCCESS;
  176.    }
  177.  
  178.    /* fetch the record */
  179.    if ( status == SS_SUCCESS )
  180.    {
  181.       bufferLength = sizeof( personRecord );
  182.  
  183.       status   = XQLFetch(
  184.                     cursorid,
  185.                     FETCH_FIRST,
  186.                     &bufferLength,
  187.                     (BTI_CHAR_PTR)&personRecord.RecLen,
  188.                     &recordsRead,
  189.                     INTERNAL_FORMAT,
  190.                     SPACING_NOT_PERTINENT );
  191.  
  192.       printf( "XQLFetch status = %d\n", status );
  193.       if ( status <= SS_SUCCESS )
  194.       {
  195.          printf( "\n" );
  196.          printf( "Selected fields from the retrieved record are:\n" );
  197.          printf( "Name:    %s %s\n", personRecord.FirstName,
  198.                                     personRecord.LastName );
  199.          printf( "Country: %s\n", personRecord.PermCountry );
  200.          printf( "Street:  %s\n", personRecord.PermStreet );
  201.          printf( "City:    %s\n", personRecord.PermCity );
  202.          printf( "State:   %s\n", personRecord.PermState );
  203.          printf( "Zip:     %s\n", personRecord.PermZip );
  204.          printf( "\n" );
  205.       }
  206.    }
  207.  
  208.    status = XQLFree( cursorid );
  209.    printf( "XQLFree status = %d\n", status );
  210.  
  211.    status = XQLLogout();
  212.    printf( "XQLLogout status = %d\n", status );
  213.  
  214.    #if !defined( BTI_DOS ) && !defined( BTI_DOS_32R ) \
  215.    && !defined( BTI_DOS_32P ) && !defined( BTI_DOS_32B ) \
  216.    && !defined( BTI_DOS_16B )
  217.  
  218.    XQLStop();
  219.    printf( "XQLStop status = %d\n", status );
  220.  
  221.    #endif
  222.  
  223.    /* Negative status codes are informative, not errors. */
  224.    if ( status <= SS_SUCCESS )
  225.    {
  226.       status = SS_SUCCESS;
  227.    }
  228.    else
  229.    {
  230.       status = FAILURE;
  231.    }
  232.  
  233.    exit( (BTI_INT)status );
  234. }
  235.