Index: openacs-4/packages/acs-content-repository/sql/postgresql/content-revision.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-content-repository/sql/postgresql/content-revision.sql,v
diff -u -r1.46 -r1.47
--- openacs-4/packages/acs-content-repository/sql/postgresql/content-revision.sql	10 Feb 2009 18:31:54 -0000	1.46
+++ openacs-4/packages/acs-content-repository/sql/postgresql/content-revision.sql	7 Jul 2011 10:46:02 -0000	1.47
@@ -19,26 +19,34 @@
 -- than the standard package_instantiate_object.  So we don't bother calling define_function_args
 -- here.
 
-create or replace function content_revision__new (varchar,varchar,timestamptz,varchar,varchar,integer,integer,integer,timestamptz,integer,varchar,integer)
-returns integer as '
-declare
-  new__title                  alias for $1;  
-  new__description            alias for $2;  -- default null 
-  new__publish_date           alias for $3;  -- default now()
-  new__mime_type              alias for $4;  -- default ''text/plain''
-  new__nls_language           alias for $5;  -- default null
+
+
+-- added
+
+--
+-- procedure content_revision__new/12
+--
+CREATE OR REPLACE FUNCTION content_revision__new(
+   new__title varchar,
+   new__description varchar,       -- default null
+   new__publish_date timestamptz,  -- default now()
+   new__mime_type varchar,         -- default 'text/plain'
+   new__nls_language varchar,      -- default null
+   new__data integer,
+   new__item_id integer,
+   new__revision_id integer,       -- default null
+   new__creation_date timestamptz, -- default now()
+   new__creation_user integer,     -- default null
+   new__creation_ip varchar,       -- default null
+   new__package_id integer         -- default null
+
+) RETURNS integer AS $$
+DECLARE
   -- lob id 
-  new__data                   alias for $6;  
-  new__item_id                alias for $7;  
-  new__revision_id            alias for $8;  -- default null
-  new__creation_date          alias for $9;  -- default now()
-  new__creation_user          alias for $10; -- default null
-  new__creation_ip            alias for $11; -- default null
-  new__package_id             alias for $12; -- default null  
   v_revision_id               integer;       
   v_package_id                acs_objects.package_id%TYPE;
   v_content_type              acs_object_types.object_type%TYPE;
-begin
+BEGIN
 
   v_content_type := content_item__get_content_type(new__item_id);
 
@@ -55,7 +63,7 @@
       new__creation_user, 
       new__creation_ip, 
       new__item_id,
-      ''t'',
+      't',
       new__title,
       v_package_id
   );
@@ -80,24 +88,31 @@
 
   return v_revision_id;
 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
-create or replace function content_revision__new (varchar,varchar,timestamptz,varchar,varchar,integer,integer,integer,timestamptz,integer,varchar)
-returns integer as '
-declare
-  new__title                  alias for $1;  
-  new__description            alias for $2;  -- default null 
-  new__publish_date           alias for $3;  -- default now()
-  new__mime_type              alias for $4;  -- default ''text/plain''
-  new__nls_language           alias for $5;  -- default null
+
+
+--
+-- procedure content_revision__new/11
+--
+CREATE OR REPLACE FUNCTION content_revision__new(
+   new__title varchar,
+   new__description varchar,       -- default null
+   new__publish_date timestamptz,  -- default now()
+   new__mime_type varchar,         -- default 'text/plain'
+   new__nls_language varchar,      -- default null
+   new__data integer,
+   new__item_id integer,
+   new__revision_id integer,       -- default null
+   new__creation_date timestamptz, -- default now()
+   new__creation_user integer,     -- default null
+   new__creation_ip varchar        -- default null
+
+) RETURNS integer AS $$
+DECLARE
   -- lob id 
