Forum OpenACS Q&A: Use of define_function_args

Collapse
Posted by Jade Rubick on
I was creating a drop script for the organization package, and was looking at Joel's documentation for reference. In it, he includes in his data model the use of define_function_args.

I searched Google for this, and found that DaveB considered not having it to be a bug (at least he did here: https://openacs.org/bugtracker/openacs/com/bulk-mail/bug?bug_number=55)

I also looked up the function.

I've used this function (without really knowing what it does) creating other data models, but for some reason, I'm getting this error now:

psql:organizations-plsql-create.sql:35: ERROR:  Attribute 'organization__new' not found
CREATE
psql:organizations-plsql-create.sql:110: ERROR:  Attribute 'organization__del' not found
CREATE
psql:organizations-plsql-create.sql:137: ERROR:  Attribute 'organization__set' not found
Strangely, the organization__name define_function_args seems to work:
CREATE
 define_function_args 
----------------------
                    1
(1 row)
I define them as so:
select define_function_args(organization__new,'legal_name,name,notes,organization_id,organization_type_id,reg_number,email,url,creation_user,creation_ip,context_id'); 

create or replace function organization__new ( 
	varchar, -- legal_name
	varchar, -- name
	text,    -- notes
	integer, -- organization_id
	integer, -- organization_type_id
	varchar, -- reg_number
	varchar, -- email
	varchar, -- url
	integer, -- creation_user
	varchar, -- creation_ip
	integer  -- context_id
) returns integer as ' 
declare 
    p_legal_name           alias for $1; -- comment
    p_name                 alias for $2; -- comment
    p_notes                alias for $3; -- comment
    p_organization_id      alias for $4; -- comment
    p_organization_typeid  alias for $5; -- comment
    p_reg_number           alias for $6; -- comment
    p_email                alias for $7; -- email
    p_url                  alias for $8;
    p_creation_user        alias for $9; -- comment
    p_creation_ip          alias for $10;
    p_context_id           alias for $11; -- comment

    -- local vars
    v_organization_id organizations.organization_id%TYPE; 
begin 
  v_organization_id := party__new (  
    null,              -- party_id
    ''organization'',
    now(), 
    p_creation_user,
    p_creation_ip,
    p_email, 
    p_url,
    p_context_id 
  );   
   

  insert into organizations (
    legal_name,
    name,
    notes,
    organization_id,
    organization_type_id,
    reg_number 
  )  
  values ( 
    p_legal_name,
    p_name,
    p_notes,
    v_organization_id,
    p_organization_type_id,
    p_reg_number 
  ); 

  PERFORM acs_permission__grant_permission (
     v_organization_id,
     p_creation_user,
     ''admin''
  );

   raise NOTICE ''Adding organization - %'',p_name;
  return v_organization_id;

end;' language 'plpgsql';


select define_function_args(organization__del,'organization_id'); 

create or replace function organization__del (integer) 
returns integer as ' 
declare 
 p_organization_id    alias for $1; 
 v_return integer := 0;  
begin 

   delete from acs_permissions 
     where object_id = p_organization_id; 

   delete from organizations 
     where organization_id = p_organization_id;

   raise NOTICE ''Deleting organization - %'',p_organization_id;

   PERFORM party_delete(p_organization_id);

   return v_return;

end;' language 'plpgsql';



select define_function_args(organization__set,'varchar,varchar,text,integer,varchar'); 

create or replace function organization__set (varchar,varchar,text,integer,varchar)
returns integer as ' 
declare 
    p_legal_name         alias for $1; -- comment
    p_name               alias for $2; -- comment
    p_notes              alias for $3; -- comment
    p_organization_id    alias for $4; -- comment
    p_reg_number         alias for $5; -- comment

    v_return integer := 0; 
begin 

  update organizations
  set 
    legal_name = p_legal_name,
    name = p_name,
    notes = p_notes,
    organization_id = p_organization_id,
    reg_number = p_reg_number
  where organization_id = p_organization_id;

  raise NOTICE ''Updating  - organization - %'',organization_id;

return v_return;
end;' language 'plpgsql';



select define_function_args('organization___name','organization_id');

create or replace function organization__name (integer)
returns varchar as '
declare
    p_organization_id    alias for $1;
    v_organization_name  organization.name%TYPE;
begin
        select name into v_organization_name
                from organizations
                where organization_id = p_organization_id;
    return v_organization_name;
end;
' language 'plpgsql';
I'd appreciate any advice on this.
Collapse
Posted by Don Baccus on
You need to enclose organization__new in single quotes in your call to define_function_args ...

It's useful because it allows you to use the automatic object generation Tcl API and in turn using this means you don't have to write both Oracle and PG queries to create an object.

Collapse
Posted by Jade Rubick on
Thanks, Don. That worked. For reference, it seems that you only want to define for __new, __del, and __name

This: select define_function_args(organization__set,'varchar,varchar,text,integer,varchar');

was not dropped by my drop script.

Collapse
Posted by Ola Hansson on
Hi Jade,

You have data types in the second arg to define_function_args when it should be the attribute names. It also looks like you need to add single quotes around the function name in this call, too.

select define_function_args('organization__set','...');

Also, is reg_number really a varchar?

BTW, using define_function_args, there's a way to specify default values for attributes by appending ";_the_default_value_" to the attribute(s). I'm not sure this is useful today, but it might be in the future ...

Collapse
Posted by Jade Rubick on
Ola, good points. Thank you.

reg_number is a varchar, because it can contain letters.