Received: from tina.andersenit.dk (tina.andersenit.dk [193.89.137.127] (may be forged)) by postgresql.org (8.11.3/8.11.4) with ESMTP id fAT1wnm10097 for ; Wed, 28 Nov 2001 20:58:50 -0500 (EST) (envelope-from pgsql@halfahead.dk) Received: from ([130.226.84.71]) by tina.andersenit.dk (Merak 4.10.040) with SMTP id BHB36607 for ; Thu, 29 Nov 2001 02:58:52 +0100 Message-ID: <200111290305140952.021C953A@mail.halfahead.dk> In-Reply-To: <200111281325520802.0018C1FB@mail.halfahead.dk> References: <200111281325520802.0018C1FB@mail.halfahead.dk> X-Mailer: Calypso Version 3.30.00.00 (4) Date: Thu, 29 Nov 2001 03:05:14 +0100 From: =?us-ascii?Q?=22Johnny_J=F8rgensen=22?= To: pgsql-sql@postgresql.org Subject: Re: PL/pgSQL loops? Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="=====_100699951427590=_" X-Archive-Number: 200111/395 X-Sequence-Number: 5777 --=====_100699951427590=_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Ok, I'll try to clarify a bit: first the version, SELECT version(); --> version ------------------------------------------------------------- PostgreSQL 7.1.3 on i686-pc-linux-gnu, compiled by GCC 2.96 (1 row) now, The cur_order_id <- cur_order.id was because it was snipped midways through= an attempt, looping through records instead (which didnt work either), bel= ow is the original function. As for the schema, it looks like this: TABLE ordre ( [PK] id int4, [FK] person_id int4, placement_date timestamp, status int2 ) TABLE item ( [PK] id int4, [FK] ordre_id int4, [FK] uniq_id int4, price decimal(10,2) ) as for the errors, i double checked, with this very scheme, and oddly, the = error message changes. I have a hard time actually numbering the lines (sin= ce \g [file] adds some space above the function), but the only line reasona= bly close is the *inner* END LOOP; - not the outer anymore? The error this time is: Parse error at or near ";" - I double checked my books, and the docs, and as far as I can tell, the s= emicolon should be there, so what gives? Actually, the two loops should not be neccessary (after each interrupted se= ssion, a cleanup will be made, so there'll be only one stranded order at a = time), but I dont like the idea of eventual hits being "silently discarded".. - hope this clarifies the issue a bit, and that someone can see through thi= s, regards, Johnny J=F8rgensen function definition below ---------------------------------------------------------------------------= ---------------------------------------------------------------------------- CREATE FUNCTION cleanup_order(integer,integer) RETURNS boolean AS ' -- [INT order_id] cleanup_order ( INT person_id, INT order_id ) -- purpose: when a session is interrupted, the order is left open, when the= next session is initiated, -- transfer the items to a new order, and mark the older or= der void ( so we can tell if f.x. people abort -- an order in the payment phase, e.g. indicating they dont = like our payment options ) DECLARE p_id ALIAS FOR $1; o_id ALIAS FOR $2; cur_order_id integer; cur_item_id integer; BEGIN -- loop through existing open orders from this person, excluding the firs= t, being the active one FOR cur_order_id IN SELECT id FROM ordre WHERE person_id =3D p_id AND sta= tus =3D 1 AND id !=3D o_id LOOP -- loop through items linked to the currently looped order FOR cur_item_id IN SELECT id FROM item WHERE order_id =3D cur_order_= id LOOP -- relink item to latest order UPDATE item SET ordre_id =3D o_id WHERE id =3D cur_item_id; END LOOP; -- mark old orders as void UPDATE ordre SET status =3D 0 WHERE id =3D cur_order_id; END LOOP; RETURN true; END; ' LANGUAGE 'plpgsql'; --=====_100699951427590=_ Content-Type: text/html; charset="us-ascii"
Ok, I'll try to clarify a bit:
first the version,
 
SELECT version();
-->
 
                           version                          
-------------------------------------------------------------
 PostgreSQL 7.1.3 on i686-pc-linux-gnu, compiled by GCC 2.96
(1 row)
 
now,
 
The cur_order_id <- cur_order.id was because it was snipped midways through an attempt, looping through records instead (which didnt work either), below is the original function.
 
As for the schema,  it looks like this:
 
TABLE ordre (
    [PK] id                        int4,
    [FK] person_id          int4,
    placement_date        timestamp,
    status                          int2
)
 
TABLE item (
    [PK] id                        int4,
    [FK] ordre_id             int4,
    [FK] uniq_id               int4,
    price                           decimal(10,2)
)
 
as for the errors, i double checked, with this very scheme, and oddly, the error message changes. I have a hard time actually numbering the lines (since \g [file] adds some space above the function), but the only line reasonably close is the *inner*
 
END LOOP;
 
 - not the outer anymore? The error this time is:
 
Parse error at or near ";"
 
 - I double checked my books, and the docs, and as far as I can tell, the semicolon should be there, so what gives?
Actually, the two loops should not be neccessary (after each interrupted session, a cleanup will be made, so there'll be only one stranded order at a time),
but I dont like the idea of eventual hits being "silently discarded"..
 
- hope this clarifies the issue a bit, and that someone can see through this,
 
regards,
 
Johnny Jørgensen
 
function definition below
-------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION cleanup_order(integer,integer)
RETURNS boolean
AS '
 -- [INT order_id] cleanup_order ( INT person_id, INT order_id )
-- purpose: when a session is interrupted, the order is left open, when the next session is initiated,
--                 transfer the items to a new order, and mark the older order void ( so we can tell if f.x. people abort
--                an order in the payment phase, e.g. indicating they dont like our payment options )

 DECLARE
  p_id  ALIAS FOR $1;
  o_id  ALIAS FOR $2;
  cur_order_id integer;
  cur_item_id integer;

 BEGIN
 
  -- loop through existing open orders from this person, excluding the first, being the active one
  FOR cur_order_id IN SELECT id FROM ordre WHERE person_id = p_id AND status = 1 AND id != o_id
  LOOP
       -- loop through items linked to the currently looped order
       FOR cur_item_id IN SELECT id FROM item WHERE order_id = cur_order_id
       LOOP 

            -- relink item to latest order
            UPDATE item SET ordre_id = o_id WHERE id = cur_item_id;
 
       END LOOP;
 
       -- mark old orders as void
       UPDATE ordre SET status = 0 WHERE id = cur_order_id;
 
  END LOOP;

  RETURN true;

 END;
'
LANGUAGE 'plpgsql';
--=====_100699951427590=_--