[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 |
|
---|
[101] | 152 | if ext = '.OLDB' then
|
---|
[93] | 153 | backend := DB_ADB
|
---|
[101] | 154 | else if ext = '.DAT' then
|
---|
[93] | 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;
|
---|
[101] | 175 | Msg := SM_OK;
|
---|
[93] | 176 | end
|
---|
| 177 | else
|
---|
| 178 | begin
|
---|
| 179 | FConnections[i].Close;
|
---|
| 180 | FConnections[i].Free;
|
---|
| 181 | FConnections[i] := nil;
|
---|
| 182 | SetLength(FConnections, Length(FConnections) - 1);
|
---|
[101] | 183 | Msg := CreateMsg;
|
---|
[93] | 184 | end;
|
---|
| 185 | end;
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | procedure TConnectionManager.RemoveConnection(ArrayIndex: Integer);
|
---|
| 189 | var
|
---|
| 190 | i: Integer;
|
---|
| 191 | begin
|
---|
| 192 | if Length(FConnections) > 1 then
|
---|
| 193 | begin
|
---|
| 194 | for i := ArrayIndex to High(FConnections) - 1 do
|
---|
| 195 | begin
|
---|
| 196 | FConnections[i] := FConnections[i + 1];
|
---|
| 197 | end;
|
---|
| 198 | end;
|
---|
| 199 | SetLength(FConnections, Length(FConnections) - 1);
|
---|
| 200 | end;
|
---|
| 201 |
|
---|
| 202 | function TConnectionManager.CloseConnectionByIndex(Index: Integer; var Msg: TStatusMessages): Boolean;
|
---|
| 203 | begin
|
---|
| 204 | Msg := SM_UnknownError;
|
---|
| 205 | Result := False;
|
---|
| 206 |
|
---|
| 207 | if Index < Length(FConnections) then
|
---|
| 208 | begin
|
---|
| 209 | FConnections[Index].Close;
|
---|
| 210 | RemoveConnection(Index);
|
---|
| 211 | Msg := SM_OK;
|
---|
| 212 | Result := True;
|
---|
| 213 | end;
|
---|
| 214 | end;
|
---|
| 215 |
|
---|
| 216 | function TConnectionManager.CloseConnection(ID: Integer; var Msg: TStatusMessages): Boolean;
|
---|
| 217 | var
|
---|
| 218 | i: Integer;
|
---|
| 219 | begin
|
---|
| 220 | Msg := SM_UnknownError;
|
---|
| 221 | Result := False;
|
---|
| 222 |
|
---|
| 223 | if Length(FConnections) > 0 then
|
---|
| 224 | begin
|
---|
| 225 | for i := 0 to High(FConnections) do
|
---|
| 226 | begin
|
---|
| 227 | if FConnections[i].ConnectionID = ID then
|
---|
| 228 | begin
|
---|
| 229 | FConnections[i].Close;
|
---|
| 230 | RemoveConnection(i);
|
---|
| 231 | Msg := SM_OK;
|
---|
| 232 | Result := True;
|
---|
| 233 | Exit;
|
---|
| 234 | end;
|
---|
| 235 | end;
|
---|
| 236 | end;
|
---|
| 237 | end;
|
---|
| 238 |
|
---|
| 239 | function TConnectionManager.CloseConnection(FileName: String; var Msg: TStatusMessages): Boolean;
|
---|
| 240 | var
|
---|
| 241 | i: Integer;
|
---|
| 242 | begin
|
---|
| 243 | Msg := SM_UnknownError;
|
---|
| 244 | Result := False;
|
---|
| 245 |
|
---|
| 246 | if Length(FConnections) > 0 then
|
---|
| 247 | begin
|
---|
| 248 | for i := 0 to High(FConnections) do
|
---|
| 249 | begin
|
---|
| 250 | if FConnections[i].FileName = FileName then
|
---|
| 251 | begin
|
---|
| 252 | FConnections[i].Close;
|
---|
| 253 | RemoveConnection(i);
|
---|
| 254 | Msg := SM_OK;
|
---|
| 255 | Result := True;
|
---|
| 256 | Exit;
|
---|
| 257 | end;
|
---|
| 258 | end;
|
---|
| 259 | end;
|
---|
| 260 | end;
|
---|
| 261 |
|
---|
| 262 |
|
---|
[97] | 263 | initialization
|
---|
| 264 | ConManager := TConnectionManager.Create;
|
---|
| 265 | finalization
|
---|
| 266 | ConManager.Free;
|
---|
[93] | 267 | end.
|
---|