home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- **
- ** Copyright 1997 Pervasive Software Inc. All Rights Reserved
- **
- ****************************************************************************/
- /****************************************************************************
- CBSMAIN.CPP
- 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 Borland's C++ Builder.
-
- This program demonstrates the C++ Builder interface for Scalable SQL
- for MS Windows NT and Windows 95. It uses SQL-level functions to
- fetch records from the 'university' 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 <vcl\vcl.h>
- #pragma hdrstop
-
- #include "CBSMain.h"
-
- #include "btitypes.h" /* BTI file with platform definitions */
- #include "sqlapi.h" /* SQL function prototypes */
- #include "sqlconst.h" /* SQL constants */
-
- #pragma resource "*.dfm"
- TForm1 *Form1;
-
- #define FETCH_FIRST 1
- #define FAILURE 1
- #define INTERNAL_FORMAT 0
-
- /* 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
-
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::exitbuttonClick(TObject *Sender)
- {
- Form1->Close();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::testbuttonClick(TObject *Sender)
- {
- runTest();
- }
- //---------------------------------------------------------------------------
-
- void printLB(TListBox *LB, char *msg)
- {
- LB->Items->Add(msg);
- }
-
- void runTest(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 = 0;
- BTI_LONG recordsRead = 1;
- BTI_SINT loggedIn = 0;
- BTI_SINT cursored = 0;
- BTI_CHAR tmpBuf[1024];
-
- printLB(Form1->ListBox1,
- "*** Scalable SQL C++ Builder Interface Demo ***");
-
- Form1->Refresh();
- status = XQLLogin(
- userid,
- password,
- ddpath,
- datapath,
- reserved,
- 0 );
-
- if ( status == SS_SUCCESS ) {
- loggedIn = 1;
- sprintf(tmpBuf, "XQLLogin status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
- Form1->Refresh();
- status = XQLCursor( &cursorid );
- }
- else {
- sprintf(tmpBuf, "XQLLogin status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
- Form1->Refresh();
- }
-
- if ( status == SS_SUCCESS ) {
- cursored = 1;
- sprintf(tmpBuf, "XQLCursor status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
- Form1->Refresh();
- }
- else {
- sprintf(tmpBuf, "XQLCursor status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
- Form1->Refresh();
- }
-
- /* Compile SQL select statement */
- /* Negative status codes are informative, not errors. */
- if ( status <= SS_SUCCESS ) {
- strcpy( statement, (const char *)sampleSelectStatement );
- statementLength = strlen( statement );
- sprintf(tmpBuf, "%s", statement);
- printLB(Form1->ListBox1, tmpBuf);
- status = XQLCompile(
- cursorid,
- &statementLength,
- statement );
- sprintf(tmpBuf, "XQLCompile status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
- }
-
- /* 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 );
-
- sprintf(tmpBuf, "XQLFetch status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
- if ( status <= SS_SUCCESS ) {
- sprintf(tmpBuf, "");
- printLB(Form1->ListBox1, tmpBuf);
- sprintf(tmpBuf, "Selected fields from the retrieved record:" );
- printLB(Form1->ListBox1, tmpBuf);
- sprintf(tmpBuf, "Name: %s %s",
- personRecord.FirstName,
- personRecord.LastName);
- printLB(Form1->ListBox1, tmpBuf);
- sprintf(tmpBuf, "Country: %s", personRecord.PermCountry);
- printLB(Form1->ListBox1, tmpBuf);
- sprintf(tmpBuf, "Street: %s", personRecord.PermStreet);
- printLB(Form1->ListBox1, tmpBuf);
- sprintf(tmpBuf, "City: %s", personRecord.PermCity);
- printLB(Form1->ListBox1, tmpBuf);
- sprintf(tmpBuf, "State: %s", personRecord.PermState);
- printLB(Form1->ListBox1, tmpBuf);
- sprintf(tmpBuf, "Zip: %s", personRecord.PermZip);
- printLB(Form1->ListBox1, tmpBuf);
- sprintf(tmpBuf, "");
- printLB(Form1->ListBox1, tmpBuf);
- }
- }
-
- if (cursored) {
- status = XQLFree(cursorid);
- sprintf(tmpBuf, "XQLFree status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
- Form1->Refresh();
- cursored = 0;
- }
- if (loggedIn) {
- status = XQLLogout();
- sprintf(tmpBuf, "XQLLogout status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
- loggedIn = 0;
- }
-
- XQLStop();
- sprintf(tmpBuf, "XQLStop status = %d", status);
- printLB(Form1->ListBox1, tmpBuf);
-
- return;
-
- }
-