source: oup/rewrite/DataAccess/ConnectionManager.pas@ 97

Last change on this file since 97 was 97, checked in by alloc, 18 years ago
File size: 6.2 KB
Line 
1unit ConnectionManager;
2interface
3
4uses TypeDefs, DataAccess, Access_OniArchive, Access_OUP_ADB;
5
6type
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 function GetConnectionIndex(ConnectionID: Integer): Integer;
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;
27 property ConnectionIndexByID[ConnectionID: Integer]: Integer read GetConnectionIndex;
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
41var
42 ConManager: TConnectionManager;
43
44
45implementation
46uses
47 SysUtils, Dialogs;
48
49(*
50 Implementation of TConnectionManager
51*)
52
53
54function TConnectionManager.GetConnectionCount: Integer;
55begin
56 Result := Length(FConnections);
57end;
58
59function TConnectionManager.GetConnectionIndex(ConnectionID: Integer): Integer;
60var
61 i: Integer;
62begin
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;
71end;
72
73function TConnectionManager.GetConnection(ConnectionID: Integer): TDataAccess;
74var
75 i: Integer;
76begin
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;
92end;
93
94
95function TConnectionManager.GetConnectionByIndex(Index: Integer): TDataAccess;
96begin
97 Result := nil;
98 if index < Length(FConnections) then
99 begin
100 Result := FConnections[index];
101 end;
102end;
103
104constructor TConnectionManager.Create;
105begin
106 inherited;
107 FLastID := 0;
108end;
109
110function TConnectionManager.Close: Boolean;
111begin
112 Result := False;
113 if Length(FConnections) > 0 then
114 Exit;
115
116 inherited;
117end;
118
119
120
121function TConnectionManager.OpenConnection(FileName: String; var Msg: TStatusMessages): Integer;
122var
123 i: Integer;
124 ext: String;
125 backend: TDataBackend;
126 CreateMsg: TStatusMessages;
127begin
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;
184end;
185
186
187procedure TConnectionManager.RemoveConnection(ArrayIndex: Integer);
188var
189 i: Integer;
190begin
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);
199end;
200
201function TConnectionManager.CloseConnectionByIndex(Index: Integer; var Msg: TStatusMessages): Boolean;
202begin
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;
213end;
214
215function TConnectionManager.CloseConnection(ID: Integer; var Msg: TStatusMessages): Boolean;
216var
217 i: Integer;
218begin
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;
236end;
237
238function TConnectionManager.CloseConnection(FileName: String; var Msg: TStatusMessages): Boolean;
239var
240 i: Integer;
241begin
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;
259end;
260
261
262initialization
263 ConManager := TConnectionManager.Create;
264finalization
265 ConManager.Free;
266end.
Note: See TracBrowser for help on using the repository browser.