Project

General

Profile

1
--
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
SET SESSION AUTHORIZATION 'bien';
13

    
14
--
15
-- Name: py_functions; Type: SCHEMA; Schema: -; Owner: bien
16
--
17

    
18
CREATE SCHEMA py_functions;
19

    
20

    
21
SET search_path = py_functions, pg_catalog;
22

    
23
--
24
-- 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
    new.result := py_functions."_dateRangeEnd"(new.value);
32
    RETURN new;
33
END;
34
$$;
35

    
36

    
37
--
38
-- 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
-- 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
    new.result := py_functions."_dateRangeStart"(new.value);
57
    RETURN new;
58
END;
59
$$;
60

    
61

    
62
--
63
-- 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
-- Name: _namePart(); Type: FUNCTION; Schema: py_functions; Owner: bien
75
--
76

    
77
CREATE FUNCTION "_namePart"() RETURNS trigger
78
    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
    LANGUAGE plpythonu IMMUTABLE
93
    AS $$
94
params = dict(first=first, middle=middle, last=last)
95

    
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
for part, value in params.iteritems():
113
    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
return _name(out_items)
120
$$;
121

    
122

    
123
--
124
-- 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
-- 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
-- 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
-- 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
-- Name: _dateRangeEnd_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace: 
205
--
206

    
207
CREATE UNIQUE INDEX "_dateRangeEnd_unique" ON "_dateRangeEnd" USING btree ((COALESCE(value, '\\N'::text)));
208

    
209

    
210
--
211
-- Name: _dateRangeStart_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace: 
212
--
213

    
214
CREATE UNIQUE INDEX "_dateRangeStart_unique" ON "_dateRangeStart" USING btree ((COALESCE(value, '\\N'::text)));
215

    
216

    
217
--
218
-- Name: _namePart_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace: 
219
--
220

    
221
CREATE UNIQUE INDEX "_namePart_unique" ON "_namePart" USING btree ((COALESCE(first, '\\N'::text)), (COALESCE(middle, '\\N'::text)), (COALESCE(last, '\\N'::text)));
222

    
223

    
224
--
225
-- 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
-- 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
-- Name: _namePart; Type: TRIGGER; Schema: py_functions; Owner: bien
240
--
241

    
242
CREATE TRIGGER "_namePart" BEFORE INSERT OR UPDATE ON "_namePart" FOR EACH ROW EXECUTE PROCEDURE py_functions."_namePart"();
243

    
244

    
245
--
246
-- PostgreSQL database dump complete
247
--
248

    
(8-8/19)