
create table DefaultParam(
	pmCreated
	pmModified
	pmHostnameID
	port
	database
	key
	value
);

-- ToughLog
\set HOSTNAME `echo "'$HOSTNAME'"`
select syslog('I', :HOSTNAME, 'parameter.sql', 'ID',
'$Id$'
);
select syslog('I', :HOSTNAME, 'parameter.sql', 'INSTALL',
'Install table Parameter.'
);

drop function cg_ParameterID(Parameter.Parameter%type);
drop table    Parameter;

create table Parameter(
	ParameterID	serial NOT NULL primary key,
	pmCreated	timestamp with time zone DEFAULT now(),
	pmFunction	text NOT NULL,
	pmKey		text NOT NULL,
	pmValue		text NOT NULL,
);
create unique index Parameter_ParameterID_uix on Parameter(ParameterID);
create unique index Parameter_Parameter_uix on Parameter(Parameter);

comment on table Parameter is 'Map between Parameter and ParameterID.
Shortname: pm.';
comment on column Parameter.ParameterID is 'Unique ID for Parameter.';
comment on column Parameter.Parameter is '';

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

comment on function cg_ParameterID(Parameter.Parameter%type) is 'Create ParameterID and get Parameter.';

select cg_ParameterID('');

--EOF
