Project

General

Profile

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 1620 2003-04-26 22:20:46Z tao $'
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

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

    
33
DROP TABLE xml_index;
34
DROP TABLE xml_access;
35
DROP TABLE xml_accesssubtree;
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
DROP TABLE accession_number;
43

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

    
58
INSERT INTO xml_replication (server, replicate, datareplicate, hub) VALUES ('localhost', '0', '0', '0');
59

    
60
/* 
61
 * Nodes -- table to store XML Nodes (both elements and attributes)
62
 */
63
CREATE SEQUENCE xml_nodes_id_seq;
64
CREATE TABLE xml_nodes (
65
	nodeid INT8 default nextval('xml_nodes_id_seq'),
66
					-- the unique node id (pk)
67
	nodeindex INT8,		-- order of nodes within parent
68
	nodetype VARCHAR(20),	-- type (DOCUMENT, COMMENT, PI,
69
					-- ELEMENT, ATTRIBUTE, TEXT)
70
	nodename VARCHAR(250),	-- the name of an element or attribute
71
        nodeprefix VARCHAR(50), -- the namespace prefix of the node
72
	nodedata VARCHAR(4000), 	-- the data for this node (e.g., 
73
					-- for TEXT it is the content)
74
	parentnodeid INT8,		-- index of the parent of this node
75
	rootnodeid INT8,		-- index of the root node of this tree
76
	docid VARCHAR(250),	-- index to the document id
77
	date_created DATE,
78
	date_updated DATE,
79
   CONSTRAINT xml_nodes_pk PRIMARY KEY (nodeid),
80
   CONSTRAINT xml_nodes_root_fk 
81
		FOREIGN KEY (rootnodeid) REFERENCES xml_nodes,
82
   CONSTRAINT xml_nodes_parent_fk 
83
		FOREIGN KEY (parentnodeid) REFERENCES xml_nodes
84
);
85
/* 
86
 * Indexes of rootnodeid, parentnodeid, and nodename in xml_nodes
87
 */
88
CREATE INDEX xml_nodes_idx1 ON xml_nodes (rootnodeid);
89
CREATE INDEX xml_nodes_idx2 ON xml_nodes (parentnodeid);
90
CREATE INDEX xml_nodes_idx3 ON xml_nodes (nodename);
91

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

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

    
139
/* 
140
 * Index of <docid,doctype> in xml_document
141
 */
142
CREATE INDEX xml_documents_idx1 ON xml_documents (docid, doctype);
143

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

    
173

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

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

    
212
/* 
213
 * Index of the paths in xml_index 
214
 */
215
CREATE INDEX xml_index_idx1 ON xml_index (path);
216

    
217

    
218
CREATE SEQUENCE xml_relation_id_seq;
219
CREATE TABLE xml_relation (
220
	relationid INT8 default nextval('xml_relation_id_seq') PRIMARY KEY,
221
					     -- unique id
222
	docid VARCHAR(250) ,         -- the docid of the package file
223
	                                     -- that this relation came from
224
        packagetype VARCHAR(250),          -- the type of the package
225
	subject VARCHAR(512) NOT NULL, -- the subject of the relation
226
	subdoctype VARCHAR(128),         	-- the doctype of the subject
227
	relationship VARCHAR(128)  NOT NULL,-- the relationship type
228
	object VARCHAR(512) NOT NULL, -- the object of the relation
229
	objdoctype VARCHAR(128),          -- the doctype of the object
230
	CONSTRAINT xml_relation_uk UNIQUE (docid, subject, relationship, object),
231
	CONSTRAINT xml_relation_docid_fk 
232
		FOREIGN KEY (docid) REFERENCES xml_documents
233
);
234

    
235
/* 
236
 * Table used as Unique ID generator for the uniqueid part of Accession#
237
 */
238
CREATE SEQUENCE accession_number_id_seq;
239
CREATE TABLE accession_number (
240
   uniqueid INT8 default nextval('accession_number_id_seq') PRIMARY KEY,
241
   site_code VARCHAR(100),
242
   date_created DATE
243
);
244

    
245
/* 
246
 * accesssubtree -- table to store access subtree info 
247
 */
248
CREATE TABLE xml_accesssubtree (
249
	docid		VARCHAR(250),	-- the document id #
250
  rev 		INT8 default 1, --the revision number of the docume
251
  controllevel VARCHAR(50), -- the level it control -- document or subtree
252
  subtreeid VARCHAR(250), -- the subtree id 
253
	startnodeid	INT8,	-- the start node id of access subtree
254
  endnodeid INT8, -- the end node if of access subtree
255
  CONSTRAINT xml_accesssubtree_docid_fk 
256
		FOREIGN KEY (docid) REFERENCES xml_documents
257
);
258

    
(18-18/18)