unit uSearch;

interface

uses
  System.SysUtils, System.Classes,Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
  FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, Data.DB,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client;

type
  TSearch = class(TDataModule)
  private
    { Private declarations }

    FIDSearchResult:TStringList;
    FCurrentSelectedID:integer;
    FCurrentIndex:Integer;
    FMaxIndex:integer;



  public
    { Public declarations }

    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;

    procedure EntrySearch(Aname:string);
    procedure ExactSearch(Aname: string);
    procedure CaseSensitiveSearch(Aname: string);


    property IDSearchResult:TStringList read FIDSearchResult write FIDSearchResult;
    property CurrentSelectedID:Integer read FCurrentSelectedID write FCurrentSelectedID;

    property CurrentIndex:Integer read FCurrentIndex write FCurrentIndex;
    property MaxIndex:integer read FMaxIndex write FMaxIndex;

  end;

//var
//  Search: TSearch;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

{ TSearch }

uses uVisualFrame,uSendFileInChunksThread,uUploadFrame;
var VisualFrame_HTTP:TVisualFrame_HTTP;



constructor TSearch.Create(AOwner:TComponent);
begin
  inherited;


      if (AOwner is TVisualFrame_HTTP)  then
      VisualFrame_HTTP:=(AOwner as TVisualFrame_HTTP);

      FIDSearchResult:=TStringList.Create;;


end;

destructor TSearch.Destroy;
begin
//

  FreeAndNil(FIDSearchResult);

  inherited;
end;

procedure TSearch.EntrySearch(Aname: string);

var qEntrySearch:TFDQuery;

begin

//qEntrySearch:=TFDQuery.Create(Self);

FIDSearchResult.Clear;

       with VisualFrame_HTTP.DataBaseUnit.qEntrySearch do begin

       //qSearch.SQL.Text:=FSQLQueries.Search.Text+'''%'+name+'%''';

       Connection:=VisualFrame_HTTP.FDConnection;


       case VisualFrame_HTTP.cbAllFilesOrByDate.ItemIndex of

         0:// all files
         begin

          SQL.Text:=
         'SELECT * FROM coffeetest_db.foldersandfilesonserver'+
         ' where name like'+ '''%'+Aname+'%'''+
         //' and idParent='+VisualFrame_HTTP.idCurrentParent.ToString+//+VisualFrame_HTTP.DataBaseUnit.IDCurrentFolder.ToString+
         ' and Users_id='+VisualFrame_HTTP.HTTP_ConnectionParams.IDUser;


         end;

         1: //by date
         begin

           SQL.Text:=
         'SELECT * FROM coffeetest_db.foldersandfilesonserver'+
         ' where name like'+ '''%'+Aname+'%'''+
         ' and idParent='+VisualFrame_HTTP.idCurrentParent.ToString+//+VisualFrame_HTTP.DataBaseUnit.IDCurrentFolder.ToString+
         ' and Users_id='+VisualFrame_HTTP.HTTP_ConnectionParams.IDUser;

         end;

       end;



       //Params.ParamValues['name']:=Aname;
       //Params.ParamValues['idParent']:=VisualFrame_HTTP.DataBaseUnit.IDCurrentFolder.ToString;
       //Params.ParamValues['Users_id']:=VisualFrame_HTTP.HTTP_ConnectionParams.IDUser;

       Open();



       if IsEmpty then

            begin
            showmessage('Нет результатов поиска');
            Exit;
            end;




           //Filling SearchResult List;


             while not EOF do begin

             FIDSearchResult.Add( FieldByName('id').AsInteger.ToString  );

             Next();
             end;




       Close();


       end;



       // Setting to the first position of Search

       with VisualFrame_HTTP do begin

       if FIDSearchResult.Count>0 then

        FDTable.Locate('id',FIDSearchResult[0],[]);

        FCurrentSelectedID:=FIDSearchResult[0].ToInteger();
        FCurrentIndex:=0;
        FMaxIndex:=FIDSearchResult.Count-1;

        lSearchResultLabel.Caption:='Результаты 1 / '+FIDSearchResult.Count.ToString;

        //ShowMessage('success');

       end;




//
//FreeAndNil(qEntrySearch);

end;




procedure TSearch.ExactSearch(Aname: string);



begin


