Project

General

Profile

1 2620 aaronmk
--
2
-- PostgreSQL database dump
3
--
4
5
SET statement_timeout = 0;
6
SET client_encoding = 'UTF8';
7
SET standard_conforming_strings = off;
8
SET check_function_bodies = false;
9
SET client_min_messages = warning;
10
SET escape_string_warning = off;
11
12
--
13 2623 aaronmk
-- Name: py_functions; Type: SCHEMA; Schema: -; Owner: bien
14 2620 aaronmk
--
15
16
CREATE SCHEMA py_functions;
17
18
19 2623 aaronmk
ALTER SCHEMA py_functions OWNER TO bien;
20
21 2630 aaronmk
SET search_path = py_functions, pg_catalog;
22
23 2620 aaronmk
--
24 2634 aaronmk
-- Name: _dateRangeEnd(); Type: FUNCTION; Schema: py_functions; Owner: bien
25
--
26
27
CREATE FUNCTION "_dateRangeEnd"() RETURNS trigger
28
    LANGUAGE plpgsql IMMUTABLE
29
    AS $$
30
BEGIN
31 2951 aaronmk
    new.result := py_functions."_dateRangeEnd"(new.value);
32 2634 aaronmk
    RETURN new;
33
END;
34
$$;
35
36
37
ALTER FUNCTION py_functions."_dateRangeEnd"() OWNER TO bien;
38
39
--
40 2951 aaronmk
-- Name: _dateRangeEnd(text); Type: FUNCTION; Schema: py_functions; Owner: bien
41
--
42
43
CREATE FUNCTION "_dateRangeEnd"(value text) RETURNS text
44
    LANGUAGE sql IMMUTABLE
45
    AS $_$
46
SELECT (py_functions.parse_date_range($1))[2]
47
$_$;
48
49
50
ALTER FUNCTION py_functions."_dateRangeEnd"(value text) OWNER TO bien;
51
52
--
53 2630 aaronmk
-- Name: _dateRangeStart(); Type: FUNCTION; Schema: py_functions; Owner: bien
54
--
55
56
CREATE FUNCTION "_dateRangeStart"() RETURNS trigger
57
    LANGUAGE plpgsql IMMUTABLE
58
    AS $$
59
BEGIN
60 2951 aaronmk
    new.result := py_functions."_dateRangeStart"(new.value);
61 2630 aaronmk
    RETURN new;
62
END;
63
$$;
64
65
66
ALTER FUNCTION py_functions."_dateRangeStart"() OWNER TO bien;
67
68
--
69 2951 aaronmk
-- Name: _dateRangeStart(text); Type: FUNCTION; Schema: py_functions; Owner: bien
70
--
71
72
CREATE FUNCTION "_dateRangeStart"(value text) RETURNS text
73
    LANGUAGE sql IMMUTABLE
74
    AS $_$
75
SELECT (py_functions.parse_date_range($1))[1]
76
$_$;
77
78
79
ALTER FUNCTION py_functions."_dateRangeStart"(value text) OWNER TO bien;
80
81
--
82 2635 aaronmk
-- Name: _namePart(); Type: FUNCTION; Schema: py_functions; Owner: bien
83
--
84
85
CREATE FUNCTION "_namePart"() RETURNS trigger
86
    LANGUAGE plpythonu IMMUTABLE
87
    AS $$
88
new = TD['new']
89
90
_name_parts_slices_items = [
91
    ('first', slice(None, 1)),
92
    ('middle', slice(1, -1)),
93
    ('last', slice(-1, None)),
94
]
95
name_parts_slices = dict(_name_parts_slices_items)
96
name_parts = [name for name, slice_ in _name_parts_slices_items]
97
98
def _name(items):
99
    items = dict(items)
100
    parts = []
101
    for part in name_parts:
102
        if part in items: parts.append(items[part])
103
    return ' '.join(parts)
104
105
out_items = []
106
for part, value in new.iteritems():
107
    if value == None: continue
108
109
    try: slice_ = name_parts_slices[part]
110
    except KeyError: pass # a non-value column
111
    else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
112
113
new['result'] = _name(out_items)
114
115
return 'MODIFY'
116
$$;
117
118
119
ALTER FUNCTION py_functions."_namePart"() OWNER TO bien;
120
121
--
122 2630 aaronmk
-- Name: parse_date_range(text); Type: FUNCTION; Schema: py_functions; Owner: bien
123
--
124
125
CREATE FUNCTION parse_date_range(str_ text) RETURNS text[]
126
    LANGUAGE plpythonu IMMUTABLE STRICT
127
    AS $$
128
import re
129
130
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
131
132
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
133
134
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
135
136
range_sep='-'
137
part_sep=' '
138
139
default = (str_, None)
140
# range_sep might be used as date part separator instead
141
if str_.find(part_sep) < 0: return default
142
143
start, sep, end = str_.partition(range_sep)
144
if sep == '': return default # not a range
145
start, end = (single_space(d).split(part_sep) for d in (start, end))
146
147
# Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
148
if len(start) == 2 and (len(end) == 1 or (
149
        len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
150
        and could_be_year(end[-1])
151
    )):
