-- fill up the database. with the operators
-- good luck, Chris


CREATE TABLE institute_t (
	id		SERIAL PRIMARY KEY,
	name 		VARCHAR(48) UNIQUE,
	street 		VARCHAR(48) NOT NULL,
	zip 		VARCHAR(16),
	town 		VARCHAR(32) NOT NULL,		
	country 	CHAR(2) NOT NULL, -- refer to ISO-3166
	phone 		VARCHAR(32) NOT NULL,
	fax 		VARCHAR(32)
);
GRANT SELECT ON institute_t TO anonym;
GRANT SELECT ON institute_t TO helper;
GRANT SELECT ON institute_t TO admin;
GRANT INSERT ON institute_t TO admin;
GRANT DELETE ON institute_t TO admin;
GRANT UPDATE ON institute_t TO admin;

CREATE TABLE user_t (
	id		SERIAL PRIMARY KEY,
	login 		VARCHAR(16) UNIQUE,
	password 	VARCHAR(16) NOT NULL,
	email 		VARCHAR(32) NOT NULL,
	firstname 	VARCHAR(16),
	surname		VARCHAR(32),
	street 		VARCHAR(48),
	zip 		VARCHAR(16),
	town 		VARCHAR(32),		
	country 	CHAR(2), -- refer to ISO-3166
	phone 		VARCHAR(32),
	fax 		VARCHAR(32),
	access		INTEGER NOT NULL,
	institute_id	INTEGER REFERENCES institute_t
		ON UPDATE CASCADE
);
-- user_t permissions: pls look below

CREATE TABLE entry_t (
	id 		SERIAL 	PRIMARY KEY,
	ent_seq		INTEGER,
	upd_date	DATE 	NOT NULL,
	upd_txt		TEXT,
	upd_author	INTEGER REFERENCES user_t
		ON UPDATE CASCADE,
	chk_date	DATE	NOT NULL,
	chk_txt		TEXT,
	chk_author	INTEGER REFERENCES user_t ON UPDATE CASCADE
);
GRANT SELECT ON entry_t TO anonym;
GRANT SELECT ON entry_t TO helper;
GRANT SELECT ON entry_t TO admin;
GRANT INSERT ON entry_t TO admin;
GRANT DELETE ON entry_t TO admin;
GRANT UPDATE ON entry_t TO admin;


CREATE TABLE dial_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT	UNIQUE,
	descr	TEXT
);
GRANT SELECT ON dial_t TO anonym;
GRANT SELECT ON dial_t TO helper;
GRANT SELECT ON dial_t TO admin;
GRANT INSERT ON dial_t TO admin;
GRANT DELETE ON dial_t TO admin;
GRANT UPDATE ON dial_t TO admin;

CREATE TABLE dial_entry_t (
	dial	INTEGER REFERENCES dial_t ON DELETE CASCADE,
	entry	INTEGER REFERENCES entry_t ON DELETE CASCADE,
	PRIMARY KEY (dial,entry)
);
GRANT SELECT ON dial_entry_t TO anonym;
GRANT SELECT ON dial_entry_t TO helper;
GRANT SELECT ON dial_entry_t TO admin;
GRANT INSERT ON dial_entry_t TO admin;
GRANT DELETE ON dial_entry_t TO admin;
GRANT UPDATE ON dial_entry_t TO admin;

CREATE TABLE lang_t (
	iso	CHAR(2) PRIMARY KEY
);
GRANT SELECT ON lang_t TO anonym;
GRANT SELECT ON lang_t TO helper;
GRANT SELECT ON lang_t TO admin;
GRANT INSERT ON lang_t TO admin;
GRANT DELETE ON lang_t TO admin;
GRANT UPDATE ON lang_t TO admin;

CREATE TABLE lang_entry_t (
	lang	CHAR(2) REFERENCES lang_t ON DELETE CASCADE,
	entry	INTEGER REFERENCES entry_t ON DELETE CASCADE,
	PRIMARY KEY (lang,entry)
);
GRANT SELECT ON lang_entry_t TO anonym;
GRANT SELECT ON lang_entry_t TO helper;
GRANT SELECT ON lang_entry_t TO admin;
GRANT INSERT ON lang_entry_t TO admin;
GRANT DELETE ON lang_entry_t TO admin;
GRANT UPDATE ON lang_entry_t TO admin;

