-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.pas
428 lines (391 loc) · 14.8 KB
/
database.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
unit database;
interface
uses
System.SysUtils, System.Classes, Data.DB, nxdb, nxsrSqlEngineBase,
nxsqlEngine, nxsdServerEngine, nxsrServerEngine, nxllTransport,
nxchCommandHandler, nxllComponent, nxptBasePooledTransport,
nxtwWinsockTransport, nxseAutoComponent,
nxreRemoteServerEngine, settings, StatConst;
type
TDatabase = class(TDataModule)
nxseAllEngines1: TnxseAllEngines;
nxListenTrans: TnxWinsockTransport;
nxLocSrvEng: TnxServerEngine;
nxSqlEng: TnxSqlEngine;
nxTrans: TnxWinsockTransport;
nxSe1: TnxSession;
nxDb1: TnxDatabase;
nxtbAnforderung: TnxTable;
nxtbResult: TnxTable;
nxtbAnforderungPK: TnxTable;
nxtbResObj: TnxTable;
dtsAnforderung: TDataSource;
dtsResult: TDataSource;
dtsAnforderungPK: TDataSource;
dtsResObj: TDataSource;
nxtbAnforderungGUID: TnxStringField;
nxtbAnforderungSTATUS: TnxStringField;
nxtbAnforderungCOMMAND: TnxStringField;
nxtbAnforderungDELFLG: TnxStringField;
nxtbAnforderungARCHIVE: TnxStringField;
nxtbAnforderungSELECTION: TnxStringField;
nxtbAnforderungRESULTHEADER: TnxStringField;
nxtbAnforderungERSTELLTVON: TnxStringField;
nxtbAnforderungERSTELLTAM: TDateTimeField;
nxtbResultANFGUID: TnxStringField;
nxtbResultGUID: TnxStringField;
nxtbResultPOS: TIntegerField;
nxtbResultDOCID: TIntegerField;
nxtbResultRESULTLINE: TnxStringField;
nxtbResultINDEXDATA: TnxStringField;
nxtbResultOBJECTLOADED: TIntegerField;
nxtbAnforderungPKGUID: TnxStringField;
nxtbAnforderungPKSTATUS: TnxStringField;
nxtbAnforderungPKCOMMAND: TnxStringField;
nxtbAnforderungPKDELFLG: TnxStringField;
nxtbAnforderungPKARCHIVE: TnxStringField;
nxtbAnforderungPKSELECTION: TnxStringField;
nxtbAnforderungPKRESULTHEADER: TnxStringField;
nxtbAnforderungPKERSTELLTVON: TnxStringField;
nxtbAnforderungPKERSTELLTAM: TDateTimeField;
nxQuery1: TnxQuery;
nxlocalCommandH: TnxServerCommandHandler;
nxRmtSrvEng: TnxRemoteServerEngine;
nxtbArchivePK: TnxTable;
dtsArchivePK: TDataSource;
nxtbArchivePKAR_GUID: TnxStringField;
nxtbArchivePKAR_ID: TnxStringField;
nxtbArchivePKAR_BEZ: TnxStringField;
nxtbArchivePKAR_DOKPATH: TnxStringField;
nxtbArchivePKAR_LTZLFDNR: TIntegerField;
nxtbArchive: TnxTable;
dtsArchive: TDataSource;
nxtbArchiveID: TnxTable;
dtaArchiveID: TDataSource;
nxtbArchiveIDAR_GUID: TnxStringField;
nxtbArchiveIDAR_ID: TnxStringField;
nxtbArchiveIDAR_BEZ: TnxStringField;
nxtbArchiveIDAR_DOKPATH: TnxStringField;
nxtbArchiveIDAR_LTZLFDNR: TIntegerField;
nxtbArchiveAR_GUID: TnxStringField;
nxtbArchiveAR_ID: TnxStringField;
nxtbArchiveAR_BEZ: TnxStringField;
nxtbArchiveAR_DOKPATH: TnxStringField;
nxtbArchiveAR_LTZLFDNR: TIntegerField;
nxtbFields: TnxTable;
dtsFields: TDataSource;
nxtbFieldsPK: TnxTable;
dtsFieldPK: TDataSource;
nxtbResObjANFGUID: TnxStringField;
nxtbResObjRESULTGUID: TnxStringField;
nxtbResObjGUID: TnxStringField;
nxtbResObjOBJEKTNAME: TnxStringField;
nxtbResObjOBJEKT: TBlobField;
dtsFieldsName: TDataSource;
nxtbFieldsName: TnxTable;
nxtbFieldsCABINETID: TnxStringField;
nxtbFieldsINDEXNR: TIntegerField;
nxtbFieldsFIELDNAME: TnxStringField;
nxtbFieldsFIELDALIAS: TnxStringField;
nxtbFieldsFIELDTYPE: TnxStringField;
nxtbFieldsSystemField: TBooleanField;
nxtbFieldsPKCABINETID: TnxStringField;
nxtbFieldsPKINDEXNR: TIntegerField;
nxtbFieldsPKFIELDNAME: TnxStringField;
nxtbFieldsPKFIELDALIAS: TnxStringField;
nxtbFieldsPKFIELDTYPE: TnxStringField;
nxtbFieldsPKSystemField: TBooleanField;
nxtbFieldsNameCABINETID: TnxStringField;
nxtbFieldsNameINDEXNR: TIntegerField;
nxtbFieldsNameFIELDNAME: TnxStringField;
nxtbFieldsNameFIELDALIAS: TnxStringField;
nxtbFieldsNameFIELDTYPE: TnxStringField;
nxtbFieldsNameSystemField: TBooleanField;
procedure nxtbAnforderungAfterInsert(DataSet: TDataSet);
procedure nxtbAnforderungBeforeDelete(DataSet: TDataSet);
procedure nxtbResultAfterInsert(DataSet: TDataSet);
procedure nxtbResultBeforeDelete(DataSet: TDataSet);
procedure nxtbResObjAfterInsert(DataSet: TDataSet);
procedure nxtbAnforderungPKAfterInsert(DataSet: TDataSet);
procedure ne(DataSet: TDataSet);
private
function GetNewGuid(): string;
property NewGuid: string read GetNewGuid;
procedure openTables();
public
AllDatasources: TArray<TDataSource>;
Constructor Create(pOwner: TComponent); override;
Destructor Destroy(); override;
procedure OpenRemoteDB(Settings: TSettings);
procedure CloseRemoteDB();
procedure OpenLocalDB(Settings: TSettings);
procedure CloseLocalDB();
end;
var
Fdatabase: TDatabase;
implementation
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
Constructor Tdatabase.Create(pOwner: TComponent);
begin
inherited Create(pOwner);
self.AllDatasources := [dtsAnforderung, dtsResult, dtsAnforderungPK, dtsResObj, dtsArchivePK, dtsArchive, dtsFields, dtsFieldPK, dtsFieldsName];
end;
Destructor Tdatabase.Destroy();
begin
nxTrans.Close();
nxtbResObj.Close();
nxtbResult.Close();
nxtbAnforderung.Close();
nxtbAnforderungPK.Close();
inherited Destroy();
end;
procedure Tdatabase.OpenRemoteDB(Settings: TSettings);
begin
// setup Session
nxSe1.ServerEngine := nxRmtSrvEng;
// setup Transport
nxTrans.ServerName := Settings.RemoteDatabaseIp;
nxTrans.Port := StrToInt(Settings.RemoteDatabasePort);
// open Transport
nxTrans.Open();
// setup Session
nxSe1.ServerEngine := nxRmtSrvEng;
nxSe1.UserName := Settings.RemoteDatabaseUser;
nxSe1.Password := Settings.RemoteDatabasePassword;
nxSe1.Timeout := StrToInt(Settings.RemoteDatabaseTimeout);
// open Session
nxSe1.Open();
// check if Session can see Database
if not nxSe1.IsAlias(Settings.RemoteDatabaseAlias) then
nxSe1.AddAlias(Settings.RemoteDatabaseAlias, ChangeFileExt(ParamStr(0), '/'), true);
// setup Database
nxDb1.Session := nxSe1;
nxDb1.AliasName := Settings.RemoteDatabaseAlias;
// open Database
nxdb1.Open();
openTables();
end;
procedure Tdatabase.openTables;
begin
// open Tables
if not nxtbAnforderung.Exists then
begin
// nxtbAnforderung.CreateTable();
nxQuery1.SQL.Text := ''
+ 'create table "ANFORDERUNG"'
+ '("GUID" VARCHAR(38),'
+ '"STATUS" VARCHAR(15),"COMMAND" VARCHAR(15),'
+ '"DELFLG" VARCHAR(1),"ARCHIVE" VARCHAR(255),'
+ '"SELECTION" VARCHAR(255),"RESULTHEADER" VARCHAR(1024),'
+ '"ERSTELLTVON" VARCHAR(30),"ERSTELLTAM" DATETIME);'
// + 'Alter table "ANFORDERUNG" SET DESCRIPTION "Anforderungen"; '
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "GUID" SET DESCRIPTION ''k'';'
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "STATUS" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "COMMAND" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "DELFLG" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "ARCHIVE" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "SELECTION" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "RESULTHEADER" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "ERSTELLTVON" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ANFORDERUNG" ALTER COLUMN "ERSTELLTAM" SET DESCRIPTION ''h'';'
+ 'create unique index "ANFORDERUNG_INDSTAT" on "ANFORDERUNG" ("STATUS" NULLS FIRST ,"GUID" NULLS FIRST);'
+ 'create unique index "ANFORDERUNG_PK" on "ANFORDERUNG" ("GUID" NULLS FIRST);';
nxQuery1.ExecSQL();
end;
nxtbAnforderung.Open();
nxtbAnforderungPK.Open();
if not nxtbResult.Exists then
begin
// nxtbResult.CreateTable();
nxQuery1.SQL.Text := ''
+ 'create table "RESULT"('
+ '"ANFGUID" VARCHAR(38),"GUID" VARCHAR(38),'
+ '"POS" INTEGER,"DOCID" INTEGER,'
+ '"RESULTLINE" VARCHAR(2024),"INDEXDATA" VARCHAR(2024),'
+ '"OBJECTLOADED" INTEGER);'
// + 'Alter table "RESULT" SET DESCRIPTION ''Results'';'
+ 'ALTER TABLE "RESULT" ALTER COLUMN "ANFGUID" SET DESCRIPTION ''o ==>ANFORDERUNG.GUID'';'
+ 'ALTER TABLE "RESULT" ALTER COLUMN "GUID" SET DESCRIPTION ''k'';'
+ 'ALTER TABLE "RESULT" ALTER COLUMN "POS" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "RESULT" ALTER COLUMN "DOCID" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "RESULT" ALTER COLUMN "RESULTLINE" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "RESULT" ALTER COLUMN "INDEXDATA" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "RESULT" ALTER COLUMN "OBJECTLOADED" SET DESCRIPTION ''h'';'
+ 'create unique index "RESULT_PK" on "RESULT" ("ANFGUID" NULLS FIRST,"GUID" NULLS FIRST);';
nxQuery1.ExecSQL;
end;
nxtbResult.Open();
if not nxtbResObj.Exists then
begin
// nxtbResObj.CreateTable();
nxQuery1.SQL.Text := ''
+ 'create table "RESULTOBJEKT"('
+ '"ANFGUID" VARCHAR(38),"RESULTGUID" VARCHAR(38),'
+ '"GUID" VARCHAR(38),"OBJEKTNAME" VARCHAR(255),'
+ '"OBJEKT" BLOB);'
// + 'Alter table "RESULTOBJEKT" SET DESCRIPTION "Resultobjekte"; '
+ 'ALTER TABLE "RESULTOBJEKT" ALTER COLUMN "ANFGUID" SET DESCRIPTION ''o ==>RESULT.ANFGUID'';'
+ 'ALTER TABLE "RESULTOBJEKT" ALTER COLUMN "RESULTGUID" SET DESCRIPTION ''o ==>RESULT.GUID'';'
+ 'ALTER TABLE "RESULTOBJEKT" ALTER COLUMN "GUID" SET DESCRIPTION ''k'';'
+ 'ALTER TABLE "RESULTOBJEKT" ALTER COLUMN "OBJEKTNAME" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "RESULTOBJEKT" ALTER COLUMN "OBJEKT" SET DESCRIPTION ''h'';'
+ 'create unique index "RESULTOBJEKT_PK" on "RESULTOBJEKT" ("ANFGUID" NULLS FIRST,"RESULTGUID" NULLS FIRST,"GUID" NULLS FIRST);';
nxQuery1.ExecSQL;
end;
nxtbResObj.Open();
if not nxtbArchivePK.Exists then
begin
nxQuery1.SQL.Text := ''
+ 'create table "ARCHIVE"('
+ '"AR_GUID" VARCHAR(38),"AR_ID" VARCHAR(35),'
+ '"AR_BEZ" VARCHAR(255),"AR_DOKPATH" VARCHAR(255), '
+ '"AR_LTZLFDNR" INTEGER);'
// + 'Alter table "ARCHIVE" SET DESCRIPTION '';'
+ 'ALTER TABLE "ARCHIVE" ALTER COLUMN "AR_GUID" SET DESCRIPTION ''k'';'
+ 'ALTER TABLE "ARCHIVE" ALTER COLUMN "AR_ID" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ARCHIVE" ALTER COLUMN "AR_BEZ" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ARCHIVE" ALTER COLUMN "AR_DOKPATH" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "ARCHIVE" ALTER COLUMN "AR_LTZLFDNR" SET DESCRIPTION ''h'';'
+ 'create unique index "ARCHIVE_INDID" on "ARCHIVE"("AR_ID" NULLS FIRST);'
+ 'create unique index "ARCHIVE_PK" on "ARCHIVE"("AR_GUID" NULLS FIRST);';
nxQuery1.ExecSQL;
end;
nxtbArchivePK.Open();
nxtbArchive.Open();
nxtbArchiveID.Open();
if not nxtbFields.Exists then
begin
nxQuery1.SQL.Text := ''
+ 'create table "FIELDS"('
+ '"CABINETID" VARCHAR(36),"INDEXNR" INTEGER,"FIELDNAME" VARCHAR(255),'
+ '"FIELDALIAS" VARCHAR(255), "FIELDTYPE" VARCHAR(64),'
+ '"SystemField" BOOLEAN);'
// + 'Alter table "FIELDS" SET DESCRIPTION '';'
+ 'ALTER TABLE "FIELDS" ALTER COLUMN "CABINETID" SET DESCRIPTION ''k'';'
+ 'ALTER TABLE "FIELDS" ALTER COLUMN "INDEXNR" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "FIELDS" ALTER COLUMN "FIELDNAME" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "FIELDS" ALTER COLUMN "FIELDALIAS" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "FIELDS" ALTER COLUMN "FIELDTYPE" SET DESCRIPTION ''h'';'
+ 'ALTER TABLE "FIELDS" ALTER COLUMN "FIELDTYPE" SET DESCRIPTION ''h'';'
+ 'create unique index "FIELDS_PK" on "FIELDS"("CABINETID" NULLS FIRST,"INDEXNR" NULLS FIRST);'
+ 'create unique index "FIELDS_NAME" on "FIELDS"("CABINETID" NULLS FIRST,"FIELDNAME" NULLS FIRST);';
nxQuery1.ExecSQL;
end;
nxtbFields.Open();
nxtbFieldsPK.Open();
nxtbFieldsName.Open();
end;
procedure Tdatabase.CloseRemoteDB();
begin
// close Transport
nxTrans.Close();
// close Engine
nxRmtSrvEng.Close();
end;
procedure Tdatabase.OpenLocalDB(Settings: TSettings);
//var
//add: TStrings;
//indexNames: TStrings;
begin
// setup Engine
nxLocSrvEng.TempStorePath := Settings.LocalDatabaseTempPath;
// setup Session
nxSe1.ServerEngine := nxLocSrvEng;
// setup Transport
nxListenTrans.ListenAddresses.Add(Settings.LocalDatabaseListenIp);
nxListenTrans.Port := StrToInt(Settings.LocalDatabaseListenPort);
nxListenTrans.ServerName := Settings.LocalDatabaseListenIp;
// open sql Engine
nxSQLEng.Open();
// open Engine
nxLocSrvEng.Open();
// open Transport
nxListenTrans.Open();
// open Session
nxSe1.UserName := Settings.LocalDatabaseUser;
nxSe1.Password := Settings.LocalDatabasePassword;
nxSe1.Open();
// check if Session can see Database
if not nxSe1.IsAlias(Settings.RemoteDatabaseAlias) then
nxSe1.AddAlias(Settings.LocalDatabaseAlias, Settings.LocalDatabasePath, true);
// setup Database
nxDb1.AliasName := Settings.LocalDatabaseAlias;
nxDb1.AliasPath := Settings.LocalDatabasePath;
// open Database
nxDb1.Open();
// init indexNames
//indexNames := TStrings.Create;
openTables();
end;
procedure Tdatabase.CloseLocalDB();
begin
// close Engine
nxLocSrvEng.Close();
// close Transport
nxListenTrans.Close();
// close command handler
nxlocalCommandH.Close();
// close sql Engine
nxSQLEng.Close();
end;
// Guid Generator
function Tdatabase.GetNewGuid() : string;
var
yGuid : TGUID;
yResult: HRESULT;
begin
yResult := CreateGUID(yGuid);
if yResult=S_OK
then begin
result := GuidToString(yGuid);
end
else begin
result := GuidToString(yGuid);
end; //if
end; //GetNewGuid
// Table specific procedures
procedure Tdatabase.nxtbAnforderungAfterInsert(DataSet: TDataSet);
begin
nxtbAnforderungGUID.AsString := NewGuid; // NewGuid
nxtbAnforderungERSTELLTVON.AsString := '';
nxtbAnforderungERSTELLTAM.AsDateTime := Date() + Time();
nxtbAnforderungSTATUS.AsString := C_STATANF; // C_STATANF
end;
procedure Tdatabase.nxtbAnforderungBeforeDelete(DataSet: TDataSet);
begin
raise Exception.Create('Deleting not allowed here. DataSet: ' + DataSet.GetNamePath );
end;
procedure Tdatabase.nxtbAnforderungPKAfterInsert(DataSet: TDataSet);
begin
nxtbAnforderungPKGUID.AsString := NewGuid; // NewGuid
nxtbAnforderungPKERSTELLTVON.AsString := '';
nxtbAnforderungPKERSTELLTAM.AsDateTime := Date() + Time();
nxtbAnforderungPKSTATUS.AsString := C_STATANF; // C_StatAnf
end;
procedure Tdatabase.ne(DataSet: TDataSet);
begin
nxtbResult.First();
while not nxtbResult.Eof do
nxtbResult.Delete();
end;
procedure Tdatabase.nxtbResObjAfterInsert(DataSet: TDataSet);
begin
nxtbResObjANFGUID.AsString := nxtbResultANFGUID.AsString;
nxtbResObjRESULTGUID.AsString := nxtbResultGUID.AsString;
nxtbResObjGUID.AsString := NewGuid; // NewGuid
end;
procedure Tdatabase.nxtbResultAfterInsert(DataSet: TDataSet);
begin
nxtbResultANFGUID.AsString := nxtbAnforderungPKGUID.AsString;
nxtbResultGUID.AsString := NewGuid; // NewGuid
nxtbResultPOS.AsInteger := 0;
nxtbResultDOCID.AsInteger := -1;
end;
procedure Tdatabase.nxtbResultBeforeDelete(DataSet: TDataSet);
begin
nxtbResObj.First();
while not nxtbResObj.Eof do
nxtbResObj.Delete();
end;
end.