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
--
13
-- Name: py_functions; Type: SCHEMA; Schema: -; Owner: bien
14
--
15

    
16
CREATE SCHEMA py_functions;
17

    
18

    
19
ALTER SCHEMA py_functions OWNER TO bien;
20

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

    
25
COMMENT ON SCHEMA py_functions 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_functions, pg_catalog;
29

    
30
--
31
-- Name: _date(timestamp with time zone); Type: FUNCTION; Schema: py_functions; 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_functions._date(date timestamp with time zone) OWNER TO bien;
42

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

    
47
CREATE FUNCTION _date(date text DEFAULT NULL::text, year text DEFAULT NULL::text, month text DEFAULT NULL::text, day text DEFAULT NULL::text) RETURNS text
48
    LANGUAGE plpythonu IMMUTABLE
49
    AS $$
50
global date, year, month, day
51

    
52
import datetime
53
import dateutil.tz
54

    
55
def e_msg(e): return e.args[0].rstrip()
56

    
57
def e_str(e): return e.__class__.__name__+': '+e_msg(e)
58

    
59
def raise_invalid_value(cause, value):
60
    raise ValueError('invalid input value: "'+str(value)+'"\nDETAIL:  '
61
        +e_str(cause))
62

    
63
utc = dateutil.tz.tzutc()
64

    
65
def naive2utc(datetime_):
66
    assert datetime_.tzinfo == None
67
    return datetime_.replace(tzinfo=utc)
68

    
69
def from_timestamp(utc_timestamp):
70
    return naive2utc(datetime.datetime.utcfromtimestamp(utc_timestamp))
71

    
72
epoch = from_timestamp(0)
73

    
74
def strtotime(str_, default=epoch):
75
    import dateutil.parser
76
    return dateutil.parser.parse(str_, default=default)
77

    
78
if date != None:
79
    str_ = date
80
    try: year = float(str_)
81
    except ValueError:
82
        try: date = strtotime(str_)
83
        except ImportError: return str_
84
        except ValueError, e: raise_invalid_value(e, str_)
85
    else: date = (datetime.date(int(year), 1, 1) +
86
        datetime.timedelta(round((year % 1.)*365)))
87
else:
88
    # Year is required
89
    if year == None:
90
        if month == None and day == None: return None # entire date is empty
91
        else: raise AssertionError(
92
            'null value in column "year" violates not-null constraint')
93
    
94
    # Convert month name to number
95
    if month != None and not month.isdigit(): # month is name
96
        month = str(strtotime(month).month)
97
    
98
    if month == None: month = 1
99
    if day == None: day = 1
100
    year, month, day = map(int, (year, month, day))
101
    
102
    for try_num in xrange(2):
103
        try:
104
            date = datetime.date(year, month, day)
105
            break
106
        except ValueError, e:
107
            if try_num > 0: raise # exception still raised after retry
108
            msg = e_msg(e)
109
            if msg == 'month must be in 1..12': # try swapping month and day
110
                month, day = day, month
111
            else: raise
112

    
113
return str(date)
114
$$;
115

    
116

    
117
ALTER FUNCTION py_functions._date(date text, year text, month text, day text) OWNER TO bien;
118

    
119
--
120
-- Name: _dateRangeEnd(text); Type: FUNCTION; Schema: py_functions; Owner: bien
121
--
122

    
123
CREATE FUNCTION "_dateRangeEnd"(value text) RETURNS text
124
    LANGUAGE sql IMMUTABLE STRICT
125
    AS $_$
126
SELECT (py_functions.parse_date_range($1))[2]
127
$_$;
128

    
129

    
130
ALTER FUNCTION py_functions."_dateRangeEnd"(value text) OWNER TO bien;
131

    
132
--
133
-- Name: _dateRangeEnd(timestamp with time zone); Type: FUNCTION; Schema: py_functions; Owner: bien
134
--
135

    
136
CREATE FUNCTION "_dateRangeEnd"(value timestamp with time zone) RETURNS timestamp with time zone
137
    LANGUAGE sql IMMUTABLE STRICT
138
    AS $_$
