-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_csv_udt.tpb
executable file
·329 lines (305 loc) · 12.4 KB
/
app_csv_udt.tpb
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
CREATE OR REPLACE TYPE BODY app_csv_udt AS
-- https:/lee-lindely/app_csv
/*
MIT License
Copyright (c) 2021 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.
*/
MEMBER PROCEDURE app_csv_constructor(
SELF IN OUT NOCOPY app_csv_udt
,p_cursor SYS_REFCURSOR
,p_separator VARCHAR2
,p_quote_all_strings VARCHAR2
,p_strip_separator VARCHAR2
,p_bulk_count INTEGER
,p_num_format VARCHAR2
,p_date_format VARCHAR2
,p_interval_format VARCHAR2
,p_protect_numstr_from_excel VARCHAR2
)
IS
-- based on dbms_sql column info, get down to basic types that can be converted into
FUNCTION lookup_col_type(p_col_type INTEGER)
RETURN VARCHAR2 -- D ate, N umber, C har
IS
BEGIN
RETURN CASE WHEN p_col_type IN (1, 8, 9, 96, 112)
THEN 'C'
WHEN p_col_type IN (12, 178, 179, 180, 181 , 231)
THEN 'D'
WHEN p_col_type IN (2, 100, 101)
THEN 'N'
ELSE NULL
END;
END;
BEGIN
SELF.app_dbms_sql_str_constructor(
p_cursor => p_cursor
,p_bulk_count => p_bulk_count
,p_default_num_fmt => p_num_format
,p_default_date_fmt => p_date_format
,p_default_interval_fmt => p_interval_format
);
SELF.separator := p_separator;
SELF.quote_all_strings := CASE WHEN UPPER(p_quote_all_strings) LIKE 'Y%' THEN 'Y' ELSE 'N' END;
SELF.strip_separator := CASE WHEN UPPER(p_strip_separator) LIKE 'Y%' THEN 'Y' ELSE 'N' END;
SELF.protect_numstr_from_excel := CASE WHEN UPPER(p_protect_numstr_from_excel) LIKE 'Y%' THEN 'Y' ELSE 'N' END;
-- although supertyp handles conversion to strings, we need to know if the original
-- column was a string or not in order to honor the quote_all_strings directive
SELF.csv_col_types := &&d_arr_varchar2_udt.();
SELF.csv_col_types.EXTEND(SELF.col_types.COUNT);
FOR i in 1..col_types.COUNT
LOOP
SELF.csv_col_types(i) := lookup_col_type(SELF.col_types(i));
END LOOP;
END;
CONSTRUCTOR FUNCTION app_csv_udt(
p_cursor SYS_REFCURSOR
,p_separator VARCHAR2 := ','
,p_quote_all_strings VARCHAR2 := 'N'
,p_strip_separator VARCHAR2 := 'N' -- strip comma from fields rather than quote
,p_bulk_count INTEGER := 100
,p_num_format VARCHAR2 := 'tm9'
,p_date_format VARCHAR2 := 'MM/DD/YYYY'
,p_interval_format VARCHAR2 := NULL
,p_protect_numstr_from_excel VARCHAR2 := 'N'
) RETURN SELF AS RESULT
IS
BEGIN
SELF.app_csv_constructor(
p_cursor => p_cursor
,p_separator => p_separator
,p_quote_all_strings => p_quote_all_strings
,p_strip_separator => p_strip_separator
,p_bulk_count => p_bulk_count
,p_num_format => p_num_format
,p_date_format => p_date_format
,p_interval_format => p_interval_format
,p_protect_numstr_from_excel => p_protect_numstr_from_excel
);
RETURN;
END;
CONSTRUCTOR FUNCTION app_csv_udt(
p_sql VARCHAR2
,p_separator VARCHAR2 := ','
,p_quote_all_strings VARCHAR2 := 'N'
,p_strip_separator VARCHAR2 := 'N' -- strip comma from fields rather than quote
,p_bulk_count INTEGER := 100
,p_num_format VARCHAR2 := 'tm9'
,p_date_format VARCHAR2 := 'MM/DD/YYYY'
,p_interval_format VARCHAR2 := NULL
,p_protect_numstr_from_excel VARCHAR2 := 'N'
) RETURN SELF AS RESULT
IS
-- based on dbms_sql column info, get down to basic types that can be converted into
FUNCTION get_src(p_sql VARCHAR2)
RETURN SYS_REFCURSOR
IS
l_src SYS_REFCURSOR;
BEGIN
OPEN l_src FOR p_sql;
RETURN l_src;
END;
BEGIN
SELF.app_csv_constructor(
p_cursor => get_src(p_sql)
,p_separator => p_separator
,p_quote_all_strings => p_quote_all_strings
,p_strip_separator => p_strip_separator
,p_bulk_count => p_bulk_count
,p_num_format => p_num_format
,p_date_format => p_date_format
,p_interval_format => p_interval_format
,p_protect_numstr_from_excel => p_protect_numstr_from_excel
);
RETURN;
END;
MEMBER PROCEDURE destructor
IS
BEGIN
IF ctx IS NOT NULL THEN
DBMS_SQL.close_cursor(ctx);
END IF;
END;
MEMBER FUNCTION get_header_row RETURN CLOB
IS
v_str CLOB; -- maxes at 4000 if calling table function from sql
v_col_name VARCHAR2(32767);
v_a &&d_arr_varchar2_udt.;
BEGIN
v_a := SELF.get_column_names;
FOR i IN 1..v_a.COUNT
LOOP
v_col_name := v_a(i);
IF strip_separator = 'Y' THEN
v_col_name := REPLACE(v_col_name, separator);
ELSIF quote_all_strings = 'Y'
OR REGEXP_INSTR(v_col_name, '['||separator||chr(10)||']') > 0 -- contains separator or newline
THEN v_col_name := '"'
||REPLACE(v_col_name, '"', '""') -- double up dquotes inside field to *quote* them
||'"'
;
END IF;
v_str := v_str||CASE WHEN i > 1 THEN separator END||v_col_name;
END LOOP;
RETURN v_str;
END;
MEMBER PROCEDURE get_next_row (
SELF IN OUT NOCOPY app_csv_udt
,p_clob OUT NOCOPY CLOB
)
IS
v_col_val CLOB;
v_a &&d_arr_clob_udt.;
FUNCTION quote_str(p_col BINARY_INTEGER) RETURN CLOB IS
l_str CLOB;
BEGIN
l_str := REPLACE(v_a(p_col), CASE WHEN strip_separator = 'Y' THEN separator END); -- null replacement string is noop
IF csv_col_types(p_col) = 'C' AND protect_numstr_from_excel = 'Y'
AND REGEXP_LIKE(l_str, '^(\s*[+-]?(\d+[.]?\d*)|([.]\d+))$') THEN -- looks like a number but is a string
-- this will turn 00123 into "=""00123""" which excel will show as 00123.
-- otherwise, excel will treat it like a number and strip leading zeros
l_str := '"=""'
||TRIM(l_str)
||'"""'
;
ELSIF csv_col_types(p_col) = 'C' AND quote_all_strings = 'Y' THEN
-- preserve leading/trailing spaces too.
l_str := '"'
||REPLACE(l_str, '"', '""') -- double up dquotes inside field to *quote* them
||'"'
;
ELSE
l_str := TRIM(l_str);
IF REGEXP_INSTR(l_str, '['||separator||chr(10)||']') > 0 THEN -- contains separator or newline
l_str := '"'
||REPLACE(l_str, '"', '""') -- double up dquotes inside field to *quote* them
||'"'
;
END IF;
END IF;
RETURN l_str;
END;
BEGIN
SELF.get_next_column_values(v_a);
IF v_a IS NOT NULL THEN
p_clob := quote_str(1);
FOR j IN 2..v_a.COUNT
LOOP
p_clob := p_clob||separator||quote_str(j);
END LOOP;
END IF;
END;
MEMBER PROCEDURE get_clob(
SELF IN OUT NOCOPY app_csv_udt
,p_clob OUT NOCOPY CLOB
,p_do_header VARCHAR2 := 'N'
,p_lf_only BOOLEAN := TRUE
)
IS
v_str CLOB;
v_crlf VARCHAR2(2) := CASE WHEN p_lf_only THEN CHR(10) ELSE CHR(13)||CHR(10) END;
BEGIN
get_next_row(v_str);
IF v_str IS NOT NULL THEN -- want to return NULL if no rows. Not a header
p_clob := '';
IF UPPER(p_do_header) LIKE 'Y%' THEN
p_clob := get_header_row||v_crlf;
END IF;
LOOP
p_clob := p_clob||v_str||v_crlf;
get_next_row(v_str);
EXIT WHEN v_str IS NULL;
END LOOP;
END IF;
END;
MEMBER PROCEDURE write_file(
SELF IN OUT NOCOPY app_csv_udt
,p_dir VARCHAR2
,p_file_name VARCHAR2
,p_do_header VARCHAR2 := 'N'
) IS
v_str CLOB;
v_file UTL_FILE.file_type;
BEGIN
v_file := UTL_FILE.fopen(
filename => p_file_name
,location => p_dir
,open_mode => 'w'
,max_linesize => 32767
);
get_next_row(v_str);
IF v_str IS NOT NULL THEN -- we only want a header or any data written if we have rows
IF UPPER(p_do_header) LIKE 'Y%' THEN
UTL_FILE.put_line(v_file, get_header_row);
END IF;
LOOP
UTL_FILE.put_line(v_file, v_str);
get_next_row(v_str);
EXIT WHEN v_str IS NULL;
END LOOP;
END IF;
UTL_FILE.fclose(v_file);
EXCEPTION WHEN OTHERS THEN
IF UTL_FILE.is_open(v_file)
THEN UTL_FILE.fclose(v_file);
END IF;
RAISE;
END;
STATIC FUNCTION get_rows(
p_cursor SYS_REFCURSOR
,p_separator VARCHAR2 := ','
,p_do_header VARCHAR2 := 'N'
,p_num_format VARCHAR2 := 'tm9'
,p_date_format VARCHAR2 := 'MM/DD/YYYY'
,p_interval_format VARCHAR2 := NULL
,p_bulk_count INTEGER := 100
,p_quote_all_strings VARCHAR2 := 'N'
,p_protect_numstr_from_excel VARCHAR2 := 'N'
) RETURN &&d_arr_varchar2_udt.
PIPELINED
IS
v_str CLOB;
v_app_csv app_csv_udt;
BEGIN
v_app_csv := app_csv_udt(
p_cursor => p_cursor
,p_separator => p_separator
,p_num_format => p_num_format
,p_date_format => p_date_format
,p_interval_format => p_interval_format
,p_bulk_count => p_bulk_count
,p_quote_all_strings => p_quote_all_strings
,p_protect_numstr_from_excel => p_protect_numstr_from_excel
);
v_app_csv.get_next_row(v_str);
IF v_str IS NOT NULL THEN -- we only want a header or any data written if we have rows
IF UPPER(p_do_header) LIKE 'Y%' THEN
PIPE ROW(v_app_csv.get_header_row);
END IF;
LOOP
-- if it is longer than 4000 chars I assume it will raise an exception
PIPE ROW(v_str);
v_app_csv.get_next_row(v_str);
EXIT WHEN v_str IS NULL;
END LOOP;
END IF;
RETURN;
END;
END;
/
show errors