query output in psql PROMPT - Mailing list pgsql-patches

From strk
Subject query output in psql PROMPT
Date
Msg-id 20050114164150.GA13702@freek.keybit.net
Whole thread Raw
Responses Re: query output in psql PROMPT
List pgsql-patches
A simple patch allow query output in psql PROMPT strings:

Index: prompt.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/prompt.c,v
retrieving revision 1.38
diff -U2 -r1.38 prompt.c
--- prompt.c    1 Jan 2005 05:43:08 -0000    1.38
+++ prompt.c    14 Jan 2005 14:39:24 -0000
@@ -61,4 +61,7 @@
  * %[ ... %]       - tell readline that the contained text is invisible
  *
+ * %.query.    - the value of the first field in the first row returned
+ *          by the specified query.
+ *
  * If the application-wide prompts become NULL somehow, the returned string
  * will be empty (not NULL!).
@@ -272,4 +275,29 @@
                     }

+                    /* execute db proc */
+                case '.':
+                    {
+                        char *cmd = pg_strdup(p + 1);
+                        PGresult *res;
+                        int cmdend;
+
+                        cmdend = strcspn(cmd, ".");
+                        cmd[cmdend] = '\0';
+                        if (cmd)
+                        {
+                            res = PSQLexec(cmd, false);
+                        }
+                        if (res && PQntuples(res) )
+                        {
+                            memcpy(buf, PQgetvalue(res, 0, 0), MAX_PROMPT_SIZE-1);
+                            PQclear(res);
+                        }
+                        if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
+                            buf[strlen(buf) - 1] = '\0';
+                        free(cmd);
+                        p += cmdend + 1;
+                        break;
+                    }
+
                     /* interpolate variable */
                 case ':':

--

For standing up against patentability of software,

  Thank You, Poland!

Read the intervention:    http://kwiki.ffii.org/ConsPolon041221En
Send your thanks:         thankyoupoland.info
Read/do more:          http://www.noepatents.org/

pgsql-patches by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: Polish translation of file pg_controldata.
Next
From: Tom Lane
Date:
Subject: Re: query output in psql PROMPT