home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kolekce / d3456 / ALEXSOFT.ZIP / DBSEARCH.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-29  |  8.3 KB  |  273 lines

  1. (*////////////////////////////////////////////////////////////////////////////
  2. //   Part of AlexSoft VCL/DLL Library.                                      //
  3. //   All rights reserved. (c) Copyright 1998.                               //
  4. //   Created by: Alex Rabichooc                                             //
  5. //**************************************************************************//
  6. //  Users of this unit must accept this disclaimer of warranty:             //
  7. //    "This unit is supplied as is. The author disclaims all warranties,    //
  8. //    expressed or implied, including, without limitation, the warranties   //
  9. //    of merchantability and of fitness for any purpose.                    //
  10. //    The author assumes no liability for damages, direct or                //
  11. //    consequential, which may result from the use of this unit."           //
  12. //                                                                          //
  13. //  This Unit is donated to the public as public domain.                    //
  14. //                                                                          //
  15. //  This Unit can be freely used and distributed in commercial and          //
  16. //  private environments provided this notice is not modified in any way.   //
  17. //                                                                          //
  18. //  If you do find this Unit handy and you feel guilty for using such a     //
  19. //  great product without paying someone - sorry :-)                        //
  20. //                                                                          //
  21. //  Please forward any comments or suggestions to Alex Rabichooc at:        //
  22. //                                                                          //
  23. //  a_rabichooc@yahoo.com or alex@carmez.mldnet.com                         //
  24. /////////////////////////////////////////////////////////////////////////////*)
  25.  
  26. unit DBSearch;
  27.  
  28. interface
  29.  
  30. uses
  31.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  32.   Db,
  33.  {$IFDEF VER140}
  34.   DesignEditors, Designintf
  35.  {$ELSE}
  36.   Dsgnintf
  37.  {$ENDIF};
  38.  
  39. type
  40.   TSearchKind = (skNormal, skExtended, skContext);
  41.  
  42.   TFields = class;
  43.  
  44.   TFieldItem = class(TCollectionItem)
  45.   private
  46.     FFieldName: String;
  47.     function GetField: TField;
  48.   public
  49.     property Field: TField read GetField;
  50.   published
  51.     property FieldName: String read FFieldName write FFieldName;
  52.   end;
  53.  
  54.   TFieldItemClass = class of TFieldItem;
  55.   TRaDBSearch = class;
  56.  
  57.   TFields = class(TCollection)
  58.   private
  59.     FDBSearch: TRaDBSearch;
  60.     function GetField(Index: Integer): TFieldItem;
  61.     procedure SetField(Index: Integer; Value: TFieldItem);
  62.   protected
  63.     function GetOwner: TPersistent; override;
  64.   public
  65.     constructor Create(ADBSearch: TRaDBSearch; AClass: TFieldItemClass);
  66.     function Add: TFieldItem;
  67.     property SearchComp: TRaDBSearch read FDBSearch;
  68.     property Fields[Index: Integer]: TFieldItem read GetField
  69.                                                 write SetField; default;
  70.   end;
  71.  
  72.   TRaDBSearch = class(TComponent)
  73.   private
  74.     FFields: TFields;
  75.     FDataSet: TDataSet;
  76.     FSearchKind: TSearchKind;
  77.     function GetDataSet: TDataSet;
  78.     procedure SetDataSet(Value: TDataSet);
  79.     procedure SetSearchKind(const Value: TSearchKind);
  80.   protected
  81.     procedure Notification(AComponent: TComponent;
  82.                                           Operation: TOperation); override;
  83.     procedure DefineProperties(Filer: TFiler); override;
  84.     procedure ReadFields(Reader: TReader);
  85.     procedure WriteFields(Writer: TWriter);
  86.   public
  87.     constructor Create(AOwner: TComponent); override;
  88.     destructor Destroy; override;
  89.     function Execute(DefaultField: TField): Boolean;
  90.     property Fields: TFields read FFields;
  91.   published
  92.     property DataSet: TDataSet read GetDataSet write SetDataSet;
  93.     property SearchKind: TSearchKind read FSearchKind write SetSearchKind;
  94.   end;
  95.  
  96.   TFieldsEditor = class(TComponentEditor)
  97.   public
  98.     function GetVerbCount: Integer; override;
  99.     function GetVerb(Index: Integer): String; override;
  100.     procedure ExecuteVerb(Index: Integer); override;
  101.   end;
  102.  
  103. implementation
  104.  
  105. uses fmFields, DBTools;
  106.  
  107. {TFieldItem}
  108. function TFieldItem.GetField: TField;
  109. var ADataSet: TDataSet;
  110. begin
  111.   if Collection is TFields then
  112.      ADataSet := (Collection as TFields).FDBSearch.DataSet
  113.     else
  114.      ADataSet := nil;
  115.   if (ADataSet <> nil) then
  116.      Result := ADataSet.FindField(FFieldName)
  117.     else
  118.      Result := nil;
  119. end;
  120.  
  121. {TFields}
  122. constructor TFields.Create(ADBSearch: TRaDBSearch; AClass: TFieldItemClass);
  123. begin
  124.   inherited Create(AClass);
  125.   FDBSearch := ADBSearch;
  126. end;
  127.  
  128. function TFields.GetOwner: TPersistent;
  129. begin
  130.   Result := FDBSearch;
  131. end;
  132.  
  133. function TFields.Add: TFieldItem;
  134. begin
  135.   Result := TFieldItem(inherited Add);
  136. end;
  137.  
  138. function TFields.GetField(Index: Integer): TFieldItem;
  139. begin
  140.   Result := TFieldItem(inherited Items[Index]);
  141. end;
  142.  
  143. procedure TFields.SetField(Index: Integer; Value: TFieldItem);
  144. begin
  145.    TFieldItem(inherited Items[Index]).Assign(Value);
  146. end;
  147.  
  148. {TRaDBSearch}
  149. constructor TRaDBSearch.Create(AOwner: TComponent);
  150. begin
  151.    Inherited;
  152.    FFields := TFields.Create(Self, TFieldItem);
  153. end;
  154.  
  155. destructor TRaDBSearch.Destroy;
  156. begin
  157.    if FFields <> nil then
  158.      FFields.Free;
  159.    Inherited;
  160. end;
  161.  
  162. procedure TRaDBSearch.Notification(AComponent: TComponent;
  163.                                              Operation: TOperation);
  164. begin
  165.   inherited Notification(AComponent, Operation);
  166.   if (Operation = opRemove) and (FDataSet <> nil) and
  167.     (AComponent = FDataSet) then
  168.       FDataSet := nil;
  169. end;
  170.  
  171. procedure TRaDBSearch.DefineProperties(Filer: TFiler);
  172. begin
  173.    Inherited;
  174.    Filer.DefineProperty('Fields', ReadFields, WriteFields, True);
  175. end;
  176.  
  177. procedure TRaDBSearch.ReadFields(Reader: TReader);
  178. begin
  179.   FFields.Clear;
  180.   Reader.ReadValue;
  181.   Reader.ReadCollection(FFields);
  182. end;
  183.  
  184. procedure TRaDBSearch.WriteFields(Writer: TWriter);
  185. begin
  186.   Writer.WriteCollection(FFields);
  187. end;
  188.  
  189. function TRaDBSearch.GetDataSet: TDataSet;
  190. begin
  191.    Result := FDataSet;
  192. end;
  193.  
  194. procedure TRaDBSearch.SetDataSet(Value: TDataSet);
  195. begin
  196.    if Value <> FDataSet then
  197.      FDataSet := Value;
  198. end;
  199.  
  200. function TRaDBSearch.Execute(DefaultField: TField): Boolean;
  201. var AList: TList;
  202.     i: Integer;
  203. begin
  204.    AList := TList.Create;
  205.    try
  206.       for i := 0 to FFields.Count-1 do
  207.         if FFields[i].Field <> nil then
  208.            AList.Add(FFields[i].Field);
  209.       Result := DBTools.DBSearch(FDataSet, AList, DefaultField, Owner, SearchKind);
  210.    finally
  211.       AList.Free;
  212.    end;
  213. end;
  214.  
  215. procedure TRaDBSearch.SetSearchKind(const Value: TSearchKind);
  216. begin
  217.   FSearchKind := Value;
  218. end;
  219.  
  220. {TFieldsEditor}
  221. function TFieldsEditor.GetVerbCount: Integer;
  222. begin
  223.    Result := 1;
  224. end;
  225.  
  226. function TFieldsEditor.GetVerb(Index: Integer): String;
  227. begin
  228.    case Index of
  229.      0: Result := '&Define fields';
  230.    end;
  231. end;
  232.  
  233. procedure TFieldsEditor.ExecuteVerb(Index: Integer);
  234. var Dialog: TfmGetFields;
  235.     SearchComponent: TRaDBSearch;
  236.     i: Integer;
  237.     AList: TList;
  238. begin
  239.    case Index of
  240.       0:
  241.         begin
  242.            SearchComponent := Component as TRaDBSearch;
  243.            AList := TList.Create;
  244.            try
  245.              for i := 0 to SearchComponent.FFields.Count-1 do
  246.                if SearchComponent.FFields[i].Field <> nil then
  247.                  AList.Add(SearchComponent.FFields[i].Field);
  248.                Dialog := TFmGetFields.CreateWithDataSet(Application,
  249.                                                     SearchComponent.FDataSet,
  250.                                                     AList);
  251.              Dialog.Caption := Format('%s.%sFields', [Component.Owner.Name,
  252.                                                     Component.Name]);
  253.              try
  254.                if Dialog.ShowModal = mrOk then
  255.                with Dialog.lbSelFields do
  256.                begin
  257.                   SearchComponent.FFields.Clear;
  258.                   for i := 0 to Items.Count-1 do
  259.                      SearchComponent.FFields.Add.FieldName := Items[i];
  260.                   Designer.Modified;
  261.                end;
  262.              finally
  263.                Dialog.Free;
  264.              end;
  265.            finally
  266.              AList.Free;
  267.            end;
  268.         end;
  269.    end;
  270. end;
  271.  
  272. end.
  273.