home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / Runimage / Delphi50 / Demos / Db / Bkquery / queryfrm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  7.2 KB  |  299 lines

  1. { Demostrates how to execute a query in a background thread.  This
  2.   files contains the main user interface for this program.  The background
  3.   query code is in ResltFrm }
  4.  
  5. unit QueryFrm;
  6.  
  7. interface
  8.  
  9. uses
  10.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  11.   StdCtrls, IniFiles, ExtCtrls, DBTables;
  12.  
  13. type
  14.   TAdhocForm = class(TForm)
  15.     AliasCombo: TComboBox;
  16.     Label1: TLabel;
  17.     QueryEdit: TMemo;
  18.     Label2: TLabel;
  19.     NameEdit: TEdit;
  20.     PasswordEdit: TEdit;
  21.     Label3: TLabel;
  22.     Label4: TLabel;
  23.     ExecuteBtn: TButton;
  24.     CloseBtn: TButton;
  25.     SavedQueryCombo: TComboBox;
  26.     Label5: TLabel;
  27.     SaveBtn: TButton;
  28.     SaveAsBtn: TButton;
  29.     NewBtn: TButton;
  30.     Bevel1: TBevel;
  31.     procedure FormCreate(Sender: TObject);
  32.     procedure CloseBtnClick(Sender: TObject);
  33.     procedure ExecuteBtnClick(Sender: TObject);
  34.     procedure FormDestroy(Sender: TObject);
  35.     procedure NewBtnClick(Sender: TObject);
  36.     procedure SaveBtnClick(Sender: TObject);
  37.     procedure SaveAsBtnClick(Sender: TObject);
  38.     procedure SavedQueryComboChange(Sender: TObject);
  39.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  40.   private
  41.     { Private declarations }
  42.     QueryName, OldAlias: string;
  43.     SavedQueries: TIniFile;
  44.     Unnamed: Boolean;
  45.     function IsModified: Boolean;
  46.     function CheckModified: Boolean;
  47.     procedure Unmodify;
  48.     procedure ReadQuery;
  49.     procedure SaveQuery;
  50.     procedure SaveQueryAs;
  51.   public
  52.     { Public declarations }
  53.   end;
  54.  
  55. var
  56.   AdhocForm: TAdhocForm;
  57.  
  58. implementation
  59.  
  60. uses DB, ResltFrm, SaveQAs;
  61.  
  62. {$R *.DFM}
  63.  
  64. function StrToIniStr(const Str: string): string;
  65. var
  66.   Buffer: array[0..4095] of Char;
  67.   B, S: PChar;
  68. begin
  69.   if Length(Str) > SizeOf(Buffer) then
  70.     raise Exception.Create('String to large to save in INI file');
  71.   S := PChar(Str);
  72.   B := Buffer;
  73.   while S^ <> #0 do
  74.     case S^ of
  75.       #13, #10:
  76.         begin
  77.           if (S^ = #13) and (S[1] = #10) then Inc(S)
  78.           else if (S^ = #10) and (S[1] = #13) then Inc(S);
  79.           B^ := '\';
  80.           Inc(B);
  81.           B^ := 'n';
  82.           Inc(B);
  83.           Inc(S);
  84.         end;
  85.     else
  86.       B^ := S^;
  87.       Inc(B);
  88.       Inc(S);
  89.     end;
  90.   B^ := #0;
  91.   Result := Buffer;
  92. end;
  93.  
  94. function IniStrToStr(const Str: string): string;
  95. var
  96.   Buffer: array[0..4095] of Char;
  97.   B, S: PChar;
  98. begin
  99.   if Length(Str) > SizeOf(Buffer) then
  100.     raise Exception.Create('String to read from an INI file');
  101.   S := PChar(Str);
  102.   B := Buffer;
  103.   while S^ <> #0 do
  104.     if (S[0] = '\') and (S[1] = 'n') then
  105.     begin
  106.       B^ := #13;
  107.       Inc(B);
  108.       B^ := #10;
  109.       Inc(B);
  110.       Inc(S);
  111.       Inc(S);
  112.     end
  113.     else
  114.     begin
  115.       B^ := S^;
  116.       Inc(B);
  117.       Inc(S);
  118.     end;
  119.   B^ := #0;
  120.   Result := Buffer;
  121. end;
  122.  
  123. function TAdhocForm.IsModified: Boolean;
  124. begin
  125.   Result := (AliasCombo.Text <> OldAlias) or NameEdit.Modified or
  126.     QueryEdit.Modified;
  127. end;
  128.  
  129. function TAdhocForm.CheckModified: Boolean;
  130. begin
  131.   Result := True;
  132.   if IsModified then
  133.     case MessageDlg(Format('Query %s modified, save?', [QueryName]),
  134.       mtConfirmation, mbYesNoCancel, 0) of
  135.       mrYes: SaveQuery;
  136.       mrCancel: Result := False;
  137.     end;
  138. end;
  139.  
  140. procedure TAdhocForm.Unmodify;
  141. begin
  142.   OldAlias := AliasCombo.Text;
  143.   NameEdit.Modified := False;
  144.   QueryEdit.Modified := False;
  145. end;
  146.  
  147. procedure TAdhocForm.ReadQuery;
  148. begin
  149.   if not CheckModified then Exit;
  150.   with SavedQueries do
  151.   begin
  152.     QueryName := SavedQueryCombo.Items[SavedQueryCombo.ItemIndex];
  153.     QueryEdit.Text := IniStrToStr(ReadString(QueryName, 'Query', ''));
  154.     AliasCombo.Text := ReadString(QueryName, 'Alias', '');
  155.     NameEdit.Text := ReadString(QueryName, 'Name', '');
  156.   end;
  157.   Unmodify;
  158.   if Showing then
  159.     if NameEdit.Text <> '' then
  160.       PasswordEdit.SetFocus else
  161.       QueryEdit.SetFocus;
  162. end;
  163.  
  164. procedure TAdhocForm.SaveQueryAs;
  165. begin
  166.   if GetNewName(QueryName) then
  167.   begin
  168.     Unnamed := False;
  169.     SaveQuery;
  170.     with SavedQueryCombo, Items do
  171.     begin
  172.       if IndexOf(QueryName) < 0 then Add(QueryName);
  173.       ItemIndex := IndexOf(QueryName);
  174.     end;
  175.   end;
  176. end;
  177.  
  178. procedure TAdhocForm.SaveQuery;
  179. begin
  180.   if Unnamed then
  181.     SaveQueryAs
  182.   else
  183.     with SavedQueries do
  184.     begin
  185.       WriteString(QueryName, 'Query', StrToIniStr(QueryEdit.Text));
  186.       WriteString(QueryName, 'Alias', AliasCombo.Text);
  187.       WriteString(QueryName, 'Name', NameEdit.Text);
  188.       Unmodify;
  189.     end;
  190. end;
  191.  
  192. procedure TAdhocForm.FormCreate(Sender: TObject);
  193.   procedure CreateInitialIni;
  194.   const
  195.     VeryInefficientName = 'IB: Very Inefficient Query';
  196.     VeryInefficientQuery =
  197.       'select EMP_NO, Avg(Salary) as Salary\n'+
  198.       '  from employee, employee, employee\n' +
  199.       '  group by EMP_NO';
  200.     AmountDueName = 'DB: Amount Due By Customer';
  201.     AmountDueByCustomer =
  202.       'select Company, Sum(ItemsTotal) - Sum(AmountPaid) as AmountDue\n' +
  203.       '  from customer, orders\n' +
  204.       '  where Customer.CustNo = Orders.CustNo\n' +
  205.       '  group by Company';
  206.   begin
  207.     { Create initial INI file when one doesn't already exisit }
  208.     with SavedQueries do
  209.     begin
  210.       WriteString(VeryInefficientName, 'Query', VeryInefficientQuery);
  211.       WriteString(VeryInefficientName, 'Alias', 'IBLOCAL');
  212.       WriteString(VeryInefficientName, 'Name', 'SYSDBA');
  213.       SavedQueryCombo.Items.Add(VeryInefficientName);
  214.       WriteString(AmountDueName, 'Query', AmountDueByCustomer);
  215.       WriteString(AmountDueName, 'Alias', 'DBDEMOS');
  216.       WriteString(AmountDueName, 'Name', '');
  217.       SavedQueryCombo.Items.Add(AmountDueName);
  218.     end;
  219.   end;
  220.  
  221.  
  222. begin
  223.   { Grab session aliases }
  224.   Session.GetAliasNames(AliasCombo.Items);
  225.  
  226.   { Load in saved queries }
  227.   SavedQueries := TIniFile.Create('BKQUERY.INI');
  228.   SavedQueries.ReadSections(SavedQueryCombo.Items);
  229.   if SavedQueryCombo.Items.Count <= 0 then CreateInitialIni;
  230.   SavedQueryCombo.ItemIndex := 0;
  231.   QueryName := SavedQueryCombo.Items[0];
  232.   Unmodify;
  233.   ReadQuery;
  234. end;
  235.  
  236. procedure TAdhocForm.FormDestroy(Sender: TObject);
  237. begin
  238.   SavedQueries.Free;
  239. end;
  240.  
  241. procedure TAdhocForm.CloseBtnClick(Sender: TObject);
  242. begin
  243.   Close;
  244. end;
  245.  
  246. procedure TAdhocForm.ExecuteBtnClick(Sender: TObject);
  247. begin
  248.   BackgroundQuery(QueryName, AliasCombo.Text, NameEdit.Text, PasswordEdit.Text,
  249.     QueryEdit.Text);
  250.   BringToFront;
  251. end;
  252.  
  253. procedure TAdhocForm.NewBtnClick(Sender: TObject);
  254.  
  255.   function UniqueName: string;
  256.   var
  257.     I: Integer;
  258.   begin
  259.     I := 1;
  260.     repeat
  261.       Result := Format('Query%d', [I]);
  262.       Inc(I);
  263.     until SavedQueryCombo.Items.IndexOf(Result) < 0;
  264.   end;
  265.  
  266. begin
  267.   AliasCombo.Text := 'DBDEMOS';
  268.   NameEdit.Text := '';
  269.   PasswordEdit.Text := '';
  270.   QueryEdit.Text := '';
  271.   QueryEdit.SetFocus;
  272.   QueryName := UniqueName;
  273.   SavedQueryCombo.ItemIndex := -1;
  274.   Unnamed := True;
  275. end;
  276.  
  277. procedure TAdhocForm.SaveBtnClick(Sender: TObject);
  278. begin
  279.   SaveQuery;
  280. end;
  281.  
  282. procedure TAdhocForm.SaveAsBtnClick(Sender: TObject);
  283. begin
  284.   SaveQueryAs;
  285. end;
  286.  
  287. procedure TAdhocForm.SavedQueryComboChange(Sender: TObject);
  288. begin
  289.   ReadQuery;
  290. end;
  291.  
  292. procedure TAdhocForm.FormCloseQuery(Sender: TObject;
  293.   var CanClose: Boolean);
  294. begin
  295.   CanClose := CheckModified;
  296. end;
  297.  
  298. end.
  299.