-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBGrid_custom_connectionFD.pas
More file actions
347 lines (278 loc) · 10.4 KB
/
Copy pathDBGrid_custom_connectionFD.pas
File metadata and controls
347 lines (278 loc) · 10.4 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
unit DBGrid_custom_connectionFD;
interface
uses
Data.DB, FireDAC.Comp.Client, System.SysUtils, Vcl.ActnList, Vcl.Dialogs, System.Classes,
Vcl.Forms, Vcl.DBGrids, Vcl.StdCtrls, FireDAC.Stan.Param, System.UITypes,
System.Generics.Collections, DBGrid_custom_table;
type
TDatabase = (MYSQL, FIREBIRD);
TDBGrid_custom_connectionFD = class(TComponent)
private
FConn : TFDConnection;
FPathLog: string;
FDBGrid: TDBGrid;
FId_user_login : integer;
FDBGrid_custom_table : TDBGrid_custom_table;
FDatabase : TDatabase;
function setConfig: String;
public
constructor Create; virtual;
destructor Destroy; override;
public procedure loadTable;
public procedure loadConfig(pForm: TForm);
public procedure save(pForm: TForm);
public procedure reset(pForm: TForm);
public procedure setDBGRID(pDBGrid: TDBGrid);
public function getDBGRID:TDBGrid;
published
property Conn : TFDConnection read FConn write FConn;
property DBGrid_custom_table : TDBGrid_custom_table read FDBGrid_custom_table write FDBGrid_custom_table;
property ID_USER_LOGIN : Integer read FId_user_login write FId_user_login;
property Database : TDatabase read FDatabase write FDatabase;
private
property DBGrid : TDBGrid read FDBGrid write FDBGrid;
end;
implementation
constructor TDBGrid_custom_connectionFD.Create;
begin
end;
destructor TDBGrid_custom_connectionFD.Destroy;
begin
inherited;
end;
function TDBGrid_custom_connectionFD.getDBGRID: TDBGrid;
begin
result := DBGrid;
end;
function TDBGrid_custom_connectionFD.setConfig: String;
var
Str: TStringStream;
begin
Str:= TStringStream.Create;
try
try
DBGrid.Columns.SaveToStream(Str);
Result := Str.DataString;
except
on e: Exception do
begin
MessageDlg('Erro: ' + e.Message , mtError,[mbok],0);
end;
end;
finally
FreeAndNil(Str);
end;
end;
procedure TDBGrid_custom_connectionFD.setDBGRID(pDBGrid: TDBGrid);
begin
DBGrid := pDBGrid;
end;
procedure TDBGrid_custom_connectionFD.loadConfig(pForm: TForm);
var
qy : TFDQuery;
Str: TStringStream;
pathSave: string;
begin
qy := TFDQuery.Create(nil);
qy.Connection := FConn;
try
try
begin
qy.SQL.Clear;
qy.SQL.Add(
' SELECT '+ DBGrid_custom_table.FieldConfig +' FROM '+DBGrid_custom_table.Table+' ' +#13+
' WHERE ' +#13+
' '+DBGrid_custom_table.FieldForm+' = :FORM ' +#13+
' AND ' +#13+
' '+DBGrid_custom_table.FieldIdUser+' = :USER ' +#13+
' AND ' +#13+
' '+DBGrid_custom_table.FieldGrid+' = :GRID_NAME '
);
qy.ParamByName('FORM').AsString := pForm.Name;
qy.ParamByName('USER').AsInteger := FId_user_login;
qy.ParamByName('GRID_NAME').AsString := DBGrid.Name;
qy.Open;
if not qy.IsEmpty then
begin
try
Str:= TStringStream.Create( qy.FieldByName(DBGrid_custom_table.FieldConfig).AsString );
DBGrid.Columns.LoadFromStream(Str);
finally
FreeAndNil(Str);
end;
end;
end;
finally
end;
finally
FreeAndNil(qy);
end;
end;
procedure TDBGrid_custom_connectionFD.loadTable;
var
TempList: TStringList;
begin
try
try
TempList:= TStringList.Create;
FConn.GetTableNames('',DBGrid_custom_table.Table,'',TempList);
if Pos(DBGrid_custom_table.Table,TempList.Text) = 0 then
begin
case Database of
MYSQL:
begin
FConn.ExecSQL
(
' CREATE TABLE '+DBGrid_custom_table.Table+' ( ' + #13 +
' '+DBGrid_custom_table.FieldIdUser+ ' INT(11) NOT NULL, ' + #13 +
' '+DBGrid_custom_table.FieldForm+ ' VARCHAR(255) NOT NULL, ' + #13 +
' '+DBGrid_custom_table.FieldGrid+ ' VARCHAR(255) NOT NULL, ' + #13 +
' '+DBGrid_custom_table.FieldConfig+ ' LONGBLOB NOT NULL, ' + #13 +
' DATAHORA TIMESTAMP NOT NULL); '
);
end;
FIREBIRD:
begin
FConn.ExecSQL(
' CREATE TABLE '+DBGrid_custom_table.Table+' ( ' + #13 +
' '+DBGrid_custom_table.FieldIdUser+ ' INTEGER NOT NULL, ' + #13 +
' '+DBGrid_custom_table.FieldForm+ ' VARCHAR(255) NOT NULL, ' + #13 +
' '+DBGrid_custom_table.FieldGrid+ ' VARCHAR(255) NOT NULL, ' + #13 +
' '+DBGrid_custom_table.FieldConfig+ ' BLOB SUB_TYPE 0 SEGMENT SIZE 10000, ' + #13 +
' DATAHORA TIMESTAMP NOT NULL); '
);
end;
end;
end;
finally
FreeAndNil(TempList);
end;
except
on e: Exception do
begin
Exit;
end;
end;
end;
procedure TDBGrid_custom_connectionFD.reset(pForm: TForm);
var
qy : TFDQuery;
begin
qy := TFDQuery.Create(nil);
qy.Connection := FConn;
try
try
begin
qy.SQL.Clear;
qy.SQL.Add(
' DELETE FROM '+DBGrid_custom_table.Table+' ' +#13+
' WHERE ' +#13+
' '+DBGrid_custom_table.FieldForm+' = :FORM ' +#13+
' AND ' +#13+
' '+DBGrid_custom_table.FieldIdUser+' = :USER ' +#13+
' AND ' +#13+
' '+DBGrid_custom_table.FieldGrid+' = :GRID_NAME '
);
qy.ParamByName('FORM').AsString := pForm.Name;
qy.ParamByName('USER').AsInteger := FId_user_login;
qy.ParamByName('GRID_NAME').AsString := DBGrid.Name;
qy.ExecSQL;
if qy.RowsAffected > 0 then
begin
MessageDlg('Grid resetado, feche a tela e abra novamente.',mtInformation,[mbOK],0);
end;
end;
except
on e: Exception do
begin
MessageDlg('Ops! Houve um erro ao resetar grid' +#13+ E.Message,mtError,[mbok],0);
end;
end;
finally
qy.Free;
end;
end;
procedure TDBGrid_custom_connectionFD.save(pForm: TForm);
var
qy : TFDQuery;
sql:string;
begin
qy := TFDQuery.Create(nil);
qy.Connection := FConn;
try
try
begin
qy.SQL.Clear;
qy.SQL.Add(
' SELECT * FROM '+DBGrid_custom_table.Table+' ' +#13+
' WHERE ' +#13+
' '+DBGrid_custom_table.FieldForm+' = :FORM ' +#13+
' AND ' +#13+
' '+DBGrid_custom_table.FieldIdUser+' = :USER ' +#13+
' AND ' +#13+
' '+DBGrid_custom_table.FieldGrid+' = :GRID_NAME '
);
qy.ParamByName('FORM').AsString := pForm.Name;
qy.ParamByName('USER').AsInteger := FId_user_login;
qy.ParamByName('GRID_NAME').AsString := DBGrid.Name;
qy.Open;
if qy.IsEmpty then
begin
qy.SQL.Clear;
qy.SQL.Add(
'INSERT INTO '+DBGrid_custom_table.Table+' ' +#13+
'('+
DBGrid_custom_table.FieldForm+','+
DBGrid_custom_table.FieldIdUser+','+
DBGrid_custom_table.FieldGrid+','+
DBGrid_custom_table.FieldConfig+','+
'datahora'+
')' + #13 +
'VALUES' + #13 +
'('+#13+
':FORM,'+
':USER,'+
':GRID_NAME,'+
':GRID_CONFIG,' +
'CURRENT_TIMESTAMP'+
')'
);
qy.ParamByName('FORM').AsString := pForm.Name;
qy.ParamByName('USER').AsInteger := FId_user_login;
qy.ParamByName('GRID_NAME').AsString := DBGrid.Name;
qy.ParamByName('GRID_CONFIG').Value := self.setConfig;
qy.ExecSQL;
end
else
begin
qy.SQL.Clear;
qy.SQL.Add(
'UPDATE '+DBGrid_custom_table.Table+' ' +#13+
'SET ' +#13+
' '+DBGrid_custom_table.FieldConfig+' = :GRID_CONFIG, ' +#13+
' DATAHORA = CURRENT_TIMESTAMP ' +#13+
' WHERE ' +#13+
' '+DBGrid_custom_table.FieldForm+' = :FORM ' +#13+
' AND ' +#13+
' '+DBGrid_custom_table.FieldIdUser+' = :USER ' +#13+
' AND ' +#13+
' '+DBGrid_custom_table.FieldGrid+' = :GRID_NAME '
);
qy.ParamByName('FORM').AsString := pForm.Name;
qy.ParamByName('USER').AsInteger := FId_user_login;
qy.ParamByName('GRID_NAME').AsString := DBGrid.Name;
qy.ParamByName('GRID_CONFIG').AsString := self.setConfig;
qy.ExecSQL;
end;
end;
except
on e: Exception do
begin
MessageDlg('Ops! Houve um erro ao gravar grid' +#13+ E.Message,mtError,[mbok],0);
end;
end;
finally
qy.Free;
end;
end;
end.