Index: openacs-4/packages/acs-kernel/sql/postgresql/lob.sql =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-kernel/sql/postgresql/lob.sql,v diff -u -N -r1.6 -r1.7 --- openacs-4/packages/acs-kernel/sql/postgresql/lob.sql 10 Mar 2010 00:44:03 -0000 1.6 +++ openacs-4/packages/acs-kernel/sql/postgresql/lob.sql 7 Jul 2011 10:46:02 -0000 1.7 @@ -24,14 +24,15 @@ refcount integer not null default 0 ); -create or replace function on_lobs_delete() returns trigger as ' -begin +CREATE OR REPLACE FUNCTION lobs_delete_tr() RETURNS trigger AS $$ +BEGIN delete from lob_data where lob_id = old.lob_id; return old; -end;' language 'plpgsql'; +END; +$$ LANGUAGE plpgsql; -create trigger lobs_delete_trig before delete on lobs -for each row execute procedure on_lobs_delete(); +create trigger lobs_delete_tr before delete on lobs +for each row execute procedure lobs_delete_tr(); create table lob_data ( lob_id integer not null @@ -51,62 +52,83 @@ -- and PG 7.0. The ACS doesn't share LOBs between tables -- or rows within a table anyway, I don't think/hope. -create or replace function on_lob_ref() returns trigger as ' -begin - if TG_OP = ''UPDATE'' then +CREATE OR REPLACE FUNCTION on_lob_ref() RETURNS trigger AS $$ +BEGIN + if TG_OP = 'UPDATE' then if new.lob = old.lob then return new; end if; end if; - if TG_OP = ''INSERT'' or TG_OP = ''UPDATE'' then + if TG_OP = 'INSERT' or TG_OP = 'UPDATE' then if new.lob is not null then insert into lobs select new.lob, 0 where 0 = (select count(*) from lobs where lob_id = new.lob); update lobs set refcount = refcount + 1 where lob_id = new.lob; end if; end if; - if TG_OP <> ''INSERT'' then + if TG_OP <> 'INSERT' then if old.lob is not null then update lobs set refcount = refcount - 1 where lob_id = old.lob; delete from lobs where lob_id = old.lob and refcount = 0; end if; end if; - if TG_OP = ''INSERT'' or TG_OP = ''UPDATE'' then return new; + if TG_OP = 'INSERT' or TG_OP = 'UPDATE' then return new; else return old; end if; -end;' language 'plpgsql'; +END; +$$ LANGUAGE plpgsql; -create or replace function empty_lob() returns integer as ' -begin - return nextval(''lob_sequence''); -end;' language 'plpgsql'; +CREATE OR REPLACE FUNCTION empty_lob() RETURNS integer AS $$ +BEGIN + return nextval('lob_sequence'); +END; +$$ LANGUAGE plpgsql; -create or replace function lob_get_data(integer) returns text as ' -declare - p_lob_id alias for $1; + + +-- added +select define_function_args('lob_get_data','lob_id'); + +-- +-- procedure lob_get_data/1 +-- +CREATE OR REPLACE FUNCTION lob_get_data( + p_lob_id integer +) RETURNS text AS $$ +DECLARE v_rec record; - v_data text default ''''; -begin + v_data text default ''; +BEGIN for v_rec in select data, segment from lob_data where lob_id = p_lob_id order by segment loop v_data := v_data || v_rec.data; end loop; return v_data; -end;' language 'plpgsql'; +END; +$$ LANGUAGE plpgsql; -create or replace function lob_copy(integer, integer) returns integer as ' -declare - from_id alias for $1; - to_id alias for $2; -begin + + +-- added +select define_function_args('lob_copy','from_id,to_id'); + +-- +-- procedure lob_copy/2 +-- +CREATE OR REPLACE FUNCTION lob_copy( + from_id integer, + to_id integer +) RETURNS integer AS $$ +DECLARE +BEGIN if from_id is null then - raise exception ''lob_copy: attempt to copy null from_id to % to_id'',to_id; + raise exception 'lob_copy: attempt to copy null from_id to % to_id',to_id; end if; insert into lobs (lob_id,refcount) values (to_id,0); @@ -118,11 +140,22 @@ return null; -end;' language 'plpgsql'; +END; +$$ LANGUAGE plpgsql; -create or replace function lob_length(integer) returns integer as ' -declare - id alias for $1; -begin + + +-- added +select define_function_args('lob_length','id'); + +-- +-- procedure lob_length/1 +-- +CREATE OR REPLACE FUNCTION lob_length( + id integer +) RETURNS integer AS $$ +DECLARE +BEGIN return sum(byte_len) from lob_data where lob_id = id; -end;' language 'plpgsql'; +END; +$$ LANGUAGE plpgsql;