home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kolekce / d6 / FRCLX.ZIP / SOURCE / DBX / FR_DBXDB.pas < prev    next >
Pascal/Delphi Source File  |  2001-07-08  |  6KB  |  224 lines

  1.  
  2. {******************************************}
  3. {                                          }
  4. {   FastReport CLX v2.4 - DBX components   }
  5. {            Database component            }
  6. {                                          }
  7. { Copyright (c) 1998-2001 by Tzyganenko A. }
  8. {                                          }
  9. {******************************************}
  10.  
  11. unit FR_DBXDB;
  12.  
  13. interface
  14.  
  15. {$I FR.inc}
  16.  
  17. uses
  18.   Types, SysUtils, Classes, QGraphics, FR_Class, QStdCtrls,
  19.   QControls, QForms, QMenus, QDialogs, DB, DBXpress, SqlExpr;
  20.  
  21. type
  22.   TfrDBXComponents = class(TComponent) // fake component
  23.   end;
  24.  
  25.   TfrDBXDatabase = class(TfrNonVisualControl)
  26.   private
  27.     FDatabase: TSQLConnection;
  28.     procedure LinesEditor(Sender: TObject);
  29.   protected
  30.     procedure SetPropValue(Index: String; Value: Variant); override;
  31.     function GetPropValue(Index: String): Variant; override;
  32.     function DoMethod(MethodName: String; Par1, Par2, Par3: Variant): Variant; override;
  33.   public
  34.     constructor Create; override;
  35.     destructor Destroy; override;
  36.     procedure LoadFromStream(Stream: TStream); override;
  37.     procedure SaveToStream(Stream: TStream); override;
  38.     procedure DefineProperties; override;
  39.     property Database: TSQLConnection read FDatabase;
  40.   end;
  41.  
  42.  
  43. implementation
  44.  
  45. uses
  46.   FR_Utils, FR_Const, FR_LEdit, FR_DBLookupCtl, FR_DBXTable,
  47.   FR_DBXQuery, Variants;
  48.  
  49. {$R FR_DBX.res}
  50.  
  51. type
  52.   THackSQLConnection = class(TSQLConnection)
  53.   end;
  54.  
  55.  
  56. { TfrDBXDatabase }
  57.  
  58. constructor TfrDBXDatabase.Create;
  59. begin
  60.   inherited Create;
  61.   FDatabase := TSQLConnection.Create(frDialogForm);
  62. // set ComponentState := csDesigning to obtain Params automatically
  63.   THackSQLConnection(FDataBase).SetDesigning(True, False);
  64.   Component := FDatabase;
  65.   BaseName := 'Database';
  66.   Bmp.LoadFromResourceName(hInstance, 'FR_DBXDB');
  67.   Flags := Flags or flDontUndo;
  68. end;
  69.  
  70. destructor TfrDBXDatabase.Destroy;
  71. begin
  72.   FDatabase.Free;
  73.   inherited Destroy;
  74. end;
  75.  
  76. procedure TfrDBXDatabase.DefineProperties;
  77.  
  78.   function _GetConnectionNames: String;
  79.   var
  80.     i: Integer;
  81.     sl: TStringList;
  82.   begin
  83.     Result := '';
  84.     sl := TStringList.Create;
  85.     GetConnectionNames(sl);
  86.     sl.Sort;
  87.     for i := 0 to sl.Count - 1 do
  88.       Result := Result + sl[i] + ';';
  89.     sl.Free;
  90.   end;
  91.  
  92.   function _GetDriverNames: String;
  93.   var
  94.     i, j: Integer;
  95.     sl: TStringList;
  96.     s: String;
  97.   begin
  98.     Result := '';
  99.     sl := TStringList.Create;
  100.     GetDriverNames(sl);
  101.     sl.Sort;
  102.     for i := 0 to sl.Count - 1 do
  103.     begin
  104.       s := sl[i];
  105.       for j := 1 to Length(s) do
  106.         if s[j] = ';' then
  107.           s[j] := ',';
  108.       Result := Result + s + ';';
  109.     end;
  110.     sl.Free;
  111.   end;
  112.  
  113. begin
  114.   inherited DefineProperties;
  115.   AddProperty('Connected', [frdtBoolean], nil);
  116.   AddEnumProperty('ConnectionName', _GetConnectionNames, [Null]);
  117.   AddEnumProperty('DriverName', _GetDriverNames, [Null]);
  118.   AddProperty('LoginPrompt', [frdtBoolean], nil);
  119.   AddProperty('Params', [frdtHasEditor, frdtOneObject], LinesEditor);
  120.   AddProperty('Params.Count', [], nil);
  121. end;
  122.  
  123. procedure TfrDBXDatabase.SetPropValue(Index: String; Value: Variant);
  124. begin
  125.   inherited SetPropValue(Index, Value);
  126.   Index := AnsiUpperCase(Index);
  127.   if Index = 'CONNECTIONNAME' then
  128.     FDatabase.ConnectionName := Value
  129.   else if Index = 'DRIVERNAME' then
  130.     FDatabase.DriverName := Value
  131.   else if Index = 'LOGINPROMPT' then
  132.     FDatabase.LoginPrompt := Value
  133.   else if Index = 'CONNECTED' then
  134.     FDatabase.Connected := Value
  135.   else if Index = 'PARAMS' then
  136.     FDatabase.Params.Text := Value
  137. end;
  138.  
  139. function TfrDBXDatabase.GetPropValue(Index: String): Variant;
  140. begin
  141.   Index := AnsiUpperCase(Index);
  142.   Result := inherited GetPropValue(Index);
  143.   if Result <> Null then Exit;
  144.   if Index = 'CONNECTIONNAME' then
  145.     Result := FDatabase.ConnectionName
  146.   else if Index = 'DRIVERNAME' then
  147.     Result := FDatabase.DriverName
  148.   else if Index = 'LOGINPROMPT' then
  149.     Result := FDatabase.LoginPrompt
  150.   else if Index = 'CONNECTED' then
  151.     Result := FDatabase.Connected
  152.   else if Index = 'PARAMS.COUNT' then
  153.     Result := FDatabase.Params.Count
  154.   else if Index = 'PARAMS' then
  155.     Result := FDatabase.Params.Text
  156. end;
  157.  
  158. function TfrDBXDataBase.DoMethod(MethodName: String; Par1, Par2, Par3: Variant): Variant;
  159. begin
  160.   Result := inherited DoMethod(MethodName, Par1, Par2, Par3);
  161.   if Result = Null then
  162.     Result := LinesMethod(FDataBase.Params, MethodName, 'PARAMS', Par1, Par2, Par3);
  163. end;
  164.  
  165. procedure TfrDBXDatabase.LoadFromStream(Stream: TStream);
  166. var
  167.   s: String;
  168. begin
  169.   inherited LoadFromStream(Stream);
  170.   FDatabase.ConnectionName := frReadString(Stream);
  171.   s := frReadString(Stream);
  172.   if s <> '' then
  173.     FDatabase.DriverName := s;
  174.   FDatabase.LoginPrompt := frReadBoolean(Stream);
  175.   frReadMemo(Stream, FDatabase.Params);
  176.   FDatabase.Connected := frReadBoolean(Stream);
  177. end;
  178.  
  179. procedure TfrDBXDatabase.SaveToStream(Stream: TStream);
  180. begin
  181.   inherited SaveToStream(Stream);
  182.   frWriteString(Stream, FDatabase.ConnectionName);
  183.   frWriteString(Stream, FDatabase.DriverName);
  184.   frWriteBoolean(Stream, FDatabase.LoginPrompt);
  185.   frWriteMemo(Stream, FDatabase.Params);
  186.   frWriteBoolean(Stream, FDatabase.Connected);
  187. end;
  188.  
  189. procedure TfrDBXDatabase.LinesEditor(Sender: TObject);
  190. var
  191.   SaveConnected: Boolean;
  192. begin
  193.   with TfrLinesEditorForm.Create(nil) do
  194.   begin
  195.     M1.Text := FDatabase.Params.Text;
  196.     if (ShowModal = mrOk) and ((Restrictions and frrfDontModify) = 0) and
  197.       M1.Modified then
  198.     begin
  199.       SaveConnected := FDatabase.Connected;
  200.       FDatabase.Connected := False;
  201.       FDatabase.Params.Text := M1.Text;
  202.       FDatabase.Connected := SaveConnected;
  203.       frDesigner.Modified := True;
  204.     end;
  205.     Free;
  206.   end;
  207. end;
  208.  
  209.  
  210. var
  211.   Bmp: TBitmap;
  212.  
  213. initialization
  214.   Bmp := TBitmap.Create;
  215.   Bmp.LoadFromResourceName(hInstance, 'FR_DBXDBCONTROL');
  216.   frRegisterControl(TfrDBXDatabase, Bmp, (SInsertDB));
  217.  
  218. finalization
  219.   frUnRegisterObject(TfrDBXDatabase);
  220.   Bmp.Free;
  221.  
  222. end.
  223.  
  224.