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

Last change on this file since 106 was 101, 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 = '.OLDB' 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 Msg := SM_OK;
176 end
177 else
178 begin
179 FConnections[i].Close;
180 FConnections[i].Free;
181 FConnections[i] := nil;
182 SetLength(FConnections, Length(FConnections) - 1);
183 Msg := CreateMsg;
184 end;
185end;
186
187
188procedure TConnectionManager.RemoveConnection(ArrayIndex: Integer);
189var
190 i: Integer;
191begin
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);
200end;
201
202function TConnectionManager.CloseConnectionByIndex(Index: Integer; var Msg: TStatusMessages): Boolean;
203begin
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;
214end;
215
216function TConnectionManager.CloseConnection(ID: Integer; var Msg: TStatusMessages): Boolean;
217var
218 i: Integer;
219begin
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;
237end;
238
239function TConnectionManager.CloseConnection(FileName: String; var Msg: TStatusMessages): Boolean;
240var
241 i: Integer;
242begin
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;
260end;
261
262
263initialization
264 ConManager := TConnectionManager.Create;
265finalization
266 ConManager.Free;
267end.
Note: See TracBrowser for help on using the repository browser.