-  new__data                   alias for $6;  
-  new__item_id                alias for $7;  
-  new__revision_id            alias for $8;  -- default null
-  new__creation_date          alias for $9;  -- default now()
-  new__creation_user          alias for $10; -- default null
-  new__creation_ip            alias for $11; -- default null
-begin
+BEGIN
         return content_revision__new(new__title,
                                      new__description,
                                      new__publish_date,
@@ -111,18 +126,26 @@
                                      new__creation_ip,
                                      null
                );
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
-create or replace function content_revision__new(varchar,varchar,timestamptz,varchar,text,integer,integer) returns integer as '
-declare
-        new__title              alias for $1;
-        new__description        alias for $2;  -- default null
-        new__publish_date       alias for $3;  -- default now()
-        new__mime_type          alias for $4;  -- default ''text/plain''
-        new__text               alias for $5;  -- default '' ''
-        new__item_id            alias for $6;
-        new__package_id         alias for $7;  -- default null
-begin
+
+
+--
+-- procedure content_revision__new/7
+--
+CREATE OR REPLACE FUNCTION content_revision__new(
+   new__title varchar,
+   new__description varchar,      -- default null
+   new__publish_date timestamptz, -- default now()
+   new__mime_type varchar,        -- default 'text/plain'
+   new__text text,                -- default ' '
+   new__item_id integer,
+   new__package_id integer        -- default null
+
+) RETURNS integer AS $$
+DECLARE
+BEGIN
         return content_revision__new(new__title,
                                      new__description,
                                      new__publish_date,
@@ -138,17 +161,25 @@
                                      new__package_id
                );
 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
-create or replace function content_revision__new(varchar,varchar,timestamptz,varchar,text,integer) returns integer as '
-declare
-        new__title              alias for $1;
-        new__description        alias for $2;  -- default null
-        new__publish_date       alias for $3;  -- default now()
-        new__mime_type          alias for $4;  -- default ''text/plain''
-        new__text               alias for $5;  -- default '' ''
-        new__item_id            alias for $6;
-begin
+
+
+--
+-- procedure content_revision__new/6
+--
+CREATE OR REPLACE FUNCTION content_revision__new(
+   new__title varchar,
+   new__description varchar,      -- default null
+   new__publish_date timestamptz, -- default now()
+   new__mime_type varchar,        -- default 'text/plain'
+   new__text text,                -- default ' '
+   new__item_id integer
+
+) RETURNS integer AS $$
+DECLARE
+BEGIN
         return content_revision__new(new__title,
                                      new__description,
                                      new__publish_date,
@@ -164,23 +195,30 @@
                                      null
                );
 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
