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