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

drop function cg_AcceptCharsetID(AcceptCharset.AcceptCharset%type);
drop table    AcceptCharset;

create table AcceptCharset(
	AcceptCharsetID	serial NOT NULL primary key,
	acCreated	timestamp with time zone DEFAULT now(),
	AcceptCharset	text unique NOT NULL
);
--create unique index AcceptCharset_AcceptCharsetID_uix on AcceptCharset(AcceptCharsetID);
--create unique index AcceptCharset_AcceptCharset_uix on AcceptCharset(AcceptCharset);

comment on table AcceptCharset is 'Map between AcceptCharset and AcceptCharsetID.
Shortname: ac.';
comment on column AcceptCharset.AcceptCharsetID is 'Unique ID for AcceptCharset.';
comment on column AcceptCharset.AcceptCharset is '';

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

comment on function cg_AcceptCharsetID(AcceptCharset.AcceptCharset%type) is 'Create AcceptCharsetID and get AcceptCharset.';

select cg_AcceptCharsetID('');

--EOF
