pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Function Anomaly?
2+ messages / 2 participants
[nested] [flat]

* Function Anomaly?
@ 2009-10-08 02:25  Gary Chambers <gwchamb@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Gary Chambers @ 2009-10-08 02:25 UTC (permalink / raw)
  To: pgsql-sql

All...

Given the following type and function:

CREATE TYPE getnote_t AS (nid BIGINT, ownerid INTEGER, ownername
VARCHAR, hostname VARCHAR,
entrytime TIMESTAMP, is_active VARCHAR, is_private VARCHAR, notetext TEXT);

CREATE OR REPLACE FUNCTION getnote(INTEGER, BIGINT) RETURNS getnote_t AS
$getnote$
SELECT n.nid AS nid, n.ownerid AS ownerid, o.ownername AS ownername,
n.hostname AS hostname,
    n.entrytime::TIMESTAMP(0) AS entrytime,
    '[' || (CASE WHEN n.is_private = TRUE THEN 'PRIVATE' ELSE 'SHARED'
END) || ']' AS is_private,
    '[' || (CASE WHEN n.is_active = FALSE THEN 'INACTIVE' ELSE
'ACTIVE' END) || ']' AS is_active,
    n.notetext AS notetext
FROM notes n, owners o
WHERE (CASE WHEN (n.ownerid != $1 AND n.is_private IS TRUE) THEN FALSE
ELSE TRUE END)
    AND n.ownerid = o.ownerid AND n.nid = $2
$getnote$ LANGUAGE SQL;

When I call it with a row where n.is_private is TRUE and n.ownerid IS
TRUE, I receive a single row of all null values:

notesdb=# select * from getnote(1, 2);
 nid | ownerid | ownername | hostname | entrytime | is_active |
is_private | notetext
-----+---------+-----------+----------+-----------+-----------+------------+----------
     |         |           |          |           |           |            |
(1 row)

When I submit the query directly (getnote.sql is simply the query with
the CASE statement forced to false):

notesdb=# \i getnote.sql
 nid | ownerid | ownername | hostname | entrytime | is_private |
is_active | notetext
-----+---------+-----------+----------+-----------+------------+-----------+----------
(0 rows)

Thanks very much in advance for any insight you can provide.

-- Gary Chambers

/* Nothing fancy and nothing Microsoft! */



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Function Anomaly?
@ 2009-10-08 08:24  Richard Huxton <dev@archonet.com>
  parent: Gary Chambers <gwchamb@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Richard Huxton @ 2009-10-08 08:24 UTC (permalink / raw)
  To: Gary Chambers <gwchamb@gmail.com>; +Cc: pgsql-sql

Gary Chambers wrote:
> CREATE OR REPLACE FUNCTION getnote(INTEGER, BIGINT) RETURNS getnote_t AS

> When I call it with a row where n.is_private is TRUE and n.ownerid IS
> TRUE, I receive a single row of all null values:
> 
> notesdb=# select * from getnote(1, 2);

> When I submit the query directly (getnote.sql is simply the query with
> the CASE statement forced to false):
> 
> notesdb=# \i getnote.sql
>  nid | ownerid | ownername | hostname | entrytime | is_private |
> is_active | notetext
> -----+---------+-----------+----------+-----------+------------+-----------+----------
> (0 rows)

You've defined getnote() as returning a single getnote_t value I think
you probably want "RETURNS SETOF getnote_t".

-- 
  Richard Huxton
  Archonet Ltd



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2009-10-08 08:24 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-10-08 02:25 Function Anomaly? Gary Chambers <gwchamb@gmail.com>
2009-10-08 08:24 ` Richard Huxton <dev@archonet.com>

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