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

drop function cg_AcceptEncodingID(AcceptEncoding.AcceptEncoding%type);
drop table    AcceptEncoding;

create table AcceptEncoding(
	AcceptEncodingID	serial NOT NULL primary key,
	aeCreated	timestamp with time zone DEFAULT now(),
	AcceptEncoding	text unique NOT NULL
);
--create unique index AcceptEncoding_AcceptEncodingID_uix on AcceptEncoding(AcceptEncodingID);
--create unique index AcceptEncoding_AcceptEncoding_uix on AcceptEncoding(AcceptEncoding);

comment on table AcceptEncoding is 'Map between AcceptEncoding and AcceptEncodingID.
Shortname: ae.';
comment on column AcceptEncoding.AcceptEncodingID is 'Unique ID for AcceptEncoding.';
comment on column AcceptEncoding.AcceptEncoding is '';

create function cg_AcceptEncodingID(AcceptEncoding.AcceptEncoding%type) returns AcceptEncoding.AcceptEncodingID%type as '
declare
	id      AcceptEncoding.AcceptEncodingID%type;
	sname   AcceptEncoding.AcceptEncoding%type;
	p_name  alias for $1;
begin
	sname := trim(p_name);
	select AcceptEncodingID into id from AcceptEncoding where (AcceptEncoding = sname);
	if not found then
		insert into AcceptEncoding (AcceptEncoding) values(sname);
		select last_value into id from AcceptEncoding_AcceptEncodingID_seq;
	end if;
	return id;
end;
' language 'PLpgSQL';

comment on function cg_AcceptEncodingID(AcceptEncoding.AcceptEncoding%type) is 'Create AcceptEncodingID and get AcceptEncoding.';

select cg_AcceptEncodingID('');

--EOF
