-- ToughLog
\set HOSTNAME `echo "'$HOSTNAME'"`
select syslog('I', :HOSTNAME, 'contenttype.sql', 'ID',
'$Id$'
);
select syslog('I', :HOSTNAME, 'contenttype.sql', 'INSTALL',
'Install table ContentType and function cg_ContentTypeID().'
);

drop function cg_ContentTypeID(ContentType.ContentType%type);
drop table    ContentType;

create table ContentType(
	ContentTypeID	serial NOT NULL primary key,
	ctCreated	timestamp with time zone DEFAULT now(),
	CanLink		bool default 'f' NOT NULL,
	ContentType	text unique NOT NULL
);

comment on table ContentType is 'Map between ContentType and ContentTypeID.
Contenttype is the same as mime-type.';
comment on column ContentType.ContentTypeID is 'Unique ID for ContentType.';
comment on column ContentType.CanLink is 'This field tells if the mime-type can link to another URL.
A ''text/html'' can link but a ''image/png'' can not.
This flag should be updated manual by the toughlog admin.';
comment on column ContentType.ContentType is '';

create function cg_ContentTypeID(ContentType.ContentType%type) returns ContentType.ContentTypeID%type as '
declare
	id      ContentType.ContentTypeID%type;
	sname   ContentType.ContentType%type;
	p_name  alias for $1;
begin
	sname := trim(p_name);
	--if p_name is null or length(sname)<1 then
	--	id := NULL;
	--else
		select ContentTypeID into id from ContentType where (ContentType = sname);
		if not found then
			insert into ContentType (ContentType) values(sname);
			select last_value into id from ContentType_ContentTypeID_seq;
		end if;
	--end if;
	return id;
end;
' language 'PLpgSQL';

comment on function cg_ContentTypeID(ContentType.ContentType%type) is 'Create ContentTypeID and get ContentType.';

select cg_ContentTypeID('');
insert into ContentType(CanLink,ContentType) Values('t','text/html');
insert into ContentType(CanLink,ContentType) Values('t','text/vnd.wap.wml');
insert into ContentType(CanLink,ContentType) Values('t','application/x-javascript');
insert into ContentType(CanLink,ContentType) Values('t','application/x-shockwave-flash');

--EOF
