| 1 | unit ConnectionManager; | 
 
 
 
 
 | 2 | interface | 
 
 
 
 
 | 3 |  | 
 
 
 
 
 | 4 | uses TypeDefs, DataAccess, Access_OniArchive, Access_OUP_ADB; | 
 
 
 
 
 | 5 |  | 
 
 
 
 
 | 6 | type | 
 
 
 
 
 | 7 | TConnections = array of TDataAccess; | 
 
 
 
 
 | 8 |  | 
 
 
 
 
 | 9 | TConnectionListChangedEvent = procedure of object; | 
 
 
 
 
 | 10 |  | 
 
 
 
 
 | 11 |  | 
 
 
 
 
 | 12 | TConnectionManager = class | 
 
 
 
 
 | 13 | private | 
 
 
 
 
 | 14 | FConnections: TConnections; | 
 
 
 
 
 | 15 | FLastID:      Integer; | 
 
 
 
 
 | 16 | FConnectionListChanged: TConnectionListChangedEvent; | 
 
 
 
 
 | 17 | function GetConnectionCount: Integer; | 
 
 
 
 
 | 18 | function GetConnection(ConnectionID: Integer): TDataAccess; | 
 
 
 
 
 | 19 | function GetConnectionByIndex(Index: Integer): TDataAccess; | 
 
 
 
 
 | 20 | procedure RemoveConnection(ArrayIndex: Integer); | 
 
 
 
 
 | 21 | protected | 
 
 
 
 
 | 22 | public | 
 
 
 
 
 | 23 | property Count: Integer read GetConnectionCount; | 
 
 
 
 
 | 24 | property Connection[ConnectionID: Integer]: TDataAccess read GetConnection; | 
 
 
 
 
 | 25 | property ConnectionByIndex[Index: Integer]: TDataAccess read GetConnectionByIndex; | 
 
 
 
 
 | 26 | property OnCoonnectionListChanged: TConnectionListChangedEvent read FConnectionListChanged write FConnectionListChanged; | 
 
 
 
 
 | 27 |  | 
 
 
 
 
 | 28 | constructor Create; | 
 
 
 
 
 | 29 | function Close: Boolean; | 
 
 
 
 
 | 30 |  | 
 
 
 
 
 | 31 | function OpenConnection(FileName: String; var Msg: TStatusMessages): Integer; | 
 
 
 
 
 | 32 | function CloseConnectionByIndex(Index: Integer; var Msg: TStatusMessages): Boolean; overload; | 
 
 
 
 
 | 33 | function CloseConnection(ID: Integer; var Msg: TStatusMessages): Boolean; overload; | 
 
 
 
 
 | 34 | function CloseConnection(FileName: String; var Msg: TStatusMessages): Boolean; overload; | 
 
 
 
 
 | 35 | published | 
 
 
 
 
 | 36 | end; | 
 
 
 
 
 | 37 |  | 
 
 
 
 
 | 38 |  | 
 
 
 
 
 | 39 | implementation | 
 
 
 
 
 | 40 | uses | 
 
 
 
 
 | 41 | SysUtils, Dialogs; | 
 
 
 
 
 | 42 |  | 
 
 
 
 
 | 43 | (* | 
 
 
 
 
 | 44 | Implementation of TConnectionManager | 
 
 
 
 
 | 45 | *) | 
 
 
 
 
 | 46 |  | 
 
 
 
 
 | 47 |  | 
 
 
 
 
 | 48 | function TConnectionManager.GetConnectionCount: Integer; | 
 
 
 
 
 | 49 | begin | 
 
 
 
 
 | 50 | Result := Length(FConnections); | 
 
 
 
 
 | 51 | end; | 
 
 
 
 
 | 52 |  | 
 
 
 
 
 | 53 | function TConnectionManager.GetConnection(ConnectionID: Integer): TDataAccess; | 
 
 
 
 
 | 54 | var | 
 
 
 
 
 | 55 | i: Integer; | 
 
 
 
 
 | 56 | begin | 
 
 
 
 
 | 57 | Result := nil; | 
 
 
 
 
 | 58 | if Length(FConnections) > 0 then | 
 
 
 
 
 | 59 | begin | 
 
 
 
 
 | 60 | for i := 0 to High(FConnections) do | 
 
 
 
 
 | 61 | begin | 
 
 
 
 
 | 62 | if FConnections[i].ConnectionID = ConnectionID then | 
 
 
 
 
 | 63 | begin | 
 
 
 
 
 | 64 | Result := FConnections[i]; | 
 
 
 
 
 | 65 | Break; | 
 
 
 
 
 | 66 | end; | 
 
 
 
 
 | 67 | end; | 
 
 
 
 
 | 68 | if i = Length(FConnections) then | 
 
 
 
 
 | 69 | ShowMessage('Couldn''t find specified ConnectionID (' + | 
 
 
 
 
 | 70 | IntToStr(ConnectionID) + '). Please contact developer!!!'); | 
 
 
 
 
 | 71 | end; | 
 
 
 
 
 | 72 | end; | 
 
 
 
 
 | 73 |  | 
 
 
 
 
 | 74 |  | 
 
 
 
 
 | 75 | function TConnectionManager.GetConnectionByIndex(Index: Integer): TDataAccess; | 
 
 
 
 
 | 76 | begin | 
 
 
 
 
 | 77 | Result := nil; | 
 
 
 
 
 | 78 | if index < Length(FConnections) then | 
 
 
 
 
 | 79 | begin | 
 
 
 
 
 | 80 | Result := FConnections[index]; | 
 
 
 
 
 | 81 | end; | 
 
 
 
 
 | 82 | end; | 
 
 
 
 
 | 83 |  | 
 
 
 
 
 | 84 | constructor TConnectionManager.Create; | 
 
 
 
 
 | 85 | begin | 
 
 
 
 
 | 86 | inherited; | 
 
 
 
 
 | 87 | FLastID := 0; | 
 
 
 
 
 | 88 | end; | 
 
 
 
 
 | 89 |  | 
 
 
 
 
 | 90 | function TConnectionManager.Close: Boolean; | 
 
 
 
 
 | 91 | begin | 
 
 
 
 
 | 92 | Result := False; | 
 
 
 
 
 | 93 | if Length(FConnections) > 0 then | 
 
 
 
 
 | 94 | Exit; | 
 
 
 
 
 | 95 |  | 
 
 
 
 
 | 96 | inherited; | 
 
 
 
 
 | 97 | end; | 
 
 
 
 
 | 98 |  | 
 
 
 
 
 | 99 |  | 
 
 
 
 
 | 100 |  | 
 
 
 
 
 | 101 | function TConnectionManager.OpenConnection(FileName: String; var Msg: TStatusMessages): Integer; | 
 
 
 
 
 | 102 | var | 
 
 
 
 
 | 103 | i: Integer; | 
 
 
 
 
 | 104 | ext: String; | 
 
 
 
 
 | 105 | backend: TDataBackend; | 
 
 
 
 
 | 106 | CreateMsg: TStatusMessages; | 
 
 
 
 
 | 107 | begin | 
 
 
 
 
 | 108 | Msg := SM_UnknownError; | 
 
 
 
 
 | 109 | Result := -1; | 
 
 
 
 
 | 110 |  | 
 
 
 
 
 | 111 | if Length(FConnections) > 0 then | 
 
 
 
 
 | 112 | begin | 
 
 
 
 
 | 113 | for i := 0 to High(FConnections) do | 
 
 
 
 
 | 114 | begin | 
 
 
 
 
 | 115 | if FConnections[i].FileName = FileName then | 
 
 
 
 
 | 116 | begin | 
 
 
 
 
 | 117 | Result := FConnections[i].ConnectionID; | 
 
 
 
 
 | 118 | Msg := SM_AlreadyOpened; | 
 
 
 
 
 | 119 | Exit; | 
 
 
 
 
 | 120 | end; | 
 
 
 
 
 | 121 | end; | 
 
 
 
 
 | 122 | end; | 
 
 
 
 
 | 123 |  | 
 
 
 
 
 | 124 | if not FileExists(FileName) then | 
 
 
 
 
 | 125 | begin | 
 
 
 
 
 | 126 | Msg := SM_FileNotFound; | 
 
 
 
 
 | 127 | Exit; | 
 
 
 
 
 | 128 | end; | 
 
 
 
 
 | 129 |  | 
 
 
 
 
 | 130 | ext := UpperCase(ExtractFileExt(FileName)); | 
 
 
 
 
 | 131 |  | 
 
 
 
 
 | 132 | if ext = 'ODB' then | 
 
 
 
 
 | 133 | backend := DB_ADB | 
 
 
 
 
 | 134 | else if ext = 'DAT' then | 
 
 
 
 
 | 135 | backend := DB_ONI | 
 
 
 
 
 | 136 | else | 
 
 
 
 
 | 137 | begin | 
 
 
 
 
 | 138 | Msg := SM_UnknownExtension; | 
 
 
 
 
 | 139 | Exit; | 
 
 
 
 
 | 140 | end; | 
 
 
 
 
 | 141 |  | 
 
 
 
 
 | 142 | SetLength(FConnections, Length(FConnections) + 1); | 
 
 
 
 
 | 143 | i := High(FConnections); | 
 
 
 
 
 | 144 | case backend of | 
 
 
 
 
 | 145 | DB_ONI: | 
 
 
 
 
 | 146 | FConnections[i] := TAccess_OniArchive.Create(FileName, FLastID + 1, CreateMsg); | 
 
 
 
 
 | 147 | DB_ADB: | 
 
 
 
 
 | 148 | FConnections[i] := TAccess_OUP_ADB.Create(FileName, FLastID + 1, CreateMsg); | 
 
 
 
 
 | 149 | end; | 
 
 
 
 
 | 150 |  | 
 
 
 
 
 | 151 | if CreateMsg = SM_OK then | 
 
 
 
 
 | 152 | begin | 
 
 
 
 
 | 153 | FLastID := FConnections[i].ConnectionID; | 
 
 
 
 
 | 154 | Result := FLastID; | 
 
 
 
 
 | 155 | end | 
 
 
 
 
 | 156 | else | 
 
 
 
 
 | 157 | begin | 
 
 
 
 
 | 158 | FConnections[i].Close; | 
 
 
 
 
 | 159 | FConnections[i].Free; | 
 
 
 
 
 | 160 | FConnections[i] := nil; | 
 
 
 
 
 | 161 | SetLength(FConnections, Length(FConnections) - 1); | 
 
 
 
 
 | 162 | Msg := SM_UnknownError; | 
 
 
 
 
 | 163 | end; | 
 
 
 
 
 | 164 | end; | 
 
 
 
 
 | 165 |  | 
 
 
 
 
 | 166 |  | 
 
 
 
 
 | 167 | procedure TConnectionManager.RemoveConnection(ArrayIndex: Integer); | 
 
 
 
 
 | 168 | var | 
 
 
 
 
 | 169 | i: Integer; | 
 
 
 
 
 | 170 | begin | 
 
 
 
 
 | 171 | if Length(FConnections) > 1 then | 
 
 
 
 
 | 172 | begin | 
 
 
 
 
 | 173 | for i := ArrayIndex to High(FConnections) - 1 do | 
 
 
 
 
 | 174 | begin | 
 
 
 
 
 | 175 | FConnections[i] := FConnections[i + 1]; | 
 
 
 
 
 | 176 | end; | 
 
 
 
 
 | 177 | end; | 
 
 
 
 
 | 178 | SetLength(FConnections, Length(FConnections) - 1); | 
 
 
 
 
 | 179 | end; | 
 
 
 
 
 | 180 |  | 
 
 
 
 
 | 181 | function TConnectionManager.CloseConnectionByIndex(Index: Integer; var Msg: TStatusMessages): Boolean; | 
 
 
 
 
 | 182 | begin | 
 
 
 
 
 | 183 | Msg := SM_UnknownError; | 
 
 
 
 
 | 184 | Result := False; | 
 
 
 
 
 | 185 |  | 
 
 
 
 
 | 186 | if Index < Length(FConnections) then | 
 
 
 
 
 | 187 | begin | 
 
 
 
 
 | 188 | FConnections[Index].Close; | 
 
 
 
 
 | 189 | RemoveConnection(Index); | 
 
 
 
 
 | 190 | Msg := SM_OK; | 
 
 
 
 
 | 191 | Result := True; | 
 
 
 
 
 | 192 | end; | 
 
 
 
 
 | 193 | end; | 
 
 
 
 
 | 194 |  | 
 
 
 
 
 | 195 | function TConnectionManager.CloseConnection(ID: Integer; var Msg: TStatusMessages): Boolean; | 
 
 
 
 
 | 196 | var | 
 
 
 
 
 | 197 | i: Integer; | 
 
 
 
 
 | 198 | begin | 
 
 
 
 
 | 199 | Msg := SM_UnknownError; | 
 
 
 
 
 | 200 | Result := False; | 
 
 
 
 
 | 201 |  | 
 
 
 
 
 | 202 | if Length(FConnections) > 0 then | 
 
 
 
 
 | 203 | begin | 
 
 
 
 
 | 204 | for i := 0 to High(FConnections) do | 
 
 
 
 
 | 205 | begin | 
 
 
 
 
 | 206 | if FConnections[i].ConnectionID = ID then | 
 
 
 
 
 | 207 | begin | 
 
 
 
 
 | 208 | FConnections[i].Close; | 
 
 
 
 
 | 209 | RemoveConnection(i); | 
 
 
 
 
 | 210 | Msg := SM_OK; | 
 
 
 
 
 | 211 | Result := True; | 
 
 
 
 
 | 212 | Exit; | 
 
 
 
 
 | 213 | end; | 
 
 
 
 
 | 214 | end; | 
 
 
 
 
 | 215 | end; | 
 
 
 
 
 | 216 | end; | 
 
 
 
 
 | 217 |  | 
 
 
 
 
 | 218 | function TConnectionManager.CloseConnection(FileName: String; var Msg: TStatusMessages): Boolean; | 
 
 
 
 
 | 219 | var | 
 
 
 
 
 | 220 | i: Integer; | 
 
 
 
 
 | 221 | begin | 
 
 
 
 
 | 222 | Msg := SM_UnknownError; | 
 
 
 
 
 | 223 | Result := False; | 
 
 
 
 
 | 224 |  | 
 
 
 
 
 | 225 | if Length(FConnections) > 0 then | 
 
 
 
 
 | 226 | begin | 
 
 
 
 
 | 227 | for i := 0 to High(FConnections) do | 
 
 
 
 
 | 228 | begin | 
 
 
 
 
 | 229 | if FConnections[i].FileName = FileName then | 
 
 
 
 
 | 230 | begin | 
 
 
 
 
 | 231 | FConnections[i].Close; | 
 
 
 
 
 | 232 | RemoveConnection(i); | 
 
 
 
 
 | 233 | Msg := SM_OK; | 
 
 
 
 
 | 234 | Result := True; | 
 
 
 
 
 | 235 | Exit; | 
 
 
 
 
 | 236 | end; | 
 
 
 
 
 | 237 | end; | 
 
 
 
 
 | 238 | end; | 
 
 
 
 
 | 239 | end; | 
 
 
 
 
 | 240 |  | 
 
 
 
 
 | 241 |  | 
 
 
 
 
 | 242 | end. |