Project

General

Profile

metacat / src / xmltables_postgres.sql @ 755

1
/*
2
 * xmltables-postgres.sql
3
 *             : Create or replace tables for storing XML in PostgreSQL
4
 *
5
 *      Purpose: creates tables needed for storing XML in PostgreSQL database
6
 * 
7
 *      Created: 08 May 2001 
8
 *       Author: John Harris
9
 * Organization: National Center for Ecological Analysis and Synthesis
10
 *    Copyright: 2000 Regents of the University of California and the
11
 *               National Center for Ecological Analysis and Synthesis
12
 *  For Details: http://www.nceas.ucsb.edu/
13
 *    File Info: '$Id: xmltables_postgres.sql 755 2001-05-29 18:09:52Z bojilova $'
14
 *
15
 *        this is sql script does the same as the sql script named 
16
 *        xmltables.sql except that this script is to be use to 
17
 *        create the database tables on a Postgresql backend rather
18
 *        than an Oracle Backend
19
 */
20

    
21
/*
22
 * Drop all of the objects in proper order
23
 */
24
set echo off
25

    
26
DROP SEQUENCE xml_nodes_id_seq;
27
DROP SEQUENCE xml_revisions_id_seq;
28
DROP SEQUENCE xml_catalog_id_seq;
29
DROP SEQUENCE xml_relation_id_seq;
30
DROP SEQUENCE xml_replication_id_seq;
31
DROP SEQUENCE accnum_uniqueid_seq;
32
DROP SEQUENCE xml_documents_id_seq;
33

    
34
DROP TABLE xml_index;
35
DROP TABLE xml_access;
36
DROP TABLE xml_revisions;
37
DROP TABLE xml_relation;
38
DROP TABLE xml_documents;
39
DROP TABLE xml_nodes;
40
DROP TABLE xml_replication;
41
DROP TABLE xml_catalog;
42

    
43
/*
44
 *Replication -- table to store servers that metacat is replicated to
45
 */
46
CREATE SEQUENCE xml_replication_id_seq;
47
CREATE TABLE xml_replication (
48
  serverid     INT8  default nextval('xml_replication_id_seq'), 
49
  server        VARCHAR(512),
50
  last_checked  DATE,
51
  replicate     INT8, 
52
  CONSTRAINT xml_replication_pk PRIMARY KEY (serverid)
53
);  
54

    
55
INSERT INTO xml_replication (serverid, server, replicate) VALUES ('1', 'localhost', '0');
56

    
57
/* 
58
 * Nodes -- table to store XML Nodes (both elements and attributes)
59
 */
60
CREATE TABLE xml_nodes (
61
        nodeid                INT8,                 -- the unique node id (pk)
62
        nodeindex        INT8,                -- order of nodes within parent
63
        nodetype        VARCHAR(20),        -- type (DOCUMENT, COMMENT, PI,
64
                                        -- ELEMENT, ATTRIBUTE, TEXT)
65
        nodename        VARCHAR(250),        -- the name of an element or attribute
66
        nodedata        VARCHAR(4000),         -- the data for this node (e.g., 
67
                                        -- for TEXT it is the content)
68
        parentnodeid        INT8,                -- index of the parent of this node
69
        rootnodeid        INT8,                -- index of the root node of this tree
70
        docid                VARCHAR(250),        -- index to the document id
71
        date_created        DATE,
72
        date_updated        DATE,
73
   CONSTRAINT xml_nodes_pk PRIMARY KEY (nodeid),
74
   CONSTRAINT xml_nodes_root_fk 
75
                FOREIGN KEY (rootnodeid) REFERENCES xml_nodes,
76
   CONSTRAINT xml_nodes_parent_fk 
77
                FOREIGN KEY (parentnodeid) REFERENCES xml_nodes
78
);
79

    
80
/* 
81
 * Indexes of rootnodeid & parentnodeid in xml_nodes
82
 */
83
CREATE INDEX xml_nodes_idx1 ON xml_nodes (rootnodeid);
84
CREATE INDEX xml_nodes_idx2 ON xml_nodes (parentnodeid);
85
CREATE INDEX xml_nodes_idx3 ON xml_nodes (nodename);
86
CREATE SEQUENCE xml_nodes_id_seq;
87

    
88

    
89
/* 
90
 * XML Catalog -- table to store all external sources for XML documents
91
 */
92
CREATE SEQUENCE xml_catalog_id_seq;
93
CREATE TABLE xml_catalog (
94
        catalog_id INT8 default nextval('xml_catalog_id_seq'), -- the id for this catalog entry
95
        entry_type        VARCHAR(500),        -- the type of this catalog entry
96
                                        -- (e.g., DTD, XSD, XSL)
97
        source_doctype        VARCHAR(500),        -- the source public_id for transforms
98
        target_doctype        VARCHAR(500),        -- the target public_id for transforms
99
        public_id        VARCHAR(500),        -- the unique id for this type
100
        system_id        VARCHAR(1000),        -- the local location of the object
101
   CONSTRAINT xml_catalog_pk PRIMARY KEY (catalog_id),
102
   CONSTRAINT xml_catalog_uk UNIQUE  (entry_type, source_doctype, target_doctype, public_id)
103
);
104

    
105

    
106
/* 
107
 * Sequence to get uniqueID for Accession #
108
 */
109
CREATE SEQUENCE xml_documents_id_seq;
110
/* 
111
 * Documents -- table to store XML documents
112
 */
