-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_html_table_pkg.pkb
334 lines (313 loc) · 12.4 KB
/
app_html_table_pkg.pkb
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
CREATE OR REPLACE PACKAGE BODY app_html_table_pkg AS
/*
MIT License
Copyright (c) 2022 Lee Lindley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
FUNCTION cursor2html(
p_src SYS_REFCURSOR
,p_right_align_col_list VARCHAR2 := NULL -- comma separated integers in string
,p_caption VARCHAR2 := NULL
,p_css_scoped_style VARCHAR2 := NULL
,p_older_css_support VARCHAR2 := NULL
-- 'G' means nuclear option for gmail, 'Y' means your css cannot be too modern and we need to work harder
-- like for Outlook clients.
,p_odd_line_bg_color VARCHAR2 := NULL -- header row is 1
,p_even_line_bg_color VARCHAR2 := NULL
)
RETURN CLOB
IS
c_valid_re CONSTANT VARCHAR2(32) := '^(\s*\d+\s*(,|$))+$';
c_split_re CONSTANT VARCHAR2(32) := '\s*(\d+)\s*(,|$)';
v_context DBMS_XMLGEN.CTXHANDLE;
v_col VARCHAR2(4);
v_clob CLOB;
v_css_style CLOB := NVL(p_css_scoped_style,
q'!table {
border: 1px solid black;
border-spacing: 0;
border-collapse: collapse;
}
caption {
font-style: italic;
font-size: larger;
margin-bottom: 0.5em;
}
th {
text-align:left;
}
th, td {
border: 1px solid black;
padding:4px 6px;
}
!')
|| CASE WHEN p_older_css_support IN ('Y','y') THEN '
td.right { text-align:right; }
td.left { text-align:left; }
'
||'tr.odd {'
||CASE WHEN p_odd_line_bg_color IS NOT NULL
THEN ' background-color: '||p_odd_line_bg_color
END
||' }
tr.even {'
||CASE WHEN p_even_line_bg_color IS NOT NULL
THEN ' background-color: '||p_even_line_bg_color
END
||' }
'
END
;
v_xsl CLOB ;
c_xsl_default CONSTANT VARCHAR2(1024) := q'!<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<tr>
<xsl:for-each select="/ROWSET/ROW[1]/*">
<th><xsl:value-of select="name()"/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="/ROWSET/*">
<tr>
<xsl:for-each select="./*">
<td><xsl:value-of select="text()"/> </td>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>!';
invalid_arguments EXCEPTION;
PRAGMA exception_init(invalid_arguments, -20881);
e_null_object_ref EXCEPTION;
PRAGMA exception_init(e_null_object_ref, -30625);
BEGIN
-- case of good CSS support and we have alternating row color requirement
IF NVL(p_older_css_support,'x') NOT IN ('Y','y', 'G','g') THEN
IF p_even_line_bg_color IS NOT NULL THEN
v_css_style := v_css_style||'tr:nth-child(even) { background-color: '||p_even_line_bg_color||' }
';
END IF;
IF p_odd_line_bg_color IS NOT NULL THEN
v_css_style := v_css_style||'tr:nth-child(odd) { background-color: '||p_odd_line_bg_color||' }
';
END IF;
END IF;
-- We separate out table from the rest of the body with a div and embed a scoped style for it
v_clob := q'!<div id="plsql-table">
<style type="text/css" scoped>
!'
||v_css_style
;
-- case of needing to put code into the XSLT transformation
IF p_right_align_col_list IS NOT NULL
OR (p_older_css_support IN ('G','g')
AND (p_even_line_bg_color IS NOT NULL OR p_odd_line_bg_color IS NOT NULL)
)
THEN
IF p_right_align_col_list IS NOT NULL AND NOT REGEXP_LIKE(p_right_align_col_list, c_valid_re) THEN
raise_application_error(-20881, 'p_right_align_col_list invalid. Does not match '||c_valid_re);
END IF;
IF p_older_css_support IN ('Y','y') THEN -- the midlevel case. Some CSS style support
-- the mod 2 = 0 goes odd thing is because it does not count the header row.
-- we want to match what the other code branch does
v_xsl := q'!<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<tr>
<xsl:for-each select="/ROWSET/ROW[1]/*">
<th><xsl:value-of select="name()"/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="/ROWSET/*">!';
IF p_right_align_col_list IS NOT NULL THEN
v_xsl := v_xsl||q'!
<xsl:variable name="eoclass">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">odd</xsl:when>
<xsl:otherwise>even</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr class="{$eoclass}">
<xsl:for-each select="./*">
<xsl:variable name="rightleft">
<xsl:choose>!';
FOR i IN 1..LENGTH(p_right_align_col_list) -- will be less than this
LOOP
v_col := REGEXP_SUBSTR(p_right_align_col_list, c_split_re, 1, i, '', 1);
EXIT WHEN v_col IS NULL;
v_xsl := v_xsl||'
<xsl:when test="position() = '||LTRIM(v_col,'0')||'">right</xsl:when>';
END LOOP;
v_xsl := v_xsl||q'!
<xsl:otherwise>left</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<td class="{$rightleft}"><xsl:value-of select="text()"/></td>!';
ELSE
v_xsl := v_xsl||q'!
<td><xsl:value-of select="text()"/></td>!';
END IF;
v_xsl := v_xsl||q'!
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>!';
--DBMS_OUTPUT.put_line(v_xsl);
ELSIF p_older_css_support IN ('G','g') THEN -- nuclear option. everything in the HTML
-- the mod 2 = 0 goes odd thing is because it does not count the header row.
-- we want to match what the other code branch does
v_xsl := q'!<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<tr>
<xsl:for-each select="/ROWSET/ROW[1]/*">
<th><xsl:value-of select="name()"/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="/ROWSET/*">!'
;
IF p_even_line_bg_color IS NOT NULL OR p_odd_line_bg_color IS NOT NULL THEN
v_xsl := v_xsl||q'!
<xsl:variable name="backgroundcolor">
<xsl:choose>!';
IF p_odd_line_bg_color IS NOT NULL THEN
v_xsl := v_xsl||q'!
<xsl:when test="position() mod 2 = 0">!'||p_odd_line_bg_color||q'!</xsl:when>!'
;
END IF;
IF p_even_line_bg_color IS NOT NULL THEN
v_xsl := v_xsl||q'!
<xsl:when test="position() mod 2 = 1">!'||p_even_line_bg_color||q'!</xsl:when>!'
;
END IF;
v_xsl := v_xsl||q'!
</xsl:choose>
</xsl:variable>
<tr style="background-color: {$backgroundcolor};">!'
;
ELSE
v_xsl := v_xsl||q'!
<tr>!';
END IF;
v_xsl := v_xsl||q'!
<xsl:for-each select="./*">!';
IF p_right_align_col_list IS NOT NULL THEN
v_xsl := v_xsl||q'!
<xsl:variable name="rightleft">
<xsl:choose>!';
FOR i IN 1..LENGTH(p_right_align_col_list) -- will be less than this
LOOP
v_col := REGEXP_SUBSTR(p_right_align_col_list, c_split_re, 1, i, '', 1);
EXIT WHEN v_col IS NULL;
v_xsl := v_xsl||'
<xsl:when test="position() = '||LTRIM(v_col,'0')||'">right</xsl:when>';
END LOOP;
v_xsl := v_xsl||q'!
<xsl:otherwise>left</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<td style="text-align: {$rightleft};"><xsl:value-of select="text()"/></td>!';
ELSE
v_xsl := v_xsl||q'!
<td><xsl:value-of select="text()"/></td>!';
END IF;
v_xsl := v_xsl||q'!
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>!';
--DBMS_OUTPUT.put_line(v_xsl);
ELSE -- modern CSS
-- we can put the logic in the css style for the browser
v_xsl := c_xsl_default;
FOR i IN 1..LENGTH(p_right_align_col_list) -- will be less than this
LOOP
v_col := REGEXP_SUBSTR(p_right_align_col_list, c_split_re, 1, i, '', 1);
EXIT WHEN v_col IS NULL;
-- just in case, we trim leading zeros
v_clob := v_clob||'tr > td:nth-of-type('||LTRIM(v_col, '0')||') {
text-align:right;
}
';
END LOOP;
END IF;
ELSE
v_xsl := c_xsl_default;
END IF;
-- end our local style and start the html table section
v_clob := v_clob||'</style>
<table>
';
IF p_caption IS NOT NULL THEN
v_clob := v_clob||'<caption>'||DBMS_XMLGEN.CONVERT(p_caption)||'</caption>
';
END IF;
--DBMS_OUTPUT.put_line('v_xsl:'||v_xsl);
--DBMS_OUTPUT.put_line('v_clob:'||v_clob);
v_context := DBMS_XMLGEN.newcontext(p_src);
DBMS_XMLGEN.setNullHandling(v_context,1);
BEGIN
v_clob := v_clob||REPLACE( -- replace munged spaces in column headers
DBMS_XMLGEN.GETXMLType(v_context, DBMS_XMLGEN.NONE).transform(XMLType(v_xsl)).getClobVal()
,'_x0020_', ' '
);
-- end the table and our div that included the local style
v_clob := v_clob||'</table></div>';
EXCEPTION WHEN e_null_object_ref THEN
v_clob := NULL;
DBMS_OUTPUT.put_line('cursor2html executed cursor that returned no rows. Returning NULL');
END;
RETURN v_clob;
END cursor2html;
FUNCTION query2html(
p_sql CLOB
,p_right_align_col_list VARCHAR2 := NULL -- comma separated integers in string
,p_caption VARCHAR2 := NULL
,p_css_scoped_style VARCHAR2 := NULL
,p_older_css_support VARCHAR2 := NULL -- 'Y' 'G' or null/'N'
,p_odd_line_bg_color VARCHAR2 := NULL
,p_even_line_bg_color VARCHAR2 := NULL
)
RETURN CLOB
IS
v_src SYS_REFCURSOR;
v_clob CLOB;
BEGIN
OPEN v_src FOR p_sql;
v_clob := cursor2html(
p_src => v_src
,p_right_align_col_list => p_right_align_col_list
,p_caption => p_caption
,p_css_scoped_style => p_css_scoped_style
,p_older_css_support => p_older_css_support
,p_odd_line_bg_color => p_odd_line_bg_color
,p_even_line_bg_color => p_even_line_bg_color
);
BEGIN
CLOSE v_src;
EXCEPTION WHEN invalid_cursor THEN NULL;
END;
RETURN v_clob;
END query2html;
END app_html_table_pkg;
/
show errors