-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitDataModuleDatas.pas
362 lines (327 loc) · 11.2 KB
/
UnitDataModuleDatas.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
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
// ********************************************
// * Unité ModuleDatas *
// * @Inrae 2020 *
// * by mario Adam [email protected] *
// ********************************************
unit UnitDataModuleDatas;
interface
uses
SysUtils, Classes, ZAbstractConnection, ZConnection, DB, DBTables, strUtils, ZAbstractRODataset, ZDataset, ZAbstractDataset, ZAbstractTable, UnitVariables, Forms;
type
TDataModuleDatas = class(TDataModule)
connectionPostgres: TZConnection;
ReadOnlySql: TZReadOnlyQuery;
ZTableLogs: TZTable;
connectionAdmin: TZConnection;
Session1: TSession;
ReadOnlyAdminSql: TZReadOnlyQuery;
procedure launchAndShowResultSql(script : string; titre : string);
procedure launchSql(script : string);
procedure launchAdminSql(script : string);
function isConnected: Boolean;
function isAdminConnected: Boolean;
function testDatabase(DB: TZConnection; tHostname: string; tPort: string; tUser: string; tPassword: string; tDatabase: string) : Boolean;
procedure paramDatabase(DB: TZConnection;tHostname: string; tPort: string; tUser: string; tPassword: string; tDatabase: string);
procedure DataModuleDestroy(Sender: TObject);
function DeconnectAllUsers: Boolean;
function DecodePWDEx(Data, SecurityString: string): string;
procedure connectionPostgresBeforeConnect(Sender: TObject);
function ouvreTable(Fichier: string; DbTable: TTable): Boolean;
procedure DataModuleCreate(Sender: TObject);
function IsProcedureExist(entry: string): boolean;
function IsSchemaExist(entry: string): boolean;
procedure GetAllProcedure(lst: TStrings);
private
{ Private declarations }
procedure LogLine(Indent: Integer; AMessage: string);
public
{ Public declarations }
end;
var
DataModuleDatas: TDataModuleDatas;
implementation
uses UnitExport, UnitGridResult;
{$R *.dfm}
procedure TDataModuleDatas.GetAllProcedure(lst: TStrings);
Begin
If isAdminConnected
Then Begin
launchAdminSql(format('SELECT proname FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_proc p ON pronamespace = n.oid WHERE nspname = %s;', [quotedStr('public')]));
While Not ReadOnlyAdminSql.eof
Do Begin
lst.add(ReadOnlyAdminSql.FieldByname('proname').AsString);
ReadOnlyAdminSql.Next;
End;
End;
End;
function TDataModuleDatas.IsProcedureExist(entry: string): boolean;
Begin
If isAdminConnected
Then Begin
launchAdminSql(format('SELECT proname as result FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_proc p ON pronamespace = n.oid WHERE nspname = %s and proname = %s;', [quotedStr('public'), quotedStr(entry)]));
Result := (ReadOnlyAdminSql.FieldByname('result').AsString = entry);
End Else Result := False;
If Result
Then LogLine(1, format('La procedure %s existe', [entry]))
Else LogLine(1, format('Procedure %s non trouvé', [entry]));
End;
function TDataModuleDatas.IsSchemaExist(entry: string): boolean;
Begin
If isAdminConnected
Then Begin
launchAdminSql(format('select count(*) AS result from information_schema.schemata where catalog_name = %s and schema_name=%s and schema_owner <> %s', [quotedStr(connectionPostgres.Database), quotedStr(entry), quotedStr('postgres')]));
Result := (ReadOnlyAdminSql.FieldByname('result').AsInteger = 1);
End Else Result := False;
If Result
Then LogLine(1, format('La schema %s existe', [entry]))
Else LogLine(1, format('Schema %s non trouvé', [entry]));
End;
function TDataModuleDatas.ouvreTable(Fichier: string; DbTable: TTable): Boolean;
Var
Err: boolean;
Begin
result := False;
With DbTable Do
Begin
filtered := false;
filter := '';
If (TableName = fichier) AND Active Then
LogLine(2, format(_HYDRAS_TABLE_ALREADY_OPEN, [Fichier]))
Else
Begin
LogLine(1, format(_HYDRAS_TABLE_OPEN, [Fichier]));
Active := false;
Err := false;
If fileExists(fichier) Then
Begin
TableName := fichier;
try
Active := true;
Except
On E: Exception Do
Begin
LogLine(2 , _EXCEPTION + E.Message);
LogLine(2 , _HYDRAS_DELETE_LOCK) ;
DeleteFiles(ExtractFilePath(fichier), '.lck');
try
Active := true;
Except
On E: Exception Do
Begin
LogLine(2 , _EXCEPTION + E.Message);
Err := true;
End;
End;
End;
End;
If Err
Then LogLine(-1, _HYDRAS_OPEN_ERROR);
End Else
Begin
LogLine(-1, format(_FILE_NOT_EXIST, [Fichier]));
exit;
End;
End;
result := Active;
End;
End;
function TDataModuleDatas.DecodePWDEx(Data, SecurityString: string): string;
Var
i, x, x2: integer;
s1, s2, ss: string;
Begin
Result := #1;
If Length(SecurityString) < 16 Then
Exit;
For i := 1 To Length(SecurityString) Do
Begin
s1 := Copy(SecurityString, i + 1,Length(securitystring));
If Pos(SecurityString[i], s1) > 0 Then
Exit;
If Pos(SecurityString[i], _CODE64) <= 0 Then
Exit;
End;
s1 := _CODE64;
s2 := '';
ss := securitystring;
For i := 1 to Length(Data) Do
If Pos(Data[i], ss) > 0 Then s2 := s2 + Data[i];
Data := s2;
s2 := '';
If Length(Data) mod 2 <> 0 Then
Exit;
For i := 0 To Length(Data) div 2 - 1 Do
Begin
x := Pos(Data[i * 2 + 1], ss) - 1;
If x < 0 Then
Exit;
ss := Copy(ss, Length(ss), 1) + Copy(ss, 1,Length(ss) - 1);
x2 := Pos(Data[i * 2 + 2], ss) - 1;
If x2 < 0 Then
Exit;
x := x + x2 * 16;
s2 := s2 + chr(x);
ss := Copy(ss, Length(ss), 1) + Copy(ss, 1,Length(ss) - 1);
End;
Result := s2;
end;
procedure TDataModuleDatas.LogLine(Indent: Integer; AMessage: string);
Begin
Export.LogLine(Indent, AMessage);
end;
procedure TDataModuleDatas.paramDatabase(DB: TZConnection; tHostname: string; tPort: string; tUser: string; tPassword: string; tDatabase: string);
Begin
With DB Do
Begin
Disconnect;
Tag := 0;
If (tHostname = '') OR (tPort = '') OR (tUser = '') OR (tPassword = '') OR (tDatabase = '') Then exit;
HostName := tHostname;
Port := strToInt(tPort);
User := tUser;
Password := DecodePWDEx(tPassword, _PWDEx);
if DEBUG Then LogLine(1, 'Password : ' + Password);
Database := tDatabase;
Tag := 1;
End;
End;
function TDataModuleDatas.testDatabase(DB: TZConnection; tHostname: string; tPort: string; tUser: string; tPassword: string; tDatabase: string) : Boolean;
Begin
result := False;
With DB Do
Begin
If DEBUG Then LogLine(1, _POSTGRES_CONNECTION_TEST);
paramDatabase(DB, tHostname, tPort, tUser, tPassword, tDatabase);
Try
Connect;
Except
On E: Exception Do
LogLine(2 , _EXCEPTION + E.Message);
End;
result := Connected;
Disconnect;
End;
End;
function TDataModuleDatas.DeconnectAllUsers: Boolean;
Begin
result := false;
LogLine(2, _POSTGRES_DATABASE_KILL_USER);
If isAdminConnected Then
Begin
connectionPostgres.Disconnect;
Try
connectionAdmin.ExecuteDirect('SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = ' + quotedStr(connectionPostgres.Database) + ';');
Except
On E: Exception Do
LogLine(2 , _EXCEPTION + E.Message);
End;
LogLine(3, _POSTGRES_DATABASE_KILL_USER_OK );
result := true;
End Else LogLine(3, _POSTGRES_DATABASE_ERROR_LOG_ADMIN );
end;
procedure TDataModuleDatas.launchSql(script : string);
begin
With connectionPostgres Do
Begin
If isConnected Then
Begin
ReadOnlySql.SQL.Text := script;
Try
ReadOnlySql.Active := true;
Except
On E: Exception Do
LogLine(2 , _EXCEPTION + E.Message);
End;
End;
End;
End;
procedure TDataModuleDatas.launchAdminSql(script : string);
begin
With connectionAdmin Do
Begin
If isConnected Then
Begin
ReadOnlyAdminSql.SQL.Text := script;
Try
ReadOnlyAdminSql.Active := true;
Except
On E: Exception Do
LogLine(2 , _EXCEPTION + E.Message);
End;
End;
End;
End;
procedure TDataModuleDatas.launchAndShowResultSql(script : string; titre : string);
begin
With connectionPostgres Do
Begin
If isConnected Then
Begin
ReadOnlySql.SQL.Text := script;
Try
ReadOnlySql.Active := true;
Except
On E: Exception Do
LogLine(2 , _EXCEPTION + E.Message);
End;
FormResultGrid.caption := titre;
FormResultGrid.ShowModal;
End;
End;
End;
procedure TDataModuleDatas.DataModuleDestroy(Sender: TObject);
begin
connectionPostgres.Disconnect;
end;
function TDataModuleDatas.isConnected: Boolean;
Begin
Result := false;
With connectionPostgres Do
Begin
If Not Connected Then
Begin
If Tag = 0 Then exit;
try
Connect;
Except
On E: Exception Do
LogLine(-1 , _EXCEPTION + E.Message);
End;
If Connected
Then LogLine(1, Format(_POSTGRES_CONNECTION_RESULT, [User, 'OK']))
Else LogLine(-1, Format(_POSTGRES_CONNECTION_RESULT, [User, 'non valide']));
End Else LogLine(1, Format(_POSTGRES_CONNECTION_ALREADY, [User]));
Result := Connected;
End;
End;
function TDataModuleDatas.isAdminConnected: Boolean;
Begin
Result := false;
With connectionAdmin Do
Begin
If Not Connected Then
Begin
If Tag = 0 Then exit;
try
Connect;
Except
On E: Exception Do
LogLine(-1 , _EXCEPTION + E.Message);
End;
If Connected
Then LogLine(1, Format(_POSTGRES_CONNECTION_RESULT_ADMIN, ['OK']))
Else LogLine(-1, Format(_POSTGRES_CONNECTION_RESULT_ADMIN, ['non valide']));
End Else LogLine(1, Format(_POSTGRES_CONNECTION_ALREADY, ['Admin']));
Result := Connected;
End;
End;
procedure TDataModuleDatas.connectionPostgresBeforeConnect(Sender: TObject);
begin
with (Sender as TZConnection) Do
if DEBUG Then LogLine(0, Format(_POSTGRES_CONNECTION_LOG, [Hostname, Port, User, Password, Database]));
end;
procedure TDataModuleDatas.DataModuleCreate(Sender: TObject);
begin
Session.NetFileDir := ExtractFilePath(Application.ExeName);
end;
end.