home *** CD-ROM | disk | FTP | other *** search
/ Datatid 1999 #6 / Datatid_1999-06.iso / internet / Tango352Promo / P.SQL / PTKPKG.1 / SQLSAMPP.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-04-14  |  7.2 KB  |  213 lines

  1. {****************************************************************************
  2. **
  3. **  Copyright 1982-1997 Pervasive Software Inc. All Rights Reserved
  4. **
  5. ****************************************************************************}
  6. {****************************************************************************
  7.    SQLSAMPP.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 Borland Pascal DOS protected mode application.
  11.  
  12.       This program demonstrates the Pascal interface for Scalable SQL for
  13.       DOS.  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.       - logs out of the database
  23.  
  24.       IMPORTANT: Be sure to provide the complete path to the sample
  25.       database location, as shown below for a particular case.
  26.       See 'IMPORTANT', below.
  27.  
  28. ****************************************************************************}
  29. PROGRAM sqlsampp (input, output);
  30.  
  31. USES
  32.    dos,
  33.    sqlapip;    { SQL Interface Unit }
  34.  
  35. {*****************************************************************************
  36.    Constants
  37. *****************************************************************************}
  38. CONST
  39.    TRUE    =  1;
  40.    FALSE   =  0;
  41.    SUCCESS =  0;
  42.    FAILURE = -1;
  43.    STATEMENT_BUFFER_SIZE = 1024;
  44.    BYTE_COUNT_SIZE       = 2;
  45.    SPACING_NOT_PERTINENT = 0;
  46.    UserID   : CHAR = #0;
  47.    Password : CHAR = #0;
  48.    Reserved : CHAR = #0;
  49.    DDpath   : string[ 20 ] = 'v:\ssql40\demodata';      { IMPORTANT }
  50.    Datapath : string[ 20 ] = 'v:\ssql40\demodata';      { IMPORTANT }
  51.    FETCH_FIRST        : integer = 1;
  52.    INTERNAL_FORMAT    : integer = 0;
  53.  
  54.  
  55. {***************************************************************************
  56.    Structures
  57.    Definition of record from the 'person' table
  58. ****************************************************************************}
  59. TYPE
  60.    PERSON_STRUCT = record
  61.       RecLen         : word;
  62.       ID             : longint;
  63.       Dummy          : longint;
  64.       FirstName      : array[1..16] of char;
  65.       LastName       : array[1..26] of char;
  66.       PermStreet     : array[1..31] of char;
  67.       PermCity       : array[1..31] of char;
  68.       PermState      : array[1..3] of char;
  69.       PermZip        : array[1..11] of char;
  70.       PermCountry    : array[1..21] of char;
  71.       Street         : array[1..31] of char;
  72.       City           : array[1..31] of char;
  73.       State          : array[1..3] of char;
  74.       Zip            : array[1..11] of char;
  75.       Phone          : array[1..10] of char;
  76.       EmergencyPhone : array[1..20] of char;
  77.       Unlisted       : char;
  78.       DateOfBirth    : array[1..4] of char;
  79.       EmailAddress   : array[1..31] of char;
  80.       Sex            : char;
  81.       Citizenship    : array[1..21] of char;
  82.       Survey         : char;
  83.       Smoker         : char;
  84.       Married        : char;
  85.       Children       : char;
  86.       Disability     : char;
  87.       Scholarship    : char;
  88.       Comments       : array[1..200] of char;
  89.    end;
  90.  
  91. VAR
  92.    cursorID     :  integer;
  93.    statement    :  string [255];
  94.    statlen      :  integer;
  95.    loginFlag    :  integer;
  96.    status       : integer;
  97.    bufferLength : integer;
  98.    personRecord : PERSON_STRUCT;
  99.    recordsRead  : longint;
  100.  
  101. {***********************************************************************
  102.    Program starts here
  103. ************************************************************************}
  104. BEGIN { sqlsampp }
  105.    writeln( '***** Scalable SQL Pascal Interface Demo *****' );
  106.    writeln;
  107.  
  108.    loginFlag := FALSE;
  109.    {**************************************************
  110.    ** Login to the database
  111.    **************************************************}
  112.    status := XQLLogin(
  113.                 UserID,
  114.                 Password,
  115.                 DDpath[1],
  116.                 Datapath[1],
  117.                 Reserved,
  118.                 1);
  119.  
  120.    writeln( 'XQLLogin status = ', status );
  121.    if status <> SUCCESS then
  122.       begin
  123.          status := FAILURE;
  124.       end
  125.    else
  126.       begin
  127.          loginFlag := TRUE;
  128.       end;
  129.  
  130.    if status = SUCCESS then
  131.       begin
  132.          {**************************************************
  133.          ** Get a cursor ID
  134.          **************************************************}
  135.          status := XQLCursor (cursorID);
  136.          writeln( 'XQLCursorID status = ', status );
  137.          if status <> SUCCESS then
  138.             begin
  139.                status := FAILURE;
  140.             end
  141.       end;
  142.  
  143.    if status = SUCCESS then
  144.       begin
  145.          {**************************************************
  146.          ** Compile the select statement
  147.          **************************************************}
  148.          statement := 'SELECT * from person where ID = 101135758 ' + #0;
  149.          Statlen   := length (Statement);
  150.  
  151.          status := XQLCompile(
  152.                       cursorID,
  153.                       statlen,
  154.                       statement [1] );
  155.  
  156.          writeln( 'XQLCompile status = ', status );
  157.          if status > SUCCESS then
  158.             begin
  159.                status := FAILURE;
  160.             end
  161.          else
  162.             begin
  163.                writeln( 'SELECT * from person where ID = 101135758' );
  164.             end;
  165.        end;
  166.  
  167.    if status = SUCCESS then
  168.       begin
  169.          {**************************************************
  170.          ** Fetch the record
  171.          **************************************************}
  172.          bufferLength := SizeOf( PERSON_STRUCT );
  173.  
  174.          recordsRead := 1;
  175.          status := XQLFetch(
  176.                       cursorID,
  177.                       FETCH_FIRST,
  178.                       bufferLength,
  179.                       personRecord,
  180.                       recordsRead,
  181.                       INTERNAL_FORMAT,
  182.                       SPACING_NOT_PERTINENT );
  183.  
  184.          writeln( 'XQLFetch status = ', status );
  185.          if status <> SUCCESS then
  186.             begin
  187.                status := FAILURE;
  188.             end
  189.          else
  190.             begin
  191.                writeln;
  192.                writeln( 'Selected fields from the retrieved record are:' );
  193.                writeln( 'Name:    ', personRecord.FirstName, ' ',
  194.                                         personRecord.LastName );
  195.                writeln( 'Country: ', personRecord.PermCountry );
  196.                writeln( 'Street:  ', personRecord.PermStreet );
  197.                writeln( 'City:    ', personRecord.PermCity );
  198.                writeln( 'State:   ', personRecord.PermState );
  199.                writeln( 'Zip:     ', personRecord.PermZip );
  200.                writeln;
  201.             end;
  202.       end;
  203.  
  204.    if loginFlag = TRUE then
  205.       begin
  206.          {**************************************************
  207.          ** Logout of the database
  208.          **************************************************}
  209.          status := XQLLogout;
  210.          writeln( 'XQLLogout status = ', status );
  211.       end;
  212. END.
  213.