unit uDBConnectionBPL;

//Здесь 3 класса,
//1) класс параметров соединения, - TDBConnectionParams
//Он нужен для показа параметров соединения в инспекторе
//2)Класс таблицы TDBTable
//3) класс непосредственно соединения TDBConnectionBPL

//Функциональность класса параметров TDBConnectionParams соединения вызывается как поле
//в классе самого соединения TDBConnectionBPL

interface

uses
  System.SysUtils, System.Classes,vcl.dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
  FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQLDef, FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
  FireDAC.Phys.MySQL, Data.DB, FireDAC.Comp.Client;


//------------------------Класс параметров соединения TDBConnectionParams--------------------------

type TDBConnectionParams=class(TComponent)

private

 FMySQLDriverDir32:string;
 FDataBase:string;
 FUser_Name:string;
 FPassword:string;
 FServer:string;
 FPort:string;
 FCharacterSet:string;



 //Геттеры

 function GetMySQLDriverDir32:string;
 function GetDataBase:string;
 function GetUser_Name:string;
 function GetPassword:string;
 function GetServer:string;
 function GetPort:string;
 function GetCharacterSet:string;

 //Сеттеры

 procedure SetMySQLDriverDir32(Value:string);
 procedure SetDataBase(Value:string);
 procedure SetUser_Name(Value:string);
 procedure SetPassword(Value:string);
 procedure SetServer(Value:string);
 procedure SetPort(Value:string);
 procedure SetCharacterSet(Value:string);


 public

 Procedure Assign(Source:TPersistent);

 Constructor Create(AOwner:TComponent); override;

 published

//Свойства

 property MySQLDriverDir32:string read GetMySQLDriverDir32 write SetMySQLDriverDir32;
 property DataBase:string read GetDataBase write SetDataBase;
 property User_Name:string read GetUser_Name write SetUser_Name;
 property Password:string read GetPassword write SetPassword;
 property Server:string read GetServer write SetServer;
 property Port:string read GetPort write SetPort;
 property CharacterSet:string read GetCharacterSet write SetCharacterSet;

end;

//---------------------------Класс таблицы для DBTreeView и 3 важных полей для DBTreeView

type TDBTable=class(TComponent)


private

Fid:string;
FidParent:string;
Fname:string;
FIndex:string;
FIsChecked:string;
FIsExpanded:string; //<<
FLevel:string;     //<<
FIsFolder:string;

FTableName:string;





//Геттеры
function GetTable:string;

function GetId:string;
function GetIdParent:string;
function GetName:string;
function GetIsFolder:string;
function GetIsChecked:string;
function GetIndex:string;
function GetExpanded:string;
function GetLevel:string;

//Сеттеры
procedure SetTable(Value:string);
procedure SetId(Value:string);
procedure SetIdParent(Value:string);
procedure SetName(Value:string);
procedure SetIsFolder(Value:string);
procedure SetIsChecked(Value:string);
procedure SetIndex(Value:string);
procedure SetExpanded(Value:string);
procedure SetLevel(Value:string);


public

constructor Create(AOwner:TComponent); override;
procedure Assign(Source:TPersistent);

published

property TableName:string read GetTable write SetTable;

property id:string read GetId write SetId;
property idParent:string read GetIdParent write SetIdParent;
property NodeName:string read GetName write SetName;
property IsFolder:string read GetIsFolder write SetIsFolder;
Property IsChecked:string read GetIsChecked write SetIsChecked;
Property IndexField:string read GetIndex write SetIndex;
Property IsExpanded:string read GetExpanded write SetExpanded;
Property Level:string read GetLevel write SetLevel;

end;


//-----------------------Класс соединения TDBConnectionBPL-----------------------------------

type

    TDBConnectionBPL = class(TDataModule)
    FDConnection: TFDConnection;
    FDPhysMySQLDriverLink: TFDPhysMySQLDriverLink;
    FDGUIxWaitCursor: TFDGUIxWaitCursor;

    procedure DataModuleCreate(Sender: TObject);

  private

    FDBConnectionParams:TDBConnectionParams;
    FDBTable:TDBTable;



  public

  property DBConnectionParams:TDBConnectionParams
  read FDBConnectionParams write FDBConnectionParams;

  property DBTable:TDBTable read FDBTable write FDBTable;

  procedure Connect;

  end;


implementation

uses UTreeView;

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}


{ TDBConnectionParams }

constructor TDBConnectionParams.Create;
begin
inherited;

//-----------------------------Убрать эту часть когда будет готов компонент------------------------
//Заполняет поля в ObjectInspector


Self.Name:='ConnParams';


Self.SetDataBase('treeview_db');
//Self.SetDataBase(FDataBase);
Self.SetPassword('masterkey');
SetUser_Name('root');
SetServer('localhost');
SetPort('3306');
SetCharacterSet('utf8');




end;

//-------------------------ГЕТТЕРЫ, РЕАЛИЗАЦИЯ---------------------------


function TDBConnectionParams.GetCharacterSet: string;
begin
Result:=FCharacterSet;
end;

function TDBConnectionParams.GetDataBase: string;
begin
Result:=FDataBase;
end;

function TDBConnectionParams.GetMySQLDriverDir32: string;
begin
Result:=FMySQLDriverDir32;
end;

function TDBConnectionParams.GetPassword: string;
begin
Result:=FPassword;
end;

function TDBConnectionParams.GetPort: string;
begin
Result:=FPort;
end;

function TDBConnectionParams.GetServer: string;
begin
Result:=FServer;
end;

function TDBConnectionParams.GetUser_Name: string;
begin
Result:=FUser_Name;
end;

//------------------------СЕТТЕРЫ, РЕАЛИЗАЦИЯ-----------------