-create or replace function content_revision__new (varchar,varchar,timestamptz,varchar,varchar,text,integer,integer,timestamptz,integer,varchar)
-returns integer as '
-declare
-  new__title                  alias for $1;  
-  new__description            alias for $2;  -- default null  
-  new__publish_date           alias for $3;  -- default now()
-  new__mime_type              alias for $4;  -- default ''text/plain''
-  new__nls_language           alias for $5;  -- default null
-  new__text                   alias for $6;  -- default '' ''
-  new__item_id                alias for $7;  
-  new__revision_id            alias for $8;  -- default null
-  new__creation_date          alias for $9;  -- default now()
-  new__creation_user          alias for $10; -- default null
-  new__creation_ip            alias for $11; -- default null
-begin
+
+
+--
+-- procedure content_revision__new/11
+--
+CREATE OR REPLACE FUNCTION content_revision__new(
+   new__title varchar,
+   new__description varchar,       -- default null
+   new__publish_date timestamptz,  -- default now()
+   new__mime_type varchar,         -- default 'text/plain'
+   new__nls_language varchar,      -- default null
+   new__text text,                 -- default ' '
+   new__item_id integer,
+   new__revision_id integer,       -- default null
+   new__creation_date timestamptz, -- default now()
+   new__creation_user integer,     -- default null
+   new__creation_ip varchar        -- default null
+
+) RETURNS integer AS $$
+DECLARE
+BEGIN
         return content_revision__new(new__title,
                                      new__description,
                                      new__publish_date,
@@ -195,24 +233,33 @@
                                      null,
                                      null
                                      );
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
-create or replace function content_revision__new (varchar,varchar,timestamptz,varchar,varchar,text,integer,integer,timestamptz,integer,varchar,integer)
-returns integer as '
-declare
-  new__title                  alias for $1;  
-  new__description            alias for $2;  -- default null  
-  new__publish_date           alias for $3;  -- default now()
-  new__mime_type              alias for $4;  -- default ''text/plain''
-  new__nls_language           alias for $5;  -- default null
-  new__text                   alias for $6;  -- default '' ''
-  new__item_id                alias for $7;  
-  new__revision_id            alias for $8;  -- default null
-  new__creation_date          alias for $9;  -- default now()
-  new__creation_user          alias for $10; -- default null
-  new__creation_ip            alias for $11; -- default null
-  new__content_length         alias for $12; -- default null
-begin
+
+
+-- added
+
+--
+-- procedure content_revision__new/12
+--
+CREATE OR REPLACE FUNCTION content_revision__new(
+   new__title varchar,
+   new__description varchar,       -- default null
+   new__publish_date timestamptz,  -- default now()
+   new__mime_type varchar,         -- default 'text/plain'
+   new__nls_language varchar,      -- default null
+   new__text text,                 -- default ' '
+   new__item_id integer,
+   new__revision_id integer,       -- default null
+   new__creation_date timestamptz, -- default now()
+   new__creation_user integer,     -- default null
+   new__creation_ip varchar,       -- default null
+   new__content_length integer     -- default null
+
+) RETURNS integer AS $$
+DECLARE
+BEGIN
         return content_revision__new(new__title,
                                      new__description,
                                      new__publish_date,
@@ -227,31 +274,41 @@
                                      new__content_length,
                                      null
                                      );
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 -- function new
-create or replace function content_revision__new (varchar,varchar,timestamptz,varchar,varchar,text,integer,integer,timestamptz,integer,varchar,integer,integer)
-returns integer as '
-declare
-  new__title                  alias for $1;  
-  new__description            alias for $2;  -- default null  
-  new__publish_date           alias for $3;  -- default now()
-  new__mime_type              alias for $4;  -- default ''text/plain''
-  new__nls_language           alias for $5;  -- default null
-  new__text                   alias for $6;  -- default '' ''
-  new__item_id                alias for $7;  
-  new__revision_id            alias for $8;  -- default null
-  new__creation_date          alias for $9;  -- default now()
-  new__creation_user          alias for $10; -- default null
-  new__creation_ip            alias for $11; -- default null
-  new__content_length         alias for $12; -- default null
-  new__package_id             alias for $13; -- default null  
+
+
+-- added
+select define_function_args('content_revision__new','title,description;null,publish_date;now(),mime_type;text/plain,nls_language;null,text; ,item_id,revision_id;null,creation_date;now(),creation_user;null,creation_ip;null,content_length;null,package_id;null');
+
+--
+-- procedure content_revision__new/13
+--
+CREATE OR REPLACE FUNCTION content_revision__new(
+   new__title varchar,
+   new__description varchar,       -- default null
+   new__publish_date timestamptz,  -- default now()
+   new__mime_type varchar,         -- default 'text/plain'
+   new__nls_language varchar,      -- default null
+   new__text text,                 -- default ' '
+   new__item_id integer,
+   new__revision_id integer,       -- default null
+   new__creation_date timestamptz, -- default now()
+   new__creation_user integer,     -- default null
+   new__creation_ip varchar,       -- default null
+   new__content_length integer,    -- default null
+   new__package_id integer         -- default null
+
+) RETURNS integer AS $$
+DECLARE
   v_revision_id               integer;       
   v_package_id                acs_objects.package_id%TYPE;
   v_content_type              acs_object_types.object_type%TYPE;
   v_storage_type              cr_items.storage_type%TYPE;
   v_length                    cr_revisions.content_length%TYPE;
-begin
+BEGIN
 
   v_content_type := content_item__get_content_type(new__item_id);
 
@@ -268,7 +325,7 @@
       new__creation_user, 
       new__creation_ip, 
       new__item_id,
