|
| 1 | +unit C4D.Conn.Configs; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + System.SysUtils, |
| 7 | + System.Classes, |
| 8 | + IniFiles, |
| 9 | + C4D.Conn.Types; |
| 10 | + |
| 11 | +type |
| 12 | + TC4DConnConfigs = class |
| 13 | + private |
| 14 | + FComponentConnection: TComponentConnection; |
| 15 | + FConnectionSingleton: Boolean; |
| 16 | + FDatabase: string; |
| 17 | + public |
| 18 | + class function New: TC4DConnConfigs; |
| 19 | + constructor Create; |
| 20 | + destructor Destroy; override; |
| 21 | + function ComponentConnection: TComponentConnection; |
| 22 | + function ComponentConnectionZeos: TC4DConnConfigs; |
| 23 | + function ComponentConnectionFireDac: TC4DConnConfigs; |
| 24 | + function ConnectionSingleton: Boolean; |
| 25 | + function ConnectionSingletonON: TC4DConnConfigs; |
| 26 | + function ConnectionSingletonOFF: TC4DConnConfigs; |
| 27 | + function Database: string; overload; |
| 28 | + function Database(Value: string): TC4DConnConfigs; overload; |
| 29 | + procedure CarregaDadosDeArqIni(const AFileNameIni: string); |
| 30 | + end; |
| 31 | + |
| 32 | +implementation |
| 33 | + |
| 34 | +class function TC4DConnConfigs.New: TC4DConnConfigs; |
| 35 | +begin |
| 36 | + Result := Self.Create; |
| 37 | +end; |
| 38 | + |
| 39 | +constructor TC4DConnConfigs.Create; |
| 40 | +begin |
| 41 | + FConnectionSingleton := True; |
| 42 | + FComponentConnection := TComponentConnection.Empty; |
| 43 | +end; |
| 44 | + |
| 45 | +destructor TC4DConnConfigs.Destroy; |
| 46 | +begin |
| 47 | + inherited; |
| 48 | +end; |
| 49 | + |
| 50 | +function TC4DConnConfigs.ComponentConnection: TComponentConnection; |
| 51 | +begin |
| 52 | + if FComponentConnection = TComponentConnection.Empty then |
| 53 | + raise Exception.Create('Database connection component not specified'); |
| 54 | + Result := FComponentConnection; |
| 55 | +end; |
| 56 | + |
| 57 | +function TC4DConnConfigs.ComponentConnectionZeos: TC4DConnConfigs; |
| 58 | +begin |
| 59 | + Result := Self; |
| 60 | + if(FComponentConnection <> TComponentConnection.Empty)then |
| 61 | + raise Exception.Create('Connection component has already been provided'); |
| 62 | + |
| 63 | + FComponentConnection := TComponentConnection.Zeos; |
| 64 | +end; |
| 65 | + |
| 66 | +function TC4DConnConfigs.ComponentConnectionFireDac: TC4DConnConfigs; |
| 67 | +begin |
| 68 | + Result := Self; |
| 69 | + if(FComponentConnection <> TComponentConnection.Empty)then |
| 70 | + raise Exception.Create('Connection component has already been provided'); |
| 71 | + FComponentConnection := TComponentConnection.FireDac; |
| 72 | +end; |
| 73 | + |
| 74 | +function TC4DConnConfigs.ConnectionSingleton: Boolean; |
| 75 | +begin |
| 76 | + Result := FConnectionSingleton; |
| 77 | +end; |
| 78 | + |
| 79 | +function TC4DConnConfigs.ConnectionSingletonON: TC4DConnConfigs; |
| 80 | +begin |
| 81 | + Result := Self; |
| 82 | + FConnectionSingleton := True; |
| 83 | +end; |
| 84 | + |
| 85 | +function TC4DConnConfigs.ConnectionSingletonOFF: TC4DConnConfigs; |
| 86 | +begin |
| 87 | + Result := Self; |
| 88 | + FConnectionSingleton := False; |
| 89 | +end; |
| 90 | + |
| 91 | +function TC4DConnConfigs.Database: string; |
| 92 | +begin |
| 93 | + Result := FDatabase; |
| 94 | +end; |
| 95 | + |
| 96 | +function TC4DConnConfigs.Database(Value: string): TC4DConnConfigs; |
| 97 | +begin |
| 98 | + Result := Self; |
| 99 | + FDatabase := Value; |
| 100 | +end; |
| 101 | + |
| 102 | +procedure TC4DConnConfigs.CarregaDadosDeArqIni(const AFileNameIni: string); |
| 103 | +var |
| 104 | + LIniFile: TIniFile; |
| 105 | +begin |
| 106 | + if AFileNameIni.Trim.IsEmpty then |
| 107 | + raise Exception.Create('Database access configuration file not provided.'); |
| 108 | + |
| 109 | + if not FileExists(AFileNameIni) then |
| 110 | + raise Exception.Create('Configuration file for database access not found.: ' + AFileNameIni); |
| 111 | + |
| 112 | + try |
| 113 | + LIniFile := TIniFile.Create(AFileNameIni); |
| 114 | + try |
| 115 | + FDatabase := LIniFile.ReadString('ConnData01', 'Database', FDatabase); |
| 116 | + finally |
| 117 | + LIniFile.Free; |
| 118 | + end; |
| 119 | + |
| 120 | + if FDatabase.Trim.IsEmpty then |
| 121 | + raise Exception.Create('Data not found in ini file (Database name)'); |
| 122 | + |
| 123 | + if not FileExists(FDatabase.Trim) then |
| 124 | + raise Exception.Create('Arquivo do banco de dados não encontrado'); |
| 125 | + |
| 126 | + except |
| 127 | + on E: Exception do |
| 128 | + raise Exception.Create('An exception occurred while fetching data for the database connection. ' + sLineBreak + |
| 129 | + 'File: ' + AFileNameIni + sLineBreak + |
| 130 | + 'Message: ' + E.Message); |
| 131 | + end; |
| 132 | +end; |
| 133 | + |
| 134 | +end. |
0 commit comments