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 2630 aaronmk
-- Name: _dateRangeStart(); Type: FUNCTION; Schema: py_functions; Owner: bien
25
--
26
27
CREATE FUNCTION "_dateRangeStart"() RETURNS trigger
28
    LANGUAGE plpgsql IMMUTABLE
29
    AS $$
30
BEGIN
31 2632 aaronmk
    new.result := (SELECT (py_functions.parse_date_range(new.value))[1]);
32 2630 aaronmk
    RETURN new;
33
END;
34
$$;
35
36
37
ALTER FUNCTION py_functions."_dateRangeStart"() OWNER TO bien;
38
39
--
40
-- Name: parse_date_range(text); Type: FUNCTION; Schema: py_functions; Owner: bien
41
--
42
43
CREATE FUNCTION parse_date_range(str_ text) RETURNS text[]
44
    LANGUAGE plpythonu IMMUTABLE STRICT
45
    AS $$
46
import re
47
48
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
49
50
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
51
52
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
53
54
range_sep='-'
55
part_sep=' '
56
57
default = (str_, None)
58
# range_sep might be used as date part separator instead
59
if str_.find(part_sep) < 0: return default
60
61
start, sep, end = str_.partition(range_sep)
62
if sep == '': return default # not a range
63
start, end = (single_space(d).split(part_sep) for d in (start, end))
64
65
# Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
66
if len(start) == 2 and (len(end) == 1 or (
67
        len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
68
        and could_be_year(end[-1])
69
    )):
70
    end.insert(0, start[0]) # make end fully specified
71
ct_diff = len(end) - len(start)
72
# Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
73
if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
74
# Other forms are invalid and will be left as-is
75
76
return [part_sep.join(d) for d in (start, end)]
77
$$;
78
79
80
ALTER FUNCTION py_functions.parse_date_range(str_ text) OWNER TO bien;
81
82
SET default_tablespace = '';
83
84
SET default_with_oids = false;
85
86
--
87
-- Name: _dateRangeStart; Type: TABLE; Schema: py_functions; Owner: bien; Tablespace:
88
--
89
90
CREATE TABLE "_dateRangeStart" (
91
    result text,
92
    not_null_col boolean DEFAULT true NOT NULL,
93
    value text
94
);
95
96
97
ALTER TABLE py_functions."_dateRangeStart" OWNER TO bien;
98
99
--
100
-- Name: _dateRangeStart_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace:
101
--
102
103
CREATE UNIQUE INDEX "_dateRangeStart_unique" ON "_dateRangeStart" USING btree (functions.ensure_not_null(value));
104
105
106
--
107
-- Name: _dateRangeStart; Type: TRIGGER; Schema: py_functions; Owner: bien
108
--
109
110
CREATE TRIGGER "_dateRangeStart" BEFORE INSERT OR UPDATE ON "_dateRangeStart" FOR EACH ROW EXECUTE PROCEDURE "_dateRangeStart"();
111
112
113
--
114 2620 aaronmk
-- PostgreSQL database dump complete
115
--