forked from motaz/turbobird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editdatafullrec.pas
199 lines (174 loc) · 4.92 KB
/
editdatafullrec.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
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
unit EditDataFullRec;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, db, sqldb, IBConnection, FileUtil, LResources, Forms,
Controls, Graphics, Dialogs, DbCtrls, StdCtrls, main,
Buttons;
type
{ TfmEditDataFullRec }
TfmEditDataFullRec = class(TForm)
bbSave: TBitBtn;
Datasource1: TDatasource;
DBNavigator1: TDBNavigator;
Label1: TLabel;
laPos: TLabel;
sqEditTable: TSQLQuery;
procedure bbSaveClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure sqEditTableAfterScroll(DataSet: TDataSet);
private
FIBConnection: TIBConnection;
FSQLTrans: TSQLTransaction;
{ private declarations }
public
procedure CurrentDateClick(Sender: TObject);
procedure Init(dbIndex: Integer; ATableName: string);
{ public declarations }
end;
var
fmEditDataFullRec: TfmEditDataFullRec;
implementation
uses Calen;
procedure TfmEditDataFullRec.FormClose(Sender: TObject;
var CloseAction: TCloseAction);
begin
if bbSave.Visible then
bbSaveClick(nil);
sqEditTable.Close;
CloseAction:= caFree;
end;
procedure TfmEditDataFullRec.bbSaveClick(Sender: TObject);
begin
if sqEditTable.State in [dsInsert, dsEdit] then
sqEditTable.Post;
sqEditTable.ApplyUpdates;
FSQLTrans.CommitRetaining;
end;
procedure TfmEditDataFullRec.sqEditTableAfterScroll(DataSet: TDataSet);
begin
laPos.Caption:= IntToStr(sqEditTable.RecNo) + ' of ' + IntToStr(sqEditTable.RecordCount);
end;
procedure TfmEditDataFullRec.CurrentDateClick(Sender: TObject);
var
FieldNum: Integer;
begin
FieldNum:= (Sender as TBitBtn).Tag;
fmCalen.DateTimeValue:= sqEditTable.Fields[FieldNum].AsDateTime;
if fmCalen.ShowModal = mrOK then
begin
sqEditTable.Edit;
sqEditTable.Fields[FieldNum].AsDateTime:= fmCalen.DateTimeValue;
end;
end;
procedure TfmEditDataFullRec.Init(dbIndex: Integer; ATableName: string);
var
ALabel: TLabel;
ADBEdit: TDBEdit;
i: Integer;
AWidth: Integer;
ADBMemo: TDBMemo;
ATop: Integer;
bbDate: TBitBtn;
begin
FIBConnection:= fmMain.RegisteredDatabases[dbIndex].IBConnection;
FIBConnection.Close;
FSQLTrans:= fmMain.RegisteredDatabases[dbIndex].SQLTrans;
sqEditTable.DataBase:= FIBConnection;
sqEditTable.SQL.Text:= 'select * from ' + ATableName;
sqEditTable.Open;
bbSave.Visible:= true;
{
// ASSUME there's a generator/trigger
//todo: verify this assumption using code to check this out. Then also modify
//insert statement to leave out the relevant fields if not present
FieldsList:= TStringList.Create;
try
if fmmain.GetPrimaryKeyFields(dbIndex, ATableName, FieldsList) then
begin
bbSave.Visible:= true;
for i:= 0 to FieldsList.Count -1 do
begin
try
sqEditTable.FieldByName(FieldsList[i]).Required:=false;
except
// field does not exist => error
bbSave.Visible:=false;
break;
end;
end;
end
else
begin
bbSave.Visible:= false;
end;
finally
FieldsList.Free;
end;
}
if not(bbSave.Visible) then
ShowMessage('Primary key is not found for this table. It can not be edited.');
ATop:= 70;
for i:= 0 to sqEditTable.Fields.Count - 1 do
begin
ALabel:= TLabel.Create(self);
ALabel.Parent:= self;
ALabel.Left:= 20;
ALabel.Top:= ATop + 5;
ALabel.Caption:= sqEditTable.Fields[i].FieldName;
// Memo
if sqEditTable.Fields[i].DataType = ftBlob then
begin
ADBMemo:= TDBMemo.Create(self);
ADBMemo.Parent:= self;
ADBMemo.Left:= 160;
ADBMemo.Top:= ATop;
ADBMemo.Width:= 400;
ADBMemo.Height:= 200;
ADBMemo.Anchors:= [akLeft, akTop, akRight];
ADBMemo.ScrollBars:= ssBoth;
ADBMemo.DataSource:= Datasource1;
ADBMemo.DataField:= sqEditTable.Fields[i].FieldName;
end
else // Edit control
begin
ADBEdit:= TDBEdit.Create(self);
ADBEdit.Parent:= self;
ADBEdit.Left:= 160;
ADBEdit.Top:= ATop;
ADBEdit.DataSource:= Datasource1;
ADBEdit.DataField:= sqEditTable.Fields[i].FieldName;
AWidth:= 80;
if sqEditTable.Fields[i].DataType = ftString then
AWidth:= sqEditTable.Fields[i].DataSize * 10
else
if sqEditTable.Fields[i].DataType = ftDateTime then
AWidth:= 140;
if AWidth > 400 then
AWidth:= 400;
ADBEdit.Width:= AWidth;
end;
// DateTime field
if sqEditTable.Fields[i].DataType in [ftDate, ftDateTime, ftTime] then
begin
bbDate:= TBitBtn.Create(self);
bbDate.Parent:= self;
bbDate.Left:= 180 + AWidth;
bbDate.Top:= ATop;
bbDate.Caption:= 'Calen.';
bbDate.Width:= 60;
bbDate.Hint:= 'Date/Time Selector';
bbDate.ShowHint:= True;
bbDate.Tag:= i;
bbDate.OnClick:= @CurrentDateClick;
end;
if sqEditTable.Fields[i].DataType = ftBlob then
Inc(ATop, 200)
else
Inc(ATop, 30);
end;
Height:= ATop + 10;
end;
initialization
{$I editdatafullrec.lrs}
end.