pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL?
4+ messages / 3 participants
[nested] [flat]

* Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL?
@ 2021-03-08 18:20 Alexander Farber <alexander.farber@gmail.com>
  2021-03-08 19:31 ` Re: Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL? Pavel Stehule <pavel.stehule@gmail.com>
  2021-03-08 19:53 ` Re: Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 4+ messages in thread

From: Alexander Farber @ 2021-03-08 18:20 UTC (permalink / raw)
  To: pgsql-general

Good evening,

in PostgreSQL 13.2 I have a custom stored function:

CREATE OR REPLACE FUNCTION words_join_new_game(
                in_uid       integer,
                in_bid       integer
        ) RETURNS table (
                -- the player to be notified (sometimes there is no such
user)
                out_uid    integer,
                -- the id of the created game
                out_gid    integer,
                out_fcm    text,
                out_apns   text,
                out_adm    text,
                out_hms    text,
                -- the most recently used social network and the user id
there
                out_social integer,
                out_sid    text,
                -- the push notification text: the opponent has joined
                out_body   text
        ) AS
$func$
....
$func$ LANGUAGE plpgsql;

However there are cases, when I only have the out_gid value, I do not want
to return any other values.

My question is: do I have to set the other OUT params explicitly to NULL?

For example here:

        -- case 1
        SELECT gid
        INTO   out_gid
        FROM   words_games
        WHERE  finished IS NULL
        AND    bid = in_bid
        AND    (
                (player1 = in_uid AND played1 IS NULL) OR
                (player2 = in_uid AND played2 IS NULL)
        ) LIMIT 1;

        IF out_gid IS NOT NULL THEN
            -- should I set all the other OUT params to NULL here?
<-----------
            RETURN;
        END IF;

I was expecting to check for out_uid, if it is a positive number in my Java
code with:

    String SQL_JOIN_GAME               =
            "SELECT " +
                "out_uid    AS uid,    " +
                // the id of the new game is never NULL, but the other
columns can be NULL
                "out_gid    AS gid,    " +
                "out_fcm    AS fcm,    " +
                "out_apns   AS apns,   " +
                "out_adm    AS adm,    " +
                "out_hms    AS hms,    " +
                "out_social AS social, " +
                "out_sid    AS sid,    " +
                "out_body   AS body    " +
            "FROM words_join_new_game(?::int, ?::int)";

        int gid = 0;
        try (Connection db = DriverManager.getConnection(mDatabaseUrl);
                PreparedStatement st = db.prepareStatement(SQL_JOIN_GAME)) {
            st.setInt(1, mUid);
            st.setInt(2, bid);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                // get the id of the new game
                gid = rs.getInt(KEY_GID);
                // get the id of the opponent
                int uid = rs.getInt(KEY_UID);
                // send notification to the other player
                if (uid > 0) {
                    Notification n = new Notification(
                        uid,
                        gid,
                        rs.getString(COLUMN_FCM),
                        rs.getString(COLUMN_APNS),
                        rs.getString(COLUMN_ADM),
                        rs.getString(COLUMN_HMS),
                        rs.getInt(COLUMN_SOCIAL),
                        rs.getString(COLUMN_SID),
                        rs.getString(COLUMN_BODY)
                    );
                    mServlet.sendNotification(n);
                }
            }
        }

but I am getting the error:

org.postgresql.util.PSQLException: ERROR: column "out_uid" does not exist|
Position: 8
        at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
        at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
        at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:323)
        at
org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401)
        at
org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:164)
        at
org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114)
        at de.afarber.WordsListener.handleNewGame(WordsListener.java:216)
        at de.afarber.WordsListener.onWebSocketText(WordsListener.java:101)

Thank you
Alex

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

* Re: Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL?
  2021-03-08 18:20 Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL? Alexander Farber <alexander.farber@gmail.com>