113
CREATE TABLE xml_documents (
114
        docid                VARCHAR(250),        -- the document id #
115
        rootnodeid        INT8,        -- reference to root node of the DOM
116
        docname                VARCHAR(100),        -- usually the root element name
117
        doctype                VARCHAR(100),        -- public id indicating document type
118
        user_owner        VARCHAR(100),        -- the user owned the document
119
        user_updated        VARCHAR(100),        -- the user updated the document
120
        server_location INT8,        -- the server on which this document resides
121
        rev                 INT8,   --the revision number of the document
122
        date_created        DATE,
123
        date_updated        DATE,
124
        public_access        INT8,        -- flag for public access
125
        catalog_id      INT8,        -- reference to xml_catalog 
126
     CONSTRAINT xml_documents_pk PRIMARY KEY (docid),
127
     CONSTRAINT xml_documents_rep_fk
128
                     FOREIGN KEY (server_location) REFERENCES xml_replication, 
129
    CONSTRAINT xml_documents_root_fk 
130
                FOREIGN KEY (rootnodeid) REFERENCES xml_nodes,
131
   CONSTRAINT xml_documents_catalog_fk 
132
                FOREIGN KEY (catalog_id) REFERENCES xml_catalog
133
);
134

    
135
/* 
136
 * Index of <docid,doctype> in xml_document
137
 */
138
CREATE INDEX xml_documents_idx1 ON xml_documents (docid, doctype);
139

    
140
/* 
141
 * Revised Documents -- table to store XML documents saved after an UPDATE
142
 *                    or DELETE
143
 */
144
CREATE SEQUENCE xml_revisions_id_seq;
145
CREATE TABLE xml_revisions (
146
        revisionid        INT8  default nextval('xml_revisions_id_seq'), -- the revision number we are saving
147
        docid                VARCHAR(250),        -- the document id #
148
        rootnodeid        INT8,        -- reference to root node of the DOM
149
        docname                VARCHAR(100),        -- usually the root element name
150
        doctype                VARCHAR(100),        -- public id indicating document type
151
        user_owner        VARCHAR(100),
152
        user_updated        VARCHAR(100),
153
        server_location INT8,
154
        rev                INT8,
155
        date_created        DATE,
156
        date_updated        DATE,
157
        public_access        INT8,        -- flag for public access
158
        catalog_id      INT8,        -- reference to xml_catalog 
159
   CONSTRAINT xml_revisions_pk PRIMARY KEY (revisionid),
160
   CONSTRAINT xml_revisions_rep_fk
161
                FOREIGN KEY (server_location) REFERENCES xml_replication,
162
   CONSTRAINT xml_revisions_root_fk 
163
                FOREIGN KEY (rootnodeid) REFERENCES xml_nodes,
164
   CONSTRAINT xml_revisions_catalog_fk 
165
                FOREIGN KEY (catalog_id) REFERENCES xml_catalog
166
);
167

    
168

    
169
/* 
170
 * ACL -- table to store ACL for XML documents by principals
171
 */
172
CREATE TABLE xml_access (
173
        docid                VARCHAR(250),        -- the document id #
174
        accessfileid        VARCHAR(250),        -- the document id # for the access file
175
        principal_name        VARCHAR(100),        -- name of user, group, etc.
176
        permission        INT8,                -- "read", "write", "all"
177
        perm_type        VARCHAR(32),        -- "allowed" or "denied"
178
        perm_order        VARCHAR(32),        -- "allow first" or "deny first"
179
        begin_time        DATE,                -- the time that permission begins
180
        end_time        DATE,                -- the time that permission ends
181
        ticket_count        INT8,                -- ticket counter for that permission
182
   CONSTRAINT xml_access_ck CHECK (begin_time < end_time),
183
   CONSTRAINT xml_access_accessfileid_fk 
184
                FOREIGN KEY (accessfileid) REFERENCES xml_documents
185
);
186

    
187
/* 
188
 * Index of Nodes -- table to store precomputed paths through tree for 
189
 * quick searching in structured searches
190
 */
191
CREATE TABLE xml_index (
192
        nodeid                INT8,                -- the unique node id
193
        path                VARCHAR(200),        -- precomputed path through tree
194
        docid                VARCHAR(250),        -- index to the document id
195
        doctype                VARCHAR(100),        -- public id indicating document type
196
        parentnodeid    INT8,     -- id of the parent of the node represented
197
                                        -- by this row
198
   CONSTRAINT xml_index_pk PRIMARY KEY (nodeid,path),
199
   CONSTRAINT xml_index_nodeid_fk FOREIGN KEY (nodeid) REFERENCES xml_nodes,
200
   CONSTRAINT xml_index_docid_fk 
201
                FOREIGN KEY (docid) REFERENCES xml_documents
202
);
203

    
204
/* 
205
 * Index of the paths in xml_index 
206
 */
207
CREATE INDEX xml_index_idx1 ON xml_index (path);
208

    
209

    
210
CREATE SEQUENCE xml_relation_id_seq;
211
CREATE TABLE xml_relation (
212
        relationid    INT8 default nextval('xml_relation_id_seq')  PRIMARY KEY, -- unique id
213
        docid         VARCHAR(250) ,         -- the docid of the package file
214
                                              -- that this relation came from
215
        packagetype   VARCHAR(250),          -- the type of the package
216
        subject       VARCHAR(512) NOT NULL, -- the subject of the relation
217
        subdoctype    VARCHAR(128),                 -- the doctype of the subject
218
        relationship  VARCHAR(128)  NOT NULL,-- the relationship type
219
        object        VARCHAR(512) NOT NULL, -- the object of the relation
220
        objdoctype    VARCHAR(128),          -- the doctype of the object
221
        CONSTRAINT xml_relation_uk UNIQUE (subject, relationship, object),
222
        CONSTRAINT xml_relation_docid_fk 
223
                FOREIGN KEY (docid) REFERENCES xml_documents
224
  );
225

    
226