Forum OpenACS Development: Porting update procedures from oracle to pg

I am working on porting a module that has procedures for updating the object table:

 
procedure set_attrs ( 
  activity_id in activities.activity_id%TYPE,
  notes in activities.notes%TYPE default null
)
is

begin

 if notes is not null then
    update activities set notes = set_attrs.notes
      where activity_id = set_attrs.activity_id;
 end if;
end set_attrs;

-- and to null a note:

procedure reset_attr ( 
  activity_id in activities.activity_id%TYPE,
  column_name in varchar
)
is
begin
 if column_name = 'notes' then
   update activities set notes = NULL
     where activity_id = reset_attr.activity_id;
 end if;

end reset_attr;

I have been looking through the ported code and I haven't found any examples for updating the object tables. Is this done through simple update statements? For updating in my tcl pages I use something like this:

db_exec_plsql end_activity {
  begin
    activity.set_attrs (
      activity_id => :activity_id,
      end_time => sysdate
  );
  end;
}

Is this to be ported to a db_dml?

Collapse
Posted by Don Baccus on
db_exec_plsql's been hacked to execute a "select" directly, discarding the result.  Multi-statement blocks are currently dumped into a temp function, executed, with the temp function then being dropped.  We haven't quite decided what we'll do to improve performance for this latter case (cache the function name and only create it once?) but it works for now.

So you should leave the db_exec_plsql alone, amd change the query in your *-postgresql.xql file to read "select activity__set_attrs(...)".

Collapse
Posted by Tom Jackson on

I guess I should look at the hacked db_exec_plsql, but I guess there are certain things I don't understand here. In Oracle/ACS, you can call a function with named parameters, but not in postgresql. So if I have a pg functions with 10 parameters, do I have to have 10 parameters for every call to this function? I'm sure that I do. If that is right, then porting calls inside db_exec_plsql will have to be expanded to include all parameters.

Another issue is NULLs. Isn't pg supposed to handle NULLs more explicitly than Oracle. NULL != ''? Can I use a NULL parameter value as a key to update as I did in the set_attrs procedure above?

Collapse
Posted by Gilbert Wong on

Tom - yes you have to include all parameters. You could overload the function for the most common cases. Dan points out where to find examples in this thread:

https://openacs.org/bboard/q-and-a-fetch-msg.tcl?msg_id=0002B7&topic_id=12&topic=OpenACS%204%2e0%20Design

Collapse
Posted by Don Baccus on
Additionally ... yes, PG handles NULLs correctly as defined by SQL92, which Oracle does not.  In insert and update statements Oracle treats the empty string ('') as NULL.  The correct SQL92 definition is that the empty string is, well, the empty string and NULL represents an unknown value.  This is consistent with other datatypes - SQL92 is very explicit on the issue.

Setting the attr NULL in this case should be OK.  Code using it, even Oracle code, should be using "IS NULL" since the empty string is *not*  treated as NULL in comparisons, etc.