home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 February / DPPCPRO0299.ISO / February / Delphi / Install / DATA.Z / QUERYFRM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-06-11  |  7.2 KB  |  301 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;
  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. var
  194.   I: Integer;
  195.  
  196.   procedure CreateInitialIni;
  197.   const
  198.     VeryInefficientName = 'IB: Very Inefficient Query';
  199.     VeryInefficientQuery =
  200.       'select EMP_NO, Avg(Salary) as Salary\n'+
  201.       '  from employee, employee, employee\n' +
  202.       '  group by EMP_NO';
  203.     AmountDueName = 'DB: Amount Due By Customer';
  204.     AmountDueByCustomer =
  205.       'select Company, Sum(ItemsTotal) - Sum(AmountPaid) as AmountDue\n' +
  206.       '  from customer, orders\n' +
  207.       '  where Customer.CustNo = Orders.CustNo\n' +
  208.       '  group by Company';
  209.   begin
  210.     { Create initial INI file when one doesn't already exisit }
  211.     with SavedQueries do
  212.     begin
  213.       WriteString(VeryInefficientName, 'Query', VeryInefficientQuery);
  214.       WriteString(VeryInefficientName, 'Alias', 'IBLOCAL');
  215.       WriteString(VeryInefficientName, 'Name', 'SYSDBA');
  216.       SavedQueryCombo.Items.Add(VeryInefficientName);
  217.       WriteString(AmountDueName, 'Query', AmountDueByCustomer);
  218.       WriteString(AmountDueName, 'Alias', 'DBDEMOS');
  219.       WriteString(AmountDueName, 'Name', '');
  220.       SavedQueryCombo.Items.Add(AmountDueName);
  221.     end;
  222.   end;
  223.  
  224.  
  225. begin
  226.   { Grab session aliases }
  227.   Session.GetAliasNames(AliasCombo.Items);
  228.  
  229.   { Load in saved queries }
  230.   SavedQueries := TIniFile.Create('BKQUERY.INI');
  231.   SavedQueries.ReadSections(SavedQueryCombo.Items);
  232.   if SavedQueryCombo.Items.Count <= 0 then CreateInitialIni;
  233.   SavedQueryCombo.ItemIndex := 0;
  234.   QueryName := SavedQueryCombo.Items[0];
  235.   Unmodify;
  236.   ReadQuery;
  237. end;
  238.  
  239. procedure TAdhocForm.FormDestroy(Sender: TObject);
  240. begin
  241.   SavedQueries.Free;
  242. end;
  243.  
  244. procedure TAdhocForm.CloseBtnClick(Sender: TObject);
  245. begin
  246.   Close;
  247. end;
  248.  
  249. procedure TAdhocForm.ExecuteBtnClick(Sender: TObject);
  250. begin
  251.   BackgroundQuery(QueryName, AliasCombo.Text, NameEdit.Text, PasswordEdit.Text,
  252.     QueryEdit.Text);
  253.   BringToFront;
  254. end;
  255.  
  256. procedure TAdhocForm.NewBtnClick(Sender: TObject);
  257.  
  258.   function UniqueName: string;
  259.   var
  260.     I: Integer;
  261.   begin
  262.     I := 1;
  263.     repeat
  264.       Result := Format('Query%d', [I]);
  265.     until SavedQueryCombo.Items.IndexOf(Result) < 0;
  266.   end;
  267.  
  268. begin
  269.   AliasCombo.Text := 'DBDEMOS';
  270.   NameEdit.Text := '';
  271.   PasswordEdit.Text := '';
  272.   QueryEdit.Text := '';
  273.   QueryEdit.SetFocus;
  274.   QueryName := UniqueName;
  275.   SavedQueryCombo.ItemIndex := -1;
  276.   Unnamed := True;
  277. end;
  278.  
  279. procedure TAdhocForm.SaveBtnClick(Sender: TObject);
  280. begin
  281.   SaveQuery;
  282. end;
  283.  
  284. procedure TAdhocForm.SaveAsBtnClick(Sender: TObject);
  285. begin
  286.   SaveQueryAs;
  287. end;
  288.  
  289. procedure TAdhocForm.SavedQueryComboChange(Sender: TObject);
  290. begin
  291.   ReadQuery;
  292. end;
  293.  
  294. procedure TAdhocForm.FormCloseQuery(Sender: TObject;
  295.   var CanClose: Boolean);
  296. begin
  297.   CanClose := CheckModified;
  298. end;
  299.  
  300. end.
  301.