query output in psql PROMPT

From: strk <strk(at)keybit(dot)net>
To: pgsql-patches(at)postgresql(dot)org
Subject: query output in psql PROMPT
Date: 2005-01-14 16:41:50
Message-ID: 20050114164150.GA13702@freek.keybit.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2005-01-14 17:06:32 Re: query output in psql PROMPT
Previous Message Peter Eisentraut 2005-01-14 08:57:57 Re: Updated version of Polish translation.