CREATE TABLE etym_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT,
	entry	INTEGER REFERENCES entry_t ON DELETE CASCADE
);
GRANT SELECT ON etym_t TO anonym;
GRANT SELECT ON etym_t TO helper;
GRANT SELECT ON etym_t TO admin;
GRANT INSERT ON etym_t TO admin;
GRANT DELETE ON etym_t TO admin;
GRANT UPDATE ON etym_t TO admin;

CREATE TABLE link_t (
	id	SERIAL	PRIMARY KEY,
	type	VARCHAR(5),
	uri	TEXT,
	descr	TEXT,
	entry	INTEGER REFERENCES entry_t ON DELETE CASCADE
);
GRANT SELECT ON link_t TO anonym;
GRANT SELECT ON link_t TO helper;
GRANT SELECT ON link_t TO admin;
GRANT INSERT ON link_t TO admin;
GRANT DELETE ON link_t TO admin;
GRANT UPDATE ON link_t TO admin;

-- Bib fehlt noch
-- For Rele and Kele
CREATE TABLE pri_t (
	id		SERIAL	PRIMARY KEY,
	info	TEXT	UNIQUE
);
GRANT SELECT ON pri_t TO anonym;
GRANT SELECT ON pri_t TO helper;
GRANT SELECT ON pri_t TO admin;
GRANT INSERT ON pri_t TO admin;
GRANT DELETE ON pri_t TO admin;
GRANT UPDATE ON pri_t TO admin;

-- ReadingElement
CREATE TABLE rele_t (
	id	SERIAL	PRIMARY KEY,
	reb	TEXT,
	entry	INTEGER	NOT NULL REFERENCES entry_t ON DELETE CASCADE
);
GRANT SELECT ON rele_t TO anonym;
GRANT SELECT ON rele_t TO helper;
GRANT SELECT ON rele_t TO admin;
GRANT INSERT ON rele_t TO admin;
GRANT DELETE ON rele_t TO admin;
GRANT UPDATE ON rele_t TO admin;

CREATE TABLE reinf_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT	UNIQUE
);
GRANT SELECT ON reinf_t TO anonym;
GRANT SELECT ON reinf_t TO helper;
GRANT SELECT ON reinf_t TO admin;
GRANT INSERT ON reinf_t TO admin;
GRANT DELETE ON reinf_t TO admin;
GRANT UPDATE ON reinf_t TO admin;

CREATE TABLE reinf_rele_t (
	reinf	INTEGER REFERENCES reinf_t ON DELETE CASCADE,
	rele	INTEGER REFERENCES rele_t ON DELETE CASCADE,
	PRIMARY KEY (rele, reinf)
);
GRANT SELECT ON reinf_rele_t TO anonym;
GRANT SELECT ON reinf_rele_t TO helper;
GRANT SELECT ON reinf_rele_t TO admin;
GRANT INSERT ON reinf_rele_t TO admin;
GRANT DELETE ON reinf_rele_t TO admin;
GRANT UPDATE ON reinf_rele_t TO admin;

CREATE TABLE pri_rele_t (
	pri	INTEGER REFERENCES pri_t ON DELETE CASCADE,
	rele	INTEGER REFERENCES rele_t ON DELETE CASCADE,
	PRIMARY KEY (rele, pri)
);
GRANT SELECT ON pri_rele_t TO anonym;
GRANT SELECT ON pri_rele_t TO helper;
GRANT SELECT ON pri_rele_t TO admin;
GRANT INSERT ON pri_rele_t TO admin;
GRANT DELETE ON pri_rele_t TO admin;
GRANT UPDATE ON pri_rele_t TO admin;

-- KanjiElement
CREATE TABLE kele_t (
	id	SERIAL	PRIMARY KEY,
	keb	TEXT,
	entry	INTEGER NOT NULL REFERENCES entry_t ON DELETE CASCADE
);
GRANT SELECT ON kele_t TO anonym;
GRANT SELECT ON kele_t TO helper;
GRANT SELECT ON kele_t TO admin;
GRANT INSERT ON kele_t TO admin;
GRANT DELETE ON kele_t TO admin;
GRANT UPDATE ON kele_t TO admin;

CREATE TABLE keinf_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT	UNIQUE
);
GRANT SELECT ON keinf_t TO anonym;
GRANT SELECT ON keinf_t TO helper;
GRANT SELECT ON keinf_t TO admin;
GRANT INSERT ON keinf_t TO admin;
GRANT DELETE ON keinf_t TO admin;
GRANT UPDATE ON keinf_t TO admin;

