Re: psql commandline conninfo - Mailing list pgsql-hackers

From Andrew Dunstan
Subject Re: psql commandline conninfo
Date
Msg-id 457F3693.9050405@dunslane.net
Whole thread Raw
In response to Re: psql commandline conninfo  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: psql commandline conninfo  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Tom Lane wrote:
> Martijn van Oosterhout <kleptog@svana.org> writes:
>
>> Does that mean that:
>> psql -d "service=myservice"
>> should Just Work(tm)? That would be nice.
>>
>
> Even more to the point,
>
>     psql "service=myservice"
>
> which is why we want to overload dbname rather than any of the other
> PQsetdbLogin parameters for this --- dbname has pride of place in the
> command line syntax for several of the client programs.
>
>             regards, tom lane
>
>

Right. Here's the patch I just knocked up, which seems to Just Work (tm) ;-)

cheers

andrew


Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.339
diff -c -r1.339 fe-connect.c
*** src/interfaces/libpq/fe-connect.c    21 Nov 2006 16:28:00 -0000    1.339
--- src/interfaces/libpq/fe-connect.c    12 Dec 2006 22:49:28 -0000
***************
*** 567,572 ****
--- 567,573 ----
               const char *pwd)
  {
      PGconn       *conn;
+     bool       have_conninfo = false;

      /*
       * Allocate memory for the conn structure
***************
*** 575,585 ****
      if (conn == NULL)
          return NULL;

      /*
       * Parse an empty conninfo string in order to set up the same defaults
!      * that PQconnectdb() would use.
       */
!     if (!connectOptions1(conn, ""))
          return conn;

      /*
--- 576,609 ----
      if (conn == NULL)
          return NULL;

+     /*
+      * Have we got something that might be a conninfo string?
+      * If so, try that first.
+      */
+     if (dbName && strchr(dbName,'='))
+     {
+         if(connectOptions1(conn,dbName))
+         {
+             /* it was a conninfo string */
+             have_conninfo = true;
+         }
+         else
+         {
+             /* put things back the way they were so we can try again */
+             freePGconn(conn);
+             conn = makeEmptyPGconn();
+             if (conn == NULL)
+                 return NULL;
+
+         }
+     }
+
      /*
       * Parse an empty conninfo string in order to set up the same defaults
!      * that PQconnectdb() would use. Skip this if we already found a
!      * conninfo string.
       */
!     if (!have_conninfo && !connectOptions1(conn, ""))
          return conn;

      /*
***************
*** 613,619 ****
          conn->pgtty = strdup(pgtty);
      }

!     if (dbName && dbName[0] != '\0')
      {
          if (conn->dbName)
              free(conn->dbName);
--- 637,643 ----
          conn->pgtty = strdup(pgtty);
      }

!     if (!have_conninfo && dbName && dbName[0] != '\0')
      {
          if (conn->dbName)
              free(conn->dbName);

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: psql commandline conninfo
Next
From: Ted Petrosky
Date:
Subject: libpq.a in a universal binary