forked from pakLebah/webCRT
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfbcrud.pas
More file actions
401 lines (382 loc) · 11.3 KB
/
fbcrud.pas
File metadata and controls
401 lines (382 loc) · 11.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
program FirebirdDemo;
uses
SysUtils, Classes, DB, SQLDB, IBConnection, webCRT;
type
TDBOperation = (dboSelect, dboInsert, dboDelete, dboUpdate);
const
INPUT_LEFT: integer = 140;
var
fb: TIBConnection;
tr: TSQLTransaction;
qr: TSQLQuery;
tl: TStringList;
dbo: TDBOperation;
btn_v: boolean;
tbl_n: string;
tbl_i: integer;
procedure writeErrorMsg(const aTitle, aMsg: string); inline;
begin
webWriteln('<p><font color="red"><b>'+aTitle+'</b>:</font> '+aMsg);
end;
function isMemoInputField(const field: string): boolean;
begin
case lowercase(field) of
'job_requirement' : Result := true;
'proj_desc' : Result := true;
else Result := false;
end;
end;
function isEditableTable(const table: string): boolean;
begin
case lowercase(table) of
'country' : Result := true;
'customer' : Result := true;
'department' : Result := false;
'employee' : Result := false;
'employee_project': Result := false;
'job' : Result := false;
'phone_list' : Result := true;
'project' : Result := true;
'proj_dept_budget': Result := false;
'salary_history' : Result := false;
'sales' : Result := false;
else Result := false;
end;
end;
function PKFieldCount(const table: string): integer;
begin
case lowercase(table) of
'country' : Result := 1;
'customer' : Result := 1;
'department' : Result := 1;
'employee' : Result := 1;
'employee_project': Result := 2;
'job' : Result := 3;
'phone_list' : Result := 1;
'project' : Result := 1;
'proj_dept_budget': Result := 3;
'salary_history' : Result := 3;
'sales' : Result := 1;
else Result := 1;
end;
end;
procedure SetupDB; inline;
begin
// resource setup
tl := TStringList.Create;
qr := TSQLQuery.Create(nil);
tr := TSQLTransaction.Create(nil);
fb := TIBConnection.Create(nil);
// db setup
fb.Hostname := 'localhost';
fb.DatabaseName := 'employee.fdb';
fb.Username := 'fbadmin';
fb.Password := 'firebird';
fb.Charset := 'UTF8';
fb.Dialect := 3;
// connection setup
fb.Transaction := tr;
tr.Database := fb;
qr.Database := fb;
qr.Transaction := tr;
end;
procedure CleanDB; inline;
begin
// free resource
tl.Free;
qr.Free;
tr.Free;
fb.Free;
end;
procedure SelectData;
var
i,col,row: integer;
s,fl,vl: string;
btn: boolean;
begin
webWriteln('<p>Show records: ');
// avoid blob column on some tables
if tbl_n = 'JOB' then
s := 'select job_code, job_grade, job_country, job_title, min_salary, max_salary, job_requirement from '+tbl_n
else if tbl_n = 'PROJ_DEPT_BUDGET' then
s := 'select fiscal_year, proj_id, dept_no, projected_budget from '+tbl_n
else
s := 'select * from '+tbl_n;
s := s + ' order by 1 asc nulls first';
// view data from selected table
qr.SQL.Text := s+';';
try
qr.Open;
col := qr.Fields.Count;
writeln('<style>table td:nth-child('+Int2Str(col+1)+') { text-align: right }</style>');
// print field name
s := ''; fl := '';
for i := 0 to col-1 do
begin
s := s+qr.Fields[i].FieldName+csvSplitter;
// column alignment based on field data type
if qr.FieldDefs[i].DataType in [ftSmallInt,ftInteger,ftWord,
ftFloat,ftCurrency,ftBCD,ftFMTBcd,
ftDateTime,ftDate,ftTime,ftTimeStamp] then
writeln('<style>table td:nth-child('+Int2Str(i+1)+') { text-align: right }</style>')
else if qr.FieldDefs[i].DataType in [ftFixedChar] then
writeln('<style>table td:nth-child('+Int2Str(i+1)+') { text-align: center }</style>');
// save field id
if isEditableTable(tbl_n) and (i < PKFieldCount(tbl_n)) then
fl := fl+'&field_'+Int2Str(i+1)+'='+qr.Fields[i].FieldName;
end;
if isEditableTable(tbl_n) then s := s+'ACTION';
webOpenTable(s);
// print field data
row := 0;
while not qr.EOF do
begin
s := ''; vl := '';
row := row+1;
for i := 0 to col-1 do
begin
// text formatting based on field data type
if qr.FieldDefs[i].DataType in [ftFloat] then
s := s+FormatFloat('#0.000',qr.Fields.Fields[i].AsFloat)
else if qr.FieldDefs[i].DataType in [ftCurrency,ftBCD,ftFMTBcd] then
s := s+FormatFloat('#,##0.00',qr.Fields.Fields[i].AsCurrency)
else if qr.FieldDefs[i].DataType in [ftDateTime,ftDate] then
s := s+FormatDateTime('dd-mm-yyyy',qr.Fields.Fields[i].AsDateTime)
else if qr.FieldDefs[i].DataType in [ftTime,ftTimeStamp] then
s := s+FormatDateTime('dd-mm-yyyy hh:nn:ss',qr.Fields.Fields[i].AsDateTime)
else
s := s+qr.Fields.Fields[i].AsString;
s := s+csvSplitter;
// save field value
if isEditableTable(tbl_n) and (i < PKFieldCount(tbl_n)) then
vl := vl+'&value_'+Int2Str(i+1)+'='+HTTPEncode(qr.Fields.Fields[i].AsString);
end;
// build data id into command link
if isEditableTable(tbl_n) then
s := s+webGetLink('?action=update&table='+tbl_n+fl+vl,
webGetGlyph('pencil','Edit record',true))+' │ '
+webGetLink('?action=delete&table='+tbl_n+fl+vl,
webGetGlyph('remove','Delete record',true));
webTableRow(s);
qr.Next;
end;
webCloseTable;
if isEditableTable(tbl_n) then webButtonAction(btn,' New record','?action=insert','plus');
// print data summary
webWriteln('<p>Summary:');
webOpenList(false);
webListItem('Table: ' +tbl_n);
webListItem('Columns: '+Int2Str(col));
webListItem('Rows: ' +Int2Str(row));
webCloseList;
qr.Close;
// catch error
except
on E: Exception do writeErrorMsg(E.ClassName,E.Message);
end;
end;
procedure InsertData;
var
s: string;
fc,i: integer;
btn: boolean;
data: array of string;
begin
webWriteln('<p>New record: ');
try
// read data structure
qr.SQL.Text := 'select * from '+tbl_n+' rows 1;';
qr.Open;
// required to override insert button
webWriteVar('table',tbl_n);
// field input
fc := qr.Fields.Count;
SetLength(data,fc);
for i := 0 to fc-1 do
begin
webWrite(qr.Fields[i].FieldName,INPUT_LEFT);
webWrite(':',10);
if isMemoInputField(qr.Fields[i].FieldName) then
webReadMemo(data[i]) else webReadln(data[i]);
end;
qr.Close;
// command button
webWrite('<p>'); webWrite('',INPUT_LEFT+10);
webButtonAction(btn,'INSERT','?action=insert','',true);
// execute
if btn then
begin
// build command
s := 'insert into '+tbl_n+' values (';
for i := 0 to fc-1 do s := s+''''+data[i]+''',';
s := Copy(s,1,Length(s)-1)+');';
// execute command
qr.SQL.Text := s;
(* // skip on demo
qr.ExecSQL;
tr.Commit; *)
webWriteBlock('<b>Generated SQL</b>:<br/><code>'+s+'</code>');
webWriteln('<p>New data inserted successfully!');
end;
except
on E: Exception do writeErrorMsg(E.ClassName,E.Message);
end;
end;
procedure DeleteData;
var
i: integer;
s,w: string;
btn: boolean;
begin
webWriteln('<p>Remove record: ');
try
// read data id
tbl_n := webReadVar('table');
w := webReadVar('where');
if w = '' then
begin
// build where clause
webWriteVar('table',tbl_n);
for i := 1 to PKFieldCount(tbl_n) do
w := w+webReadVar('field_'+Int2Str(i))+'='+
''''+webReadVar('value_'+Int2Str(i))+''' and ';
w := Copy(w,1,Length(w)-5);
webWriteVar('where',w);
// read data structure
qr.SQL.Text := 'select * from '+tbl_n+' where '+w;
qr.Open;
// show selected data
for i := 0 to qr.Fields.Count-1 do
begin
webWrite(qr.Fields[i].FieldName,INPUT_LEFT);
webWrite(':',10);
webWriteln(qr.Fields.Fields[i].AsString);
end;
qr.Close;
// command button
webWrite('<p>'); webWrite('',INPUT_LEFT+10);
webButtonAction(btn,'DELETE','?action=delete','',true);
end
// execute
else
begin
// execute command
s := 'delete from '+tbl_n+' where '+w+';';
qr.SQL.Text := s;
(* // skip on demo
qr.ExecSQL;
tr.Commit; *)
webWriteBlock('<b>Generated SQL</b>:<br/><code>'+s+'</code>');
webWriteln('<p>Old data deleted successfully!');
end;
except
on E: Exception do writeErrorMsg(E.ClassName,E.Message);
end;
end;
procedure UpdateData;
var
s,w: string;
fc,i: integer;
btn: boolean;
data: array of string;
begin
webWriteln('<p>Update record: ');
try
// read data id
tbl_n := webReadVar('table');
webWriteVar('table',tbl_n);
w := webReadVar('where');
if w = '' then
begin
// build where clause
for i := 1 to PKFieldCount(tbl_n) do
w := w+webReadVar('field_'+Int2Str(i))+'='+
''''+webReadVar('value_'+Int2Str(i))+''' and ';
w := Copy(w,1,Length(w)-5);
end;
// show selected data
qr.SQL.Text := 'select * from '+tbl_n+' where '+w+';';
qr.Open;
// field input
s := '';
fc := qr.Fields.Count;
SetLength(data,fc);
for i := 0 to fc-1 do
begin
webWrite(qr.Fields[i].FieldName,INPUT_LEFT);
webWrite(':',10);
data[i] := qr.Fields.Fields[i].AsString;
if isMemoInputField(qr.Fields[i].FieldName) then
webReadMemo(data[i]) else webReadln(data[i]);
// build new value list
s := s+qr.Fields[i].FieldName+'='''+data[i]+''', ';
end;
s := Copy(s,1,Length(s)-2);
qr.Close;
// this new var must be issued last to prevent interference with input var
webWriteVar('where',w);
// command button
webWrite('<p>'); webWrite('',INPUT_LEFT+10);
webButtonAction(btn,'UPDATE','?action=update','',true);
// execute
if btn then
begin
// build command
s := 'update '+tbl_n+' set '+s+' where '+w+';';
// execute command
qr.SQL.Text := s;
(* // skip on demo
qr.ExecSQL;
tr.Commit; *)
webWriteBlock('<b>Generated SQL</b>:<br/><code>'+s+'</code>');
webWriteln('<p>Data updated successfully!');
end;
except
on E: Exception do writeErrorMsg(E.ClassName,E.Message);
end;
end;
(*** main program ***)
var
i: integer;
s: string;
begin
OpenHTML(true,true);
csvSplitter := '|';
SetupDB;
try
// open db
fb.Open;
if fb.Connected then fb.GetTableNames(tl);
// show available tables
webPageHeader('Firebird - SQLdb CRUD Demo');
webWrite('<p>Table: ');
for i := 0 to tl.Count-1 do
begin
s := s+tl[i]+csvSplitter;
// allow select from web var
if webReadVar('table') = tl[i] then tbl_i := i+1;
end;
// select table
tbl_n := webReadSelect(tbl_i,s);
webReadButton(btn_v,'VIEW');
webWriteln;
// check db operation mode
if webReadVar('action') = 'update' then dbo := dboUpdate
else if webReadVar('action') = 'delete' then dbo := dboDelete
else if webReadVar('action') = 'insert' then dbo := dboInsert
else dbo := dboSelect;
// process data
if isWebInput then
case dbo of
dboSelect: SelectData;
dboInsert: InsertData;
dboDelete: DeleteData;
dboUpdate: UpdateData;
end;
// close db
fb.Close;
finally
CleanDB;
end;
CloseHTML(false);
end.