-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_public_use_245_as_title.sql
59 lines (52 loc) · 1.49 KB
/
01_public_use_245_as_title.sql
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
--
-- change functions so 245 attribute is used as book.title when present
--
CREATE OR REPLACE FUNCTION public.books_title_update(_fk_books integer)
RETURNS text
LANGUAGE sql
AS $_$
-- helper
-- calculates book title from attributes table
SELECT ltrim (substring (attributes.text FROM attributes.nonfiling))
FROM attributes
WHERE attributes.fk_books = $1
AND attributes.fk_attriblist = ANY (ARRAY[240, 245, 246])
-- take 245 first, the others if there is no 245 for this book.
ORDER BY
CASE WHEN attributes.fk_attriblist = 245
THEN 1
ELSE attributes.fk_attriblist
END
LIMIT 1;
$_$;
CREATE OR REPLACE FUNCTION public._books_title(r public.books) RETURNS record
LANGUAGE plpgsql
AS $$
-- helper
-- calculates book title from attributes table
DECLARE
r2 RECORD;
BEGIN
FOR r2 IN SELECT attributes.text, attributes.nonfiling
FROM attributes
WHERE attributes.fk_books = r.pk
AND attributes.fk_attriblist = ANY (ARRAY[240, 245, 246])
ORDER BY
CASE WHEN attributes.fk_attriblist = 245
THEN 1
ELSE attributes.fk_attriblist
END
LIMIT 1 LOOP
RETURN r2;
END LOOP;
r2.text := NULL;
r2.nonfiling := 0;
RETURN r2;
END;
$$;
-- apply the updated function
UPDATE books
SET title = NULL
FROM public.attributes
WHERE attributes.fk_books = books.pk
AND attributes.fk_attriblist = 240 ;