home *** CD-ROM | disk | FTP | other *** search
/ Datatid 1999 #6 / Datatid_1999-06.iso / internet / Tango352Promo / P.SQL / PTKPKG.1 / SQLSAM16.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-11-17  |  8.9 KB  |  314 lines

  1. {****************************************************************************
  2. **
  3. **  Copyright 1982-1997 Pervasive Software Inc. All Rights Reserved
  4. **
  5. ****************************************************************************}
  6. {****************************************************************************
  7.      SQLSAM16.PAS
  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 16-bit environment using Borland Delphi.
  11.  
  12.             This program demonstrates the Delphi interface for Scalable SQL for
  13.             MS Windows.  It uses SQL-level functions to fetch records from the
  14.             'university' database that is  included with  Scalable SQL.
  15.  
  16.             This program does the following operations on the sample database:
  17.             - logs into the database
  18.             - gets a cursor
  19.             - compiles a select statement
  20.             - gets a record
  21.             - displays selected portions of the retrieved record
  22.             - frees resources
  23.             - logs out of the database
  24.  
  25.             IMPORTANT: Be sure to provide the complete path to the sample
  26.             database location, as shown below for a particular case.
  27.             See 'IMPORTANT', below.
  28.  
  29.             PROJECT FILES:
  30.                  - sql16.dpr     Borland project file
  31.                  - sqlsam16.dfm  Borland project file
  32.                  - sqlsam16.pas  Source code for the simple sample
  33.                  - sqlapi16.pas  BTI interface to Scalable SQL
  34.  
  35. ****************************************************************************}
  36. unit Sqlsam16;
  37.  
  38. interface
  39.  
  40. uses
  41.      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  42.      Forms, Dialogs, StdCtrls, sqlapi16;
  43.  
  44. {*****************************************************************************
  45.      Constants
  46. *****************************************************************************}
  47. CONST
  48.      TRUE    =  1;
  49.      FALSE   =  0;
  50.      SUCCESS =  0;
  51.      FAILURE = -1;
  52.      STATEMENT_BUFFER_SIZE = 1024;
  53.      BYTE_COUNT_SIZE       = 2;
  54.      SPACING_NOT_PERTINENT = 0;
  55.      UserID   : CHAR = #0;
  56.      Password : CHAR = #0;
  57.      Reserved : CHAR = #0;
  58.    DDpath   : string[ 20 ] = 'c:\pvsw\demodata';      { IMPORTANT }
  59.    Datapath : string[ 20 ] = 'c:\pvsw\demodata';      { IMPORTANT }
  60.      FETCH_FIRST        : integer = 1;
  61.      INTERNAL_FORMAT    : integer = 0;
  62.  
  63.  
  64. {***************************************************************************
  65.      Structures
  66.      Definition of record from the 'person' table
  67. ****************************************************************************}
  68. TYPE
  69.      PERSON_STRUCT = record
  70.             RecLen         : word;
  71.             ID             : longint;
  72.       Dummy          : longint;
  73.             FirstName      : array[0..15] of char;
  74.             LastName       : array[0..25] of char;
  75.             PermStreet     : array[0..30] of char;
  76.             PermCity       : array[0..30] of char;
  77.             PermState      : array[0..2] of char;
  78.             PermZip        : array[0..10] of char;
  79.             PermCountry    : array[0..20] of char;
  80.             Street         : array[0..30] of char;
  81.             City           : array[0..30] of char;
  82.             State          : array[0..2] of char;
  83.             Zip            : array[0..10] of char;
  84.             Phone          : array[0..9] of char;
  85.             EmergencyPhone : array[0..19] of char;
  86.             Unlisted       : char;
  87.             DateOfBirth    : array[0..3] of char;
  88.             EmailAddress   : array[0..30] of char;
  89.             Sex            : char;
  90.             Citizenship    : array[0..20] of char;
  91.             Survey         : char;
  92.             Smoker         : char;
  93.             Married        : char;
  94.             Children       : char;
  95.             Disability     : char;
  96.             Scholarship    : char;
  97.             Comments       : array[0..199] of char;
  98.      end;
  99.  
  100.      TForm1 = class(TForm)
  101.             RunButton: TButton;
  102.             ExitButton: TButton;
  103.             ListBox1: TListBox;
  104.             procedure FormCreate(Sender: TObject);
  105.       procedure ExitButtonClick(Sender: TObject);
  106.       procedure RunButtonClick(Sender: TObject);
  107.    private
  108.             { Private declarations }
  109.             ArrowCursor,
  110.             WaitCursor:   HCursor;
  111.             status:       smallint;
  112.             bufferLength: integer;
  113.             personRecord: PERSON_STRUCT;
  114.             recordsRead:  longint;
  115.  
  116.             procedure RunTest;
  117.      public
  118.             { Public declarations }
  119.      end;
  120.  
  121. var
  122.      Form1: TForm1;
  123.  
  124. implementation
  125.  
  126. {$R *.DFM}
  127. VAR
  128.      cursorID     :  integer;
  129.      statement    :  string [255];
  130.      statlen      :  integer;
  131.      loginFlag    :  integer;
  132.      cursorIDFlag :  integer;
  133.  
  134. procedure WritelnLB( LB: TListBox; Str: String);
  135. begin
  136.    LB.Items.Add(Str);
  137. end;
  138.  
  139. procedure TForm1.FormCreate(Sender: TObject);
  140. begin
  141.    ArrowCursor  :=  LoadCursor(0, IDC_ARROW);
  142.    WaitCursor   :=  LoadCursor(0, IDC_WAIT);
  143.    loginFlag    :=  FALSE;
  144.    cursorIDFlag :=  FALSE;
  145. end;
  146.  
  147. procedure TForm1.ExitButtonClick(Sender: TObject);
  148. begin
  149.    {**************************************************
  150.    ** Stop the engine
  151.    **************************************************}
  152.    status := XQLStop;
  153.    if status > SUCCESS then
  154.       begin
  155.  
  156.         status := FAILURE;
  157.       end
  158.    else
  159.       begin
  160.                 status := SUCCESS;
  161.       end;
  162.    Close;
  163. end;
  164.  
  165. procedure TForm1.RunButtonClick(Sender: TObject);
  166. begin
  167.    SetCursor(WaitCursor);
  168.    RunTest;
  169.    SetCursor(ArrowCursor);
  170. end;
  171.  
  172. procedure TForm1.RunTest;
  173. begin
  174.      ListBox1.Clear;
  175.      WritelnLB( ListBox1, 'Test started ...' );
  176.      {**************************************************
  177.      ** Login to the database
  178.      **************************************************}
  179.      status := XQLLogin(
  180.                                 UserID,
  181.                                 Password,
  182.                                 DDpath[1],
  183.                                 Datapath[1],
  184.                                 Reserved,
  185.                                 1);
  186.  
  187.      WritelnLB( ListBox1, 'XQLLogin status = ' + IntToStr(status) );
  188.      if status <> SUCCESS then
  189.             begin
  190.                  status := FAILURE;
  191.             end
  192.      else
  193.             begin
  194.                  loginFlag := TRUE;
  195.             end;
  196.  
  197.      if status = SUCCESS then
  198.             begin
  199.                  {**************************************************
  200.                  ** Get a cursor ID
  201.                  **************************************************}
  202.                  status := XQLCursor (cursorID);
  203.                  WritelnLB( ListBox1, 'XQLCursorID status = ' + IntToStr(status) );
  204.                  if status <> SUCCESS then
  205.                         begin
  206.                              status := FAILURE;
  207.                         end
  208.                  else
  209.                         begin
  210.                              cursorIDFlag := TRUE;
  211.                         end;
  212.             end;
  213.  
  214.      if status = SUCCESS then
  215.             begin
  216.                  {**************************************************
  217.                  ** Compile the select statement
  218.                  **************************************************}
  219.                  statement := 'SELECT * from person where ID = 101135758 ' + #0;
  220.                  Statlen   := length (Statement);
  221.  
  222.                  status := XQLCompile(
  223.                                             cursorID,
  224.                                             statlen,
  225.                                             statement [1] );
  226.  
  227.                  WritelnLB( ListBox1, 'XQLCompile status = ' + IntToStr(status) );
  228.                  if status > SUCCESS then
  229.                         begin
  230.                              status := FAILURE;
  231.                         end
  232.                  else
  233.                         begin
  234.                              WritelnLB( ListBox1, 'SELECT * from person where ID = 101135758' );
  235.                         end;
  236.              end;
  237.  
  238.      if status = SUCCESS then
  239.             begin
  240.                  {**************************************************
  241.                  ** Fetch the record
  242.                  **************************************************}
  243.                  bufferLength := SizeOf( PERSON_STRUCT );
  244.  
  245.                  recordsRead := 1;
  246.                  status := XQLFetch(
  247.                                             cursorID,
  248.                                             FETCH_FIRST,
  249.                                             bufferLength,
  250.                                             personRecord,
  251.                                             recordsRead,
  252.                                             INTERNAL_FORMAT,
  253.                                             SPACING_NOT_PERTINENT );
  254.  
  255.                  WritelnLB( ListBox1, 'XQLFetch status = ' + IntToStr(status) );
  256.                  if status <> SUCCESS then
  257.                         begin
  258.                              status := FAILURE;
  259.                         end
  260.                  else
  261.                         begin
  262.                              WritelnLB( ListBox1, '');
  263.                              WritelnLB( ListBox1, 'Selected fields from the retrieved record are:' );
  264.                              WritelnLB( ListBox1, format('Name:    %s  %s',
  265.                                            [personRecord.FirstName,
  266.                                             personRecord.LastName]));
  267.                              WritelnLB( ListBox1, 'Country: ' + personRecord.PermCountry );
  268.                              WritelnLB( ListBox1, 'Street:  ' + personRecord.PermStreet );
  269.                              WritelnLB( ListBox1, 'City:    ' + personRecord.PermCity );
  270.                              WritelnLB( ListBox1, 'State:   ' + personRecord.PermState );
  271.                              WritelnLB( ListBox1, 'Zip:     ' + personRecord.PermZip );
  272.                              WritelnLB( ListBox1, '');
  273.                         end;
  274.             end;
  275.  
  276.      if cursorIDFlag = TRUE then
  277.             begin
  278.                  {**************************************************
  279.                  ** Free the resources
  280.                  **************************************************}
  281.                  status := XQLFree( cursorID );
  282.                  WritelnLB( ListBox1, 'XQLFree status = ' + IntToStr(status) );
  283.                  if status > SUCCESS then
  284.                         begin
  285.                              status := FAILURE;
  286.                         end
  287.                  else
  288.                         begin
  289.                              status := SUCCESS;
  290.                         end;
  291.             end;
  292.  
  293.      if loginFlag = TRUE then
  294.             begin
  295.                  {**************************************************
  296.                  ** Logout of the database
  297.                  **************************************************}
  298.                  status := XQLLogout;
  299.                  WritelnLB( ListBox1, 'XQLLogout status = ' + IntToStr(status) );
  300.                  if status > SUCCESS then
  301.                         begin
  302.                              status := FAILURE;
  303.                         end
  304.                  else
  305.                         begin
  306.                              status := SUCCESS;
  307.                         end;
  308.             end;
  309.      WritelnLB( ListBox1, 'Test ended ...' );
  310.  
  311. end;
  312.  
  313. end.
  314.