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

drop function cg_AcceptID(Accept.Accept%type);
drop table    Accept;

create table Accept(
	AcceptID	serial NOT NULL primary key,
	atCreated	timestamp with time zone DEFAULT now(),
	Accept	text unique NOT NULL
);
create unique index Accept_AcceptID_uix on Accept(AcceptID);
create unique index Accept_Accept_uix on Accept(Accept);

comment on table Accept is 'Map between Accept and AcceptID.
Shortname: at.';
comment on column Accept.AcceptID is 'Unique ID for Accept.';
comment on column Accept.Accept is '';

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

comment on function cg_AcceptID(Accept.Accept%type) is 'Create AcceptID and get Accept.';

select cg_AcceptID('');

--EOF
