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 3396 aaronmk
SET SESSION AUTHORIZATION 'bien';
13
14 2620 aaronmk
--
15 2623 aaronmk
-- Name: py_functions; Type: SCHEMA; Schema: -; Owner: bien
16 2620 aaronmk
--
17
18
CREATE SCHEMA py_functions;
19
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
--
38 2951 aaronmk
-- Name: _dateRangeEnd(text); Type: FUNCTION; Schema: py_functions; Owner: bien
39
--
40
41
CREATE FUNCTION "_dateRangeEnd"(value text) RETURNS text
42
    LANGUAGE sql IMMUTABLE
43
    AS $_$
44
SELECT (py_functions.parse_date_range($1))[2]
45
$_$;
46
47
48
--
49 2630 aaronmk
-- Name: _dateRangeStart(); Type: FUNCTION; Schema: py_functions; Owner: bien
50
--
51
52
CREATE FUNCTION "_dateRangeStart"() RETURNS trigger
53
    LANGUAGE plpgsql IMMUTABLE
54
    AS $$
55
BEGIN
56 2951 aaronmk
    new.result := py_functions."_dateRangeStart"(new.value);
57 2630 aaronmk
    RETURN new;
58
END;
59
$$;
60
61
62
--
63 2951 aaronmk
-- Name: _dateRangeStart(text); Type: FUNCTION; Schema: py_functions; Owner: bien
64
--
65
66
CREATE FUNCTION "_dateRangeStart"(value text) RETURNS text
67
    LANGUAGE sql IMMUTABLE
68
    AS $_$
69
SELECT (py_functions.parse_date_range($1))[1]
70
$_$;
71
72
73
--
74 2635 aaronmk
-- Name: _namePart(); Type: FUNCTION; Schema: py_functions; Owner: bien
75
--
76
77
CREATE FUNCTION "_namePart"() RETURNS trigger
78 2952 aaronmk
    LANGUAGE plpgsql IMMUTABLE
79
    AS $$
80
BEGIN
81
    new.result := py_functions."_namePart"(new.first, new.middle, new.last);
82
    RETURN new;
83
END;
84
$$;
85
86
87
--
88
-- Name: _namePart(text, text, text); Type: FUNCTION; Schema: py_functions; Owner: bien
89
--
90
91
CREATE FUNCTION "_namePart"(first text DEFAULT NULL::text, middle text DEFAULT NULL::text, last text DEFAULT NULL::text) RETURNS text
92 2635 aaronmk
    LANGUAGE plpythonu IMMUTABLE
93
    AS $$
94 2952 aaronmk
params = dict(first=first, middle=middle, last=last)
95 2635 aaronmk
96
_name_parts_slices_items = [
97
    ('first', slice(None, 1)),
98
    ('middle', slice(1, -1)),
99
    ('last', slice(-1, None)),
100
]
101
name_parts_slices = dict(_name_parts_slices_items)
102
name_parts = [name for name, slice_ in _name_parts_slices_items]
103
104
def _name(items):
105
    items = dict(items)
106
    parts = []
107
    for part in name_parts:
108
        if part in items: parts.append(items[part])
109
    return ' '.join(parts)
110
111
out_items = []
112 2952 aaronmk
for part, value in params.iteritems():
113 2635 aaronmk
    if value == None: continue
114
115
    try: slice_ = name_parts_slices[part]
116
    except KeyError: pass # a non-value column
117
    else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
118
119 2952 aaronmk
return _name(out_items)
120 2635 aaronmk
$$;
121
122
123
--
124 2630 aaronmk
-- Name: parse_date_range(text); Type: FUNCTION; Schema: py_functions; Owner: bien
125
--
126
127
CREATE FUNCTION parse_date_range(str_ text) RETURNS text[]
128
    LANGUAGE plpythonu IMMUTABLE STRICT
129
    AS $$
130
import re
131
132
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
133
134
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
135
136
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
137
138
range_sep='-'
139
part_sep=' '
140
141
default = (str_, None)
142
# range_sep might be used as date part separator instead
143
if str_.find(part_sep) < 0: return default
144
145
start, sep, end = str_.partition(range_sep)
146
if sep == '': return default # not a range
147
start, end = (single_space(d).split(part_sep) for d in (start, end))
148
149
# Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
150
if len(start) == 2 and (len(end) == 1 or (
151
        len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
152
        and could_be_year(end[-1])
153
    )):
154
    end.insert(0, start[0]) # make end fully specified
155
ct_diff = len(end) - len(start)
156
# Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
157
if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
158
# Other forms are invalid and will be left as-is
159
160
return [part_sep.join(d) for d in (start, end)]
161
$$;
162
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
--
180 2630 aaronmk
-- Name: _dateRangeStart; Type: TABLE; Schema: py_functions; Owner: bien; Tablespace:
181
--
182
183
CREATE TABLE "_dateRangeStart" (
184
    result text,
185
    not_null_col boolean DEFAULT true NOT NULL,
186
    value text
187
);
188
189
190
--
191 2635 aaronmk
-- Name: _namePart; Type: TABLE; Schema: py_functions; Owner: bien; Tablespace:
192
--
193
194
CREATE TABLE "_namePart" (
195
    result text,
196
    not_null_col boolean DEFAULT true NOT NULL,
197
    first text,
198
    middle text,
199
    last text
200
);
201
202
203
--
204 2634 aaronmk
-- Name: _dateRangeEnd_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
205
--
206
207 2867 aaronmk
CREATE UNIQUE INDEX "_dateRangeEnd_unique" ON "_dateRangeEnd" USING btree ((COALESCE(value, '\\N'::text)));
208 2634 aaronmk
209
210
--
211 2630 aaronmk
-- Name: _dateRangeStart_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
212
--
213
214 2867 aaronmk
CREATE UNIQUE INDEX "_dateRangeStart_unique" ON "_dateRangeStart" USING btree ((COALESCE(value, '\\N'::text)));
215 2630 aaronmk
216
217
--
218 2635 aaronmk
-- Name: _namePart_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
219
--
220
221 2866 aaronmk
CREATE UNIQUE INDEX "_namePart_unique" ON "_namePart" USING btree ((COALESCE(first, '\\N'::text)), (COALESCE(middle, '\\N'::text)), (COALESCE(last, '\\N'::text)));
222 2635 aaronmk
223
224
--
225 2634 aaronmk
-- Name: _dateRangeEnd; Type: TRIGGER; Schema: py_functions; Owner: bien
226
--
227
228
CREATE TRIGGER "_dateRangeEnd" BEFORE INSERT OR UPDATE ON "_dateRangeEnd" FOR EACH ROW EXECUTE PROCEDURE "_dateRangeEnd"();
229
230
231
--
232 2630 aaronmk
-- Name: _dateRangeStart; Type: TRIGGER; Schema: py_functions; Owner: bien
233
--
234
235
CREATE TRIGGER "_dateRangeStart" BEFORE INSERT OR UPDATE ON "_dateRangeStart" FOR EACH ROW EXECUTE PROCEDURE "_dateRangeStart"();
236
237
238
--
239 2635 aaronmk
-- Name: _namePart; Type: TRIGGER; Schema: py_functions; Owner: bien
240
--
241
242 2952 aaronmk
CREATE TRIGGER "_namePart" BEFORE INSERT OR UPDATE ON "_namePart" FOR EACH ROW EXECUTE PROCEDURE py_functions."_namePart"();
243 2635 aaronmk
244
245
--
246 2620 aaronmk
-- PostgreSQL database dump complete
247
--