]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/002.schema.config.sql
75bb60bc6ebb3d382571689ca16951ebbc12e452
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 002.schema.config.sql
1 /*
2  * Copyright (C) 2004-2008  Georgia Public Library Service
3  * Copyright (C) 2008  Equinox Software, Inc.
4  * Mike Rylander <miker@esilibrary.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18
19
20 DROP SCHEMA stats CASCADE;
21 DROP SCHEMA config CASCADE;
22
23 BEGIN;
24 CREATE SCHEMA stats;
25
26 CREATE SCHEMA config;
27 COMMENT ON SCHEMA config IS $$
28 /*
29  * Copyright (C) 2005  Georgia Public Library Service 
30  * Mike Rylander <mrylander@gmail.com>
31  *
32  * The config schema holds static configuration data for the
33  * Open-ILS installation.
34  *
35  * ****
36  *
37  * This program is free software; you can redistribute it and/or
38  * modify it under the terms of the GNU General Public License
39  * as published by the Free Software Foundation; either version 2
40  * of the License, or (at your option) any later version.
41  *
42  * This program is distributed in the hope that it will be useful,
43  * but WITHOUT ANY WARRANTY; without even the implied warranty of
44  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45  * GNU General Public License for more details.
46  */
47 $$;
48
49 CREATE TABLE config.internal_flag (
50     name    TEXT    PRIMARY KEY,
51     value   TEXT,
52     enabled BOOL    NOT NULL DEFAULT FALSE
53 );
54 INSERT INTO config.internal_flag (name) VALUES ('ingest.metarecord_mapping.skip_on_insert');
55 INSERT INTO config.internal_flag (name) VALUES ('ingest.reingest.force_on_same_marc');
56 INSERT INTO config.internal_flag (name) VALUES ('ingest.reingest.skip_located_uri');
57
58 CREATE TABLE config.upgrade_log (
59     version         TEXT    PRIMARY KEY,
60     install_date    TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
61 );
62
63 INSERT INTO config.upgrade_log (version) VALUES ('0216'); -- miker
64
65 CREATE TABLE config.bib_source (
66         id              SERIAL  PRIMARY KEY,
67         quality         INT     CHECK ( quality BETWEEN 0 AND 100 ),
68         source          TEXT    NOT NULL UNIQUE,
69         transcendant    BOOL    NOT NULL DEFAULT FALSE
70 );
71 COMMENT ON TABLE config.bib_source IS $$
72 /*
73  * Copyright (C) 2005  Georgia Public Library Service 
74  * Mike Rylander <mrylander@gmail.com>
75  *
76  * Valid sources of MARC records
77  *
78  * This is table is used to set up the relative "quality" of each
79  * MARC source, such as OCLC.
80  *
81  * ****
82  *
83  * This program is free software; you can redistribute it and/or
84  * modify it under the terms of the GNU General Public License
85  * as published by the Free Software Foundation; either version 2
86  * of the License, or (at your option) any later version.
87  *
88  * This program is distributed in the hope that it will be useful,
89  * but WITHOUT ANY WARRANTY; without even the implied warranty of
90  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
91  * GNU General Public License for more details.
92  */
93 $$;
94
95 CREATE TABLE config.standing (
96         id              SERIAL  PRIMARY KEY,
97         value           TEXT    NOT NULL UNIQUE
98 );
99 COMMENT ON TABLE config.standing IS $$
100 /*
101  * Copyright (C) 2005  Georgia Public Library Service 
102  * Mike Rylander <mrylander@gmail.com>
103  *
104  * Patron Standings
105  *
106  * This table contains the values that can be applied to a patron
107  * by a staff member.  These values should not be changed, other
108  * than for translation, as the ID column is currently a "magic
109  * number" in the source. :(
110  *
111  * ****
112  *
113  * This program is free software; you can redistribute it and/or
114  * modify it under the terms of the GNU General Public License
115  * as published by the Free Software Foundation; either version 2
116  * of the License, or (at your option) any later version.
117  *
118  * This program is distributed in the hope that it will be useful,
119  * but WITHOUT ANY WARRANTY; without even the implied warranty of
120  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121  * GNU General Public License for more details.
122  */
123 $$;
124
125 CREATE TABLE config.standing_penalty (
126         id                      SERIAL  PRIMARY KEY,
127         name            TEXT    NOT NULL UNIQUE,
128         label           TEXT    NOT NULL,
129         block_list      TEXT,
130         org_depth       INTEGER
131 );
132 INSERT INTO config.standing_penalty (id,name,label,block_list)
133         VALUES (1,'PATRON_EXCEEDS_FINES','Patron exceeds fine threshold','CIRC|HOLD|RENEW');
134 INSERT INTO config.standing_penalty (id,name,label,block_list)
135         VALUES (2,'PATRON_EXCEEDS_OVERDUE_COUNT','Patron exceeds max overdue item threshold','CIRC|HOLD|RENEW');
136 INSERT INTO config.standing_penalty (id,name,label,block_list)
137         VALUES (3,'PATRON_EXCEEDS_CHECKOUT_COUNT','Patron exceeds max checked out item threshold','CIRC');
138 INSERT INTO config.standing_penalty (id,name,label,block_list)
139         VALUES (4,'PATRON_EXCEEDS_COLLECTIONS_WARNING','Patron exceeds pre-collections warning fine threshold','CIRC|HOLD|RENEW');
140
141 INSERT INTO config.standing_penalty (id,name,label) VALUES (20,'ALERT_NOTE','Alerting Note, no blocks');
142 INSERT INTO config.standing_penalty (id,name,label) VALUES (21,'SILENT_NOTE','Note, no blocks');
143 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (22,'STAFF_C','Alerting block on Circ','CIRC');
144 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (23,'STAFF_CH','Alerting block on Circ and Hold','CIRC|HOLD');
145 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (24,'STAFF_CR','Alerting block on Circ and Renew','CIRC|RENEW');
146 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (25,'STAFF_CHR','Alerting block on Circ, Hold and Renew','CIRC|HOLD|RENEW');
147 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (26,'STAFF_HR','Alerting block on Hold and Renew','HOLD|RENEW');
148 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (27,'STAFF_H','Alerting block on Hold','HOLD');
149 INSERT INTO config.standing_penalty (id,name,label,block_list) VALUES (28,'STAFF_R','Alerting block on Renew','RENEW');
150 INSERT INTO config.standing_penalty (id,name,label) VALUES (29,'INVALID_PATRON_ADDRESS','Patron has an invalid address');
151 INSERT INTO config.standing_penalty (id,name,label) VALUES (30,'PATRON_IN_COLLECTIONS','Patron has been referred to a collections agency');
152
153 SELECT SETVAL('config.standing_penalty_id_seq', 100);
154
155 CREATE TABLE config.xml_transform (
156         name            TEXT    PRIMARY KEY,
157         namespace_uri   TEXT    NOT NULL,
158         prefix          TEXT    NOT NULL,
159         xslt            TEXT    NOT NULL
160 );
161
162 CREATE TABLE config.biblio_fingerprint (
163         id                      SERIAL  PRIMARY KEY,
164         name            TEXT    NOT NULL, 
165         xpath           TEXT    NOT NULL,
166     first_word  BOOL    NOT NULL DEFAULT FALSE,
167         format          TEXT    NOT NULL DEFAULT 'marcxml'
168 );
169
170 INSERT INTO config.biblio_fingerprint (name, xpath, format)
171     VALUES (
172         'Title',
173         '//marc:datafield[@tag="700"]/marc:subfield[@code="t"]|' ||
174             '//marc:datafield[@tag="240"]/marc:subfield[@code="a"]|' ||
175             '//marc:datafield[@tag="242"]/marc:subfield[@code="a"]|' ||
176             '//marc:datafield[@tag="246"]/marc:subfield[@code="a"]|' ||
177             '//marc:datafield[@tag="245"]/marc:subfield[@code="a"]',
178         'marcxml'
179     );
180
181 INSERT INTO config.biblio_fingerprint (name, xpath, format, first_word)
182     VALUES (
183         'Author',
184         '//marc:datafield[@tag="700" and ./*[@code="t"]]/marc:subfield[@code="a"]|'
185             '//marc:datafield[@tag="100"]/marc:subfield[@code="a"]|'
186             '//marc:datafield[@tag="110"]/marc:subfield[@code="a"]|'
187             '//marc:datafield[@tag="111"]/marc:subfield[@code="a"]|'
188             '//marc:datafield[@tag="260"]/marc:subfield[@code="b"]',
189         'marcxml',
190         TRUE
191     );
192
193 CREATE TABLE config.metabib_class (
194     name    TEXT    PRIMARY KEY,
195     label   TEXT    NOT NULL UNIQUE
196 );
197
198 CREATE TABLE config.metabib_field (
199         id              SERIAL  PRIMARY KEY,
200         field_class     TEXT    NOT NULL REFERENCES config.metabib_class (name),
201         name            TEXT    NOT NULL,
202         label           TEXT    NOT NULL,
203         xpath           TEXT    NOT NULL,
204         weight          INT     NOT NULL DEFAULT 1,
205         format          TEXT    NOT NULL DEFAULT 'mods33',
206         search_field    BOOL    NOT NULL DEFAULT TRUE,
207         facet_field     BOOL    NOT NULL DEFAULT FALSE
208 );
209 COMMENT ON TABLE config.metabib_field IS $$
210 /*
211  * Copyright (C) 2005  Georgia Public Library Service 
212  * Mike Rylander <mrylander@gmail.com>
213  *
214  * XPath used for record indexing ingest
215  *
216  * This table contains the XPath used to chop up MODS into its
217  * indexable parts.  Each XPath entry is named and assigned to
218  * a "class" of either title, subject, author, keyword or series.
219  * 
220  *
221  * ****
222  *
223  * This program is free software; you can redistribute it and/or
224  * modify it under the terms of the GNU General Public License
225  * as published by the Free Software Foundation; either version 2
226  * of the License, or (at your option) any later version.
227  *
228  * This program is distributed in the hope that it will be useful,
229  * but WITHOUT ANY WARRANTY; without even the implied warranty of
230  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
231  * GNU General Public License for more details.
232  */
233 $$;
234
235 CREATE UNIQUE INDEX config_metabib_field_class_name_idx ON config.metabib_field (field_class, name);
236
237 CREATE TABLE config.metabib_search_alias (
238     alias       TEXT    PRIMARY KEY,
239     field_class TEXT    NOT NULL REFERENCES config.metabib_class (name),
240     field       INT     REFERENCES config.metabib_field (id)
241 );
242
243 CREATE TABLE config.non_cataloged_type (
244         id              SERIAL          PRIMARY KEY,
245         owning_lib      INT             NOT NULL, -- REFERENCES actor.org_unit (id),
246         name            TEXT            NOT NULL,
247         circ_duration   INTERVAL        NOT NULL DEFAULT '14 days'::INTERVAL,
248         in_house        BOOL            NOT NULL DEFAULT FALSE,
249         CONSTRAINT noncat_once_per_lib UNIQUE (owning_lib,name)
250 );
251 COMMENT ON TABLE config.non_cataloged_type IS $$
252 /*
253  * Copyright (C) 2005  Georgia Public Library Service 
254  * Mike Rylander <mrylander@gmail.com>
255  *
256  * Types of valid non-cataloged items.
257  *
258  *
259  * ****
260  *
261  * This program is free software; you can redistribute it and/or
262  * modify it under the terms of the GNU General Public License
263  * as published by the Free Software Foundation; either version 2
264  * of the License, or (at your option) any later version.
265  *
266  * This program is distributed in the hope that it will be useful,
267  * but WITHOUT ANY WARRANTY; without even the implied warranty of
268  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
269  * GNU General Public License for more details.
270  */
271 $$;
272
273 CREATE TABLE config.identification_type (
274         id              SERIAL  PRIMARY KEY,
275         name            TEXT    NOT NULL UNIQUE
276 );
277 COMMENT ON TABLE config.identification_type IS $$
278 /*
279  * Copyright (C) 2005  Georgia Public Library Service 
280  * Mike Rylander <mrylander@gmail.com>
281  *
282  * Types of valid patron identification.
283  *
284  * Each patron must display at least one valid form of identification
285  * in order to get a library card.  This table lists those forms.
286  * 
287  *
288  * ****
289  *
290  * This program is free software; you can redistribute it and/or
291  * modify it under the terms of the GNU General Public License
292  * as published by the Free Software Foundation; either version 2
293  * of the License, or (at your option) any later version.
294  *
295  * This program is distributed in the hope that it will be useful,
296  * but WITHOUT ANY WARRANTY; without even the implied warranty of
297  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
298  * GNU General Public License for more details.
299  */
300 $$;
301
302 CREATE TABLE config.rule_circ_duration (
303         id              SERIAL          PRIMARY KEY,
304         name            TEXT            NOT NULL UNIQUE CHECK ( name ~ E'^\\w+$' ),
305         extended        INTERVAL        NOT NULL,
306         normal          INTERVAL        NOT NULL,
307         shrt            INTERVAL        NOT NULL,
308         max_renewals    INT             NOT NULL
309 );
310 COMMENT ON TABLE config.rule_circ_duration IS $$
311 /*
312  * Copyright (C) 2005  Georgia Public Library Service 
313  * Mike Rylander <mrylander@gmail.com>
314  *
315  * Circulation Duration rules
316  *
317  * Each circulation is given a duration based on one of these rules.
318  * 
319  *
320  * ****
321  *
322  * This program is free software; you can redistribute it and/or
323  * modify it under the terms of the GNU General Public License
324  * as published by the Free Software Foundation; either version 2
325  * of the License, or (at your option) any later version.
326  *
327  * This program is distributed in the hope that it will be useful,
328  * but WITHOUT ANY WARRANTY; without even the implied warranty of
329  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
330  * GNU General Public License for more details.
331  */
332 $$;
333
334 CREATE TABLE config.rule_max_fine (
335     id          SERIAL          PRIMARY KEY,
336     name        TEXT            NOT NULL UNIQUE CHECK ( name ~ E'^\\w+$' ),
337     amount      NUMERIC(6,2)    NOT NULL,
338     is_percent  BOOL            NOT NULL DEFAULT FALSE
339 );
340 COMMENT ON TABLE config.rule_max_fine IS $$
341 /*
342  * Copyright (C) 2005  Georgia Public Library Service 
343  * Mike Rylander <mrylander@gmail.com>
344  *
345  * Circulation Max Fine rules
346  *
347  * Each circulation is given a maximum fine based on one of
348  * these rules.
349  * 
350  *
351  * ****
352  *
353  * This program is free software; you can redistribute it and/or
354  * modify it under the terms of the GNU General Public License
355  * as published by the Free Software Foundation; either version 2
356  * of the License, or (at your option) any later version.
357  *
358  * This program is distributed in the hope that it will be useful,
359  * but WITHOUT ANY WARRANTY; without even the implied warranty of
360  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
361  * GNU General Public License for more details.
362  */
363 $$;
364
365 CREATE TABLE config.rule_recurring_fine (
366         id                      SERIAL          PRIMARY KEY,
367         name                    TEXT            NOT NULL UNIQUE CHECK ( name ~ E'^\\w+$' ),
368         high                    NUMERIC(6,2)    NOT NULL,
369         normal                  NUMERIC(6,2)    NOT NULL,
370         low                     NUMERIC(6,2)    NOT NULL,
371         recurrence_interval     INTERVAL        NOT NULL DEFAULT '1 day'::INTERVAL
372 );
373 COMMENT ON TABLE config.rule_recurring_fine IS $$
374 /*
375  * Copyright (C) 2005  Georgia Public Library Service 
376  * Mike Rylander <mrylander@gmail.com>
377  *
378  * Circulation Recurring Fine rules
379  *
380  * Each circulation is given a recurring fine amount based on one of
381  * these rules.  The recurrence_interval should not be any shorter
382  * than the interval between runs of the fine_processor.pl script
383  * (which is run from CRON), or you could miss fines.
384  * 
385  *
386  * ****
387  *
388  * This program is free software; you can redistribute it and/or
389  * modify it under the terms of the GNU General Public License
390  * as published by the Free Software Foundation; either version 2
391  * of the License, or (at your option) any later version.
392  *
393  * This program is distributed in the hope that it will be useful,
394  * but WITHOUT ANY WARRANTY; without even the implied warranty of
395  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
396  * GNU General Public License for more details.
397  */
398 $$;
399
400
401 CREATE TABLE config.rule_age_hold_protect (
402         id      SERIAL          PRIMARY KEY,
403         name    TEXT            NOT NULL UNIQUE CHECK ( name ~ E'^\\w+$' ),
404         age     INTERVAL        NOT NULL,
405         prox    INT             NOT NULL
406 );
407 COMMENT ON TABLE config.rule_age_hold_protect IS $$
408 /*
409  * Copyright (C) 2005  Georgia Public Library Service 
410  * Mike Rylander <mrylander@gmail.com>
411  *
412  * Hold Item Age Protection rules
413  *
414  * A hold request can only capture new(ish) items when they are
415  * within a particular proximity of the home_ou of the requesting
416  * user.  The proximity ('prox' column) is calculated by counting
417  * the number of tree edges between the user's home_ou and the owning_lib
418  * of the copy that could fulfill the hold.
419  * 
420  *
421  * ****
422  *
423  * This program is free software; you can redistribute it and/or
424  * modify it under the terms of the GNU General Public License
425  * as published by the Free Software Foundation; either version 2
426  * of the License, or (at your option) any later version.
427  *
428  * This program is distributed in the hope that it will be useful,
429  * but WITHOUT ANY WARRANTY; without even the implied warranty of
430  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
431  * GNU General Public License for more details.
432  */
433 $$;
434
435 CREATE TABLE config.copy_status (
436         id              SERIAL  PRIMARY KEY,
437         name            TEXT    NOT NULL UNIQUE,
438         holdable        BOOL    NOT NULL DEFAULT FALSE,
439         opac_visible    BOOL    NOT NULL DEFAULT FALSE
440 );
441 COMMENT ON TABLE config.copy_status IS $$
442 /*
443  * Copyright (C) 2005  Georgia Public Library Service 
444  * Mike Rylander <mrylander@gmail.com>
445  *
446  * Copy Statuses
447  *
448  * The available copy statuses, and whether a copy in that
449  * status is available for hold request capture.  0 (zero) is
450  * the only special number in this set, meaning that the item
451  * is available for immediate checkout, and is counted as available
452  * in the OPAC.
453  *
454  * Statuses with an ID below 100 are not removable, and have special
455  * meaning in the code.  Do not change them except to translate the
456  * textual name.
457  *
458  * You may add and remove statuses above 100, and these can be used
459  * to remove items from normal circulation without affecting the rest
460  * of the copy's values or its location.
461  *
462  * ****
463  *
464  * This program is free software; you can redistribute it and/or
465  * modify it under the terms of the GNU General Public License
466  * as published by the Free Software Foundation; either version 2
467  * of the License, or (at your option) any later version.
468  *
469  * This program is distributed in the hope that it will be useful,
470  * but WITHOUT ANY WARRANTY; without even the implied warranty of
471  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
472  * GNU General Public License for more details.
473  */
474 $$;
475
476 CREATE TABLE config.net_access_level (
477         id      SERIAL          PRIMARY KEY,
478         name    TEXT            NOT NULL UNIQUE
479 );
480 COMMENT ON TABLE config.net_access_level IS $$
481 /*
482  * Copyright (C) 2005  Georgia Public Library Service 
483  * Mike Rylander <mrylander@gmail.com>
484  *
485  * Patron Network Access level
486  *
487  * This will be used to inform the in-library firewall of how much
488  * internet access the using patron should be allowed.
489  *
490  * ****
491  *
492  * This program is free software; you can redistribute it and/or
493  * modify it under the terms of the GNU General Public License
494  * as published by the Free Software Foundation; either version 2
495  * of the License, or (at your option) any later version.
496  *
497  * This program is distributed in the hope that it will be useful,
498  * but WITHOUT ANY WARRANTY; without even the implied warranty of
499  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
500  * GNU General Public License for more details.
501  */
502 $$;
503
504
505 CREATE TABLE config.remote_account (
506     id          SERIAL  PRIMARY KEY,
507     label       TEXT    NOT NULL,
508     host        TEXT    NOT NULL,   -- name or IP, :port optional
509     username    TEXT,               -- optional, since we could default to $USER
510     password    TEXT,               -- optional, since we could use SSH keys, or anonymous login.
511     account     TEXT,               -- aka profile or FTP "account" command
512     path        TEXT,               -- aka directory
513     owner       INT     NOT NULL,   -- REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
514     last_activity TIMESTAMP WITH TIME ZONE
515 );
516
517 CREATE TABLE config.audience_map (
518         code            TEXT    PRIMARY KEY,
519         value           TEXT    NOT NULL,
520         description     TEXT
521 );
522
523 CREATE TABLE config.lit_form_map (
524         code            TEXT    PRIMARY KEY,
525         value           TEXT    NOT NULL,
526         description     TEXT
527 );
528
529 CREATE TABLE config.language_map (
530         code    TEXT    PRIMARY KEY,
531         value   TEXT    NOT NULL
532 );
533
534 CREATE TABLE config.item_form_map (
535         code    TEXT    PRIMARY KEY,
536         value   TEXT    NOT NULL
537 );
538
539 CREATE TABLE config.item_type_map (
540         code    TEXT    PRIMARY KEY,
541         value   TEXT    NOT NULL
542 );
543
544 CREATE TABLE config.bib_level_map (
545         code    TEXT    PRIMARY KEY,
546         value   TEXT    NOT NULL
547 );
548
549 CREATE TABLE config.marc21_rec_type_map (
550     code        TEXT    PRIMARY KEY,
551     type_val    TEXT    NOT NULL,
552     blvl_val    TEXT    NOT NULL
553 );
554
555 CREATE TABLE config.marc21_ff_pos_map (
556     id          SERIAL  PRIMARY KEY,
557     fixed_field TEXT    NOT NULL,
558     tag         TEXT    NOT NULL,
559     rec_type    TEXT    NOT NULL,
560     start_pos   INT     NOT NULL,
561     length      INT     NOT NULL,
562     default_val TEXT    NOT NULL DEFAULT ' '
563 );
564
565 CREATE TABLE config.marc21_physical_characteristic_type_map (
566     ptype_key   TEXT    PRIMARY KEY,
567     label       TEXT    NOT NULL -- I18N
568 );
569
570 CREATE TABLE config.marc21_physical_characteristic_subfield_map (
571     id          SERIAL  PRIMARY KEY,
572     ptype_key   TEXT    NOT NULL REFERENCES config.marc21_physical_characteristic_type_map (ptype_key) ON DELETE CASCADE ON UPDATE CASCADE,
573     subfield    TEXT    NOT NULL,
574     start_pos   INT     NOT NULL,
575     length      INT     NOT NULL,
576     label       TEXT    NOT NULL -- I18N
577 );
578
579 CREATE TABLE config.marc21_physical_characteristic_value_map (
580     id              SERIAL  PRIMARY KEY,
581     value           TEXT    NOT NULL,
582     ptype_subfield  INT     NOT NULL REFERENCES config.marc21_physical_characteristic_subfield_map (id),
583     label           TEXT    NOT NULL -- I18N
584 );
585
586
587 CREATE TABLE config.z3950_source (
588     name                TEXT    PRIMARY KEY,
589     label               TEXT    NOT NULL UNIQUE,
590     host                TEXT    NOT NULL,
591     port                INT     NOT NULL,
592     db                  TEXT    NOT NULL,
593     record_format       TEXT    NOT NULL DEFAULT 'FI',
594     transmission_format TEXT    NOT NULL DEFAULT 'usmarc',
595     auth                BOOL    NOT NULL DEFAULT TRUE
596 );
597
598 COMMENT ON TABLE config.z3950_source IS $$
599 Z39.50 Sources
600
601 Each row in this table represents a database searchable via Z39.50.
602 $$;
603
604 COMMENT ON COLUMN config.z3950_source.record_format IS $$
605 Z39.50 element set.
606 $$;
607
608 COMMENT ON COLUMN config.z3950_source.transmission_format IS $$
609 Z39.50 preferred record syntax..
610 $$;
611
612
613 CREATE TABLE config.z3950_attr (
614     id          SERIAL  PRIMARY KEY,
615     source      TEXT    NOT NULL REFERENCES config.z3950_source (name) DEFERRABLE INITIALLY DEFERRED,
616     name        TEXT    NOT NULL,
617     label       TEXT    NOT NULL,
618     code        INT     NOT NULL,
619     format      INT     NOT NULL,
620     truncation  INT     NOT NULL DEFAULT 0,
621     CONSTRAINT z_code_format_once_per_source UNIQUE (code,format,source)
622 );
623
624 CREATE TABLE config.i18n_locale (
625     code        TEXT    PRIMARY KEY,
626     marc_code   TEXT    NOT NULL REFERENCES config.language_map (code) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
627     name        TEXT    UNIQUE NOT NULL,
628     description TEXT
629 );
630
631 CREATE TABLE config.i18n_core (
632     id              BIGSERIAL   PRIMARY KEY,
633     fq_field        TEXT        NOT NULL,
634     identity_value  TEXT        NOT NULL,
635     translation     TEXT        NOT NULL    REFERENCES config.i18n_locale (code) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
636     string          TEXT        NOT NULL
637 );
638
639 CREATE UNIQUE INDEX i18n_identity ON config.i18n_core (fq_field,identity_value,translation);
640
641 CREATE TABLE config.billing_type (
642     id              SERIAL  PRIMARY KEY,
643     name            TEXT    NOT NULL,
644     owner           INT     NOT NULL, -- REFERENCES actor.org_unit (id)
645     default_price   NUMERIC(6,2),
646     CONSTRAINT billing_type_once_per_lib UNIQUE (name, owner)
647 );
648
649 CREATE TABLE config.org_unit_setting_type (
650     name            TEXT    PRIMARY KEY,
651     label           TEXT    UNIQUE NOT NULL,
652     description     TEXT,
653     datatype        TEXT    NOT NULL DEFAULT 'string',
654     fm_class        TEXT,
655     view_perm       INT,
656     update_perm     INT,
657     --
658     -- define valid datatypes
659     --
660     CONSTRAINT coust_valid_datatype CHECK ( datatype IN
661     ( 'bool', 'integer', 'float', 'currency', 'interval',
662       'date', 'string', 'object', 'array', 'link' ) ),
663     --
664     -- fm_class is meaningful only for 'link' datatype
665     --
666     CONSTRAINT coust_no_empty_link CHECK
667     ( ( datatype =  'link' AND fm_class IS NOT NULL ) OR
668       ( datatype <> 'link' AND fm_class IS NULL ) )
669 );
670
671 CREATE TABLE config.usr_setting_type (
672
673     name TEXT PRIMARY KEY,
674     opac_visible BOOL NOT NULL DEFAULT FALSE,
675     label TEXT UNIQUE NOT NULL,
676     description TEXT,
677     datatype TEXT NOT NULL DEFAULT 'string',
678     fm_class TEXT,
679
680     --
681     -- define valid datatypes
682     --
683     CONSTRAINT coust_valid_datatype CHECK ( datatype IN
684     ( 'bool', 'integer', 'float', 'currency', 'interval',
685         'date', 'string', 'object', 'array', 'link' ) ),
686
687     --
688     -- fm_class is meaningful only for 'link' datatype
689     --
690     CONSTRAINT coust_no_empty_link CHECK
691     ( ( datatype = 'link' AND fm_class IS NOT NULL ) OR
692         ( datatype <> 'link' AND fm_class IS NULL ) )
693
694 );
695
696 -- Some handy functions, based on existing ones, to provide optional ingest normalization
697
698 CREATE OR REPLACE FUNCTION public.left_trunc( TEXT, INT ) RETURNS TEXT AS $func$
699         SELECT SUBSTRING($1,$2);
700 $func$ LANGUAGE SQL STRICT IMMUTABLE;
701
702 CREATE OR REPLACE FUNCTION public.right_trunc( TEXT, INT ) RETURNS TEXT AS $func$
703         SELECT SUBSTRING($1,1,$2);
704 $func$ LANGUAGE SQL STRICT IMMUTABLE;
705
706 CREATE OR REPLACE FUNCTION public.split_date_range( TEXT ) RETURNS TEXT AS $func$
707         SELECT REGEXP_REPLACE( $1, E'(\\d{4})-(\\d{4})', E'\\1 \\2', 'g' );
708 $func$ LANGUAGE SQL STRICT IMMUTABLE;
709
710 -- And ... a table in which to register them
711
712 CREATE TABLE config.index_normalizer (
713         id              SERIAL  PRIMARY KEY,
714         name            TEXT    UNIQUE NOT NULL,
715         description     TEXT,
716         func            TEXT    NOT NULL,
717         param_count     INT     NOT NULL DEFAULT 0
718 );
719
720 CREATE TABLE config.metabib_field_index_norm_map (
721         id      SERIAL  PRIMARY KEY,
722         field   INT     NOT NULL REFERENCES config.metabib_field (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
723         norm    INT     NOT NULL REFERENCES config.index_normalizer (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
724         params  TEXT,
725         pos     INT     NOT NULL DEFAULT 0
726 );
727
728 CREATE OR REPLACE FUNCTION oils_tsearch2 () RETURNS TRIGGER AS $$
729 DECLARE
730         normalizer      RECORD;
731         value           TEXT := '';
732 BEGIN
733         IF NEW.index_vector = ''::tsvector THEN
734             RETURN NEW;
735         END IF;
736
737         value := NEW.value;
738
739         IF TG_TABLE_NAME::TEXT ~ 'field_entry$' THEN
740                 FOR normalizer IN
741                         SELECT  n.func AS func,
742                                 n.param_count AS param_count,
743                                 m.params AS params
744                           FROM  config.index_normalizer n
745                                 JOIN config.metabib_field_index_norm_map m ON (m.norm = n.id)
746                           WHERE field = NEW.field
747                           ORDER BY m.pos
748                 LOOP
749                         EXECUTE 'SELECT ' || normalizer.func || '(' ||
750                                         quote_literal( value ) ||
751                                         CASE
752                                                 WHEN normalizer.param_count > 0 THEN ',' || BTRIM(normalizer.params,'[]')
753                                                 ELSE ''
754                                         END ||
755                                 ')' INTO value;
756
757                 END LOOP;
758         END IF;
759
760         IF REGEXP_REPLACE(VERSION(),E'^.+?(\\d+\\.\\d+).*?$',E'\\1')::FLOAT > 8.2 THEN
761                 NEW.index_vector = to_tsvector((TG_ARGV[0])::regconfig, value);
762         ELSE
763                 NEW.index_vector = to_tsvector(TG_ARGV[0], value);
764         END IF;
765
766         RETURN NEW;
767 END;
768 $$ LANGUAGE PLPGSQL;
769
770 COMMIT;