Revision 2635
Added by Aaron Marcuse-Kubitza over 12 years ago
py_functions.sql | ||
---|---|---|
53 | 53 |
ALTER FUNCTION py_functions."_dateRangeStart"() OWNER TO bien; |
54 | 54 |
|
55 | 55 |
-- |
56 |
-- Name: _namePart(); Type: FUNCTION; Schema: py_functions; Owner: bien |
|
57 |
-- |
|
58 |
|
|
59 |
CREATE FUNCTION "_namePart"() RETURNS trigger |
|
60 |
LANGUAGE plpythonu IMMUTABLE |
|
61 |
AS $$ |
|
62 |
new = TD['new'] |
|
63 |
|
|
64 |
_name_parts_slices_items = [ |
|
65 |
('first', slice(None, 1)), |
|
66 |
('middle', slice(1, -1)), |
|
67 |
('last', slice(-1, None)), |
|
68 |
] |
|
69 |
name_parts_slices = dict(_name_parts_slices_items) |
|
70 |
name_parts = [name for name, slice_ in _name_parts_slices_items] |
|
71 |
|
|
72 |
def _name(items): |
|
73 |
items = dict(items) |
|
74 |
parts = [] |
|
75 |
for part in name_parts: |
|
76 |
if part in items: parts.append(items[part]) |
|
77 |
return ' '.join(parts) |
|
78 |
|
|
79 |
out_items = [] |
|
80 |
for part, value in new.iteritems(): |
|
81 |
if value == None: continue |
|
82 |
|
|
83 |
try: slice_ = name_parts_slices[part] |
|
84 |
except KeyError: pass # a non-value column |
|
85 |
else: out_items.append((part, ' '.join(value.split(' ')[slice_]))) |
|
86 |
|
|
87 |
new['result'] = _name(out_items) |
|
88 |
|
|
89 |
return 'MODIFY' |
|
90 |
$$; |
|
91 |
|
|
92 |
|
|
93 |
ALTER FUNCTION py_functions."_namePart"() OWNER TO bien; |
|
94 |
|
|
95 |
-- |
|
56 | 96 |
-- Name: parse_date_range(text); Type: FUNCTION; Schema: py_functions; Owner: bien |
57 | 97 |
-- |
58 | 98 |
|
... | ... | |
126 | 166 |
ALTER TABLE py_functions."_dateRangeStart" OWNER TO bien; |
127 | 167 |
|
128 | 168 |
-- |
169 |
-- Name: _namePart; Type: TABLE; Schema: py_functions; Owner: bien; Tablespace: |
|
170 |
-- |
|
171 |
|
|
172 |
CREATE TABLE "_namePart" ( |
|
173 |
result text, |
|
174 |
not_null_col boolean DEFAULT true NOT NULL, |
|
175 |
first text, |
|
176 |
middle text, |
|
177 |
last text |
|
178 |
); |
|
179 |
|
|
180 |
|
|
181 |
ALTER TABLE py_functions."_namePart" OWNER TO bien; |
|
182 |
|
|
183 |
-- |
|
129 | 184 |
-- Name: _dateRangeEnd_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace: |
130 | 185 |
-- |
131 | 186 |
|
... | ... | |
140 | 195 |
|
141 | 196 |
|
142 | 197 |
-- |
198 |
-- Name: _namePart_unique; Type: INDEX; Schema: py_functions; Owner: bien; Tablespace: |
|
199 |
-- |
|
200 |
|
|
201 |
CREATE UNIQUE INDEX "_namePart_unique" ON "_namePart" USING btree (functions.ensure_not_null(first), functions.ensure_not_null(middle), functions.ensure_not_null(last)); |
|
202 |
|
|
203 |
|
|
204 |
-- |
|
143 | 205 |
-- Name: _dateRangeEnd; Type: TRIGGER; Schema: py_functions; Owner: bien |
144 | 206 |
-- |
145 | 207 |
|
... | ... | |
154 | 216 |
|
155 | 217 |
|
156 | 218 |
-- |
219 |
-- Name: _namePart; Type: TRIGGER; Schema: py_functions; Owner: bien |
|
220 |
-- |
|
221 |
|
|
222 |
CREATE TRIGGER "_namePart" BEFORE INSERT OR UPDATE ON "_namePart" FOR EACH ROW EXECUTE PROCEDURE "_namePart"(); |
|
223 |
|
|
224 |
|
|
225 |
-- |
|
157 | 226 |
-- PostgreSQL database dump complete |
158 | 227 |
-- |
159 | 228 |
|
Also available in: Unified diff
schemas/py_functions.sql: Added _namePart relational function