drop sequence filenum;
drop sequence symnum;
drop sequence declnum;
drop table files;
drop table symbols;
drop table indexes;
drop table releases;
drop table usage;
drop table status;
drop table declarations;

create sequence filenum cache 50;
create sequence symnum cache 50;
create sequence declnum cache 10;

create table files (
	filename	varchar,
	revision	varchar,
	fileid		int,
	primary key	(fileid),
	unique		(filename, revision)

);

create table symbols (
	symname		varchar,
	symid		int,
	primary key	(symid),
	unique		(symname)

);

create table declarations (
	declid     	smallint not null,
	langid		smallint not null,
	declaration	char(255) not null,
	primary key (declid, langid)
); 

create table indexes (
	symid		int		references symbols,
	fileid		int		references files,
	line		int,
	langid		smallint not null,
	type		smallint not null,
	relsym		int		references symbols,
	foreign key (langid, type) references declarations (langid, declid)
);

create table releases 
	(fileid		int		references files,
	release		varchar,
	primary key	(fileid,release)
);

create table usage
	(fileid		int		references files,
	line		int,
	symid		int		references symbols
);

create table status
	(fileid		int		references files,
	status		smallint,
	primary key	(fileid)
);

create index indexindex on indexes using btree (symid);
create index symbolindex on symbols using btree (symname);
create index usageindex on usage using btree (symid);
create index filelookup on files using btree (filename);

grant select on files to public;
grant select on symbols to public;
grant select on indexes to public;
grant select on releases to public;
grant select on usage to public;
grant select on status to public;
grant select on declarations to public;