home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / sdktab.pak / MAIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-07-24  |  15.9 KB  |  537 lines

  1. {$A+,B-,D+,F-,G+,I+,K+,L+,N+,P+,Q-,R-,S+,T-,V+,W-,X+,Y+}
  2. {$M 25600,4096}
  3. { unit main.pas }
  4. unit Main;
  5.  
  6. interface
  7.  
  8. uses
  9.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  10.   Forms, Dialogs, StdCtrls, DB, DBTables, DbiTypes, DbiProcs, DbiErrs,
  11.   ExtCtrls, Grids, DBGrids, TableEnh, ShowNew;
  12.  
  13. const
  14.   copiedTable = 'COPY.DB';
  15.  
  16. type
  17.   TMainForm = class(TForm)
  18.     Panel1: TPanel;
  19.     SharedBtn: TButton;
  20.     TblLockedBtn: TButton;
  21.     RecLockedBtn: TButton;
  22.     SysVersionBtn: TButton;
  23.     SysInfoBtn: TButton;
  24.     SysConfigBtn: TButton;
  25.     Label8: TLabel;
  26.     Panel2: TPanel;
  27.     Label9: TLabel;
  28.     DataSource1: TDataSource;
  29.     DBGrid1: TDBGrid;
  30.     FastInsertBtn: TButton;
  31.     Panel3: TPanel;
  32.     Label1: TLabel;
  33.     Label2: TLabel;
  34.     Label3: TLabel;
  35.     Label4: TLabel;
  36.     Label5: TLabel;
  37.     Label6: TLabel;
  38.     Label7: TLabel;
  39.     BlockInsertBtn: TButton;
  40.     CountLabel: TLabel;
  41.     PackTableBtn: TButton;
  42.     CopyTableBtn: TButton;
  43.     StatusLabel: TLabel;
  44.     TableEnhanced1: TTableEnhanced;
  45.     RemoveLocksBtn: TButton;
  46.     FastAppendBtn: TButton;
  47.     OpenDialog1: TOpenDialog;
  48.     procedure SharedBtnClick(Sender: TObject);
  49.     procedure TblLockedBtnClick(Sender: TObject);
  50.     procedure RecLockedBtnClick(Sender: TObject);
  51.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  52.     procedure SysVersionBtnClick(Sender: TObject);
  53.     procedure SysInfoBtnClick(Sender: TObject);
  54.     procedure SysConfigBtnClick(Sender: TObject);
  55.     procedure FormCreate(Sender: TObject);
  56.     procedure DataSource1DataChange(Sender: TObject; Field: TField);
  57.     procedure FastInsertBtnClick(Sender: TObject);
  58.     procedure PackTableBtnClick(Sender: TObject);
  59.     procedure CopyTableBtnClick(Sender: TObject);
  60.     procedure BlockInsertBtnClick(Sender: TObject);
  61.     procedure RemoveLocksBtnClick(Sender: TObject);
  62.     procedure FastAppendBtnClick(Sender: TObject);
  63.   private
  64.     { Private declarations }
  65.     RecCount: Longint;
  66.     procedure ClearLabels;
  67.     procedure CreateTempTable;
  68.     procedure InsertRecords;
  69.   end;
  70.  
  71. var
  72.   MainForm: TMainForm;
  73.  
  74. implementation
  75.  
  76. {$R *.DFM}
  77.  
  78. { METHOD: InsertRecords
  79.   PURPOSE: Inserts blocks of records specified by the BlockSize and
  80.     BlockTotal parameters of TTableEnhanced.
  81. }
  82. procedure TMainForm.InsertRecords;
  83. var
  84.   W: Word;
  85.   MoveDone: Boolean;
  86.  
  87. begin
  88.   MoveDone := False;
  89.   { Continue to call the WriteBlock method until it returns True }
  90.   with TableEnhanced1 do
  91.   begin
  92.     repeat
  93.       { Allocate and Initialize the record buffer }
  94.       InitializeBuffer;
  95.       { Fill the record buffer }
  96.       for W := 1 to BlockSize do
  97.       begin
  98.         Inc(RecCount);
  99.         InitIntegerField(FieldByName('IntField').Index, RecCount);
  100.         InitStringField(FieldByName('StrField').Index,
  101.                         Format('Record %d', [RecCount]));
  102.         InitTimeField(FieldByName('TimeField').Index, SysUtils.Time);
  103.         { After each record in the record buffer has been filled, move the
  104.           record buffer ponter to the next record in the record buffer }
  105.         NextRecord;
  106.       end;
  107.       { Place the block of records into the table }
  108.       MoveDone := WriteBlock;
  109.     { Check to see if all records have been placed into the table }
  110.     until MoveDone = True;
  111.   end
  112. end;
  113.  
  114. { METHOD: CreateTempTable
  115.   PURPOSE: Creates a table that is used for this exmaple.
  116. }
  117. procedure TMainForm.CreateTempTable;
  118. begin
  119.   with TableEnhanced1 do
  120.   begin
  121.     TableName := 'TEMPTABL';
  122.     TableType := ttParadox;
  123.     FieldDefs.Clear;
  124.     IndexDefs.Clear;
  125.     try
  126.       FieldDefs.Add('IntField', ftInteger, 0, True);
  127.       FieldDefs.Add('StrField', ftString, 14, True);
  128.       FieldDefs.Add('TimeField', ftTime, 0, True);
  129.       IndexDefs.Add('', 'IntField', [ixPrimary]);
  130.       CreateTable;
  131.     except
  132.       on E:EDatabaseError do
  133.       begin
  134.         Screen.Cursor := crDefault;
  135.         if MessageDlg(E.Message + '.  Would you like to create the table in ' +
  136.                    ' the executable''s directory?', mtConfirmation,
  137.                    [mbYes, mbNo], 0) =  mrYes then
  138.         begin
  139.           DatabaseName := '';
  140.           try
  141.             CreateTable;
  142.           except
  143.             on E:EDatabaseError do
  144.             begin
  145.               raise EEnhDBError.Create('Application is terminating');
  146.               Application.Terminate;
  147.             end;
  148.           end;
  149.         end
  150.         else
  151.         begin
  152.           raise EEnhDBError.Create('Application is terminating');
  153.           Application.Terminate;
  154.         end;
  155.       end;
  156.     end;
  157.   end;
  158. end;
  159.  
  160. { METHOD: ClearLabels
  161.   PURPOSE: Clears the IDAPI information labels.
  162. }
  163. procedure TMainForm.ClearLabels;
  164. begin
  165.   Label1.Caption := '';
  166.   Label2.Caption := '';
  167.   Label3.Caption := '';
  168.   Label4.Caption := '';
  169.   Label5.Caption := '';
  170.   Label6.Caption := '';
  171.   Label7.Caption := '';
  172. end;
  173.  
  174. { METHOD: SharedBtnClick
  175.   PURPOSE: Check IDAPI to see if the currently opened table is shared.
  176. }
  177. procedure TMainForm.SharedBtnClick(Sender: TObject);
  178. begin
  179.   ClearLabels;
  180.   if TableEnhanced1.IsTableShared = True then
  181.     Label1.Caption := 'Table is shared'
  182.   else
  183.     Label1.Caption := 'Table is NOT shared';
  184. end;
  185.  
  186. { METHOD: TblLockedBtnClick
  187.   PURPOSE: Check IDAPI to see if the currently opened table is locked.
  188. }
  189. procedure TMainForm.TblLockedBtnClick(Sender: TObject);
  190. begin
  191.   ClearLabels;
  192.   Label1.Caption := Format('Table has %d table locks',
  193.                     [TableEnhanced1.IsTableLocked(dbiWriteLock)]);
  194. end;
  195.  
  196. { METHOD: RecLockedBtnClick
  197.   PURPOSE: Check IDAPI to see if the current record is locked.
  198. }
  199. procedure TMainForm.RecLockedBtnClick(Sender: TObject);
  200. begin
  201.   ClearLabels;
  202.   if TableEnhanced1.IsRecordLocked = True then
  203.     Label1.Caption := 'Current record is locked'
  204.   else
  205.     Label1.Caption := 'Current record is NOT locked';
  206. end;
  207.  
  208. { METHOD: SysVersionBtnClick
  209.   PURPOSE:  Check IDAPI for system version information.
  210. }
  211. procedure TMainForm.SysVersionBtnClick(Sender: TObject);
  212. var
  213.   Version: SysVersion;
  214.  
  215. begin
  216.   ClearLabels;
  217.   Version := TableEnhanced1.GetSysVersion;
  218.   with Version do
  219.   begin
  220.     Label1.Caption := Format('Engine version = %d', [iVersion]);
  221.     Label2.Caption := Format('Client interface level = %d', [iIntfLevel]);
  222.     Label3.Caption := 'Version date = ' + DateToStr(DateVer);
  223.     Label4.Caption := 'Version time = ' + TimeToStr(TimeVer);
  224.   end;
  225. end;
  226.  
  227. { METHOD: SysVersionBtnClick
  228.   PURPOSE:  Check IDAPI for system information.
  229. }
  230. procedure TMainForm.SysInfoBtnClick(Sender: TObject);
  231. var
  232.   Info: SysInfo;
  233.  
  234. begin
  235.   ClearLabels;
  236.   Info := TableEnhanced1.GetSysInfo;
  237.   with Info do
  238.   begin
  239.     Label1.Caption := Format('Buffer size in kb = %d', [iBufferSpace]);
  240.     Label2.Caption := Format('Heap size in kb = %d', [iHeapSpace]);
  241.     Label3.Caption := Format('# of loaded drivers = %d', [iDrivers]);
  242.     Label4.Caption := Format('# of active clients = %d', [iClients]);
  243.     Label5.Caption := Format('# of active sessions = %d', [iSessions]);
  244.     Label6.Caption := Format('# of open databases = %d', [iDatabases]);
  245.     Label7.Caption := Format('# of open cursors (all clients) = %d',
  246.                              [iCursors]);
  247.   end;
  248. end;
  249.  
  250. { METHOD: SysVersionBtnClick
  251.   PURPOSE:  Check IDAPI for system configuration information.
  252. }
  253. procedure TMainForm.SysConfigBtnClick(Sender: TObject);
  254. var
  255.   Config: SysConfig;
  256. begin
  257.   ClearLabels;
  258.   Config := TableEnhanced1.GetSysConfig;
  259.   with Config do
  260.   begin
  261.     if bLocalShare = True then
  262.       Label1.Caption := 'Table can be shared with non-idapi applications'
  263.     else
  264.       Label1.Caption := 'Table cannot be shared with non-idapi applications';
  265.     Label2.Caption := 'Network type = ' + szNetType;
  266.     Label3.Caption := 'Network user name = ' + szUserName;
  267.     Label4.Caption := 'Configuration file name = ' + szIniFile;
  268.     Label5.Caption := 'System language driver = ' + szLangDriver;
  269.   end;
  270. end;
  271.  
  272. { METHOD: FormCloseQuery
  273.   PURPOSE:  Close and delete the temporary table when the applivcstion
  274.     is closed.
  275. }
  276. procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  277. begin
  278.   Screen.Cursor := crHourGlass;
  279.   try
  280.     if TableEnhanced1.Active = true then
  281.     begin
  282.       TableEnhanced1.Close;
  283.       TableEnhanced1.DeleteTable;
  284.     end;
  285.   except
  286.     on E:EDatabaseError do
  287.       MessageDlg(E.Message, mtError, [mbOk], 0);
  288.   end;
  289.   Screen.Cursor := crDefault;
  290. end;
  291.  
  292. { METHOD: FormCreate
  293.   PURPOSE:  Create and open the temporary table.
  294. }
  295. procedure TMainForm.FormCreate(Sender: TObject);
  296. begin
  297.   Screen.Cursor := crHourGlass;
  298.   { Set the record counter to zero }
  299.   RecCount := 0;
  300.   Application.HintColor := clTeal;
  301.   ClearLabels;
  302.   try
  303.     { Treate the temporary table }
  304.     CreateTempTable;
  305.     { Open the table }
  306.     TableEnhanced1.Open;
  307.     { Add initial records to the table }
  308.     InsertRecords;
  309.     { Display the new records in the DBGrid }
  310.     TableEnhanced1.Refresh;
  311.     { Set the cursor to the first record in the table }
  312.     TableEnhanced1.First;
  313.   except
  314.     on E:EDatabaseError do
  315.     begin
  316.       Screen.Cursor := crDefault;
  317.       MessageDlg(E.Message, mtError, [mbOk], 0);
  318.       Application.Terminate;
  319.     end;
  320.   end;
  321.   Screen.Cursor := crDefault;
  322. end;
  323.  
  324. { METHOD: DataSource1DataChange
  325.   PURPOSE:  Update the record count whenever the data in the
  326.     table has changed.
  327. }
  328. procedure TMainForm.DataSource1DataChange(Sender: TObject; Field: TField);
  329. begin
  330.   CountLabel.Caption := Format('Record count = %d',
  331.                                [TableEnhanced1.RecordCount]);
  332. end;
  333.  
  334. { METHOD: FastInsertBtnClick
  335.   PURPOSE:  Insert one record using the InsertFast Method.
  336. }
  337. procedure TMainForm.FastInsertBtnClick(Sender: TObject);
  338. begin
  339.   Inc(RecCount);
  340.   { Set the BlockSize to one so extra record buffers are not allocated.
  341.     NOTE: It would not effect the behavior of InsertFast to have BlockSize
  342.     greater than one;  it would just be a waste of memory and
  343.     slightly slower. }
  344.   TableEnhanced1.BlockSize := 1;
  345.   try
  346.     with TableEnhanced1 do
  347.     begin
  348.       { Allocate and Initialize the record buffer }
  349.       InitializeBuffer;
  350.       { Fill the record buffer }
  351.       InitIntegerField(FieldByName('IntField').Index, RecCount);
  352.       InitStringField(FieldByName('StrField').Index, Format('Record %d',
  353.                              [RecCount]));
  354.       InitTimeField(FieldByName('TimeField').Index, SysUtils.Time);
  355.       { Insert the record }
  356.       InsertFast;
  357.       { Make sure the Delphi controls are aware of the new record }
  358.       Refresh;
  359.       { Move the cursor to the last record in the table }
  360.       Last;
  361.       StatusLabel.Caption := 'Status = Success';
  362.     end;
  363.   except
  364.     on E:EDatabaseError do
  365.     begin
  366.       Dec(RecCount);
  367.       MessageDlg(E.Message, mtError, [mbOk], 0);
  368.     end;
  369.   end;
  370. end;
  371.  
  372. { METHOD: PackTableBtnClick
  373.   PURPOSE:  Pack the currently opened table.
  374. }
  375. procedure TMainForm.PackTableBtnClick(Sender: TObject);
  376. begin
  377.   Screen.Cursor := crHourGlass;
  378.   StatusLabel.Caption := 'Status = Packing...';
  379.   Application.ProcessMessages;
  380.   try
  381.     { Pack table }
  382.     TableEnhanced1.Pack;
  383.     StatusLabel.Caption := 'Status = Success';
  384.   except
  385.     on E:EEnhDBError do
  386.     begin
  387.       Screen.Cursor := crDefault;
  388.       StatusLabel.Caption := 'Status = ERROR';
  389.       MessageDlg(E.Message, mtError, [mbOk], 0);
  390.     end;
  391.   end;
  392.   Screen.Cursor := crDefault;
  393. end;
  394.  
  395. { METHOD: CopyTableBtnClick
  396.   PURPOSE:  Copy the currently opened table to another table of a
  397.     different name.
  398. }
  399. procedure TMainForm.CopyTableBtnClick(Sender: TObject);
  400. begin
  401.   Screen.Cursor := crHourGlass;
  402.   StatusLabel.Caption := 'Status = Copying...';
  403.   Application.ProcessMessages;
  404.   try
  405.     { Set the table name on the ViewNew form to "copiedTable" }
  406.     ViewNew.TableEnhanced1.DatabaseName :=
  407.         TableEnhanced1.DatabaseName;
  408.     ViewNew.TableEnhanced1.TableName := copiedTable;
  409.     { Copy the table }
  410.     TableEnhanced1.CopyTable(copiedTable);
  411.     { Open the table on the ViewNew form }
  412.     ViewNew.TableEnhanced1.Open;
  413.     Screen.Cursor := crDefault;
  414.     StatusLabel.Caption := 'Status = Success';
  415.     { Display the ViewNew form thus showing the copied table }
  416.     ViewNew.ShowModal;
  417.     ViewNew.TableEnhanced1.Close;
  418.     { If the user decided to delete the table, delete it }
  419.     if ViewNew.DeleteCopy.Checked = True then
  420.       ViewNew.TableEnhanced1.DeleteTable;
  421.   except
  422.     { If the user decided to copy a table over a previously created
  423.       table, ask the user if the old table should be deleted.
  424.       NOTE: The table can always be overwritten if
  425.       TTableEnhanced.Overwrite is True. }
  426.     on EEnhDBFileExists do
  427.     begin
  428.       Screen.Cursor := crDefault;
  429.       StatusLabel.Caption := 'Status = Table Exists';
  430.       if MessageDlg('Table exists.  Delete', mtConfirmation, [mbYes, mbNo], 0)
  431.                     = mrYes then
  432.       begin
  433.         { Delete the old table }
  434.         ViewNew.TableEnhanced1.DeleteTable;
  435.         StatusLabel.Caption := 'Status = Deleted';
  436.       end
  437.       else
  438.         StatusLabel.Caption := 'Status = Not deleted';
  439.     end;
  440.     on E:EDatabaseError do
  441.     begin
  442.       Screen.Cursor := crDefault;
  443.       StatusLabel.Caption := 'Status = ERROR';
  444.       MessageDlg(E.Message, mtError, [mbOk], 0);
  445.     end;
  446.   end;
  447. end;
  448.  
  449. { METHOD: BlockInsertBtnClick
  450.   PURPOSE: Insert a block of records into the table.
  451. }
  452. procedure TMainForm.BlockInsertBtnClick(Sender: TObject);
  453. begin
  454.   Screen.cursor := crHourGlass;
  455.   { Set the BlockSize and BlockTotal.  The following means: Insert a
  456.     total of 500 records, 100 records at a time. }
  457.   TableEnhanced1.BlockTotal := 500;
  458.   TableEnhanced1.BlockSize := 100;
  459.   StatusLabel.Caption := 'Status = Inserting...';
  460.   Application.ProcessMessages;
  461.   try
  462.     { Insert the records }
  463.     InsertRecords;
  464.     { Make sure the Delphi controls are aware of the new record }
  465.     TableEnhanced1.Refresh;
  466.     { Move the cursor to the last record in the table }
  467.     TableEnhanced1.Last;
  468.     StatusLabel.Caption := 'Status = Success';
  469.     Screen.Cursor := crDefault;
  470.   except
  471.     on E:EDatabaseError do
  472.     begin
  473.       Screen.Cursor := crDefault;
  474.       StatusLabel.Caption := 'Status = ERROR';
  475.       MessageDlg(E.Message, mtError, [mbOk], 0);
  476.     end;
  477.   end;
  478. end;
  479.  
  480. { METHOD: RemoveLocksBtnClick
  481.   PURPOSE: Remove all record locks on the table.  NOTE: There are
  482.     record locks on records inserted with InsertFast.  If no locks
  483.     are desired, set TTableEnhanced.InsertMode = "imNoLock".  Also,
  484.     removing record locks before block or reocrd insertion increases
  485.     performance.
  486. }
  487. procedure TMainForm.RemoveLocksBtnClick(Sender: TObject);
  488. begin
  489.   try
  490.     { Release all record locks }
  491.     TableEnhanced1.ReleaseRecordLock(True);
  492.   except
  493.     on E:EDatabaseError do
  494.     begin
  495.       StatusLabel.Caption := 'Status = ERROR';
  496.       MessageDlg(E.Message, mtError, [mbOk], 0);
  497.     end;
  498.   end;
  499. end;
  500.  
  501. procedure TMainForm.FastAppendBtnClick(Sender: TObject);
  502. begin
  503.   Inc(RecCount);
  504.   { Set the BlockSize to one so extra record buffers are not allocated.
  505.     NOTE: It would not effect the behavior of AppendFast to have BlockSize
  506.     greater than one;  it would just be a waste of memory and
  507.     slightly slower. }
  508.   TableEnhanced1.BlockSize := 1;
  509.   try
  510.     with TableEnhanced1 do
  511.     begin
  512.       { Allocate and Initialize the record buffer }
  513.       InitializeBuffer;
  514.       { Fill the record buffer }
  515.       InitIntegerField(FieldByName('IntField').Index, RecCount);
  516.       InitStringField(FieldByName('StrField').Index, Format('Record %d',
  517.                              [RecCount]));
  518.       InitTimeField(FieldByName('TimeField').Index, SysUtils.Time);
  519.       { Append the record }
  520.       AppendFast;
  521.       { Make sure the Delphi controls are aware of the new record }
  522.       Refresh;
  523.       { Move the cursor to the last record in the table }
  524.       Last;
  525.       StatusLabel.Caption := 'Status = Success';
  526.     end;
  527.   except
  528.     on E:EDatabaseError do
  529.     begin
  530.       Dec(RecCount);
  531.       MessageDlg(E.Message, mtError, [mbOk], 0);
  532.     end;
  533.   end;
  534. end;
  535.  
  536. end.
  537.