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