CREATE TABLE keinf_kele_t (
	keinf	INTEGER REFERENCES keinf_t ON DELETE CASCADE,
	kele	INTEGER REFERENCES kele_t ON DELETE CASCADE,
	PRIMARY KEY (kele, keinf)
);
GRANT SELECT ON keinf_kele_t TO anonym;
GRANT SELECT ON keinf_kele_t TO helper;
GRANT SELECT ON keinf_kele_t TO admin;
GRANT INSERT ON keinf_kele_t TO admin;
GRANT DELETE ON keinf_kele_t TO admin;
GRANT UPDATE ON keinf_kele_t TO admin;

CREATE TABLE pri_kele_t (
	pri	INTEGER REFERENCES pri_t ON DELETE CASCADE,
	kele	INTEGER REFERENCES kele_t ON DELETE CASCADE,
	PRIMARY KEY (kele, pri)
);
GRANT SELECT ON pri_kele_t TO anonym;
GRANT SELECT ON pri_kele_t TO helper;
GRANT SELECT ON pri_kele_t TO admin;
GRANT INSERT ON pri_kele_t TO admin;
GRANT DELETE ON pri_kele_t TO admin;
GRANT UPDATE ON pri_kele_t TO admin;

-- For Rele and Kele
CREATE TABLE kele_rele_t (
	kele	INTEGER REFERENCES kele_t ON DELETE CASCADE,
	rele	INTEGER REFERENCES rele_t ON DELETE CASCADE,
	PRIMARY KEY (kele,rele)
);
GRANT SELECT ON kele_rele_t TO anonym;
GRANT SELECT ON kele_rele_t TO helper;
GRANT SELECT ON kele_rele_t TO admin;
GRANT INSERT ON kele_rele_t TO admin;
GRANT DELETE ON kele_rele_t TO admin;
GRANT UPDATE ON kele_rele_t TO admin;

-- SenseElement
CREATE TABLE sele_t (
	id	SERIAL	PRIMARY KEY,
	entry	INTEGER REFERENCES entry_t ON DELETE CASCADE
);
GRANT SELECT ON sele_t TO anonym;
GRANT SELECT ON sele_t TO helper;
GRANT SELECT ON sele_t TO admin;
GRANT INSERT ON sele_t TO admin;
GRANT DELETE ON sele_t TO admin;
GRANT UPDATE ON sele_t TO admin;

CREATE TABLE sele_rele_t (
	sele	INTEGER REFERENCES sele_t ON DELETE CASCADE,
	rele	INTEGER REFERENCES rele_t ON DELETE CASCADE,
	PRIMARY KEY (sele,rele)
);
GRANT SELECT ON sele_rele_t TO anonym;
GRANT SELECT ON sele_rele_t TO helper;
GRANT SELECT ON sele_rele_t TO admin;
GRANT INSERT ON sele_rele_t TO admin;
GRANT DELETE ON sele_rele_t TO admin;
GRANT UPDATE ON sele_rele_t TO admin;

CREATE TABLE sele_kele_t (
	sele	INTEGER REFERENCES sele_t ON DELETE CASCADE,
	kele	INTEGER REFERENCES kele_t ON DELETE CASCADE,
	PRIMARY KEY (sele,kele)
);
GRANT SELECT ON sele_kele_t TO anonym;
GRANT SELECT ON sele_kele_t TO helper;
GRANT SELECT ON sele_kele_t TO admin;
GRANT INSERT ON sele_kele_t TO admin;
GRANT DELETE ON sele_kele_t TO admin;
GRANT UPDATE ON sele_kele_t TO admin;

CREATE TABLE gloss_t (
	id	SERIAL	PRIMARY KEY,
	gloss	TEXT	NOT NULL,
	iso	CHAR(2) REFERENCES lang_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES sele_t ON DELETE CASCADE
);
GRANT SELECT ON gloss_t TO anonym;
GRANT SELECT ON gloss_t TO helper;
GRANT SELECT ON gloss_t TO admin;
GRANT INSERT ON gloss_t TO admin;
GRANT DELETE ON gloss_t TO admin;
GRANT UPDATE ON gloss_t TO admin;

CREATE TABLE gram_t (
	id		SERIAL	PRIMARY KEY,
	info	TEXT	UNIQUE
);
GRANT SELECT ON gram_t TO anonym;
GRANT SELECT ON gram_t TO helper;
GRANT SELECT ON gram_t TO admin;
GRANT INSERT ON gram_t TO admin;
GRANT DELETE ON gram_t TO admin;
GRANT UPDATE ON gram_t TO admin;

