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

drop function cg_MethodID(Method.Method%type);
drop table    Method;

create table Method(
	MethodID	serial NOT NULL primary key,
	meCreated	timestamp with time zone DEFAULT now(),
	Method	text unique NOT NULL
);
--create unique index Method_MethodID_uix on Method(MethodID);
--create unique index Method_Method_uix on Method(Method);

comment on table Method is 'Map between Method and MethodID.';
comment on column Method.MethodID is 'Unique ID for Method.';
comment on column Method.Method is '';

create function cg_MethodID(Method.Method%type) returns Method.MethodID%type as '
declare
	id      Method.MethodID%type;
	sname   Method.Method%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 MethodID into id from Method where (Method = sname);
		if not found then
			insert into Method (Method) values(sname);
			select last_value into id from Method_MethodID_seq;
		end if;
	--end if;
	return id;
end;
' language 'PLpgSQL';

comment on function cg_MethodID(Method.Method%type) is 'Create MethodID and get Method.';

select cg_MethodID('');

--EOF
