/TrafficCount/trunk/sql/db-update/update-1_1_0-1_2_0.sql |
---|
0,0 → 1,127 |
-- |
-- TraficCounter |
-- Copyleft: Anatoli Klassen (anatoli@aksoft.net) |
-- |
-- SQL script for PostgeSQL |
-- |
-- Update from 1.1.0 to 1.2.0 |
-- |
update tversion set major=1, minor=2, build=0; |
drop function insert_count(varchar(100), numeric(20,0)); |
drop function insert_count_stage2( |
varchar(100), numeric(20,0), numeric(20,0), numeric(20,0)); |
CREATE TABLE tdays ( |
source varchar(100) not null, |
day DATE not null, |
beginvalue numeric(20,0) not null, |
endvalue numeric(20,0) not null, |
stamp timestamp not null, |
CONSTRAINT cdays_source FOREIGN KEY (source) REFERENCES tsources(name) |
); |
create index idays_main on tdays using btree (source, day); |
create index idays_general on tdays using btree (source); |
insert into tdays (source, day, beginvalue, endvalue, stamp) |
select c.source, c.day, |
(select COALESCE(max(c2.value), 0) from tcount c2 |
where c2.source = c.source and c2.stamp < c.day) as beginvalue, |
(select max(c2.value) from tcount c2 |
where c2.source = c.source and date_trunc('day', c2.stamp) = c.day) as endvalue, |
CURRENT_TIMESTAMP |
from |
(select distinct source, date_trunc('day', stamp) as day from tcount) c |
; |
drop table tcount; |
-- |
-- Create a new day counter. |
-- Intern use only |
-- |
-- @param 1 source name |
-- @param 2 absolute value of the counter |
-- |
CREATE FUNCTION insert_counter_stage4(varchar(100), numeric(20,0)) |
RETURNS integer AS ' |
insert into tdays (source, day, beginvalue, endvalue, stamp) |
values ($1, CURRENT_DATE, |
(select COALESCE(max(endvalue), $2) from tdays |
where source = $1 and day = (CURRENT_DATE - interval \'1 day\')), |
$2, CURRENT_TIMESTAMP); |
select 1; |
' LANGUAGE SQL; |
-- |
-- Update day counter. |
-- Intern use only |
-- |
-- @param 1 source name |
-- @param 2 absolute value of the counter |
-- |
CREATE FUNCTION update_counter_stage4(varchar(100), numeric(20,0)) |
RETURNS integer AS ' |
update tdays set endvalue = $2, stamp = CURRENT_TIMESTAMP |
where source = $1 and day = CURRENT_DATE; |
select 1; |
' LANGUAGE SQL; |
-- |
-- Update internal counters. |
-- Intern use only |
-- |
-- @param 1 source name |
-- @param 2 value of the counter |
-- @param 3 value of recalculated base |
-- |
CREATE FUNCTION update_counter_stage3( |
varchar(100), numeric(20,0), numeric(20,0)) |
RETURNS integer AS ' |
select (case |
when (select count(*) from tdays |
where day = CURRENT_DATE and source = $1) = 0 |
then insert_counter_stage4($1, $2 + $3) |
else update_counter_stage4($1, $2 + $3) |
end |
); |
update tsources set base = $3, delta = $2, countstamp = CURRENT_TIMESTAMP |
where name = $1; |
select 1; |
' LANGUAGE SQL; |
-- |
-- Update internal counters, calculate real counter value. |
-- Intern use only |
-- |
-- @param 1 source name |
-- @param 2 value of the counter |
-- @param 3 value of base |
-- @param 4 value of delta |
-- |
CREATE FUNCTION update_counter_stage2( |
varchar(100), numeric(20,0), numeric(20,0), numeric(20,0)) |
RETURNS integer AS ' |
select update_counter_stage3($1, $2, |
(select (case when $2 >= $4 then $3 else ($3 + $4) end))); |
' LANGUAGE SQL; |
-- |
-- Update current day counter for given source. Public |
-- |
-- @param 1 source name |
-- @param 2 value of the counter |
-- |
CREATE FUNCTION update_counter(varchar(100), numeric(20,0)) |
RETURNS integer AS ' |
select update_counter_stage2($1, $2, |
(select base from tsources where name = $1), |
(select delta from tsources where name = $1)); |
' LANGUAGE SQL; |
/TrafficCount/trunk/sql/db-update/update-1_0_0-1_1_0.sql |
---|
0,0 → 1,110 |
-- |
-- TraficCounter |
-- Copyleft: Anatoli Klassen (anatoli@aksoft.net) |
-- |
-- SQL script for PostgeSQL |
-- |
-- Update from 1.0.0 to 1.1.0 |
-- |
CREATE TABLE tversion ( |
major integer not null, |
minor integer not null, |
build integer not null |
); |
insert into tversion (major, minor, build) values (1, 1, 0); |
drop function insert_count( |
character varying, character varying, numeric); |
drop function insert_count_stage2( |
character varying, character varying, numeric, timestamp without time zone); |
drop function insert_count_stage3( |
character varying, character varying, numeric, numeric, numeric); |
alter table tsources add column host varchar(100); |
update tsources set host = (select max(name) from thosts); |
alter table tsources alter column host set not null; |
alter table tsources add column base numeric(20,0); |
update tsources |
set base = (select base from tcount |
where source = tsources.name |
order by stamp desc limit 1); |
alter table tsources alter column base set not null; |
alter table tsources alter column base set default 0; |
alter table tsources add column delta numeric(20,0); |
update tsources |
set delta = (select delta from tcount |
where source = tsources.name |
order by stamp desc limit 1); |
alter table tsources alter column delta set not null; |
alter table tsources alter column delta set default 0; |
alter table tsources add column countstamp timestamp; |
update tsources set countstamp = CURRENT_TIMESTAMP; |
alter table tsources alter column countstamp set not null; |
alter table tsources alter column countstamp set default CURRENT_TIMESTAMP; |
alter table tsources add |
CONSTRAINT csources_host FOREIGN KEY (host) REFERENCES thosts(name); |
drop index tcount_main; |
drop index tcount_general; |
alter table tcount drop column host; |
alter table tcount add column value numeric(20,0); |
update tcount set value = base + delta; |
alter table tcount alter column value set not null; |
alter table tcount drop column base; |
alter table tcount drop column delta; |
create index tcount_main on tcount using btree (source, stamp); |
create index tcount_general on tcount using btree (source); |
-- **************************************************************************** |
-- * Functions * |
-- **************************************************************************** |
-- |
-- Insert a given base and delta into the traffic counter. |
-- Intern use only |
-- |
-- @param 1 source name |
-- @param 2 value of the counter |
-- @param 3 value of base |
-- @param 4 value of delta |
-- |
CREATE FUNCTION insert_count_stage2( |
varchar(100), numeric(20,0), numeric(20,0), numeric(20,0)) |
RETURNS integer AS ' |
insert into tcount (source, value, stamp) |
values ( |
$1, |
(select (case when $2 >= $4 then $3 else ($3 + $4) end)) + $2, |
CURRENT_TIMESTAMP |
); |
update tsources set |
base = (select (case when $2 >= $4 then $3 else ($3 + $4) end)), |
delta = $2, |
countstamp = CURRENT_TIMESTAMP |
where name = $1; |
select 1; |
' LANGUAGE SQL; |
-- |
-- Insert a new data into the traffic counter. Public |
-- |
-- @param 1 source name |
-- @param 2 value of the counter |
-- |
CREATE FUNCTION insert_count(varchar(100), numeric(20,0)) |
RETURNS integer AS ' |
select insert_count_stage2($1, $2, |
(select base from tsources where name = $1), |
(select delta from tsources where name = $1)); |
' LANGUAGE SQL; |