-
Notifications
You must be signed in to change notification settings - Fork 0
/
referen.pas
172 lines (154 loc) · 4.42 KB
/
referen.pas
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
unit Referen;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, sqldb, db, BufDataset, FileUtil, Forms, Controls, Graphics,
Dialogs, DBGrids, StdCtrls, ExtCtrls, DbCtrls, Connect, MetaUnit, CardInsert,
CardEdit, Filters;
type
{ TReferenForm }
TReferenForm = class(TForm)
type
TFilterInfoArray = array of TFilterInfo;
published
BtnInsert: TButton;
BtnEdit: TButton;
BtnRemove: TButton;
Datasource: TDatasource;
DBGrid: TDBGrid;
FilterGroup: TGroupBox;
SQLQuery: TSQLQuery;
procedure BtnEditClick(Sender: TObject);
procedure BtnInsertClick(Sender: TObject);
procedure BtnRemoveClick(Sender: TObject);
procedure DBGridTitleClick(Column: TColumn);
procedure PopupForm(ATable: TTableInfo; AFilterType: Integer = 0;
AFilterInfo: TFilterInfoArray = nil);
procedure ShowTable;
public
ThisTable: TTableInfo;
ThisQuery, DeleteConst: String;
FilterType: TComboBox;
SortStatus: array of (NONE, ASC, DESC);
SortType, SortIcon: String;
SortIndex: Integer;
InsFrm: TCardInsertForm;
F: TFilters;
function MakeLocalQuery: String;
end;
var
ReferenForm: TReferenForm;
implementation
procedure TReferenForm.ShowTable;
var
i: Integer;
FSortIcon: String;
begin
ThisQuery := TMeta.MakeQuery(ThisTable, MakeLocalQuery);
SQLQuery.Close;
SQLQuery.SQL.Text := ThisQuery;
for i := 0 to High(F.FilterPanels) do begin
with F.FilterPanels[i] do begin
if not FCheckBox.Checked then Continue;
SQLQuery.ParamByName(Format('FConst_Text%d', [i])).AsString := FConstraint.Text;
end;
end;
SQLQuery.Open;
for i := 0 to High(ThisTable.FFields) do begin
if i = SortIndex then
FSortIcon := ' ' + SortIcon
else
FSortIcon := '';
DBGrid.Columns[i].Title.Caption := ThisTable.FFields[i].FCaption + FSortIcon;
DBGrid.Columns[i].Width := ThisTable.FFields[i].FWidth * 10 + 5;
end;
//DBGrid.Columns[0].Visible := False;
end;
procedure TReferenForm.PopupForm(ATable: TTableInfo; AFilterType: Integer = 0;
AFilterInfo: TFilterInfoArray = nil);
begin
ThisTable := ATable;
DeleteConst := 'ID <> 0';
SortIndex := -1;
SetLength(SortStatus, Length(ATable.FFields));
ReferenForm := TReferenForm.Create(nil);
Caption := ATable.FCaption;
DBGrid.Top := 40;
F := TFilters.Create(ThisTable, FilterGroup, @ShowTable, 10, AFilterType, AFilterInfo);
ShowTable;
Show;
end;
function TReferenForm.MakeLocalQuery(): String;
var
query, field: String;
begin
query := '';
if SortIndex <> -1 then begin
with ThisTable.FFields[SortIndex] do begin
if FForeignKeyTable <> '' then
field := Format('%s.Name', [FForeignKeyTable])
else
field := Format('%s.%s', [ThisTable.FName, FName]);
end;
query += Format(' ORDER BY %s %s', [field, SortType]);
end;
query := Format('%s %s', [F.MakeQuery, query]);
Result := query;
end;
procedure TReferenForm.DBGridTitleClick(Column: TColumn);
begin
if SortIndex <> Column.Index then
SortStatus[SortIndex] := NONE;
SortIndex := Column.Index;
case SortStatus[SortIndex] of
NONE: begin
SortStatus[SortIndex] := DESC;
SortType := 'ASC';
SortIcon := '»';
end;
DESC: begin
SortStatus[SortIndex] := ASC;
SortType := 'DESC';
SortIcon := '«';
end;
ASC: begin
SortStatus[SortIndex] := NONE;
SortType := '';
SortIndex := -1;
SortIcon := '';
end;
end;
ShowTable;
end;
procedure TReferenForm.BtnInsertClick(Sender: TObject);
begin
TCardEditForm.ShowEditor(ThisTable, 0, False, @ShowTable);
end;
procedure TReferenForm.BtnEditClick(Sender: TObject);
var
forEdit: Integer;
begin
forEdit := Datasource.DataSet.FieldByName('ID').AsInteger;
if forEdit = 0 then Exit;
TCardEditForm.ShowEditor(ThisTable, forEdit, True, @ShowTable);
end;
procedure TReferenForm.BtnRemoveClick(Sender: TObject);
var
forRemove, answer: Integer;
begin
forRemove := Datasource.DataSet.FieldByName('ID').AsInteger;
if forRemove = 0 then Exit;
answer := MessageDlg(Format('Вы действительно хотите удалить выбранную запись (ID %d)', [forRemove]), mtCustom, [mbYes, mbNo], 0);
if answer = 6 then begin
with SQLQuery do begin
Close;
SQL.Clear;
SQL.Text := Format('DELETE FROM %s WHERE ID = %d', [ThisTable.FName, forRemove]);
ExecSQL;
end;
ConnectModule.SQLTransaction.Commit;
ShowTable;
end;
end;
{$R *.lfm}
end.