|
| 1 | +// Maiores Informações |
| 2 | +// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki |
| 3 | + |
| 4 | +unit DAC.MySQL.FireDAC; |
| 5 | + |
| 6 | +interface |
| 7 | + |
| 8 | +uses |
| 9 | + JSON, SysUtils, |
| 10 | + FireDAC.Comp.Client, FireDAC.Phys.MySQL; |
| 11 | + |
| 12 | +type |
| 13 | + TDAC = class |
| 14 | + private |
| 15 | + FDriver: TFDPhysMySQLDriverLink; |
| 16 | + FConnection: TFDConnection; |
| 17 | + FQuery: TFDQuery; |
| 18 | + function GetDefaultLibDir: string; |
| 19 | + public |
| 20 | + constructor Create(aJSON: TJSONObject); |
| 21 | + destructor Destroy; override; |
| 22 | + function getConnection: TFDConnection; |
| 23 | + function getQuery: TFDQuery; |
| 24 | + end; |
| 25 | + |
| 26 | +implementation |
| 27 | + |
| 28 | +constructor TDAC.Create(aJSON: TJSONObject); |
| 29 | +begin |
| 30 | + FDriver := TFDPhysMySQLDriverLink.Create(nil); |
| 31 | + FDriver.DriverID := 'MySQL'; |
| 32 | + FDriver.VendorLib := GetDefaultLibDir; |
| 33 | + |
| 34 | + FConnection := TFDConnection.Create(nil); |
| 35 | + try |
| 36 | + with FConnection do |
| 37 | + begin |
| 38 | + LoginPrompt := false; |
| 39 | + Params.Add('DriverID=FB'); |
| 40 | + Params.Add('Server=' + aJSON.GetValue('dbserver').Value); |
| 41 | + Params.Add('User_Name=' + aJSON.GetValue('dbuser').Value); |
| 42 | + Params.Add('Password=' + aJSON.GetValue('dbpassword').Value); |
| 43 | + Params.Add('Port=' + aJSON.GetValue('dbport').Value); |
| 44 | + if aJSON.GetValue('banco') <> nil then |
| 45 | + Params.Add('Database=' + aJSON.GetValue('banco').Value); |
| 46 | + |
| 47 | + FQuery := TFDQuery.Create(nil); |
| 48 | + FQuery.Connection := FConnection; |
| 49 | + FQuery.ResourceOptions.SilentMode := true; |
| 50 | + end; |
| 51 | + except |
| 52 | + // log |
| 53 | + end; |
| 54 | +end; |
| 55 | + |
| 56 | +destructor TDAC.Destroy; |
| 57 | +begin |
| 58 | + if FDriver <> nil then |
| 59 | + FDriver.Free; |
| 60 | + if FQuery <> nil then |
| 61 | + FQuery.Free; |
| 62 | + if FConnection <> nil then |
| 63 | + FConnection.Free; |
| 64 | + inherited; |
| 65 | +end; |
| 66 | + |
| 67 | +function TDAC.getConnection: TFDConnection; |
| 68 | +begin |
| 69 | + Result := FConnection; |
| 70 | +end; |
| 71 | + |
| 72 | +function TDAC.GetDefaultLibDir: string; |
| 73 | +var |
| 74 | + DefaultDir: string; |
| 75 | +begin |
| 76 | + Result := ''; |
| 77 | + DefaultDir := ExtractFileDir(ParamStr(0)); |
| 78 | + // libmysql.dll, libmariadb or libmysqld.dll |
| 79 | + // procurando no diretório do exe primeiro, depois no diretório \lib\ |
| 80 | + |
| 81 | + if FileExists(DefaultDir + 'libmysql.dll') then |
| 82 | + Result := DefaultDir + 'libmysql.dll' |
| 83 | + else if FileExists(DefaultDir + 'libmariadb.dll') then |
| 84 | + Result := DefaultDir + 'libmariadb.dll' |
| 85 | + else if FileExists(DefaultDir + 'libmysqld.dll') then |
| 86 | + Result := DefaultDir + 'libmysqld.dll' |
| 87 | + else if FileExists(DefaultDir + '\lib\libmysql.dll') then |
| 88 | + Result := DefaultDir + '\lib\libmysql.dll' |
| 89 | + else if FileExists(DefaultDir + '\lib\libmariadb.dll') then |
| 90 | + Result := DefaultDir + '\lib\libmariadb.dll' |
| 91 | + else if FileExists(DefaultDir + '\lib\libmysqld.dll') then |
| 92 | + Result := DefaultDir + '\lib\libmysqld.dll' |
| 93 | + else |
| 94 | + raise Exception.Create('libmysql.dll, libmariadb.dll ou libmysqld.dll' + |
| 95 | + ' precisam estar na raiz do executável ou na pasta \lib\'); |
| 96 | +end; |
| 97 | + |
| 98 | +function TDAC.getQuery: TFDQuery; |
| 99 | +begin |
| 100 | + if not Assigned(FQuery) then |
| 101 | + FQuery := TFDQuery.Create(nil); |
| 102 | + Result := FQuery; |
| 103 | +end; |
| 104 | + |
| 105 | +end. |
0 commit comments