Project

General

Profile

1
--
2
-- PostgreSQL database dump
3
--
4

    
5
SET statement_timeout = 0;
6
SET lock_timeout = 0;
7
SET client_encoding = 'UTF8';
8
SET standard_conforming_strings = on;
9
SET check_function_bodies = false;
10
SET client_min_messages = warning;
11

    
12
--
13
-- Name: py_util; Type: SCHEMA; Schema: -; Owner: bien
14
--
15

    
16
CREATE SCHEMA py_util;
17

    
18

    
19
ALTER SCHEMA py_util OWNER TO bien;
20

    
21
--
22
-- Name: SCHEMA py_util; Type: COMMENT; Schema: -; Owner: bien
23
--
24

    
25
COMMENT ON SCHEMA py_util IS 'IMPORTANT: Functions must always return NULL in place of '''' (the empty string). This ensures that empty strings do not find their way into VegBIEN.';
26

    
27

    
28
SET search_path = py_util, pg_catalog;
29

    
30
--
31
-- Name: _date(timestamp with time zone); Type: FUNCTION; Schema: py_util; Owner: bien
32
--
33

    
34
CREATE FUNCTION _date(date timestamp with time zone) RETURNS timestamp with time zone
35
    LANGUAGE sql IMMUTABLE STRICT
36
    AS $_$
37
SELECT $1
38
$_$;
39

    
40

    
41
ALTER FUNCTION py_util._date(date timestamp with time zone) OWNER TO bien;
42

    
43
--
44
-- Name: _date(integer, integer, integer); Type: FUNCTION; Schema: py_util; Owner: bien
45
--
46

    
47
CREATE FUNCTION _date(year integer DEFAULT NULL::integer, month integer DEFAULT NULL::integer, day integer DEFAULT NULL::integer) RETURNS text
48
    LANGUAGE sql IMMUTABLE STRICT
49
    AS $_$
50
SELECT py_util._date($1::text, $2::text, $3::text)
51
$_$;
52

    
53

    
54
ALTER FUNCTION py_util._date(year integer, month integer, day integer) OWNER TO bien;
55

    
56
--
57
-- Name: _date(text, text, text); Type: FUNCTION; Schema: py_util; Owner: bien
58
--
59

    
60
CREATE FUNCTION _date(year text DEFAULT NULL::text, month text DEFAULT NULL::text, day text DEFAULT NULL::text) RETURNS text
61
    LANGUAGE plpython3u IMMUTABLE
62
    AS $$
63
global date, year, month, day
64

    
65
import datetime
66

    
67
def e_msg(e): return e.args[0].rstrip()
68

    
69
# Year is required
70
if year == None:
71
    if month == None and day == None: return None # entire date is empty
72
    else: raise AssertionError(
73
        'null value in column "year" violates not-null constraint')
74

    
75
# Convert month name to number
76
if month != None and not month.isdigit(): # month is name
77
    month = str(datetime.datetime.strptime(month, '%b').month)
78

    
79
if month == None: month = 1
80
if day == None: day = 1
81
year, month, day = map(int, (year, month, day))
82

    
83
for try_num in range(2):
84
    try:
85
        date = datetime.date(year, month, day)
86
        break
87
    except ValueError as e:
88
        if try_num > 0: raise # exception still raised after retry
89
        msg = e_msg(e)
90
        if msg == 'month must be in 1..12': # try swapping month and day
91
            month, day = day, month
92
        else: raise
93

    
94
return str(date)
95
$$;
96

    
97

    
98
ALTER FUNCTION py_util._date(year text, month text, day text) OWNER TO bien;
99

    
100
--
101
-- Name: _dateRangeEnd(text); Type: FUNCTION; Schema: py_util; Owner: bien
102
--
103

    
104
CREATE FUNCTION "_dateRangeEnd"(value text) RETURNS text
105
    LANGUAGE sql IMMUTABLE STRICT
106
    AS $_$
107
SELECT (py_util.parse_date_range($1))[2]
108
$_$;
109

    
110

    
111
ALTER FUNCTION py_util."_dateRangeEnd"(value text) OWNER TO bien;
112

    
113
--
114
-- Name: _dateRangeEnd(timestamp with time zone); Type: FUNCTION; Schema: py_util; Owner: bien
115
--
116

    
117
CREATE FUNCTION "_dateRangeEnd"(value timestamp with time zone) RETURNS timestamp with time zone
118
    LANGUAGE sql IMMUTABLE STRICT
119
    AS $_$
120
SELECT $1
121
$_$;
122

    
123

    
124
ALTER FUNCTION py_util."_dateRangeEnd"(value timestamp with time zone) OWNER TO bien;
125

    
126
--
127
-- Name: _dateRangeStart(text); Type: FUNCTION; Schema: py_util; Owner: bien
128
--
129

    
130
CREATE FUNCTION "_dateRangeStart"(value text) RETURNS text
131
    LANGUAGE sql IMMUTABLE STRICT
132
    AS $_$
133
SELECT (py_util.parse_date_range($1))[1]
134
$_$;
135

    
136

    
137
ALTER FUNCTION py_util."_dateRangeStart"(value text) OWNER TO bien;
138

    
139
--
140
-- Name: _dateRangeStart(timestamp with time zone); Type: FUNCTION; Schema: py_util; Owner: bien
141
--
142

    
143
CREATE FUNCTION "_dateRangeStart"(value timestamp with time zone) RETURNS timestamp with time zone
144
    LANGUAGE sql IMMUTABLE STRICT
145
    AS $_$
146
SELECT $1
147
$_$;
148

    
149

    
150
ALTER FUNCTION py_util."_dateRangeStart"(value timestamp with time zone) OWNER TO bien;
151

    
152
--
153
-- Name: _namePart(text, text, text); Type: FUNCTION; Schema: py_util; Owner: bien
154
--
155

    
156
CREATE FUNCTION "_namePart"(first text DEFAULT NULL::text, middle text DEFAULT NULL::text, last text DEFAULT NULL::text) RETURNS text
157
    LANGUAGE plpython3u IMMUTABLE
158
    AS $$
159
params = dict(first=first, middle=middle, last=last)
160

    
161
def none_if(val, *none_vals):
162
    for none_val in none_vals:
163
        if val == none_val: return None
164
    return val
165

    
166
_name_parts_slices_items = [
167
    ('first', slice(None, -1)),
168
    ('middle', slice(1, -1)),
169
    ('last', slice(-1, None)),
170
]
171
name_parts_slices = dict(_name_parts_slices_items)
172
name_parts = [name for name, slice_ in _name_parts_slices_items]
173

    
174
def _name(items):
175
    items = dict(items)
176
    parts = []
177
    for part in name_parts:
178
        if part in items: parts.append(items[part])
179
    return none_if(' '.join(parts), '')
180

    
181
out_items = []
182
for part, value in params.iteritems():
183
    if value == None: continue
184
    
185
    try: slice_ = name_parts_slices[part]
186
    except KeyError: pass # a non-value column
187
    else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
188

    
189
return _name(out_items)
190
$$;
191

    
192

    
193
ALTER FUNCTION py_util."_namePart"(first text, middle text, last text) OWNER TO bien;
194

    
195
--
196
-- Name: parse_date_range(text); Type: FUNCTION; Schema: py_util; Owner: bien
197
--
198

    
199
CREATE FUNCTION parse_date_range(str_ text) RETURNS text[]
200
    LANGUAGE plpython3u IMMUTABLE STRICT
201
    AS $$
202
import re
203

    
204
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
205

    
206
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
207

    
208
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
209

    
210
range_sep='-'
211
part_sep=' '
212

    
213
default = (str_, str_)
214
if str_.find(':') >= 0: return default
215
# range_sep might be used as date part separator instead
216
if str_.find(part_sep) < 0: return default
217

    
218
start, sep, end = str_.partition(range_sep)
219
if sep == '': return default # not a range
220
start, end = (single_space(d).split(part_sep) for d in (start, end))
221

    
222
# Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
223
if len(start) == 2 and (len(end) == 1 or (
224
        len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
225
        and could_be_year(end[-1])
226
    )):
227
    end.insert(0, start[0]) # make end fully specified
228
ct_diff = len(end) - len(start)
229
# Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
230
if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
231
# Other forms are invalid and will be left as-is
232

    
233
return [part_sep.join(d) for d in (start, end)]
234
$$;
235

    
236

    
237
ALTER FUNCTION py_util.parse_date_range(str_ text) OWNER TO bien;
238

    
239
--
240
-- PostgreSQL database dump complete
241
--
242

    
(16-16/29)