CREATE TABLE gram_sele_t (
	gram	INTEGER REFERENCES gram_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES sele_t ON DELETE CASCADE,
	PRIMARY KEY (gram,sele)
);
GRANT SELECT ON gram_sele_t TO anonym;
GRANT SELECT ON gram_sele_t TO helper;
GRANT SELECT ON gram_sele_t TO admin;
GRANT INSERT ON gram_sele_t TO admin;
GRANT DELETE ON gram_sele_t TO admin;
GRANT UPDATE ON gram_sele_t TO admin;

CREATE TABLE field_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT	UNIQUE
);
GRANT SELECT ON field_t TO anonym;
GRANT SELECT ON field_t TO helper;
GRANT SELECT ON field_t TO admin;
GRANT INSERT ON field_t TO admin;
GRANT DELETE ON field_t TO admin;
GRANT UPDATE ON field_t TO admin;

CREATE TABLE field_sele_t (
	field	INTEGER REFERENCES field_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES sele_t ON DELETE CASCADE,
	PRIMARY KEY (field,sele)
);
GRANT SELECT ON field_sele_t TO anonym;
GRANT SELECT ON field_sele_t TO helper;
GRANT SELECT ON field_sele_t TO admin;
GRANT INSERT ON field_sele_t TO admin;
GRANT DELETE ON field_sele_t TO admin;
GRANT UPDATE ON field_sele_t TO admin;

CREATE TABLE sinf_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT,
	sele	INTEGER REFERENCES sele_t ON DELETE CASCADE
);
GRANT SELECT ON sinf_t TO anonym;
GRANT SELECT ON sinf_t TO helper;
GRANT SELECT ON sinf_t TO admin;
GRANT INSERT ON sinf_t TO admin;
GRANT DELETE ON sinf_t TO admin;
GRANT UPDATE ON sinf_t TO admin;

CREATE TABLE misc_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT	UNIQUE
);
GRANT SELECT ON misc_t TO anonym;
GRANT SELECT ON misc_t TO helper;
GRANT SELECT ON misc_t TO admin;
GRANT INSERT ON misc_t TO admin;
GRANT DELETE ON misc_t TO admin;
GRANT UPDATE ON misc_t TO admin;

CREATE TABLE misc_sele_t (
	misc	INTEGER REFERENCES misc_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES sele_t ON DELETE CASCADE,
	PRIMARY KEY (misc,sele)
);
GRANT SELECT ON misc_sele_t TO anonym;
GRANT SELECT ON misc_sele_t TO helper;
GRANT SELECT ON misc_sele_t TO admin;
GRANT INSERT ON misc_sele_t TO admin;
GRANT DELETE ON misc_sele_t TO admin;
GRANT UPDATE ON misc_sele_t TO admin;

CREATE TABLE example_t (
	id		SERIAL	PRIMARY KEY,
	iso		INTEGER	REFERENCES lang_t ON DELETE CASCADE,
	info		TEXT	UNIQUE,
	groupref	INTEGER	REFERENCES example_t ON DELETE CASCADE
);
GRANT SELECT ON example_t TO anonym;
GRANT SELECT ON example_t TO helper;
GRANT SELECT ON example_t TO admin;
GRANT INSERT ON example_t TO admin;
GRANT DELETE ON example_t TO admin;
GRANT UPDATE ON example_t TO admin;

CREATE TABLE example_sele_t (
	example	INTEGER REFERENCES example_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES sele_t ON DELETE CASCADE,
	PRIMARY KEY (example,sele)
);
GRANT SELECT ON example_sele_t TO anonym;
GRANT SELECT ON example_sele_t TO helper;
GRANT SELECT ON example_sele_t TO admin;
GRANT INSERT ON example_sele_t TO admin;
GRANT DELETE ON example_sele_t TO admin;
GRANT UPDATE ON example_sele_t TO admin;


-- CREATE GROUP dictusers WITH USER anonym, helper, admin;
-- CREATE GROUP accessusers WITH USER helper, admin;
GRANT SELECT ON user_t TO anonym;
GRANT SELECT ON user_t TO helper;
GRANT SELECT ON user_t TO admin;

GRANT INSERT ON user_t TO anonym;
GRANT INSERT ON user_t TO helper;
GRANT INSERT ON user_t TO admin;

GRANT DELETE ON user_t TO helper;
GRANT DELETE ON user_t TO admin;

