-
Notifications
You must be signed in to change notification settings - Fork 1
/
WispReportManager.pas
168 lines (142 loc) · 4.86 KB
/
WispReportManager.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
unit WispReportManager;
interface
uses
SysUtils,
DateUtils,
Classes,
Forms,
ZDataset,
ZConnection,
ZAbstractConnection,
WispEntity,
WispArrayTools,
Dialogs,
WispDbStructureTools,
WispDbConnection,
WispQueryTools,
WispStrTools,
WispTimeTools,
frxClass,
frxDesgn,
DB,
DBTables;
type
TReportManager = Class(TObject)
Private
Public
Constructor Create; reintroduce;
Procedure RegisterEntityReportToDb(ParamEntityName: String);
// Read about "reintroduce" in the link below :
// http://docwiki.embarcadero.com/RADStudio/XE8/fr/W1010_La_m%C3%A9thode_'%25s'_cache_la_m%C3%A9thode_virtuelle_du_type_de_base_'%25s'_(Delphi)
Destructor Destroy; override;
End;
var
Global_Singleton_ReportManager: TReportManager;
R: TReportManager; // Shorter version
implementation
// =============================================================================
Constructor TReportManager.Create;
begin
if Global_Singleton_ReportManager <> nil then
begin
// Lets try ...
Self := Global_Singleton_ReportManager;
end
else
begin
inherited Create();
Global_Singleton_ReportManager := Self;
R := Global_Singleton_ReportManager;
with Global_Singleton_ReportManager do
// Database structure : reports
If Not(CheckIfTableExists(Global_Singleton_DbConnection,
'wisp_reports')) Then
begin
ExecuteQuery(Global_Singleton_DbConnection,
'CREATE TABLE ' + 'wisp_reports' + sLineBreak + '(' + sLineBreak +
' ID INT NOT NULL AUTO_INCREMENT,' + sLineBreak + ' REPORT BLOB,' +
sLineBreak + ' NAME VARCHAR(255) NOT NULL,' + sLineBreak +
' TYPE VARCHAR(255) NOT NULL,' + sLineBreak +
' LABEL VARCHAR(255) NOT NULL,' + sLineBreak +
' PRIMARY KEY (`ID`));');
end;
end;
end;
// =============================================================================
Destructor TReportManager.Destroy;
begin
if Global_Singleton_ReportManager = Self then
begin
Global_Singleton_ReportManager := nil;
R := nil;
end;
inherited Destroy;
end;
// =============================================================================
Procedure FreeGlobalObjects; far;
begin
if Global_Singleton_ReportManager <> nil then
begin
Global_Singleton_ReportManager.Free;
end;
end;
// =============================================================================
Procedure TReportManager.RegisterEntityReportToDb(ParamEntityName: String);
var
TmpId: String;
Report: TFrxReport;
Stream: TStream;
FileStream: TFileStream;
TmpQ: TZQuery;
begin
// Create empty report for single entity instance
if Not(OpenQuery(Global_Singleton_DbConnection,
'SELECT * FROM wisp_reports WHERE NAME=''' + ParamEntityName +
''' AND TYPE=''SINGLE'';').RecordsAvailable) then
begin
ExecuteQuery(Global_Singleton_DbConnection,
'INSERT INTO wisp_reports (NAME,TYPE,LABEL) VALUES (''' + ParamEntityName
+ ''',''SINGLE'',''No Label'');');
TmpId := OpenQuery(Global_Singleton_DbConnection,
'SELECT LAST_INSERT_ID();').FirstFieldAsString;
TmpQ := OpenQuery(Global_Singleton_DbConnection,
'SELECT REPORT FROM wisp_reports WHERE ID=' + TmpId + ';').ZQuery;
FileStream := TFileStream.Create(ExtractFilePath(Application.ExeName) +
'Default.fr3', fmShareDenyWrite);
FileStream.Position := 0;
TmpQ.SQL.Clear;
TmpQ.SQL.Text := 'UPDATE wisp_reports SET REPORT=:blobparam WHERE ID=' +
TmpId + ';';
TmpQ.ParamByName('blobparam').LoadFromStream(FileStream, ftBlob);
TmpQ.ExecSQL;
FileStream.Free;
Stream.Free;
end;
// Create empty report for multiple entity instances
if Not(OpenQuery(Global_Singleton_DbConnection,
'SELECT * FROM wisp_reports WHERE NAME=''' + ParamEntityName +
''' AND TYPE=''MULTI'';').RecordsAvailable) then
begin
ExecuteQuery(Global_Singleton_DbConnection,
'INSERT INTO wisp_reports (NAME,TYPE,LABEL) VALUES (''' + ParamEntityName
+ ''',''MULTI'',''No Label'');');
TmpId := OpenQuery(Global_Singleton_DbConnection,
'SELECT LAST_INSERT_ID();').FirstFieldAsString;
TmpQ := OpenQuery(Global_Singleton_DbConnection,
'SELECT REPORT FROM wisp_reports WHERE ID=' + TmpId + ';').ZQuery;
FileStream := TFileStream.Create(ExtractFilePath(Application.ExeName) +
'Default.fr3', fmShareDenyWrite);
FileStream.Position := 0;
TmpQ.SQL.Clear;
TmpQ.SQL.Text := 'UPDATE wisp_reports SET REPORT=:blobparam WHERE ID=' +
TmpId + ';';
TmpQ.ParamByName('blobparam').LoadFromStream(FileStream, ftBlob);
TmpQ.ExecSQL;
FileStream.Free;
Stream.Free;
end;
end;
// =============================================================================
begin
AddExitProc(FreeGlobalObjects);
end.