FIDSearchResult.Clear;

       with VisualFrame_HTTP.DataBaseUnit.qExactSearch do begin


        case VisualFrame_HTTP.cbAllFilesOrByDate.ItemIndex of

         0: begin

         SQL.Text:=
         'SELECT * FROM coffeetest_db.foldersandfilesonserver '
          +' where name=:name '
//          +'and idParent=:idParent'
          +' and Users_id=:Users_id;';

         Params.ParamValues['name']:=Aname;
//         Params.ParamValues['idParent']:=VisualFrame_HTTP.DataBaseUnit.IDCurrentFolder.ToString;
         Params.ParamValues['Users_id']:=VisualFrame_HTTP.HTTP_ConnectionParams.IDUser;



         end;

         1: begin

         SQL.Text:=
         'SELECT * FROM coffeetest_db.foldersandfilesonserver '
          +' where name=:name'
          +' and idParent=:idParent'
          +' and Users_id=:Users_id;';

         Params.ParamValues['name']:=Aname;
         Params.ParamValues['idParent']:=VisualFrame_HTTP.DataBaseUnit.IDCurrentFolder.ToString;
         Params.ParamValues['Users_id']:=VisualFrame_HTTP.HTTP_ConnectionParams.IDUser;


         end;

        end;




       Open();



       if IsEmpty then

            begin
            showmessage('Нет результатов поиска');
            Exit;
            end;




           //Filling SearchResult List;


             while not EOF do begin

             FIDSearchResult.Add( FieldByName('id').AsInteger.ToString  );

             Next();
             end;




       Close();


       end;



       // Setting to the first position of Search

       with VisualFrame_HTTP do begin

       if FIDSearchResult.Count>0 then

        FDTable.Locate('id',FIDSearchResult[0],[]);

        FCurrentSelectedID:=FIDSearchResult[0].ToInteger();
        FCurrentIndex:=0;
        FMaxIndex:=FIDSearchResult.Count-1;

        lSearchResultLabel.Caption:='Результаты 1 / '+FIDSearchResult.Count.ToString;

        //ShowMessage('success');

       end;



end;


procedure TSearch.CaseSensitiveSearch(Aname: string);



begin


FIDSearchResult.Clear;

       with VisualFrame_HTTP.DataBaseUnit.qCaseSensitiveSearch do begin



       case VisualFrame_HTTP.cbAllFilesOrByDate.ItemIndex of

         0: begin

           SQL.Text:='SELECT * FROM coffeetest_db.foldersandfilesonserver'
        +' where BINARY name=:name'
//        +' and idParent=:idParent'
        +' and Users_id=:Users_id;';

       Params.ParamValues['name']:=Aname;
//       Params.ParamValues['idParent']:=VisualFrame_HTTP.DataBaseUnit.IDCurrentFolder.ToString;
       Params.ParamValues['Users_id']:=VisualFrame_HTTP.HTTP_ConnectionParams.IDUser;


         end;

         1: begin

           SQL.Text:='SELECT * FROM coffeetest_db.foldersandfilesonserver'
        +' where BINARY name=:name'
        +' and idParent=:idParent'
        +' and Users_id=:Users_id;';

       Params.ParamValues['name']:=Aname;
       Params.ParamValues['idParent']:=VisualFrame_HTTP.DataBaseUnit.IDCurrentFolder.ToString;
       Params.ParamValues['Users_id']:=VisualFrame_HTTP.HTTP_ConnectionParams.IDUser;


         end;

        end;





       Open();



       if IsEmpty then

            begin
            showmessage('Нет результатов поиска');
            Exit;
            end;




           //Filling SearchResult List;


             while not EOF do begin

             FIDSearchResult.Add( FieldByName('id').AsInteger.ToString  );

             Next();
             end;




       Close();


       end;



       // Setting to the first position of Search

       with VisualFrame_HTTP do begin

       if FIDSearchResult.Count>0 then

        FDTable.Locate('id',FIDSearchResult[0],[]);

        FCurrentSelectedID:=FIDSearchResult[0].ToInteger();
        FCurrentIndex:=0;
        FMaxIndex:=FIDSearchResult.Count-1;

        lSearchResultLabel.Caption:='Результаты 1 / '+FIDSearchResult.Count.ToString;

        //ShowMessage('success');

       end;


end;






end.
