Forum OpenACS Q&A: Converting some code from

Collapse
Posted by Matthew Geddert on
Hello, i am working on transferring the events package data model Michael Steigman has been working on in oracle, to postgresql - we hope to present it to CVS here once it is more mature. I am stuck on two bits of oracle code... and am completely at a loss on how to transfer these things to postgresql...
create or replace trigger event_ship_date_trigger
before insert or update on events_registrations
for each row
when (old.reg_state <> 'shipped' and new.reg_state = 'shipped')
begin
        :new.shipped_date := sysdate;
end;
/
show errors
I don't know how to change this and what is wrong with it, it just doesn't work
create or replace view events_orders_states 
as
select  o.*,
o_states.order_state
from events_orders o,
 (select
 order_id,
 decode (floor(avg (decode (reg_state, 
                   'canceled', 0,
                   'waiting', 1,
                   'pending', 2,
                   'shipped', 3,
                   0))),
             0, 'canceled',
             1, 'incomplete',
             2, 'incomplete',
             3, 'fulfilled',
             'void') as order_state
 from events_registrations
 group by order_id) o_states
where o_states.order_id = o.order_id;
For the following there is a problem at or near "("
create view events_organizers
as
select eor.*, eom.user_id, eorem.event_id
  from events_organizer_roles eor,
       events_organizers_map eom,
       events_org_role_event_map eorem
 where eor.role_id=eom.role_id(+)
   and eorem.role_id = eor.role_id;
Also, there is a function that 25 arguments, however there are only 16 allowed by postgresql, is there a common fix for this, or does it just mean that we need to split up this function?

if anybody could help it would be greatly appreaciated.

Collapse
Posted by Jonathan Ellis on
1) I don't see anything tricky here.  Should be easy to do with PG triggers or rules.  What in particular is causing problems?

2) "decode" is an oracleism, use "case when" instead.

3) the (+) notation for outer joins is another oraclism.  use "left outer join" instead.

Re the argument limitation, I'm not sure what the standard workaround is but pg 7.3 is in beta2 and supports 32 args per function.

Collapse
Posted by Andrei Popov on
I am away from any PG database I could test it against, but this (or something very similar) should work just fine for (1): create or replace function event_ship_date_trigger_func () returns opaque as ' begin if (OLD.reg_state <> ''shipped'' and NEW.reg_state = ''shipped'') then new.shipped_date := now(); end if; return NEW; end; ' language 'plpgsql'; create or replace trigger event_ship_date_trigger before insert or update on events_registrations for each row execute event_ship_date_trigger_func();
Collapse
Posted by Andrei Popov on
ooops, make it just 'create trigger' -- cannot do an 'or replace'
Thanks for the responses. All but the middle view (with the decodes in it) are now solved. I now know that it is supposed to be "case when", but i can't get it to take the floor of the average of a group that has been converted to numbers. Does anybody have any suggestions for coding? Or am i going to be force to create one view in pg that references another view?