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

drop function cg_AcceptLanguageID(AcceptLanguage.AcceptLanguage%type);
drop table    AcceptLanguage;

create table AcceptLanguage(
	AcceptLanguageID	serial NOT NULL primary key,
	alCreated	timestamp with time zone DEFAULT now(),
	AcceptLanguage	text unique NOT NULL
);
create unique index AcceptLanguage_AcceptLanguageID_uix on AcceptLanguage(AcceptLanguageID);
create unique index AcceptLanguage_AcceptLanguage_uix on AcceptLanguage(AcceptLanguage);

comment on table AcceptLanguage is 'Map between AcceptLanguage and AcceptLanguageID.
Shortname: al.';
comment on column AcceptLanguage.AcceptLanguageID is 'Unique ID for AcceptLanguage.';
comment on column AcceptLanguage.AcceptLanguage is '';

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

comment on function cg_AcceptLanguageID(AcceptLanguage.AcceptLanguage%type) is 'Create AcceptLanguageID and get AcceptLanguage.';

select cg_AcceptLanguageID('');

--EOF