139
SELECT $1
140
$_$;
141

    
142

    
143
ALTER FUNCTION py_functions."_dateRangeEnd"(value timestamp with time zone) OWNER TO bien;
144

    
145
--
146
-- Name: _dateRangeStart(text); Type: FUNCTION; Schema: py_functions; Owner: bien
147
--
148

    
149
CREATE FUNCTION "_dateRangeStart"(value text) RETURNS text
150
    LANGUAGE sql IMMUTABLE STRICT
151
    AS $_$
152
SELECT (py_functions.parse_date_range($1))[1]
153
$_$;
154

    
155

    
156
ALTER FUNCTION py_functions."_dateRangeStart"(value text) OWNER TO bien;
157

    
158
--
159
-- Name: _dateRangeStart(timestamp with time zone); Type: FUNCTION; Schema: py_functions; Owner: bien
160
--
161

    
162
CREATE FUNCTION "_dateRangeStart"(value timestamp with time zone) RETURNS timestamp with time zone
163
    LANGUAGE sql IMMUTABLE STRICT
164
    AS $_$
165
SELECT $1
166
$_$;
167

    
168

    
169
ALTER FUNCTION py_functions."_dateRangeStart"(value timestamp with time zone) OWNER TO bien;
170

    
171
--
172
-- Name: _namePart(text, text, text); Type: FUNCTION; Schema: py_functions; Owner: bien
173
--
174

    
175
CREATE FUNCTION "_namePart"(first text DEFAULT NULL::text, middle text DEFAULT NULL::text, last text DEFAULT NULL::text) RETURNS text
176
    LANGUAGE plpythonu IMMUTABLE
177
    AS $$
178
params = dict(first=first, middle=middle, last=last)
179

    
180
def none_if(val, *none_vals):
181
    for none_val in none_vals:
182
        if val == none_val: return None
183
    return val
184

    
185
_name_parts_slices_items = [
186
    ('first', slice(None, -1)),
187
    ('middle', slice(1, -1)),
188
    ('last', slice(-1, None)),
189
]
190
name_parts_slices = dict(_name_parts_slices_items)
191
name_parts = [name for name, slice_ in _name_parts_slices_items]
192

    
193
def _name(items):
194
    items = dict(items)
195
    parts = []
196
    for part in name_parts:
197
        if part in items: parts.append(items[part])
198
    return none_if(' '.join(parts), '')
199

    
200
out_items = []
201
for part, value in params.iteritems():
202
    if value == None: continue
203
    
204
    try: slice_ = name_parts_slices[part]
205
    except KeyError: pass # a non-value column
206
    else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
207

    
208
return _name(out_items)
209
$$;
210

    
211

    
212
ALTER FUNCTION py_functions."_namePart"(first text, middle text, last text) OWNER TO bien;
213

    
214
--
215
-- Name: parse_date_range(text); Type: FUNCTION; Schema: py_functions; Owner: bien
216
--
217

    
218
CREATE FUNCTION parse_date_range(str_ text) RETURNS text[]
219
    LANGUAGE plpythonu IMMUTABLE STRICT
220
    AS $$
221
import re
222

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

    
225
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
226

    
227
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
228

    
229
range_sep='-'
230
part_sep=' '
231

    
232
default = (str_, str_)
233
# range_sep might be used as date part separator instead
234
if str_.find(part_sep) < 0: return default
235

    
236
start, sep, end = str_.partition(range_sep)
237
if sep == '': return default # not a range
238
start, end = (single_space(d).split(part_sep) for d in (start, end))
239

    
240
# Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
241
if len(start) == 2 and (len(end) == 1 or (
242
        len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
243
        and could_be_year(end[-1])
244
    )):
245
    end.insert(0, start[0]) # make end fully specified
246
ct_diff = len(end) - len(start)
247
# Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
248
if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
249
# Other forms are invalid and will be left as-is
250

    
251
return [part_sep.join(d) for d in (start, end)]
252
$$;
253

    
254

    
255
ALTER FUNCTION py_functions.parse_date_range(str_ text) OWNER TO bien;
256

    
257
--
258
-- PostgreSQL database dump complete
259
--
260

    
(8-8/20)