596495ddb7118c6566c33aa5c40db7d0e480492f
[migration-tools.git] / sql / base / 06-ils-specific.sql
1 CREATE OR REPLACE FUNCTION migration_tools.attempt_hz_date (TEXT,TEXT) RETURNS DATE AS $$
2     DECLARE
3         attempt_value ALIAS FOR $1;
4         fail_value ALIAS FOR $2;
5         output DATE;
6     BEGIN
7         FOR output IN
8             EXECUTE E'SELECT (\'1970-01-01\'::date + \'' || attempt_value || E' days\'::interval)::date AS a;'
9         LOOP
10             RETURN output;
11         END LOOP;
12     EXCEPTION
13         WHEN OTHERS THEN
14             FOR output IN
15                 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::date AS a;'
16             LOOP
17                 RETURN output;
18             END LOOP;
19     END;
20 $$ LANGUAGE PLPGSQL STRICT STABLE;
21
22 CREATE OR REPLACE FUNCTION migration_tools.attempt_sierra_timestamp (TEXT,TEXT) RETURNS TIMESTAMP AS $$
23     DECLARE
24         attempt_value ALIAS FOR $1;
25         fail_value ALIAS FOR $2;
26         output TIMESTAMP;
27     BEGIN
28             output := REGEXP_REPLACE(attempt_value,E'^(..)(..)(..)(..)(..)$',E'20\\1-\\2-\\3 \\4:\\5')::TIMESTAMP;
29             RETURN output;
30     EXCEPTION
31         WHEN OTHERS THEN
32             FOR output IN
33                 EXECUTE 'SELECT ' || quote_literal(fail_value) || '::TIMESTAMP AS a;'
34             LOOP
35                 RETURN output;
36             END LOOP;
37     END;
38 $$ LANGUAGE PLPGSQL STRICT STABLE;
39
40 CREATE OR REPLACE FUNCTION migration_tools.openbiblio2marc (x_bibid TEXT) RETURNS TEXT AS $func$
41 BEGIN
42 -- Expects the following table/columns:
43
44 -- export_biblio_tsv:
45 -- l_bibid               | 1
46 -- l_create_dt           | 2007-03-07 09:03:09
47 -- l_last_change_dt      | 2015-01-23 11:18:54
48 -- l_last_change_userid  | 2
49 -- l_material_cd         | 10
50 -- l_collection_cd       | 13
51 -- l_call_nmbr1          | Canada
52 -- l_call_nmbr2          | ON
53 -- l_call_nmbr3          | Ottawa 18
54 -- l_title               | Art and the courts : France ad England
55 -- l_title_remainder     | from 1259-1328
56 -- l_responsibility_stmt |
57 -- l_author              | National Gallery of Canada
58 -- l_topic1              |
59 -- l_topic2              |
60 -- l_topic3              |
61 -- l_topic4              |
62 -- l_topic5              |
63 -- l_opac_flg            | Y
64 -- l_flag_attention      | 0
65
66 -- export_biblio_field_tsv:
67 -- l_bibid       | 1
68 -- l_fieldid     | 1
69 -- l_tag         | 720
70 -- l_ind1_cd     | N
71 -- l_ind2_cd     | N
72 -- l_subfield_cd | a
73 -- l_field_data  | Brieger, Peter Henry
74
75 -- Map export_biblio_tsv as follows:
76 -- l_call_nmbr?             -> 099a
77 -- l_author                 -> 100a
78 -- l_title                  -> 245a
79 -- l_title_remainder        -> 245b
80 -- l_responsibility_stmt    -> 245c
81 -- l_topic?                 -> 650a
82 -- l_bibid                  -> 001
83
84 RETURN
85     migration_tools.make_stub_bib(y.tag,y.ind1,y.ind2,y.data)
86 FROM (
87     select
88         array_agg(lpad(l_tag,3,'0') || l_subfield_cd) as "tag",
89         array_agg(l_ind1_cd) as "ind1",
90         array_agg(l_ind2_cd) as "ind2",
91         array_agg(l_field_data) as "data"
92     from (
93         select
94             l_tag,
95             l_subfield_cd,
96             l_ind1_cd,
97             l_ind2_cd,
98             l_field_data
99         from export_biblio_field_tsv
100         where l_bibid = x_bibid
101     union
102         select
103             '099' as "l_tag",
104             'a' as "l_subfield_cd",
105             ' ' as "l_ind1_cd",
106             ' ' as "l_ind2_cd",
107             concat_ws(' ',
108                 nullif(btrim(l_call_nmbr1),''),
109                 nullif(btrim(l_call_nmbr2),''),
110                 nullif(btrim(l_call_nmbr3),'')
111             ) as "l_field_data"
112         from export_biblio_tsv
113         where l_bibid = x_bibid
114     union
115         select
116             '100' as "l_tag",
117             'a' as "l_subfield_cd",
118             ' ' as "l_ind1_cd",
119             ' ' as "l_ind2_cd",
120             l_author as "l_field_data"
121         from export_biblio_tsv
122         where l_bibid = x_bibid and nullif(btrim(l_author),'') is not null
123     union
124         select
125             '245' as "l_tag",
126             'a' as "l_subfield_cd",
127             ' ' as "l_ind1_cd",
128             ' ' as "l_ind2_cd",
129             l_title as "l_field_data"
130         from export_biblio_tsv
131         where l_bibid = x_bibid and nullif(btrim(l_title),'') is not null
132     union
133         select
134             '245' as "l_tag",
135             'b' as "l_subfield_cd",
136             ' ' as "l_ind1_cd",
137             ' ' as "l_ind2_cd",
138             l_title_remainder as "l_field_data"
139         from export_biblio_tsv
140         where l_bibid = x_bibid and nullif(btrim(l_title_remainder),'') is not null
141     union
142         select
143             '650' as "l_tag",
144             'a' as "l_subfield_cd",
145             ' ' as "l_ind1_cd",
146             ' ' as "l_ind2_cd",
147             l_topic1 as "l_field_data"
148         from export_biblio_tsv
149         where l_bibid = x_bibid and nullif(btrim(l_topic1),'') is not null
150     union
151         select
152             '650' as "l_tag",
153             'a' as "l_subfield_cd",
154             ' ' as "l_ind1_cd",
155             ' ' as "l_ind2_cd",
156             l_topic2 as "l_field_data"
157         from export_biblio_tsv
158         where l_bibid = x_bibid and nullif(btrim(l_topic2),'') is not null
159     union
160         select
161             '650' as "l_tag",
162             'a' as "l_subfield_cd",
163             ' ' as "l_ind1_cd",
164             ' ' as "l_ind2_cd",
165             l_topic3 as "l_field_data"
166         from export_biblio_tsv
167         where l_bibid = x_bibid and nullif(btrim(l_topic3),'') is not null
168     union
169         select
170             '650' as "l_tag",
171             'a' as "l_subfield_cd",
172             ' ' as "l_ind1_cd",
173             ' ' as "l_ind2_cd",
174             l_topic4 as "l_field_data"
175         from export_biblio_tsv
176         where l_bibid = x_bibid and nullif(btrim(l_topic4),'') is not null
177     union
178         select
179             '650' as "l_tag",
180             'a' as "l_subfield_cd",
181             ' ' as "l_ind1_cd",
182             ' ' as "l_ind2_cd",
183             l_topic5 as "l_field_data"
184         from export_biblio_tsv
185         where l_bibid = x_bibid and nullif(btrim(l_topic5),'') is not null
186     union
187         select
188             '001' as "l_tag",
189             '' as "l_subfield_cd",
190             '' as "l_ind1_cd",
191             '' as "l_ind2_cd",
192             l_bibid as "l_field_data"
193         from export_biblio_tsv
194         where l_bibid = x_bibid
195     ) x
196 ) y;
197
198 END
199 $func$ LANGUAGE plpgsql;
200