- Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathstring-tracker.sql
237 lines (201 loc) · 7.46 KB
/
string-tracker.sql
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
/*
The string_tracker package allows you to keep track of whether a certain name has
already been used within a particular list.
Besides hopefully being useful to you, it is a nice little demonstration of
string-indexed associative arrays and nested collections (collections within collections)
*/
-- Create the Public API
CREATE OR REPLACE PACKAGE string_tracker
IS
SUBTYPE maxvarchar2_t IS VARCHAR2 (32767);
SUBTYPE list_name_t IS maxvarchar2_t;
SUBTYPE value_string_t IS maxvarchar2_t;
PROCEDURE clear_all_lists;
PROCEDURE clear_list (list_name_in IN list_name_t);
PROCEDURE create_list (
list_name_in IN list_name_t
, case_sensitive_in INBOOLEAN DEFAULT FALSE
, overwrite_in INBOOLEAN DEFAULT TRUE
);
-- Is the string already in use?
FUNCTION string_in_use (
list_name_in IN list_name_t
, value_string_in IN value_string_t
)
RETURN BOOLEAN;
-- Mark this string as being used.
PROCEDURE mark_as_used (
list_name_in IN list_name_t
, value_string_in IN value_string_t
);
END string_tracker;
/
-- Imlpement the API
CREATE OR REPLACE PACKAGE BODY string_tracker
/*
Overview: string_tracker allows you to keep track of whether a
certain name has already been used within a particular list.
Author: Steven Feuerstein
*/
IS
/* List of used strings - so why a collection of Booleans?
Because the string is the index value, so the element value
in the collection is of no importance. */
TYPE used_aat IS TABLE OF BOOLEAN INDEX BY value_string_t;
/* No need to include the list name in the list record.
The index value that points to this record is the list name. */
TYPE list_rt IS RECORD (
case_sensitive BOOLEAN
, list_of_values used_aat
);
TYPE list_of_lists_aat IS TABLE OF list_rt
INDEX BY list_name_t;
g_list_of_lists list_of_lists_aat;
PROCEDURE assert (expr_in INBOOLEAN, text_in INVARCHAR2)
IS
BEGIN
IF NOT expr_in OR expr_in IS NULL
THEN
raise_application_error (-20000, text_in);
END IF;
END assert;
PROCEDURE clear_all_lists
IS
BEGIN
g_list_of_lists.DELETE;
END clear_all_lists;
PROCEDURE clear_list (list_name_in IN list_name_t)
IS
BEGIN
g_list_of_lists.DELETE (list_name_in);
END CLEAR_LIST;
PROCEDURE create_list (
list_name_in IN list_name_t
, case_sensitive_in INBOOLEAN DEFAULT FALSE
, overwrite_in INBOOLEAN DEFAULT TRUE
)
IS
l_create_list BOOLEAN DEFAULT TRUE;
l_new_list list_rt;
BEGIN
IF g_list_of_lists.EXISTS (list_name_in)
THEN
l_create_list := overwrite_in;
END IF;
IF l_create_list
THEN
l_new_list.case_sensitive := case_sensitive_in;
g_list_of_lists (list_name_in) := l_new_list;
END IF;
END create_list;
FUNCTION sensitized_value (
list_name_in IN list_name_t
, value_string_in IN value_string_t
)
RETURN value_string_t
IS
BEGIN
RETURN CASE g_list_of_lists (list_name_in).case_sensitive
WHEN TRUE
THEN value_string_in
ELSE UPPER (value_string_in)
END;
END sensitized_value;
FUNCTION string_in_use (
list_name_in IN list_name_t
, value_string_in IN value_string_t
)
RETURN BOOLEAN
IS
BEGIN
/* Don't assume inputs are valid! */
assert (list_name_in IS NOT NULL
, 'You must provide a non-NULL name for your list!'
);
assert (value_string_in IS NOT NULL
, 'You must provide a non-NULL string for tracking!'
);
assert (g_list_of_lists.EXISTS (list_name_in)
, 'You must create your list named "'
|| list_name_in
||'" before you can use it.'
);
RETURN g_list_of_lists (list_name_in).list_of_values.EXISTS
(sensitized_value (list_name_in
, value_string_in
)
);
END string_in_use;
PROCEDURE mark_as_used (
list_name_in IN list_name_t
, value_string_in IN value_string_t
)
IS
BEGIN
/* Don't assume inputs are valid! */
assert (list_name_in IS NOT NULL
, 'You must provide a non-NULL name for your list!'
);
assert (list_name_in IS NOT NULL
, 'You must provide a non-NULL name for your list!'
);
assert (value_string_in IS NOT NULL
, 'You must provide a non-NULL string for tracking!'
);
assert (g_list_of_lists.EXISTS (list_name_in)
, 'You must create your list named "'
|| list_name_in
||'" before you can use it.'
);
g_list_of_lists (list_name_in).list_of_values
(sensitized_value (list_name_in
, value_string_in
)
) := TRUE;
END mark_as_used;
END string_tracker;
/
-- Exercise the API
-- This is NOT a comprehensive regression test, but it demonstrates some key features.
DECLARE
/* Create a constant with the list name to avoid multiple,
hard-coded references. Notice the use of the subtype
declared in the string_tracker package to declare the
list name. */
c_list_name CONSTANT string_tracker.list_name_t :='outcomes';
BEGIN
/* Create the list, wiping out anything that was there before. */
string_tracker.create_list (list_name_in => c_list_name,
case_sensitive_in => FALSE,
overwrite_in => TRUE);
string_tracker.mark_as_used (list_name_in => c_list_name,
value_string_in =>'abc');
string_tracker.mark_as_used (list_name_in => c_list_name,
value_string_in =>'def');
IF string_tracker.string_in_use (list_name_in => c_list_name,
value_string_in =>'ABC')
THEN
DBMS_OUTPUT.put_line ('Case insenstive list - match found for ABC');
END IF;
IF NOT string_tracker.string_in_use (list_name_in => c_list_name,
value_string_in =>'notinlist')
THEN
DBMS_OUTPUT.put_line ('No match found for notinlist');
END IF;
/* Now case sensitive. */
string_tracker.create_list (list_name_in => c_list_name,
case_sensitive_in => TRUE,
overwrite_in => TRUE);
string_tracker.mark_as_used (list_name_in => c_list_name,
value_string_in =>'abc');
IF NOT string_tracker.string_in_use (list_name_in => c_list_name,
value_string_in =>'ABC')
THEN
DBMS_OUTPUT.put_line ('Case senstive list - match NOT found for ABC');
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack ());
END;
/