152
    end.insert(0, start[0]) # make end fully specified
153
ct_diff = len(end) - len(start)
154
# Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
155
if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
156
# Other forms are invalid and will be left as-is
157
158
return [part_sep.join(d) for d in (start, end)]
159
$$;
160
161
162
ALTER FUNCTION py_functions.parse_date_range(str_ text) OWNER TO bien;
163
164
SET default_tablespace = '';
165
166
SET default_with_oids = false;
167
168
--
169 2634 aaronmk
-- Name: _dateRangeEnd; Type: TABLE; Schema: py_functions; Owner: bien; Tablespace:
170
--
171
172
CREATE TABLE "_dateRangeEnd" (
173
    result text,
174
    not_null_col boolean DEFAULT true NOT NULL,
175
    value text
176
);
177
178
179
ALTER TABLE py_functions."_dateRangeEnd" OWNER TO bien;
180
181
--
182 2630 aaronmk
-- Name: _dateRangeStart; Type: TABLE; Schema: py_functions; Owner: bien; Tablespace:
183
--
184
185
CREATE TABLE "_dateRangeStart" (
186
    result text,
187
    not_null_col boolean DEFAULT true NOT NULL,
188
    value text
189
);
190
191
192
ALTER TABLE py_functions."_dateRangeStart" OWNER TO bien;
193
194
--
195 2635 aaronmk
-- Name: _namePart; Type: TABLE; Schema: py_functions; Owner: bien; Tablespace:
196
--
197
198
CREATE TABLE "_namePart" (
199
    result text,
200
    not_null_col boolean DEFAULT true NOT NULL,
201
    first text,
202
    middle text,
203
    last text
204
);
205
206
207
ALTER TABLE py_functions."_namePart" OWNER TO bien;
208
209
--
210 2634 aaronmk
-- Name: _dateRangeEnd_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
211
--
212
213 2867 aaronmk
CREATE UNIQUE INDEX "_dateRangeEnd_unique" ON "_dateRangeEnd" USING btree ((COALESCE(value, '\\N'::text)));
214 2634 aaronmk
215
216
--
217 2630 aaronmk
-- Name: _dateRangeStart_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
218
--
219
220 2867 aaronmk
CREATE UNIQUE INDEX "_dateRangeStart_unique" ON "_dateRangeStart" USING btree ((COALESCE(value, '\\N'::text)));
221 2630 aaronmk
222
223
--
224 2636 aaronmk
-- Name: _namePart_first; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
225
--
226
227 2866 aaronmk
CREATE INDEX "_namePart_first" ON "_namePart" USING btree ((COALESCE(first, '\\N'::text)));
228 2636 aaronmk
229
230
--
231
-- Name: _namePart_last; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
232
--
233
234 2866 aaronmk
CREATE INDEX "_namePart_last" ON "_namePart" USING btree ((COALESCE(last, '\\N'::text)));
235 2636 aaronmk
236
237
--
238
-- Name: _namePart_middle; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
239
--
240
241 2866 aaronmk
CREATE INDEX "_namePart_middle" ON "_namePart" USING btree ((COALESCE(middle, '\\N'::text)));
242 2636 aaronmk
243
244
--
245 2635 aaronmk
-- Name: _namePart_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
246
--
247
248 2866 aaronmk
CREATE UNIQUE INDEX "_namePart_unique" ON "_namePart" USING btree ((COALESCE(first, '\\N'::text)), (COALESCE(middle, '\\N'::text)), (COALESCE(last, '\\N'::text)));
249 2635 aaronmk
250
251
--
252 2634 aaronmk
-- Name: _dateRangeEnd; Type: TRIGGER; Schema: py_functions; Owner: bien
253
--
254
255
CREATE TRIGGER "_dateRangeEnd" BEFORE INSERT OR UPDATE ON "_dateRangeEnd" FOR EACH ROW EXECUTE PROCEDURE "_dateRangeEnd"();
256
257
258
--
259 2630 aaronmk
-- Name: _dateRangeStart; Type: TRIGGER; Schema: py_functions; Owner: bien
260
--
261
262
CREATE TRIGGER "_dateRangeStart" BEFORE INSERT OR UPDATE ON "_dateRangeStart" FOR EACH ROW EXECUTE PROCEDURE "_dateRangeStart"();
263
264
265
--
266 2635 aaronmk
-- Name: _namePart; Type: TRIGGER; Schema: py_functions; Owner: bien
267
--
268
269
CREATE TRIGGER "_namePart" BEFORE INSERT OR UPDATE ON "_namePart" FOR EACH ROW EXECUTE PROCEDURE "_namePart"();
270
271
272
--
273 2620 aaronmk
-- PostgreSQL database dump complete
274
--