GRANT UPDATE ON user_t TO helper;
GRANT UPDATE ON user_t TO admin;


INSERT INTO institute_t (name, street, zip, town, country, phone, fax)
	VALUES (
	'Lehrstuhl fuer Japanologie', 'Bismarckstr. 1', '91054', 'Erlangen',
	'DE', '++49 (0)9131 852 9193', '++49 (0)9131 852 6266'
);
INSERT INTO user_t (login, password, email, firstname, surname, street, zip,
					town, country, phone, fax, access) 
	VALUES (
	'ourtongue', 'sunproject','czeyka@skwea.co.jp', 'Chris', 'XXX',
	'Bismarckstr. 1', '91054', 'Erlangen', 'DE', '++49 (0)9131 852 9193',
	'++49 (0)9131 852 6266', 20
);

INSERT INTO user_t (login, password, email, access)
	VALUES (
	'tongue', 'who_am_i', 'billyboy@microsoft.com', 5
);

SELECT * FROM institute_t;
SELECT * FROM user_t;

-- The Queue Table

CREATE TABLE queue_entry_t (
	id 		SERIAL 	PRIMARY KEY,
	oldid		INTEGER,
	ent_seq		INTEGER,
	upd_date	DATE 	NOT NULL,
	upd_txt		TEXT,
	upd_author	INTEGER REFERENCES user_t
		ON UPDATE CASCADE,
	chk_date	DATE	NOT NULL,
	chk_txt		TEXT,
	chk_author	INTEGER REFERENCES user_t
);
GRANT SELECT ON queue_entry_t TO helper;
GRANT INSERT ON queue_entry_t TO helper;
GRANT DELETE ON queue_entry_t TO helper;
GRANT UPDATE ON queue_entry_t TO helper;
GRANT SELECT ON queue_entry_t TO admin;
GRANT INSERT ON queue_entry_t TO admin;
GRANT DELETE ON queue_entry_t TO admin;
GRANT UPDATE ON queue_entry_t TO admin;


CREATE TABLE queue_dial_entry_t (
	dial	INTEGER REFERENCES dial_t ON DELETE CASCADE,
	entry	INTEGER REFERENCES queue_entry_t ON DELETE CASCADE,
	PRIMARY KEY (dial,entry)
);
GRANT SELECT ON queue_dial_entry_t TO helper;
GRANT INSERT ON queue_dial_entry_t TO helper;
GRANT DELETE ON queue_dial_entry_t TO helper;
GRANT UPDATE ON queue_dial_entry_t TO helper;
GRANT SELECT ON queue_dial_entry_t TO admin;
GRANT INSERT ON queue_dial_entry_t TO admin;
GRANT DELETE ON queue_dial_entry_t TO admin;
GRANT UPDATE ON queue_dial_entry_t TO admin;

CREATE TABLE queue_lang_entry_t (
	lang	CHAR(2) REFERENCES lang_t ON DELETE CASCADE,
	entry	INTEGER REFERENCES queue_entry_t ON DELETE CASCADE,
	PRIMARY KEY (lang,entry)
);
GRANT SELECT ON queue_lang_entry_t TO helper;
GRANT INSERT ON queue_lang_entry_t TO helper;
GRANT DELETE ON queue_lang_entry_t TO helper;
GRANT UPDATE ON queue_lang_entry_t TO helper;
GRANT SELECT ON queue_lang_entry_t TO admin;
GRANT INSERT ON queue_lang_entry_t TO admin;
GRANT DELETE ON queue_lang_entry_t TO admin;
GRANT UPDATE ON queue_lang_entry_t TO admin;

CREATE TABLE queue_etym_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT,
	entry	INTEGER REFERENCES queue_entry_t ON DELETE CASCADE
);
GRANT SELECT ON queue_etym_t TO helper;
GRANT INSERT ON queue_etym_t TO helper;
GRANT DELETE ON queue_etym_t TO helper;
GRANT UPDATE ON queue_etym_t TO helper;
GRANT SELECT ON queue_etym_t TO admin;
GRANT INSERT ON queue_etym_t TO admin;
GRANT DELETE ON queue_etym_t TO admin;
GRANT UPDATE ON queue_etym_t TO admin;

