Re: CREATE RULE with WHERE clause

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Ranieri Mazili <ranieri(dot)oliveira(at)terra(dot)com(dot)br>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: CREATE RULE with WHERE clause
Date: 2007-06-05 14:40:50
Message-ID: 41FB1D71-534B-4358-9B09-A60C35F948B9@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql


On Jun 5, 2007, at 8:11 , Ranieri Mazili wrote:

> CREATE RULE rule_role_sul AS
> ON SELECT TO t1 WHERE roles = 'role_sul'
> DO INSTEAD SELECT field1, field2 FROM t2;
>
> CREATE RULE rule_role_sul AS
> ON SELECT TO t1 WHERE roles = 'role_norte'
> DO INSTEAD SELECT field3, field4 FROM t2;

I don't believe you can include a WHERE clause like this. From the
documentation[1]:

http://www.postgresql.org/docs/8.2/interactive/rules-views.html#RULES-
SELECT

> Currently, there can be only one action in an ON SELECT rule, and
> it must be an unconditional SELECT action that is INSTEAD. This
> restriction was required to make rules safe enough to open them for
> ordinary users, and it restricts ON SELECT rules to act like views.

You can use views instead (which are implemented using the rule
system), but I'm not sure how you would handle it in this case. I
believe you'd have to implement two views:

CREATE VIEW rule_role_sul AS
SELECT field1, field2
FROM t2
WHERE roles = 'role_sul';

CREATE VIEW rule_role_norte AS
SELECT field3, field4
FROM t2
WHERE roles = 'role_norte';

Hope this helps.

Michael Glaesemann
grzm seespotcode net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Greg Smith 2007-06-05 14:54:58 Re: Corruption of files in PostgreSQL
Previous Message Marko Kreen 2007-06-05 14:39:36 Re: Encrypted column

Browse pgsql-sql by date

  From Date Subject
Next Message Loredana Curugiu 2007-06-05 14:46:43 Re: [SQL] JOIN
Previous Message Marko Kreen 2007-06-05 14:39:36 Re: Encrypted column