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

drop function cg_RefererID(Referer.Referer%type, HostName.HostName%type, Path.Path%type, Query.Query%type);
drop function cg_RefererRecord(Referer.Referer%type, HostName.HostName%type, Path.Path%type, Query.Query%type);
drop table    Referer;

create table Referer(
	RefererID	serial primary key,
	rfCreated	timestamp with time zone DEFAULT now(),
	Referer		text unique not null,
	HostNameID	int4 REFERENCES HostName, -- .HostNameID%type,
	PathID	int4,
	QueryID	int4
);
create unique index Referer_Referer_uix on Referer(Referer);

comment on table Referer is 'Map between Referer and RefererID.
The layout of this record is used directly in function::addhit and any change in column order or column type must be matched in function::addhit.';
comment on column Referer.RefererID is 'Unique ID for Referer.';
comment on column Referer.Referer is '';
comment on column Referer.HostNameID is ' field::HostName.HostNameID ';

create function cg_RefererID(Referer.Referer%type, HostName.HostName%type, Path.Path%type, Query.Query%type) returns int4 as '
declare
	p_referer	alias for $1;
	p_refererhost	alias for $2;
	p_refererpath	alias for $3;
	p_refererquery	alias for $4;
	v_id	Referer.RefererID%type;
	v_ip	Referer.Referer%type;
begin
	--if p_referer is null or length(trim(p_referer))<1 then
	--	v_id := NULL;
	--else
		select RefererID into v_id from Referer where (Referer = p_referer);
		if not found then
			insert into Referer (Referer,HostNameID,PathID,QueryID)
				values(p_referer,cg_HostNameID(p_refererhost),cg_PathID(p_refererpath),cg_QueryID(p_refererquery));
			select last_value into v_id from Referer_RefererID_seq;
		end if;
	--end if;
	return v_id;
end;
' language 'PLpgSQL';

create function cg_RefererRecord(Referer.Referer%type, HostName.HostName%type, Path.Path%type, Query.Query%type) returns record as '
declare
	p_referer	alias for $1;
	p_refererhost	alias for $2;
	p_refererpath	alias for $3;
	p_refererquery	alias for $4;
	r_referer	Referer%rowtype;
begin
	--RAISE NOTICE ''Godt igang'';
	--if p_referer is null or length(p_referer)<1 then
	--	-- r_referer := NULL;
	--else
		select * into r_referer from Referer where (Referer = p_referer);
		if not found then
			r_referer.Referer := p_referer;
			r_referer.HostNameID := cg_HostNameID(p_refererhost);
			r_referer.PathID := cg_PathID(p_refererpath);
			r_referer.QueryID := cg_QueryID(p_refererquery);
			insert into Referer (Referer,HostNameID,PathID,QueryID)
				values(r_referer.Referer, r_referer.HostNameID, r_referer.PathID, r_referer.QueryID);
			select last_value into r_referer.RefererID from Referer_RefererID_seq;
		end if;
	--end if;
	return r_referer;
end;
' language 'PLpgSQL';

-- select cg_RefererRecord('http://www.google.dk/search?q=autostol+test&hl=da&lr=lang_da&ie=UTF-8&start=10&sa=N', 'www.google.dk', '/search', '?q=autostol+test&hl=da&lr=lang_da&ie=UTF-8&start=10&sa=N');
   
comment on function cg_RefererID(Referer.Referer%type, HostName.HostName%type, Path.Path%type, Query.Query%type)
is 'Create RefererID and cg_RefererID().';

select cg_RefererID('','','','');

--EOF
