Index: openacs-4/packages/acs-kernel/sql/postgresql/security-create.sql =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-kernel/sql/postgresql/security-create.sql,v diff -u -r1.8 -r1.9 --- openacs-4/packages/acs-kernel/sql/postgresql/security-create.sql 27 Oct 2014 16:39:36 -0000 1.8 +++ openacs-4/packages/acs-kernel/sql/postgresql/security-create.sql 30 Jun 2015 21:08:32 -0000 1.9 @@ -49,3 +49,46 @@ create sequence t_sec_id_seq cache 100 increment 100; create view sec_id_seq as select nextval('t_sec_id_seq') as nextval; + + +select define_function_args('sec_session_property__upsert','session_id,module,name,secure_p,last_hit'); + +CREATE OR REPLACE FUNCTION sec_session_property__upsert( + p_session_id bigint, + p_module varchar, + p_name varchar, + p_value varchar, + p_secure_p boolean, + p_last_hit integer +) RETURNS void as +$$ +BEGIN + LOOP + -- first try to update the key + update sec_session_properties + set property_value = p_value, secure_p = p_secure_p, last_hit = p_last_hit + where session_id = p_session_id and module = p_module and property_name = p_name; + IF found THEN + return; + END IF; + -- not there, so try to insert the key + -- if someone else inserts the same key concurrently, + -- we could get a unique-key failure + BEGIN + insert into sec_session_properties + (session_id, module, property_name, secure_p, last_hit) + values (p_session_id, p_module, p_name, p_secure_p, p_last_hit); + RETURN; + EXCEPTION WHEN unique_violation THEN + -- Do nothing, and loop to try the UPDATE again. + END; + END LOOP; +END; +$$ LANGUAGE plpgsql; + + + + + + +