CREATE TABLE queue_link_t (
	id	SERIAL	PRIMARY KEY,
	type	VARCHAR(5),
	uri	TEXT,
	descr	TEXT,
	entry	INTEGER REFERENCES queue_entry_t ON DELETE CASCADE
);
GRANT SELECT ON queue_link_t TO helper;
GRANT INSERT ON queue_link_t TO helper;
GRANT DELETE ON queue_link_t TO helper;
GRANT UPDATE ON queue_link_t TO helper;
GRANT SELECT ON queue_link_t TO admin;
GRANT INSERT ON queue_link_t TO admin;
GRANT DELETE ON queue_link_t TO admin;
GRANT UPDATE ON queue_link_t TO admin;

-- Bib fehlt noch
-- For Rele and Kele

-- ReadingElement
CREATE TABLE queue_rele_t (
	id	SERIAL	PRIMARY KEY,
	reb	TEXT,
	entry	INTEGER	NOT NULL REFERENCES entry_t ON DELETE CASCADE
);
GRANT SELECT ON queue_rele_t TO helper;
GRANT INSERT ON queue_rele_t TO helper;
GRANT DELETE ON queue_rele_t TO helper;
GRANT UPDATE ON queue_rele_t TO helper;
GRANT SELECT ON queue_rele_t TO admin;
GRANT INSERT ON queue_rele_t TO admin;
GRANT DELETE ON queue_rele_t TO admin;
GRANT UPDATE ON queue_rele_t TO admin;


CREATE TABLE queue_reinf_rele_t (
	reinf	INTEGER REFERENCES reinf_t ON DELETE CASCADE,
	rele	INTEGER REFERENCES queue_rele_t ON DELETE CASCADE,
	PRIMARY KEY (rele, reinf)
);
GRANT SELECT ON queue_reinf_rele_t TO helper;
GRANT INSERT ON queue_reinf_rele_t TO helper;
GRANT DELETE ON queue_reinf_rele_t TO helper;
GRANT UPDATE ON queue_reinf_rele_t TO helper;
GRANT SELECT ON queue_reinf_rele_t TO admin;
GRANT INSERT ON queue_reinf_rele_t TO admin;
GRANT DELETE ON queue_reinf_rele_t TO admin;
GRANT UPDATE ON queue_reinf_rele_t TO admin;

CREATE TABLE queue_pri_rele_t (
	pri	INTEGER REFERENCES pri_t ON DELETE CASCADE,
	rele	INTEGER REFERENCES queue_rele_t ON DELETE CASCADE,
	PRIMARY KEY (rele, pri)
);
GRANT SELECT ON queue_pri_rele_t TO helper;
GRANT INSERT ON queue_pri_rele_t TO helper;
GRANT DELETE ON queue_pri_rele_t TO helper;
GRANT UPDATE ON queue_pri_rele_t TO helper;
GRANT SELECT ON queue_pri_rele_t TO admin;
GRANT INSERT ON queue_pri_rele_t TO admin;
GRANT DELETE ON queue_pri_rele_t TO admin;
GRANT UPDATE ON queue_pri_rele_t TO admin;

-- KanjiElement
CREATE TABLE queue_kele_t (
	id	SERIAL	PRIMARY KEY,
	keb	TEXT,
	entry	INTEGER NOT NULL REFERENCES queue_entry_t ON DELETE CASCADE
);
GRANT SELECT ON queue_kele_t TO helper;
GRANT INSERT ON queue_kele_t TO helper;
GRANT DELETE ON queue_kele_t TO helper;
GRANT UPDATE ON queue_kele_t TO helper;
GRANT SELECT ON queue_kele_t TO admin;
GRANT INSERT ON queue_kele_t TO admin;
GRANT DELETE ON queue_kele_t TO admin;
GRANT UPDATE ON queue_kele_t TO admin;

CREATE TABLE queue_keinf_kele_t (
	keinf	INTEGER REFERENCES keinf_t ON DELETE CASCADE,
	kele	INTEGER REFERENCES queue_kele_t ON DELETE CASCADE,
	PRIMARY KEY (kele, keinf)
);
GRANT SELECT ON queue_keinf_kele_t TO helper;
GRANT INSERT ON queue_keinf_kele_t TO helper;
GRANT DELETE ON queue_keinf_kele_t TO helper;
GRANT UPDATE ON queue_keinf_kele_t TO helper;
GRANT SELECT ON queue_keinf_kele_t TO admin;
GRANT INSERT ON queue_keinf_kele_t TO admin;
GRANT DELETE ON queue_keinf_kele_t TO admin;
GRANT UPDATE ON queue_keinf_kele_t TO admin;

