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

  1. {****************************************************************************
  2. **
  3. **  Copyright 1982-1997 Pervasive Software Inc. All Rights Reserved
  4. **
  5. ****************************************************************************}
  6. {****************************************************************************
  7.    SQLSAMPW.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 Windows application.
  11.  
  12.       This program demonstrates the Pascal 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. ****************************************************************************}
  30. PROGRAM sqlsamp;
  31.  
  32. USES
  33.    sqlapiw,    { SQL Interface Unit }
  34.    WinCrt;     { text mode I/O library for Windows }
  35.  
  36. {*****************************************************************************
  37.    Constants
  38. *****************************************************************************}
  39. CONST
  40.    TRUE    =  1;
  41.    FALSE   =  0;
  42.    SUCCESS =  0;
  43.    FAILURE = -1;
  44.    STATEMENT_BUFFER_SIZE = 1024;
  45.    BYTE_COUNT_SIZE       = 2;
  46.    SPACING_NOT_PERTINENT = 0;
  47.    UserID   : CHAR = #0;
  48.    Password : CHAR = #0;
  49.    Reserved : CHAR = #0;
  50.    DDpath   : string[ 20 ] = 'v:\ssql40\demodata';      { IMPORTANT }
  51.    Datapath : string[ 20 ] = 'v:\ssql40\demodata';      { IMPORTANT }
  52.    FETCH_FIRST        : integer = 1;
  53.    INTERNAL_FORMAT    : integer = 0;
  54.  
  55.  
  56. {***************************************************************************
  57.    Structures
  58.    Definition of record from the 'person' table
  59. ****************************************************************************}
  60. TYPE
  61.    PERSON_STRUCT = record
  62.       RecLen         : word;
  63.       ID             : longint;
  64.       Dummy          : longint;
  65.       FirstName      : array[1..16] of char;
  66.       LastName       : array[1..26] of char;
  67.       PermStreet     : array[1..31] of char;
  68.       PermCity       : array[1..31] of char;
  69.       PermState      : array[1..3] of char;
  70.       PermZip        : array[1..11] of char;
  71.       PermCountry    : array[1..21] of char;
  72.       Street         : array[1..31] of char;
  73.       City           : array[1..31] of char;
  74.       State          : array[1..3] of char;
  75.       Zip            : array[1..11] of char;
  76.       Phone          : array[1..10] of char;
  77.       EmergencyPhone : array[1..20] of char;
  78.       Unlisted       : char;
  79.       DateOfBirth    : array[1..4] of char;
  80.       EmailAddress   : array[1..31] of char;
  81.       Sex            : char;
  82.       Citizenship    : array[1..21] of char;
  83.       Survey         : char;
  84.       Smoker         : char;
  85.       Married        : char;
  86.       Children       : char;
  87.       Disability     : char;
  88.       Scholarship    : char;
  89.       Comments       : array[1..200] of char;
  90.    end;
  91.  
  92. VAR
  93.    cursorID     :  integer;
  94.    statement    :  string [255];
  95.    statlen      :  integer;
  96.    loginFlag    :  integer;
  97.    cursorIDFlag :  integer;
  98.    status       : integer;
  99.    bufferLength : integer;
  100.    personRecord : PERSON_STRUCT;
  101.    recordsRead  : longint;
  102.  
  103. {***********************************************************************
  104.    Program starts here
  105. ************************************************************************}
  106. BEGIN { sqlsamp }
  107.    writeln( '***** Scalable SQL Pascal Interface Demo *****' );
  108.    writeln;
  109.  
  110.    loginFlag := FALSE;
  111.    cursorIDFlag := FALSE;
  112.    {**************************************************
  113.    ** Login to the database
  114.    **************************************************}
  115.    status := XQLLogin(
  116.                 UserID,
  117.                 Password,
  118.                 DDpath[1],
  119.                 Datapath[1],
  120.                 Reserved,
  121.                 1);
  122.  
  123.    writeln( 'XQLLogin status = ', status );
  124.    if status <> SUCCESS then
  125.       begin
  126.          status := FAILURE;
  127.       end
  128.    else
  129.       begin
  130.          loginFlag := TRUE;
  131.       end;
  132.  
  133.    if status = SUCCESS then
  134.       begin
  135.          {**************************************************
  136.          ** Get a cursor ID
  137.          **************************************************}
  138.          status := XQLCursor (cursorID);
  139.          writeln( 'XQLCursorID status = ', status );
  140.          if status <> SUCCESS then
  141.             begin
  142.                status := FAILURE;
  143.             end
  144.          else
  145.             begin
  146.                cursorIDFlag := TRUE;
  147.             end;
  148.       end;
  149.  
  150.    if status = SUCCESS then
  151.       begin
  152.          {**************************************************
  153.          ** Compile the select statement
  154.          **************************************************}
  155.          statement := 'SELECT * from person where ID = 101135758 ' + #0;
  156.          Statlen   := length (Statement);
  157.  
  158.          status := XQLCompile(
  159.                       cursorID,
  160.                       statlen,
  161.                       statement [1] );
  162.  
  163.          writeln( 'XQLCompile status = ', status );
  164.          if status > SUCCESS then
  165.             begin
  166.                status := FAILURE;
  167.             end
  168.          else
  169.             begin
  170.                writeln( 'SELECT * from person where ID = 101135758' );
  171.             end;
  172.        end;
  173.  
  174.    if status = SUCCESS then
  175.       begin
  176.          {**************************************************
  177.          ** Fetch the record
  178.          **************************************************}
  179.          bufferLength := SizeOf( PERSON_STRUCT );
  180.  
  181.          recordsRead := 1;
  182.          status := XQLFetch(
  183.                       cursorID,
  184.                       FETCH_FIRST,
  185.                       bufferLength,
  186.                       personRecord,
  187.                       recordsRead,
  188.                       INTERNAL_FORMAT,
  189.                       SPACING_NOT_PERTINENT );
  190.  
  191.          writeln( 'XQLFetch status = ', status );
  192.          if status <> SUCCESS then
  193.             begin
  194.                status := FAILURE;
  195.             end
  196.          else
  197.             begin
  198.                writeln;
  199.                writeln( 'Selected fields from the retrieved record are:' );
  200.                writeln( 'Name:    ', personRecord.FirstName, ' ',
  201.                                         personRecord.LastName );
  202.                writeln( 'Country: ', personRecord.PermCountry );
  203.                writeln( 'Street:  ', personRecord.PermStreet );
  204.                writeln( 'City:    ', personRecord.PermCity );
  205.                writeln( 'State:   ', personRecord.PermState );
  206.                writeln( 'Zip:     ', personRecord.PermZip );
  207.                writeln;
  208.             end;
  209.       end;
  210.  
  211.    if cursorIDFlag = TRUE then
  212.       begin
  213.          {**************************************************
  214.          ** Free the resources
  215.          **************************************************}
  216.          status := XQLFree( cursorID );
  217.          writeln( 'XQLFree status = ', status );
  218.          if status > SUCCESS then
  219.             begin
  220.                status := FAILURE;
  221.             end
  222.          else
  223.             begin
  224.                status := SUCCESS;
  225.             end;
  226.       end;
  227.  
  228.    if loginFlag = TRUE then
  229.       begin
  230.          {**************************************************
  231.          ** Logout of the database
  232.          **************************************************}
  233.          status := XQLLogout;
  234.          writeln( 'XQLLogout status = ', status );
  235.          if status > SUCCESS then
  236.             begin
  237.                status := FAILURE;
  238.             end
  239.          else
  240.             begin
  241.                status := SUCCESS;
  242.             end;
  243.       end;
  244.  
  245.       {**************************************************
  246.       ** Stop the engine
  247.       **************************************************}
  248.       status := XQLStop;
  249.       writeln( 'XQLStop status = ', status );
  250.       if status > SUCCESS then
  251.          begin
  252.             status := FAILURE;
  253.          end
  254.       else
  255.          begin
  256.             status := SUCCESS;
  257.          end;
  258. END.
  259.