@ 2021-03-08 19:31 ` Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Pavel Stehule @ 2021-03-08 19:31 UTC (permalink / raw)
  To: Alexander Farber <alexander.farber@gmail.com>; +Cc: pgsql-general

Hi

po 8. 3. 2021 v 19:20 odesílatel Alexander Farber <
alexander.farber@gmail.com> napsal:

> Good evening,
>
> in PostgreSQL 13.2 I have a custom stored function:
>
> CREATE OR REPLACE FUNCTION words_join_new_game(
>                 in_uid       integer,
>                 in_bid       integer
>         ) RETURNS table (
>                 -- the player to be notified (sometimes there is no such
> user)
>                 out_uid    integer,
>                 -- the id of the created game
>                 out_gid    integer,
>                 out_fcm    text,
>                 out_apns   text,
>                 out_adm    text,
>                 out_hms    text,
>                 -- the most recently used social network and the user id
> there
>                 out_social integer,
>                 out_sid    text,
>                 -- the push notification text: the opponent has joined
>                 out_body   text
>         ) AS
> $func$
> ....
> $func$ LANGUAGE plpgsql;
>
> However there are cases, when I only have the out_gid value, I do not want
> to return any other values.
>

This is same like

CREATE OR REPLACE FUNCTION words_join_new_game(IN in_uid int, IN in_bid
integer, OUT out_uid int, OUT ....)


> My question is: do I have to set the other OUT params explicitly to NULL?
>
> For example here:
>
>         -- case 1
>         SELECT gid
>         INTO   out_gid
>         FROM   words_games
>         WHERE  finished IS NULL
>         AND    bid = in_bid
>         AND    (
>                 (player1 = in_uid AND played1 IS NULL) OR
>                 (player2 = in_uid AND played2 IS NULL)
>         ) LIMIT 1;
>
>         IF out_gid IS NOT NULL THEN
>             -- should I set all the other OUT params to NULL here?
> <-----------
>             RETURN;
>         END IF;
>
> I was expecting to check for out_uid, if it is a positive number in my
> Java code with:
>
>     String SQL_JOIN_GAME               =
>             "SELECT " +
>                 "out_uid    AS uid,    " +
>                 // the id of the new game is never NULL, but the other
> columns can be NULL
>                 "out_gid    AS gid,    " +
>                 "out_fcm    AS fcm,    " +
>                 "out_apns   AS apns,   " +
>                 "out_adm    AS adm,    " +
>                 "out_hms    AS hms,    " +
>                 "out_social AS social, " +
>                 "out_sid    AS sid,    " +
>                 "out_body   AS body    " +
>             "FROM words_join_new_game(?::int, ?::int)";
>
>         int gid = 0;
>         try (Connection db = DriverManager.getConnection(mDatabaseUrl);
>                 PreparedStatement st = db.prepareStatement(SQL_JOIN_GAME))
> {
>             st.setInt(1, mUid);
>             st.setInt(2, bid);
>             ResultSet rs = st.executeQuery();
>             if (rs.next()) {
>                 // get the id of the new game
>                 gid = rs.getInt(KEY_GID);
>                 // get the id of the opponent
>                 int uid = rs.getInt(KEY_UID);
>                 // send notification to the other player
>                 if (uid > 0) {
>                     Notification n = new Notification(
>                         uid,
>                         gid,
>                         rs.getString(COLUMN_FCM),
>                         rs.getString(COLUMN_APNS),
>                         rs.getString(COLUMN_ADM),
>                         rs.getString(COLUMN_HMS),
>                         rs.getInt(COLUMN_SOCIAL),
>                         rs.getString(COLUMN_SID),
>                         rs.getString(COLUMN_BODY)
>                     );
>                     mServlet.sendNotification(n);
>                 }
>             }
>         }
>
> but I am getting the error:
>
> org.postgresql.util.PSQLException: ERROR: column "out_uid" does not
> exist|  Position: 8
>         at
> org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
>         at
> org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
>         at
> org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:323)
>         at
> org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
>         at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401)
>         at
> org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:164)
>         at
> org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114)
>         at de.afarber.WordsListener.handleNewGame(WordsListener.java:216)
>         at de.afarber.WordsListener.onWebSocketText(WordsListener.java:101)
>

are you sure so you have not more functions with the same name?

Regards

Pavel


>
> Thank you
> Alex
>
>

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

* Re: Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL?
  2021-03-08 18:20 Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL? Alexander Farber <alexander.farber@gmail.com>
@ 2021-03-08 19:53 ` Tom Lane <tgl@sss.pgh.pa.us>
  2021-03-09 14:49   ` Re: Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL? Alexander Farber <alexander.farber@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Tom Lane @ 2021-03-08 19:53 UTC (permalink / raw)
  To: Alexander Farber <alexander.farber@gmail.com>; +Cc: pgsql-general

Alexander Farber <alexander.farber@gmail.com> writes:
> However there are cases, when I only have the out_gid value, I do not want
> to return any other values.
> My question is: do I have to set the other OUT params explicitly to NULL?

plpgsql initializes them to null by default, I believe, just like ordinary
local variables.

> org.postgresql.util.PSQLException: ERROR: column "out_uid" does not exist|

This isn't related to what your function does internally.

I think the issue is you renamed out_uid to uid in your SELECT:

>    String SQL_JOIN_GAME               =
>            "SELECT " +
>                "out_uid    AS uid,    " +

			regards, tom lane





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

* Re: Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL?
  2021-03-08 18:20 Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL? Alexander Farber <alexander.farber@gmail.com>
  2021-03-08 19:53 ` Re: Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL? Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-03-09 14:49   ` Alexander Farber <alexander.farber@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Alexander Farber @ 2021-03-09 14:49 UTC (permalink / raw)
  To: ; +Cc: pgsql-general

Tom, you are so eagle eyed -

On Mon, Mar 8, 2021 at 8:53 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Alexander Farber <alexander.farber@gmail.com> writes:
> > However there are cases, when I only have the out_gid value, I do not
> want
> > to return any other values.
> > My question is: do I have to set the other OUT params explicitly to NULL?
>
> plpgsql initializes them to null by default, I believe, just like ordinary
> local variables.
>
> > org.postgresql.util.PSQLException: ERROR: column "out_uid" does not
> exist|
>
> This isn't related to what your function does internally.
>
> I think the issue is you renamed out_uid to uid in your SELECT:
>
> >    String SQL_JOIN_GAME               =
> >            "SELECT " +
> >                "out_uid    AS uid,    " +
>
>
thank you and sorry for my silly mistake

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


end of thread, other threads:[~2021-03-09 14:49 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-08 18:20 Stored function RETURNS table, but in some cases columns are missing - should I set them to NULL? Alexander Farber <alexander.farber@gmail.com>
2021-03-08 19:31 ` Pavel Stehule <pavel.stehule@gmail.com>
2021-03-08 19:53 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-03-09 14:49   ` Alexander Farber <alexander.farber@gmail.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