-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.sql
More file actions
206 lines (180 loc) · 4.87 KB
/
functions.sql
File metadata and controls
206 lines (180 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
-- Make well operator:
-- Takes three parameters, the well_id, the operator name (an organization)
-- and a boolean to indicate if the operator is current(true) or
-- a previous operator(false)
DROP FUNCTION IF EXISTS public.mk_welloperator(INT, VARCHAR, BOOLEAN);
CREATE FUNCTION public.mk_welloperator(
wid INT, operator VARCHAR, current BOOLEAN
) RETURNS int AS $$
DECLARE
oid INT;
BEGIN
SELECT organization_id INTO oid
FROM organization
WHERE LOWER(TRIM(name)) = LOWER(TRIM(operator))
LIMIT 1;
IF NOT FOUND THEN
INSERT INTO organization (
name, organization_type_id
) VALUES (
LOWER(TRIM(operator)),
(
SELECT organization_type_id
FROM organization_type
WHERE name = 'energy'
)
) RETURNING organization_id INTO oid;
END IF;
PERFORM well_id FROM well_operator
WHERE well_id = wid AND organization_id = oid;
IF NOT FOUND THEN
INSERT INTO well_operator (
well_id, organization_id, is_current
) VALUES (
wid, oid, current
);
END IF;
RETURN oid;
END;
$$ LANGUAGE plpgsql;
-- Make well point:
-- Takes two parameters, well_id, point (as a geography)
DROP FUNCTION IF EXISTS public.mk_wellpoint(INT, GEOGRAPHY);
CREATE FUNCTION public.mk_wellpoint(
wid INT, pnt GEOGRAPHY
) RETURNS int AS $$
DECLARE
pid INT;
BEGIN
SELECT point_id INTO pid
FROM point
WHERE geog = pnt
LIMIT 1;
IF NOT FOUND THEN
INSERT INTO point (description, geog)
VALUES ('well surface point', pnt)
RETURNING point_id INTO pid;
END IF;
PERFORM well_id FROM well_point
WHERE well_id = wid AND point_id = pid;
IF NOT FOUND THEN
INSERT INTO well_point (
well_id, point_id
) VALUES (
wid, pid
);
END IF;
RETURN pid;
END
$$ LANGUAGE plpgsql;
-- Makes a container path, that is, ANC/01/02 creates three containers,
-- with three paths. The barcode is attached to the final of the
-- created path elements.
-- Takes three parameters: An array of container names, an array
-- of container types (which it creates if they aren't found
-- and a barcode.
DROP FUNCTION IF EXISTS public.mk_container_path(VARCHAR[], VARCHAR[], VARCHAR);
CREATE FUNCTION public.mk_container_path(
paths VARCHAR[], types VARCHAR[], bcode VARCHAR
) RETURNS int AS $$
DECLARE
pid INT := NULL;
cid INT;
plen INT;
tid INT;
BEGIN
plen := ARRAY_LENGTH(paths, 1);
-- Die if the array lengths don't match
IF plen <> ARRAY_LENGTH(types, 1) THEN
RAISE EXCEPTION 'Path and type lengths are not the same.';
END IF;
-- Loop over container path
FOR i IN 1 .. plen LOOP
cid := NULL;
-- Does this container exist?
IF pid IS NULL THEN
SELECT container_id INTO cid
FROM container
WHERE LOWER(name) = LOWER(paths[i])
AND parent_container_id IS NULL
LIMIT 1;
ELSE
SELECT container_id INTO cid
FROM container
WHERE LOWER(name) = LOWER(paths[i])
AND parent_container_id = pid
LIMIT 1;
END IF;
IF cid IS NULL THEN
-- Find the type
SELECT container_type_id INTO tid
FROM container_type
WHERE LOWER(name) = LOWER(types[i])
LIMIT 1;
-- or insert as needed
IF NOT FOUND THEN
INSERT INTO container_type (name) VALUES (types[i])
RETURNING container_type_id INTO tid;
END IF;
-- Insert container
INSERT INTO container (
name, parent_container_id, container_type_id,
barcode
) VALUES (
paths[i], pid, tid,
CASE WHEN i <> plen THEN NULL ELSE bcode END
) RETURNING container_id INTO cid;
END IF;
-- Set parent_id to current container_id for next iteration
pid := cid;
END LOOP;
RETURN pid;
END
$$ LANGUAGE plpgsql;
-- Makes a prospect and borehole, but only if they don't exist.
-- Checks for both the prospect and the borehole independently.
-- Takes four parameters - prospect name, prospect alt names,
-- ardf numnber, borehole name. Prospects are compared by
-- name only. Boreholes are compared by name and prospect id.
DROP FUNCTION IF EXISTS public.mk_prospectborehole(
VARCHAR, VARCHAR, VARCHAR, VARCHAR
);
CREATE FUNCTION public.mk_prospectborehole(
prospect_name VARCHAR, prospect_altnames VARCHAR,
ardf VARCHAR, borehole_name VARCHAR
) RETURNS int AS $$
DECLARE
pid INT;
bid INT;
BEGIN
SELECT prospect_id INTO pid
FROM prospect
WHERE LOWER(name) = LOWER(prospect_name)
LIMIT 1;
IF NOT FOUND THEN
INSERT INTO prospect (
name, alt_names, ardf_number
) VALUES (
prospect_name, prospect_altnames, ardf
) RETURNING prospect_id INTO pid;
INSERT INTO borehole (
prospect_id, name
) VALUES (
pid, borehole_name
) RETURNING borehole_id INTO bid;
ELSE
SELECT borehole_id INTO bid
FROM borehole
WHERE LOWER(name) = LOWER(borehole_name)
AND prospect_id = pid;
IF NOT FOUND THEN
INSERT INTO borehole (
prospect_id, name
) VALUES (
pid, borehole_name
) RETURNING borehole_id INTO bid;
END IF;
END IF;
RETURN bid;
END
$$ LANGUAGE plpgsql;