procedure TDBConnectionParams.SetCharacterSet(Value: string);
begin
FCharacterSet:=Value;
end;

procedure TDBConnectionParams.SetDataBase(Value: string);
begin
FDataBase:=Value;
end;

procedure TDBConnectionParams.SetMySQLDriverDir32(Value: string);
begin
FMySQLDriverDir32:=trim(Value);
end;

procedure TDBConnectionParams.SetPassword(Value: string);
begin
FPassword:=trim(Value);
end;

procedure TDBConnectionParams.SetPort(Value: string);
begin
FPort:=trim(Value);
end;

procedure TDBConnectionParams.SetServer(Value: string);
begin
FServer:=trim(Value);
end;

procedure TDBConnectionParams.SetUser_Name(Value: string);
begin
FUser_Name:=trim(Value);
end;


//---------------------Реализация Assign-------------------------

procedure TDBConnectionParams.Assign(Source: TPersistent);
begin

if Source is TDBConnectionParams then

  with Source As TDBConnectionParams

       do begin

       Self.MySQLDriverDir32:=MySQLDriverDir32;
       Self.DataBase:=DataBase;
       Self.User_Name:=User_Name;
       Self.Password:=Password;
       Self.Server:=Server;
       Self.Port:=Port;
       Self.CharacterSet:=CharacterSet;

       end;

  end;

                                     { TDBConnectionBPL }

procedure TDBConnectionBPL.Connect;
var oParams:Tstrings; s:string;
begin

if not Assigned(DBTreeViewGlobal) then exit;

//if FDBConnectionParams.FMySQLDriverDir32<>'' then
//FDPhysMySQLDriverLink.VendorLib:=FDBConnectionParams.FMySQLDriverDir32;
//showmessage(FDPhysMySQLDriverLink.VendorLib);


      oParams := TStringList.Create;
      oParams.Add('DataBase='+FDBConnectionParams.DataBase);
      oParams.Add('Password='+FDBConnectionParams.Password);
      oParams.Add('User_Name='+FDBConnectionParams.User_Name);
      oParams.Add('Port='+FDBConnectionParams.Port);
      oParams.Add('Server='+FDBConnectionParams.Server);
      oParams.Add('CharacterSet='+FDBConnectionParams.CharacterSet);


      FDConnection.Params.Assign(oParams);
      FDConnection.DriverName:='MySQL';

       try
        FDConnection.Connected:=true;
       except

       end;

       oParams.Free;
end;


procedure TDBConnectionBPL.DataModuleCreate(Sender: TObject);
begin

// Передача параметров соединения и таблицы из запущенной программы

FDBConnectionParams:=TDBConnectionParams.Create(Self);
Self.FDBConnectionParams.Assign(DBTreeViewGlobal.DBTreeViewConnParams); // <<<<<
FDBTable:=TDBTable.Create(Self);
Self.FDBTable.Assign(DBTreeViewGlobal.DBTreeViewTable); // <<<<<

if Assigned(DBTreeViewGlobal) then
Self.Connect;


end;

//-------------------------{ TDBTables }

procedure TDBTable.Assign(Source: TPersistent);
begin

if Source is TDBTable then

with Source as TDBTable
 do begin

   Self.TableName:=TableName;
   Self.id:=id;
   Self.idParent:=idParent;
   Self.NodeName:=NodeName;
   Self.IsFolder:=IsFolder;
   Self.IsChecked:=IsChecked;

 end;

end;

constructor TDBTable.Create;
begin
inherited;


//---------------Временные значения для тестирования компонента
Self.Name:='TableSettings';

Self.SetTable('tree');

Self.SetId('id');

Self.SetIdParent('idParent');

Self.SetIndex('Index');

Self.SetExpanded('IsExpanded');

Self.SetLevel('Level');

Self.SetName('Name');
Self.SetIsFolder('IsFolder');
Self.SetIsChecked('IsChecked');



end;

//---------------Геттеры--------------------

function TDBTable.GetExpanded: string;
begin
Result:=FIsExpanded;
end;

function TDBTable.GetId: string;
begin
Result:=Fid;
end;

function TDBTable.GetIdParent: string;
begin
Result:=FIdParent;
end;

function TDBTable.GetIndex: string;
begin
Result:=FIndex;
end;

function TDBTable.GetIsChecked: string;
begin
Result:=FIsChecked;
end;


function TDBTable.GetIsFolder: string;
begin
Result:=FIsfolder;
end;

function TDBTable.GetLevel: string;
begin
Result:=FLevel;
end;

function TDBTable.GetName: string;
begin
Result:=FName;
end;

function TDBTable.GetTable: string;
begin
Result:=FTableName;
end;

//-------------------Сеттеры---------------------------

procedure TDBTable.SetId(Value: string);
begin
Fid:=trim(Value);
end;

procedure TDBTable.SetIdParent(Value: string);
begin
FIdParent:=trim(Value);
end;

procedure TDBTable.SetIndex(Value: string);
begin
FIndex:=trim(Value);
end;

procedure TDBTable.SetIsChecked(Value: string);
begin
FIsChecked:=trim(Value);
end;

procedure TDBTable.SetExpanded(Value: string);
begin
FIsExpanded:=trim(Value);
end;

procedure TDBTable.SetIsFolder(Value: string);
begin
FIsFolder:=trim(Value);
end;

procedure TDBTable.SetLevel(Value: string);
begin
FLevel:=trim(Value);
end;

procedure TDBTable.SetName(Value: string);
begin
FName:=trim(Value);
end;


procedure TDBTable.SetTable(Value: string);
begin
FTableName:=trim(Value);
end;
//-------------------------------------------------

end.
