unit uDataBaseUnit;

interface

uses
  SysUtils, Classes, 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.VCLUI.Wait, Data.DB,
  FireDAC.Comp.Client, FireDAC.Phys.MySQLDef, FireDAC.Phys.MySQL, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
  FireDAC.DApt, FireDAC.Comp.DataSet;

type
  TDataBaseUnit = class(TDataModule)
    FDConnection: TFDConnection;
    FDPhysMySQLDriverLink: TFDPhysMySQLDriverLink;
    qAddSomeNewProduct: TFDQuery;
    qGetMaxID: TFDQuery;
    qUpdateImageLink: TFDQuery;
    qUpdateImageThumbnailLink: TFDQuery;
    qUpdateName: TFDQuery;
    qUpdateCountry: TFDQuery;
    qUpdatePrice_begin: TFDQuery;
    qSelectAll: TFDQuery;
    qDeleteProduct: TFDQuery;
  private
    { Private declarations }
  public
    { Public declarations }

    constructor Create(AOwner: TComponent); override;

    function Connect:boolean;

  end;

function DataBaseUnit: TDataBaseUnit;

implementation

{$R *.dfm}

uses
  UniGUIVars, uniGUIMainModule, MainModule;

function DataBaseUnit: TDataBaseUnit;
begin
  Result := TDataBaseUnit(UniMainModule.GetModuleInstance(TDataBaseUnit));
end;

{ TDataBaseUnit }

function TDataBaseUnit.Connect:Boolean;
var oParams:TStrings;  ErrorInfo:string;

begin

  //Драйвер << Можно отдельно прописать, если он не в папке с EXE
//  FDPhysMySQLDriverLink1.VendorLib:='C:\MySQLDriver\libmysql.dll';
// либо положить его в c:\windows\SysWow64\

 oParams:=TStringList.Create;


 try

  oParams.Add('DataBase=showcase_db');
  oParams.Add('Password=masterkey');
  oParams.Add('User_Name=root');
  oParams.Add('Port=3306');
  oParams.Add('Server=localhost');
  oParams.Add('CharacterSet=utf8');

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



   //Пробуем подключиться

   try

   FDConnection.Connected:=true;

   if FDConnection.Connected then

     begin
     Result:=true;

//     showmessage('Connected');
     end else Result:=false;



   except



   on E: EFDDBEngineException do

    case E.Kind of
    ekUserPwdInvalid:


       // user name or password are incorrect
       raise Exception.Create('DBConnection Error. User name or password are incorrect'+
       #13#10+#13#10+E.ClassName+' поднята ошибка, с сообщением : '+E.Message);



    ekUserPwdExpired:


      raise Exception.Create('DBConnection Error. User password is expired'
    +#13#10+#13#10+E.ClassName+' поднята ошибка, с сообщением : '+E.Message);



    ekServerGone:



      raise Exception.Create('DBConnection Error. DBMS is not accessible due to some reason'
      +#13#10+#13#10+E.ClassName+' поднята ошибка, с сообщением : '+E.Message);




    else                // other issues



      raise Exception.Create('DBConnection Error. UnknownMistake'
      +#13#10+#13#10+E.ClassName+' поднята ошибка, с сообщением : '+E.Message);





    end;

    on E : Exception do

      raise Exception.Create(
      E.ClassName+' поднята ошибка, с сообщением : '+#13#10+#13#10+E.Message
      );





   end;




 finally

  FreeAndNil(oParams);

 end;


end;

constructor TDataBaseUnit.Create(AOwner: TComponent);
var
  i: Integer;
begin
  inherited;

Connect;


for i := 0 to Self.ComponentCount-1 do
if (Self.Components[i] is TFDQuery) then
(Self.Components[i] as TFDQuery).Connection:=FDConnection;


end;

initialization
  RegisterModuleClass(TDataBaseUnit);

end.
