-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDBForm.pas
More file actions
236 lines (206 loc) · 5.93 KB
/
UDBForm.pas
File metadata and controls
236 lines (206 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
unit UDBForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
UNotifications, sqldb, DB, UDBConnection, UDBObjects, UVector;
type
TDBField = UDBObjects.TDBField;
TDBTable = UDBObjects.TDBTable;
TDbConnection = UDBConnection.TDBConnection;
TNClass = UNotifications.TNClass;
TParams = DB.TParams;
TSQLQuery = sqldb.TSQLQuery;
TDBForm = class;
TDBFormType = class of TDBForm;
TDBForms = specialize TVector<TDBForm>;
{ TDBForm }
TDBForm = class(TForm)
private
FID: integer;
FOnPerform: TEvent;
FOnExec: TEvent;
FQuery: TSQLQuery;
FTable: TDBTable;
FParentForm: TDBForm;
FDataSource: TDataSource;
FThisSubscriber: TSubscriber;
FDBConnection: TDbConnection;
procedure RemoveChildForm(Form: TDBForm);
procedure EmptyEvent;
protected
FCForms: TDBForms;
FSelectAll: TQueryContainer;
procedure ShowQuery(QContainer: TQueryContainer);
function PerformQuery(QContainer: TQueryContainer): boolean; virtual;
function ExecQuery(QContainer: TQueryContainer): boolean; virtual;
function CreateForm(ANClass: TNClass; ATable: TDBTable;
FormType: TDBFormType; Params: TParams = nil): TDBForm;
function CreateChildForm(ANClass: TNClass; ATable: TDBTable;
FormType: TDBFormType; Params: TParams; ID: integer): TDBForm;
public
procedure InitConnection(DBConnection: TDbConnection); virtual;
procedure CreateTransaction;
procedure Load(ANClass: TNClass; ATable: TDBTable; Params: TParams = nil); virtual;
procedure CloseChildForms;
published
procedure FormCreate(Sender: TObject); virtual;
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); virtual;
property ChildForms: TDBForms read FCForms;
property FormID: integer read FID;
property Connection: TDbConnection read FDBConnection write FDBConnection;
property OnPerformQuery: TEvent write FOnPerform;
property OnExecQuery: TEvent write FOnExec;
property ThisSubscriber: TSubscriber read FThisSubscriber write FThisSubscriber;
property FormQuery: TSQLQuery read FQuery;
property Table: TDBTable read FTable write FTable;
property DataSource: TDataSource read FDataSource;
end;
function ToNClass(A: array of integer): TNClass;
implementation
uses UFormManager;
{$R *.lfm}
procedure TDBForm.EmptyEvent;
begin
end;
function ToNClass(A: array of integer): TNClass;
var
i: integer;
begin
SetLength(Result, Length(A));
for i := 0 to High(A) do
Result[i] := A[i];
end;
procedure TDBForm.RemoveChildForm(Form: TDBForm);
var
Index: integer;
begin
Index := FCForms.FindInd(Form);
if Index <> -1 then
FCForms.DeleteInd(Index);
end;
procedure TDBForm.ShowQuery(QContainer: TQueryContainer);
var
Str: string;
i: integer;
begin
Str := QContainer.Query + #13#10 + '__________' + #13#10;
with QContainer do
if Params <> nil then
for i := 0 to Params.Count - 1 do begin
Str += Params.Items[i].Name + ' ' + string(Params.Items[i].Value) + #13#10;
end;
ShowMessage(Str);
end;
procedure TDBForm.FormCreate(Sender: TObject);
begin
FSelectAll.Query := '';
FSelectAll.Params := nil;
FTable := nil;
FThisSubscriber := nil;
FOnExec := @EmptyEvent;
FOnPerform := @EmptyEvent;
FCForms := TDBForms.Create;
FParentForm := nil;
end;
procedure TDBForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
CloseAction := caFree;
if FParentForm <> nil then
FParentForm.RemoveChildForm(Self);
CloseChildForms;
if FThisSubscriber <> nil then
FThisSubscriber.Free;
if FormManager <> nil then
FormManager.UpDateTree;
FQuery.Free;
FDataSource.Free;
FCForms.Free;
end;
procedure TDBForm.InitConnection(DBConnection: TDbConnection);
begin
FDBConnection := DBConnection;
FQuery := TSQLQuery.Create(FQuery);
FDataSource := TDataSource.Create(FDataSource);
FQuery.DataBase := DBConnection.Connection;
FQuery.Transaction := DBConnection.Transaction;
FDataSource.DataSet := FQuery;
end;
procedure TDBForm.CreateTransaction;
begin
FDBConnection := FDBConnection.NewTransaction;
end;
procedure TDBForm.Load(ANClass: TNClass; ATable: TDBTable; Params: TParams = nil);
begin
FTable := ATable;
FThisSubscriber := TSubscriber.Create;
FThisSubscriber.NClass := ANClass;
FSelectAll := Table.Query.Select(nil);
end;
function TDBForm.PerformQuery(QContainer: TQueryContainer): boolean;
begin
Result := True;
FQuery.Close;
FQuery.SQL.Clear;
FQuery.SQL.Text := QContainer.Query;
if QContainer.Params <> nil then
FQuery.Params := QContainer.Params;
try
FQuery.Open;
FOnPerform;
except
Result := False;
end;
end;
function TDBForm.ExecQuery(QContainer: TQueryContainer): boolean;
begin
Result := True;
FQuery.Close;
FQuery.SQL.Clear;
FQuery.SQL.Text := QContainer.Query;
if QContainer.Params <> nil then
FQuery.Params := QContainer.Params;
try
FQuery.ExecSQL;
FDBConnection.Transaction.Commit;
FOnExec;
except
Result := False;
end;
end;
function TDBForm.CreateForm(ANClass: TNClass; ATable: TDBTable;
FormType: TDBFormType; Params: TParams = nil): TDBForm;
begin
Application.CreateForm(FormType, Result);
Result.InitConnection(FDBConnection);
Result.Load(ANClass, ATable, Params);
ThisSubscriber.Subscribe(Result.ThisSubscriber);
Result.Show;
end;
function TDBForm.CreateChildForm(ANClass: TNClass; ATable: TDBTable;
FormType: TDBFormType; Params: TParams; ID: integer): TDBForm;
var
i: integer;
begin
if ID <> -1 then
for i := 0 to FCForms.Size - 1 do
if FCForms[i].FID = ID then begin
FCForms[i].BringToFront;
Exit(FCForms[i]);
end;
Result := CreateForm(ANClass, ATable, FormType, Params);
Result.FID := ID;
Result.FParentForm := Self;
FCForms.PushBack(Result);
if FormManager <> nil then
FormManager.UpDateTree;
Exit(Result);
end;
procedure TDBForm.CloseChildForms;
var
i: integer;
begin
for i := 0 to FCForms.Size - 1 do
FCForms[i].Close;
end;
end.