CREATE TABLE queue_pri_kele_t (
	pri	INTEGER REFERENCES pri_t ON DELETE CASCADE,
	kele	INTEGER REFERENCES queue_kele_t ON DELETE CASCADE,
	PRIMARY KEY (kele, pri)
);
GRANT SELECT ON queue_pri_kele_t TO helper;
GRANT INSERT ON queue_pri_kele_t TO helper;
GRANT DELETE ON queue_pri_kele_t TO helper;
GRANT UPDATE ON queue_pri_kele_t TO helper;
GRANT SELECT ON queue_pri_kele_t TO admin;
GRANT INSERT ON queue_pri_kele_t TO admin;
GRANT DELETE ON queue_pri_kele_t TO admin;
GRANT UPDATE ON queue_pri_kele_t TO admin;

-- For Rele and Kele
CREATE TABLE queue_kele_rele_t (
	kele	INTEGER REFERENCES queue_kele_t ON DELETE CASCADE,
	rele	INTEGER REFERENCES queue_rele_t ON DELETE CASCADE,
	PRIMARY KEY (kele,rele)
);
GRANT SELECT ON queue_kele_rele_t TO helper;
GRANT INSERT ON queue_kele_rele_t TO helper;
GRANT DELETE ON queue_kele_rele_t TO helper;
GRANT UPDATE ON queue_kele_rele_t TO helper;
GRANT SELECT ON queue_kele_rele_t TO admin;
GRANT INSERT ON queue_kele_rele_t TO admin;
GRANT DELETE ON queue_kele_rele_t TO admin;
GRANT UPDATE ON queue_kele_rele_t TO admin;

-- SenseElement
CREATE TABLE queue_sele_t (
	id	SERIAL	PRIMARY KEY,
	entry	INTEGER REFERENCES queue_entry_t ON DELETE CASCADE
);
GRANT SELECT ON queue_sele_t TO helper;
GRANT INSERT ON queue_sele_t TO helper;
GRANT DELETE ON queue_sele_t TO helper;
GRANT UPDATE ON queue_sele_t TO helper;
GRANT SELECT ON queue_sele_t TO admin;
GRANT INSERT ON queue_sele_t TO admin;
GRANT DELETE ON queue_sele_t TO admin;
GRANT UPDATE ON queue_sele_t TO admin;

CREATE TABLE queue_sele_rele_t (
	sele	INTEGER REFERENCES queue_sele_t ON DELETE CASCADE,
	rele	INTEGER REFERENCES queue_rele_t ON DELETE CASCADE,
	PRIMARY KEY (sele,rele)
);
GRANT SELECT ON queue_sele_rele_t TO helper;
GRANT INSERT ON queue_sele_rele_t TO helper;
GRANT DELETE ON queue_sele_rele_t TO helper;
GRANT UPDATE ON queue_sele_rele_t TO helper;
GRANT SELECT ON queue_sele_rele_t TO admin;
GRANT INSERT ON queue_sele_rele_t TO admin;
GRANT DELETE ON queue_sele_rele_t TO admin;
GRANT UPDATE ON queue_sele_rele_t TO admin;

CREATE TABLE queue_sele_kele_t (
	sele	INTEGER REFERENCES queue_sele_t ON DELETE CASCADE,
	kele	INTEGER REFERENCES queue_kele_t ON DELETE CASCADE,
	PRIMARY KEY (sele,kele)
);
GRANT SELECT ON queue_sele_kele_t TO helper;
GRANT INSERT ON queue_sele_kele_t TO helper;
GRANT DELETE ON queue_sele_kele_t TO helper;
GRANT UPDATE ON queue_sele_kele_t TO helper;
GRANT SELECT ON queue_sele_kele_t TO admin;
GRANT INSERT ON queue_sele_kele_t TO admin;
GRANT DELETE ON queue_sele_kele_t TO admin;
GRANT UPDATE ON queue_sele_kele_t TO admin;

CREATE TABLE queue_gloss_t (
	id	SERIAL	PRIMARY KEY,
	gloss	TEXT	NOT NULL,
	iso	CHAR(2) REFERENCES lang_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES queue_sele_t ON DELETE CASCADE
);
GRANT SELECT ON queue_gloss_t TO helper;
GRANT INSERT ON queue_gloss_t TO helper;
GRANT DELETE ON queue_gloss_t TO helper;
GRANT UPDATE ON queue_gloss_t TO helper;
GRANT SELECT ON queue_gloss_t TO admin;
GRANT INSERT ON queue_gloss_t TO admin;
GRANT DELETE ON queue_gloss_t TO admin;
GRANT UPDATE ON queue_gloss_t TO admin;

