|
| 1 | +// Maiores Informações |
| 2 | +// https://github.com/OpenSourceCommunityBrasil/PascalLibs/wiki |
| 3 | + |
| 4 | +unit DAC.MySQL.SQLDB; |
| 5 | + |
| 6 | +{$mode ObjFPC}{$H+} |
| 7 | + |
| 8 | +interface |
| 9 | + |
| 10 | +uses |
| 11 | + {$IFDEF Windows}windows,{$ENDIF} |
| 12 | + fpJSON, SysUtils, Classes, |
| 13 | + mysql80conn, SQLDB; |
| 14 | + |
| 15 | +type |
| 16 | + TConnection = TSQLConnector; |
| 17 | + TQuery = TSQLQuery; |
| 18 | + |
| 19 | + { TDAC } |
| 20 | + |
| 21 | + TDAC = class |
| 22 | + private |
| 23 | + FConnection: TConnection; |
| 24 | + FQuery: TQuery; |
| 25 | + FTransaction: TSQLTransaction; |
| 26 | + function GetDefaultLibDir: string; |
| 27 | + procedure SetEnvironmentPath; |
| 28 | + public |
| 29 | + constructor Create(aJSON: TJSONObject); |
| 30 | + destructor Destroy; override; |
| 31 | + function getConnection: TConnection; |
| 32 | + function getQuery: TQuery; |
| 33 | + end; |
| 34 | + |
| 35 | +implementation |
| 36 | + |
| 37 | +constructor TDAC.Create(aJSON: TJSONObject); |
| 38 | +begin |
| 39 | + FConnection := nil; |
| 40 | + FQuery := nil; |
| 41 | + FConnection := TConnection.Create(nil); |
| 42 | + FTransaction := TSQLTransaction.Create(nil); |
| 43 | + try |
| 44 | + SetEnvironmentPath; |
| 45 | + with FConnection do |
| 46 | + begin |
| 47 | + ConnectorType := 'MySQL 8.0'; |
| 48 | + Transaction := FTransaction; |
| 49 | + LoginPrompt := False; |
| 50 | + CharSet := 'utf-8'; |
| 51 | + |
| 52 | + Params.Add('port=%d', [aJSON.Get('dbport', 3306)]); |
| 53 | + HostName := aJSON.Get('dbserver', ''); |
| 54 | + DatabaseName := aJSON.Get('banco', ''); |
| 55 | + UserName := aJSON.Get('dbuser', ''); |
| 56 | + Password := aJSON.Get('dbpassword', ''); |
| 57 | + end; |
| 58 | + FQuery := TQuery.Create(nil); |
| 59 | + FQuery.DataBase := FConnection; |
| 60 | + FConnection.Open; |
| 61 | + except |
| 62 | + // log |
| 63 | + end; |
| 64 | +end; |
| 65 | + |
| 66 | +destructor TDAC.Destroy; |
| 67 | +begin |
| 68 | + if Assigned(FQuery) then |
| 69 | + begin |
| 70 | + FQuery.DataBase := nil; |
| 71 | + FreeAndNil(FQuery); |
| 72 | + end; |
| 73 | + |
| 74 | + if Assigned(FConnection) then FreeAndNil(FConnection); |
| 75 | + if Assigned(FTransaction) then FreeAndNil(FTransaction); |
| 76 | + inherited; |
| 77 | +end; |
| 78 | + |
| 79 | +function TDAC.getConnection: TConnection; |
| 80 | +begin |
| 81 | + Result := FConnection; |
| 82 | +end; |
| 83 | + |
| 84 | +function TDAC.GetDefaultLibDir: string; |
| 85 | +var |
| 86 | + DefaultDir: string; |
| 87 | +begin |
| 88 | + Result := ''; |
| 89 | + DefaultDir := ExtractFileDir(ParamStr(0)); |
| 90 | + // libmysql.dll, libmariadb or libmysqld.dll |
| 91 | + |
| 92 | + if FileExists(DefaultDir + '\lib\libmysql.dll') then |
| 93 | + Result := DefaultDir + '\lib\libmysql.dll' |
| 94 | + else if FileExists(DefaultDir + '\lib\libmariadb.dll') then |
| 95 | + Result := DefaultDir + '\lib\libmariadb.dll' |
| 96 | + else if FileExists(DefaultDir + '\lib\libmysqld.dll') then |
| 97 | + Result := DefaultDir + '\lib\libmysqld.dll' |
| 98 | + else if FileExists(DefaultDir + 'libmysql.dll') then |
| 99 | + Result := DefaultDir + 'libmysql.dll' |
| 100 | + else if FileExists(DefaultDir + 'libmariadb.dll') then |
| 101 | + Result := DefaultDir + 'libmariadb.dll' |
| 102 | + else if FileExists(DefaultDir + 'libmysqld.dll') then |
| 103 | + Result := DefaultDir + 'libmysqld.dll' |
| 104 | + else |
| 105 | + raise Exception.Create('libmysql.dll, libmariadb.dll ou libmysqld.dll' + |
| 106 | + ' precisam estar na raiz do executável ou na pasta \lib\'); |
| 107 | +end; |
| 108 | + |
| 109 | +procedure TDAC.SetEnvironmentPath; |
| 110 | +var |
| 111 | + envpath: TStringList; |
| 112 | + I: integer; |
| 113 | +begin |
| 114 | + envpath := TStringList.Create; |
| 115 | + try |
| 116 | + // get environment path |
| 117 | + envpath.AddDelimitedText(GetEnvironmentVariable('path'), ';', True); |
| 118 | + envpath.Sorted := True; |
| 119 | + |
| 120 | + // detect if the dblib is defined on the path |
| 121 | + if not envpath.Find(GetDefaultLibDir, I) then |
| 122 | + begin |
| 123 | + envpath.Add(ExtractFileDir(GetDefaultLibDir)); |
| 124 | + envpath.Delimiter := ';'; |
| 125 | + |
| 126 | + // save new path |
| 127 | + {$IF Defined(Windows)} |
| 128 | + SetEnvironmentVariable(Pchar('Path'), PChar(envpath.DelimitedText)); |
| 129 | + {$IFEND} |
| 130 | + end; |
| 131 | + finally |
| 132 | + envpath.Free; |
| 133 | + end; |
| 134 | +end; |
| 135 | + |
| 136 | +function TDAC.getQuery: TQuery; |
| 137 | +begin |
| 138 | + if not Assigned(FQuery) then |
| 139 | + FQuery := TFDQuery.Create(nil); |
| 140 | + Result := FQuery; |
| 141 | +end; |
| 142 | + |
| 143 | +end. |
0 commit comments