home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / Runimage / Delphi50 / Demos / Midas / Adhoc / servdata.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  2.7 KB  |  101 lines

  1. unit ServData;
  2.  
  3. {
  4.   This is the remote datamodule for this demo.  It contains the implementaion
  5.   of the OLE automation object that the client application talks to.  The
  6.   datamodule contains a TProvider component that has an OnDataRequest event
  7.   which is used for dynamically assigning a SQL string.  This demo also
  8.   shows how to use automation methods which are used by the client.
  9. }
  10.  
  11. interface
  12.  
  13. uses
  14.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  15.   ComServ, ComObj, VCLCom, StdVcl, DataBkr, Serv_TLB, Db, DBTables,
  16.   Provider;
  17.  
  18. type
  19.   TAdHocQueryDemo = class(TRemoteDataModule, IAdHocQueryDemo)
  20.     AdHocQuery: TQuery;
  21.     AdHocProvider: TProvider;
  22.     Database1: TDatabase;
  23.     Session1: TSession;
  24.     procedure AdHocQueryDemoCreate(Sender: TObject);
  25.     procedure AdHocQueryDemoDestroy(Sender: TObject);
  26.     procedure AdHocQueryAfterOpen(DataSet: TDataSet);
  27.   protected
  28.     function GetDatabaseNames: OleVariant; safecall;
  29.     procedure SetDatabaseName(const DBName, Password: WideString); safecall;
  30.   end;
  31.  
  32. var
  33.   AdHocQueryDemo: TAdHocQueryDemo;
  34.  
  35. implementation
  36.  
  37. uses ServMain, BDE;
  38.  
  39. {$R *.DFM}
  40.  
  41. function TAdHocQueryDemo.GetDatabaseNames: OleVariant;
  42. var
  43.   I: Integer;
  44.   DBNames: TStrings;
  45. begin
  46.   { Return a list of all of the database names to the client }
  47.   DBNames := TStringList.Create;
  48.   try
  49.     Session1.GetDatabaseNames(DBNames);
  50.     Result := VarArrayCreate([0, DBNames.Count - 1], varOleStr);
  51.     for I := 0 to DBNames.Count - 1 do
  52.       Result[I] := DBNames[I];
  53.   finally
  54.     DBNames.Free;
  55.   end;
  56. end;
  57.  
  58. procedure TAdHocQueryDemo.SetDatabaseName(const DBName,
  59.   Password: WideString);
  60. begin
  61.   { Assign a new Database name }
  62.   try
  63.     Database1.Close;
  64.     Database1.AliasName := DBName;
  65.     if Password <> '' then
  66.       Database1.Params.Values['PASSWORD'] := Password;
  67.     Database1.Open;
  68.   except
  69.     { If the DB open fails, assume it is because a password is required and
  70.       raise a special exception which will cause the client to prompt the
  71.       user for a password }
  72.     on E: EDBEngineError do
  73.       if (Password = '') then
  74.         raise Exception.Create('Password Required') else
  75.         raise;
  76.   end;
  77. end;
  78.  
  79. procedure TAdHocQueryDemo.AdHocQueryDemoCreate(Sender: TObject);
  80. begin
  81.   { Update the client counter }
  82.   MainForm.UpdateClientCount(1);
  83. end;
  84.  
  85. procedure TAdHocQueryDemo.AdHocQueryDemoDestroy(Sender: TObject);
  86. begin
  87.   { Update the client counter }
  88.   MainForm.UpdateClientCount(-1);
  89. end;
  90.  
  91. procedure TAdHocQueryDemo.AdHocQueryAfterOpen(DataSet: TDataSet);
  92. begin
  93.   { Update the query counter }
  94.   MainForm.IncQueryCount;
  95. end;
  96.  
  97. initialization
  98.   TComponentFactory.Create(ComServer, TAdHocQueryDemo,
  99.     Class_AdHocQueryDemo, ciMultiInstance);
  100. end.
  101.