CREATE TABLE queue_gram_sele_t (
	gram	INTEGER REFERENCES gram_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES queue_sele_t ON DELETE CASCADE,
	PRIMARY KEY (gram,sele)
);
GRANT SELECT ON queue_gram_sele_t TO helper;
GRANT INSERT ON queue_gram_sele_t TO helper;
GRANT DELETE ON queue_gram_sele_t TO helper;
GRANT UPDATE ON queue_gram_sele_t TO helper;
GRANT SELECT ON queue_gram_sele_t TO admin;
GRANT INSERT ON queue_gram_sele_t TO admin;
GRANT DELETE ON queue_gram_sele_t TO admin;
GRANT UPDATE ON queue_gram_sele_t TO admin;

CREATE TABLE queue_field_sele_t (
	field	INTEGER REFERENCES field_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES queue_sele_t ON DELETE CASCADE,
	PRIMARY KEY (field,sele)
);
GRANT SELECT ON queue_field_sele_t TO helper;
GRANT INSERT ON queue_field_sele_t TO helper;
GRANT DELETE ON queue_field_sele_t TO helper;
GRANT UPDATE ON queue_field_sele_t TO helper;
GRANT SELECT ON queue_field_sele_t TO admin;
GRANT INSERT ON queue_field_sele_t TO admin;
GRANT DELETE ON queue_field_sele_t TO admin;
GRANT UPDATE ON queue_field_sele_t TO admin;

CREATE TABLE queue_sinf_t (
	id	SERIAL	PRIMARY KEY,
	info	TEXT,
	sele	INTEGER REFERENCES queue_sele_t ON DELETE CASCADE
);
GRANT SELECT ON queue_sinf_t TO helper;
GRANT INSERT ON queue_sinf_t TO helper;
GRANT DELETE ON queue_sinf_t TO helper;
GRANT UPDATE ON queue_sinf_t TO helper;
GRANT SELECT ON queue_sinf_t TO admin;
GRANT INSERT ON queue_sinf_t TO admin;
GRANT DELETE ON queue_sinf_t TO admin;
GRANT UPDATE ON queue_sinf_t TO admin;

CREATE TABLE queue_misc_sele_t (
	misc	INTEGER REFERENCES misc_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES queue_sele_t ON DELETE CASCADE,
	PRIMARY KEY (misc,sele)
);
GRANT SELECT ON queue_misc_sele_t TO helper;
GRANT INSERT ON queue_misc_sele_t TO helper;
GRANT DELETE ON queue_misc_sele_t TO helper;
GRANT UPDATE ON queue_misc_sele_t TO helper;
GRANT SELECT ON queue_misc_sele_t TO admin;
GRANT INSERT ON queue_misc_sele_t TO admin;
GRANT DELETE ON queue_misc_sele_t TO admin;
GRANT UPDATE ON queue_misc_sele_t TO admin;

CREATE TABLE queue_example_t (
	id		SERIAL	PRIMARY KEY,
	iso		INTEGER	REFERENCES lang_t ON DELETE CASCADE,
	inf		TEXT	UNIQUE,
	groupref	INTEGER	REFERENCES queue_example_t ON DELETE CASCADE
);
GRANT SELECT ON queue_example_t TO helper;
GRANT INSERT ON queue_example_t TO helper;
GRANT DELETE ON queue_example_t TO helper;
GRANT UPDATE ON queue_example_t TO helper;
GRANT SELECT ON queue_example_t TO admin;
GRANT INSERT ON queue_example_t TO admin;
GRANT DELETE ON queue_example_t TO admin;
GRANT UPDATE ON queue_example_t TO admin;

CREATE TABLE queue_example_sele_t (
	example	INTEGER REFERENCES queue_example_t ON DELETE CASCADE,
	sele	INTEGER REFERENCES queue_sele_t ON DELETE CASCADE,
	PRIMARY KEY (example,sele)
);
GRANT SELECT ON queue_example_sele_t TO helper;
GRANT INSERT ON queue_example_sele_t TO helper;
GRANT DELETE ON queue_example_sele_t TO helper;
GRANT UPDATE ON queue_example_sele_t TO helper;
GRANT SELECT ON queue_example_sele_t TO admin;
GRANT INSERT ON queue_example_sele_t TO admin;
GRANT DELETE ON queue_example_sele_t TO admin;
GRANT UPDATE ON queue_example_sele_t TO admin;
