home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- **
- ** Copyright 1997 Pervasive Software Inc. All Rights Reserved
- **
- ****************************************************************************/
- /****************************************************************************
- SQLSAMP.C
- This is a simple sample designed to allow you to confirm your
- ability to compile, link, and execute a Scalable SQL application for
- your target environment using your compiler tools.
-
- This program demonstrates the C/C++ interface for Scalable SQL for DOS,
- Extended DOS, OS2 16-bit, MS Windows, and NLMs. It uses SQL-level
- functions to fetch records from the 'patients' database that is included
- with Scalable SQL. See the prologue to sqlapi.h for more info.
-
- This program does the following operations on the sample database:
- - logs into the database
- - gets a cursor
- - compiles a select statement
- - gets a record
- - displays selected portions of the retrieved record
- - frees resources
- - logs out of the database
-
- IMPORTANT: Be sure to provide the complete path to the sample
- database location, as shown below for a particular case.
- See 'IMPORTANT', below.
- ****************************************************************************/
- #include <stdlib.h>
- #include <stdio.h>
- #include <stddef.h>
- #include <string.h>
- #include <btitypes.h> /* BTI file with platform definitions */
- #include <sqlapi.h> /* SQL function prototypes */
- #include <sqlconst.h> /* SQL constants */
-
- /*****************************************************************************
- Constants
- *****************************************************************************/
- #define DATA_OFFSET 2
- #define DATA_LENGTH_OFFSET 0
- #define FETCH_FIRST 1
- #define FAILURE 1
- #define INTERNAL_FORMAT 0
-
- /***************************************************************************
- Definition of record from the 'person' table
- ****************************************************************************/
- /* structure packing - one byte */
- #if defined(__BORLANDC__)
- #pragma option -a-
- #else
- #if defined(_MSC_VER) || defined(__WATCOMC__)
- #pragma pack(1)
- #endif
- #endif
-
- typedef struct
- {
- BTI_SINT RecLen;
- BTI_LONG ID;
- BTI_LONG Dummy;
- BTI_CHAR FirstName[ 16 ];
- BTI_CHAR LastName[ 26 ];
- BTI_CHAR PermStreet[ 31 ];
- BTI_CHAR PermCity[ 31 ];
- BTI_CHAR PermState[ 3 ];
- BTI_CHAR PermZip[ 11 ];
- BTI_CHAR PermCountry[ 21 ];
- BTI_CHAR Street[ 31 ];
- BTI_CHAR City[ 31 ];
- BTI_CHAR State[ 3 ];
- BTI_CHAR Zip[ 11 ];
- BTI_CHAR Phone[ 10 ];
- BTI_CHAR EmergencyPhone[ 20 ];
- BTI_CHAR Unlisted;
- BTI_CHAR DateOfBirth[ 4 ];
- BTI_CHAR EmailAddress[ 31 ];
- BTI_CHAR Sex;
- BTI_CHAR Citizenship[ 21 ];
- BTI_CHAR Survey;
- BTI_CHAR Smoker;
- BTI_CHAR Married;
- BTI_CHAR Children;
- BTI_CHAR Disability;
- BTI_CHAR Scholarship;
- BTI_CHAR Comments[ 200 ];
- } PERSON_STRUCT, BTI_FAR* PERSON_STRUCT_PTR;
-
- /* restore structure packing to previous setting */
- #if defined(__BORLANDC__)
- #pragma option -a.
- #else
- #if defined(_MSC_VER) || defined(__WATCOMC__)
- #pragma pack()
- #endif
- #endif
-
- /*****************************************************************************
- main
- *****************************************************************************/
- void main( void )
- {
- #define STATEMENT_BUFFER_SIZE 1024
- #define BYTE_COUNT_SIZE 2
- #define SPACING_NOT_PERTINENT 0
-
- BTI_CHAR userid [] = "";
- BTI_CHAR password[] = "";
- /********************************************************************
- IMPORTANT: You should modify the following to specify the
- complete path to sample database for your environment.
- ********************************************************************/
- #if defined BTI_NLM
- BTI_CHAR ddpath [] = "sys:\\pvsw\\demodata";
- BTI_CHAR datapath[] = "sys:\\pvsw\\demodata";
- #else
- BTI_CHAR ddpath [] = "c:\\pvsw\\demodata";
- BTI_CHAR datapath[] = "c:\\pvsw\\demodata";
- #endif
- BTI_CHAR reserved[] = "";
-
- const BTI_CHAR_PTR sampleSelectStatement =
- "SELECT * from person where ID = 101135758 ";
-
- BTI_SINT cursorid;
- PERSON_STRUCT personRecord;
- BTI_SIZE bufferLength;
- BTI_CHAR statement[ STATEMENT_BUFFER_SIZE ];
- BTI_SIZE statementLength;
- BTI_SINT status;
- BTI_LONG recordsRead = 1;
-
- /************************************************************************
- PRINT THE PROGRAM TITLE
- ************************************************************************/
- printf( "**************** Scalable SQL C/C++ Interface Demo ****************\n\n" );
-
- status = XQLLogin(
- userid,
- password,
- ddpath,
- datapath,
- reserved,
- 0 );
-
- printf( "XQLLogin status = %d\n", status );
-
- if ( status == SS_SUCCESS )
- {
- status = XQLCursor( &cursorid );
- printf( "XQLCursor status = %d\n", status );
- }
-
- /* COMPILE SQL SELECT STATEMENT */
- /* Negative status codes are informative, not errors. */
- if ( status <= SS_SUCCESS )
- {
- strcpy( statement, (const char *)sampleSelectStatement );
- statementLength = strlen( statement );
- printf( "%s\n", statement );
-
- status = XQLCompile(
- cursorid,
- &statementLength,
- statement );
-
- printf( "XQLCompile status = %d\n", status );
- }
-
- /* Negative status codes are informative, not errors. */
- if ( status <= SS_SUCCESS )
- {
- status = SS_SUCCESS;
- }
-
- /* fetch the record */
- if ( status == SS_SUCCESS )
- {
- bufferLength = sizeof( personRecord );
-
- status = XQLFetch(
- cursorid,
- FETCH_FIRST,
- &bufferLength,
- (BTI_CHAR_PTR)&personRecord.RecLen,
- &recordsRead,
- INTERNAL_FORMAT,
- SPACING_NOT_PERTINENT );
-
- printf( "XQLFetch status = %d\n", status );
- if ( status <= SS_SUCCESS )
- {
- printf( "\n" );
- printf( "Selected fields from the retrieved record are:\n" );
- printf( "Name: %s %s\n", personRecord.FirstName,
- personRecord.LastName );
- printf( "Country: %s\n", personRecord.PermCountry );
- printf( "Street: %s\n", personRecord.PermStreet );
- printf( "City: %s\n", personRecord.PermCity );
- printf( "State: %s\n", personRecord.PermState );
- printf( "Zip: %s\n", personRecord.PermZip );
- printf( "\n" );
- }
- }
-
- status = XQLFree( cursorid );
- printf( "XQLFree status = %d\n", status );
-
- status = XQLLogout();
- printf( "XQLLogout status = %d\n", status );
-
- #if !defined( BTI_DOS ) && !defined( BTI_DOS_32R ) \
- && !defined( BTI_DOS_32P ) && !defined( BTI_DOS_32B ) \
- && !defined( BTI_DOS_16B )
-
- XQLStop();
- printf( "XQLStop status = %d\n", status );
-
- #endif
-
- /* Negative status codes are informative, not errors. */
- if ( status <= SS_SUCCESS )
- {
- status = SS_SUCCESS;
- }
- else
- {
- status = FAILURE;
- }
-
- exit( (BTI_INT)status );
- }
-