-      ''t'',
+      't',
       new__title,
       v_package_id
   );
@@ -277,7 +334,7 @@
     from cr_items
    where item_id = new__item_id;
 
-  if v_storage_type = ''text'' then 
+  if v_storage_type = 'text' then 
      v_length := length(new__text);
   else
      v_length := coalesce(new__content_length,0);
@@ -297,25 +354,31 @@
 
   return v_revision_id;
  
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 
 -- procedure copy_attributes
 select define_function_args('content_revision__copy_attributes','content_type,revision_id,copy_id');
-create or replace function content_revision__copy_attributes (varchar,integer,integer)
-returns integer as '
-declare
-  copy_attributes__content_type           alias for $1;  
-  copy_attributes__revision_id            alias for $2;  
-  copy_attributes__copy_id                alias for $3;  
+
+
+--
+-- procedure content_revision__copy_attributes/3
+--
+CREATE OR REPLACE FUNCTION content_revision__copy_attributes(
+   copy_attributes__content_type varchar,
+   copy_attributes__revision_id integer,
+   copy_attributes__copy_id integer
+) RETURNS integer AS $$
+DECLARE
   v_table_name                            acs_object_types.table_name%TYPE;
   v_id_column                             acs_object_types.id_column%TYPE;
-  cols                                    varchar default ''''; 
+  cols                                    varchar default ''; 
   attr_rec                                record;
-begin
+BEGIN
 
   if copy_attributes__content_type is null or copy_attributes__revision_id is null or copy_attributes__copy_id is null then 
-     raise exception ''content_revision__copy_attributes called with null % % %'',copy_attributes__content_type,copy_attributes__revision_id, copy_attributes__copy_id;
+     raise exception 'content_revision__copy_attributes called with null % % %',copy_attributes__content_type,copy_attributes__revision_id, copy_attributes__copy_id;
   end if;
 
   select table_name, id_column into v_table_name, v_id_column
@@ -328,33 +391,44 @@
                   where
                     object_type = copy_attributes__content_type 
   LOOP
-    cols := cols || '', '' || attr_rec.attribute_name;
+    cols := cols || ', ' || attr_rec.attribute_name;
   end loop;
 
-    execute ''insert into '' || v_table_name || ''('' || v_id_column || cols || '')'' || '' select '' || copy_attributes__copy_id || 
-          '' as '' || v_id_column || cols || '' from '' || 
-          v_table_name || '' where '' || v_id_column || '' = '' || 
+    execute 'insert into ' || v_table_name || '(' || v_id_column || cols || ')' || ' select ' || copy_attributes__copy_id || 
+          ' as ' || v_id_column || cols || ' from ' || 
+          v_table_name || ' where ' || v_id_column || ' = ' || 
           copy_attributes__revision_id;
 
   return 0; 
 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 
 -- function copy
-select define_function_args('content_revision__copy','revision_id,copy_id,target_item_id,creation_user,creation_ip');
-create or replace function content_revision__copy (integer,integer,integer,integer,varchar)
-returns integer as '
-declare
-  copy__revision_id            alias for $1;  
-  copy__copy_id                alias for $2;  -- default null  
-  copy__target_item_id         alias for $3;  -- default null
-  copy__creation_user          alias for $4;  -- default null
-  copy__creation_ip            alias for $5;  -- default null
+
+-- old define_function_args('content_revision__copy','revision_id,copy_id,target_item_id,creation_user,creation_ip')
+-- new
+select define_function_args('content_revision__copy','revision_id,copy_id;null,target_item_id;null,creation_user;null,creation_ip;null');
+
+
+
+--
+-- procedure content_revision__copy/5
+--
+CREATE OR REPLACE FUNCTION content_revision__copy(
+   copy__revision_id integer,
+   copy__copy_id integer,        -- default null
+   copy__target_item_id integer, -- default null
+   copy__creation_user integer,  -- default null
+   copy__creation_ip varchar     -- default null
+
+) RETURNS integer AS $$
+DECLARE
   v_copy_id                    cr_revisions.revision_id%TYPE;
   v_target_item_id             cr_items.item_id%TYPE;
   type_rec                     record;
-begin
+BEGIN
   -- use the specified item_id or the item_id of the original revision 
   --   if none is specified
   if copy__target_item_id is null then
@@ -367,7 +441,7 @@
   -- use the copy_id or generate a new copy_id if none is specified
   --   the copy_id is a revision_id
   if copy__copy_id is null then
-    select nextval(''t_acs_object_id_seq'') into v_copy_id from dual;
+    select nextval('t_acs_object_id_seq') into v_copy_id from dual;
   else
     v_copy_id := copy__copy_id;
   end if;
@@ -425,8 +499,8 @@
   -- iterate over the ancestor types and copy attributes
   for type_rec in select ot2.object_type, tree_level(ot2.tree_sortkey) as level
                   from acs_object_types ot1, acs_object_types ot2, acs_objects o
-                  where ot2.object_type <> ''acs_object''                       
-                    and ot2.object_type <> ''content_revision''
+                  where ot2.object_type <> 'acs_object'                       
+                    and ot2.object_type <> 'content_revision'
                     and o.object_id = copy__revision_id 
                     and ot1.object_type = o.object_type 
                     and ot1.tree_sortkey between ot2.tree_sortkey and tree_right(ot2.tree_sortkey)
@@ -438,20 +512,26 @@
 
   return v_copy_id;
  
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 
 -- procedure delete
 select define_function_args('content_revision__del','revision_id');
-create or replace function content_revision__del (integer)
-returns integer as '
-declare
-  delete__revision_id    alias for $1;  
+
+
+--
+-- procedure content_revision__del/1
+--
+CREATE OR REPLACE FUNCTION content_revision__del(
+   delete__revision_id integer
+) RETURNS integer AS $$
+DECLARE
   v_item_id              cr_items.item_id%TYPE;
   v_latest_revision      cr_revisions.revision_id%TYPE;
   v_live_revision        cr_revisions.revision_id%TYPE;
   v_rec                  record;                                      
-begin
+BEGIN
 
   -- Get item id and latest/live revisions
   select item_id into v_item_id from cr_revisions 
@@ -503,29 +583,41 @@
   PERFORM acs_object__delete(delete__revision_id);
 
   return 0; 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 select define_function_args('content_revision__delete','revision_id');
 
-create or replace function content_revision__delete (integer)
-returns integer as '
-declare
-  delete__revision_id    alias for $1;  
-begin
+
+
+--
+-- procedure content_revision__delete/1
+--
+CREATE OR REPLACE FUNCTION content_revision__delete(
+   delete__revision_id integer
+) RETURNS integer AS $$
+DECLARE
+BEGIN
         PERFORM content_revision__del(delete__revision_id);
   return 0; 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 -- function get_number
 select define_function_args('content_revision__get_number','revision_id');
-create or replace function content_revision__get_number (integer)
-returns integer as '
-declare
-  get_number__revision_id            alias for $1;  
+
+
+--
+-- procedure content_revision__get_number/1
+--
+CREATE OR REPLACE FUNCTION content_revision__get_number(
+   get_number__revision_id integer
+) RETURNS integer AS $$
+DECLARE
   v_revision                         cr_revisions.revision_id%TYPE;
   v_row_count                        integer default 0;
   rev_cur                            record;
-begin
+BEGIN
   for rev_cur in select
                    revision_id
                  from 
@@ -547,32 +639,44 @@
 
   return null;
  
-end;' language 'plpgsql' stable strict;
+END;
+$$ LANGUAGE plpgsql stable strict;
 
 select define_function_args('content_revision__revision_name','revision_id');
-create or replace function content_revision__revision_name(integer)
-returns text as '
-declare
-        p_revision_id   alias for $1;
-begin
-        return ''Revision '' || content_revision__get_number(revision_id) || 
-               '' of '' || (select count(*) from cr_revisions where item_id = r.item_id) || '' for item: '' 
+
+
+--
+-- procedure content_revision__revision_name/1
+--
+CREATE OR REPLACE FUNCTION content_revision__revision_name(
+   p_revision_id integer
+) RETURNS text AS $$
+DECLARE
+BEGIN
+        return 'Revision ' || content_revision__get_number(revision_id) || 
+               ' of ' || (select count(*) from cr_revisions where item_id = r.item_id) || ' for item: ' 
                || content_item__get_title(item_id)
                from cr_revisions r where r.revision_id = p_revision_id;
-end;' language 'plpgsql' stable strict;
+END;
+$$ LANGUAGE plpgsql stable strict;
 
 -- procedure to_html
 select define_function_args('content_revision__to_html','revision_id');
-create or replace function content_revision__to_html (integer)
-returns integer as '
-declare
-  to_html__revision_id            alias for $1;  
+
+
+--
+-- procedure content_revision__to_html/1
+--
+CREATE OR REPLACE FUNCTION content_revision__to_html(
+   to_html__revision_id integer
+) RETURNS integer AS $$
+DECLARE
   tmp_clob                        text;          
   blob_loc                        integer;          
-begin
+BEGIN
 
   -- FIXME
-  -- ctx_doc.filter(''cr_doc_filter_index'', revision_id, tmp_clob);
+  -- ctx_doc.filter('cr_doc_filter_index', revision_id, tmp_clob);
 
   select 
     content into blob_loc
@@ -587,50 +691,71 @@
  PERFORM dbms_lob__freetemporary(tmp_clob);
 
  return 0; 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 
 -- function is_live
 select define_function_args('content_revision__is_live','revision_id');
-create or replace function content_revision__is_live (integer)
-returns boolean as '
-declare
-  is_live__revision_id            alias for $1;  
-begin
 
+
+--
+-- procedure content_revision__is_live/1
+--
+CREATE OR REPLACE FUNCTION content_revision__is_live(
+   is_live__revision_id integer
+) RETURNS boolean AS $$
+DECLARE
+BEGIN
+
   return count(*) > 0 from cr_items
    where live_revision = is_live__revision_id;
 
-end;' language 'plpgsql' strict;
+END;
+$$ LANGUAGE plpgsql strict;
 
 
 -- function is_latest
 select define_function_args('content_revision__is_latest','revision_id');
-create or replace function content_revision__is_latest (integer)
-returns boolean as '
-declare
-  is_latest__revision_id            alias for $1;  
-begin
 
+
+--
+-- procedure content_revision__is_latest/1
+--
+CREATE OR REPLACE FUNCTION content_revision__is_latest(
+   is_latest__revision_id integer
+) RETURNS boolean AS $$
+DECLARE
+BEGIN
+
   return count(*) > 0 from cr_items
     where latest_revision = is_latest__revision_id;
  
-end;' language 'plpgsql' stable;
+END;
+$$ LANGUAGE plpgsql stable;
 
 
 -- procedure to_temporary_clob
-create or replace function content_revision__to_temporary_clob (integer)
-returns integer as '
-declare
-  to_temporary_clob__revision_id            alias for $1;  
+
+
+-- added
+select define_function_args('content_revision__to_temporary_clob','revision_id');
+
+--
+-- procedure content_revision__to_temporary_clob/1
+--
+CREATE OR REPLACE FUNCTION content_revision__to_temporary_clob(
+   to_temporary_clob__revision_id integer
+) RETURNS integer AS $$
+DECLARE
   -- b                                         blob;          
   -- c                                         text;          
-begin
+BEGIN
   -- FIXME:  I cannot find an instance in the 4.2 beta code where this
   --         is used so I am not worrying about porting it for now.
   --         DCW - 2001-03-28.
 
-  raise EXCEPTION ''not implemented content_revision.to_temporary_clob'';
+  raise EXCEPTION 'not implemented content_revision.to_temporary_clob';
 /*
   insert into cr_content_text (
     revision_id, content
@@ -644,26 +769,37 @@
   PERFORM blob_to_clob(b, c);
 */
   return 0; 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 
 -- procedure content_copy
-select define_function_args('content_revision__content_copy','revision_id,revision_id_dest');
-create or replace function content_revision__content_copy (integer,integer)
-returns integer as '
-declare
-  content_copy__revision_id            alias for $1;  
-  content_copy__revision_id_dest       alias for $2;  -- default null  
+
+-- old define_function_args('content_revision__content_copy','revision_id,revision_id_dest')
+-- new
+select define_function_args('content_revision__content_copy','revision_id,revision_id_dest;null');
+
+
+
+--
+-- procedure content_revision__content_copy/2
+--
+CREATE OR REPLACE FUNCTION content_revision__content_copy(
+   content_copy__revision_id integer,
+   content_copy__revision_id_dest integer -- default null
+
+) RETURNS integer AS $$
+DECLARE
   v_item_id                            cr_items.item_id%TYPE;
   v_content_length                     cr_revisions.content_length%TYPE;
   v_revision_id_dest                   cr_revisions.revision_id%TYPE;
   v_content                            cr_revisions.content%TYPE;
   v_lob                                cr_revisions.lob%TYPE;
   v_new_lob                            cr_revisions.lob%TYPE;
   v_storage_type                       varchar;
-begin
+BEGIN
   if content_copy__revision_id is null then 
-	raise exception ''content_revision__content_copy attempt to copy a null revision_id'';
+	raise exception 'content_revision__content_copy attempt to copy a null revision_id';
   end if;
 
   select
@@ -703,7 +839,7 @@
      where r.item_id = i.item_id 
        and r.revision_id = content_copy__revision_id;
 
-    if v_storage_type = ''lob'' then
+    if v_storage_type = 'lob' then
         v_new_lob := empty_lob();
 
 	PERFORM lob_copy(v_lob, v_new_lob);
@@ -725,7 +861,7 @@
         -- with the new relative file path:
 
         -- update cr_revisions
-        -- set content = ''[cr_create_content_file $item_id $revision_id [cr_fs_path]$old_rel_path]''
+        -- set content = '[cr_create_content_file $item_id $revision_id [cr_fs_path]$old_rel_path]'
         -- where revision_id = :revision_id
         
         -- old_rel_path is the content attribute value of the content revision
@@ -740,45 +876,53 @@
   end if;
 
   return 0; 
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 
 -- procedure content__get_content
 select define_function_args('content_revision__get_content','revision_id');
-create or replace function content_revision__get_content (integer)
-returns text as '
-declare
-  get_content__revision_id            alias for $1;  
+
+
+--
+-- procedure content_revision__get_content/1
+--
+CREATE OR REPLACE FUNCTION content_revision__get_content(
+   get_content__revision_id integer
+) RETURNS text AS $$
+DECLARE
   v_storage_type                      varchar;
   v_lob_id                            integer;
   v_data                              text;
-begin
+BEGIN
        select i.storage_type, r.lob 
          into v_storage_type, v_lob_id
          from cr_items i, cr_revisions r
         where i.item_id = r.item_id 
           and r.revision_id = get_content__revision_id;
         
-        if v_storage_type = ''lob'' then
+        if v_storage_type = 'lob' then
            return v_lob_id::text;
         else 
            return content
              from cr_revisions
             where revision_id = get_content__revision_id;
         end if;
 
-end;' language 'plpgsql' stable strict;
+END;
+$$ LANGUAGE plpgsql stable strict;
 
 -- show errors
 
 -- Trigger to maintain latest_revision in cr_items
 
-create function cr_revision_latest_tr () returns opaque as '
-begin
+CREATE OR REPLACE FUNCTION cr_revision_latest_tr () RETURNS trigger AS $$
+BEGIN
   update cr_items set latest_revision = new.revision_id
   where item_id = new.item_id;
   return new;
-end;' language 'plpgsql';
+END;
+$$ LANGUAGE plpgsql;
 
 create trigger cr_revision_latest_tr after insert on cr_revisions
 for each row execute procedure cr_revision_latest_tr ();
