pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Anthony Chavez <acc@anthonychavez.org>
To: pgsql-sql@postgresql.org
Subject: ON INSERT view rule
Date: Tue, 11 Jul 2006 04:53:40 -0600
Message-ID: <m23bd8h0p7.fsf@pegasos.aegaeum.anthonychavez.org> (raw)

Hello, pgsql-sql!

What I've got here are a couple of ON INSERT rules for a view.  The
second rule is what I'm concerned about.  I wrote it with PostgreSQL's
ACID compliance in mind, but can I trust it?

From what I gather, if I were to simply use NEW.address_line_id rather
than address_lines_id_seq.last_value, it would be replaced by
nextval(address_line_id_seq), so I'm trying to work around that.

If there is there a better way to do this, I'm all ears.  Would
lastval() work for me in this case?  Thanks!

-- 
Anthony Chavez                                 http://anthonychavez.org/
mailto:acc@anthonychavez.org         jabber:acc@jabber.anthonychavez.org

CREATE OR REPLACE VIEW addresses_address_lines
AS
SELECT a.id AS address_id,
       al.id AS address_line_id,
       line,
       ordering
  FROM addresses a
       INNER JOIN
       address_lines al
       ON al.address_id = a.id
       LEFT OUTER JOIN
       junctions
       ON parent_table = 'address_lines'
          AND parent_id = al.id
          AND child_table = 'display_orderings'
       LEFT OUTER JOIN
       display_orderings o
       ON o.id = child_id;

CREATE OR REPLACE RULE insert_address_lines
AS ON INSERT
TO addresses_address_lines
DO INSTEAD
INSERT INTO address_lines (address_id, line)
VALUES (NEW.address_id, NEW.line);

CREATE OR REPLACE RULE insert_display_orderings
AS ON INSERT
TO addresses_address_lines
DO
UPDATE display_orderings
   SET ordering = NEW.ordering
  FROM address_lines_id_seq
       INNER JOIN
       junctions
       ON parent_table = 'address_lines'
          AND parent_id = last_value
          AND child_table = 'display_orderings'
 WHERE ordering <> NEW.ordering
       AND display_orderings.id = child_id;

view thread (2+ messages)  latest in thread

Message-ID: <m23bd8h0p7.fsf@pegasos.aegaeum.anthonychavez.org>
Permalink:  ../m23bd8h0p7.fsf@pegasos.aegaeum.anthonychavez.org/
Also on:    postgresql.org/message-id/m23bd8h0p7.fsf@pegasos.aegaeum.anthonychavez.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: acc@anthonychavez.org
  Subject: Re: ON INSERT view rule
  In-Reply-To: <m23bd8h0p7.fsf@pegasos.aegaeum.anthonychavez.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox