Re: [HACKERS] psql commandline conninfo

Поиск
Список
Период
Сортировка
Искать

Re: [HACKERS] psql commandline conninfo

От:
Andrew Dunstan <andrew@dunslane.net>
Дата:
Andrew Dunstan wrote:
>
>
> Tom Lane wrote:
>> Bruce Momjian  writes:
>>  
>>> OK, good to know.  Does the patch need documentation too?
>>>     
>>
>> Certainly.
>>   
>
>
> That's why I haven't committed it yet. I intend to put info in the 
> psql manual as well as in the libpq reference.
>>  
>>> Are we
>>> deprecating the psql options now duplicated by the new functionality,
>>> like host/port/username/password?
>>>     
>>
>> I'd vote not.  This is just another way to do it.
>>
>>            
>>   
>
> I entirely agree. It lets you do some nice things that aren't obvious 
> now, like:
>
>  psql 'service=foo sslmode=require'
>
>

Patch (Tom's code, my docs) attached.

cheers

andrew

Index: doc/src/sgml/libpq.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v
retrieving revision 1.220
diff -c -r1.220 libpq.sgml
*** doc/src/sgml/libpq.sgml	10 Nov 2006 22:15:26 -0000	1.220
--- doc/src/sgml/libpq.sgml	18 Dec 2006 16:52:39 -0000
***************
*** 324,336 ****
                       const char *login,
                       const char *pwd);
  
! 
  
! 
!    This is the predecessor of PQconnectdb with a fixed
!    set of parameters.  It has the same functionality except that the
!    missing parameters will always take on default values.  Write NULL or an
!    empty string for any one of the fixed parameters that is to be defaulted.
     
    
   
--- 324,342 ----
                       const char *login,
                       const char *pwd);
  
!     
  
!     
!      This is the predecessor of PQconnectdb with a fixed
!      set of parameters.  It has the same functionality except that the
!      missing parameters will always take on default values.  Write NULL or an
!      empty string for any one of the fixed parameters that is to be defaulted.
!    
!    
!      If the dbName contains an = sign, it
! 	 is taken as a conninfo string in exactly the same way as
! 	 if it had been passed to PQconnectdb, and the remaining
! 	 parameters are then applied as above.
     
    
   
Index: doc/src/sgml/ref/psql-ref.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v
retrieving revision 1.175
diff -c -r1.175 psql-ref.sgml
*** doc/src/sgml/ref/psql-ref.sgml	21 Nov 2006 17:01:58 -0000	1.175
--- doc/src/sgml/ref/psql-ref.sgml	18 Dec 2006 16:52:40 -0000
***************
*** 112,117 ****
--- 112,121 ----
        class="parameter">dbname as the first non-option
        argument on the command line.
        
+ 	  
+ 	  If this parameter contains an = sign, it it treated as a
+ 	  conninfo string. See  for more information.
+ 	  
        
      
  
***************
*** 554,559 ****
--- 558,575 ----
      passwords. See  for more information.
      
  
+ 	
+     An alternative way to specify connection parameters is in a
+ 	conninfo	string, which is used instead of a 
+ 	database name. This mechanism give you very wide control over the 
+ 	connection. For example,
+ 
+ $ psql "service=myservice sslmode=require"
+ 
+     See  for more information on all the
+ 	available connection options.
+ 	
+ 
      
      If the connection could not be made for any reason (e.g., insufficient
      privileges, server is not running on the targeted host, etc.),
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	18 Dec 2006 16:52:43 -0000
***************
*** 574,589 ****
  	conn = makeEmptyPGconn();
  	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;
! 
! 	/*
! 	 * Absorb specified options into conn structure, overriding defaults
  	 */
  	if (pghost && pghost[0] != '\0')
  	{
--- 574,609 ----
  	conn = makeEmptyPGconn();
  	if (conn == NULL)
  		return NULL;
!     /*
!      * If the dbName parameter contains '=', assume it's a conninfo
!      * string.
!      */
!     if (dbName && strchr(dbName,'='))
!     {
!         if (!connectOptions1(conn, dbName))
!             return conn;
!     }
!     else
!     {
!         /*
!          * Old-style path: first, parse an empty conninfo string in
!          * order to set up the same defaults that PQconnectdb() would use.
!          */
!         if (!connectOptions1(conn, ""))
!             return conn;
! 
!         /* Insert dbName parameter value into struct */
!         if (dbName && dbName[0] != '\0')
!         {
!             if (conn->dbName)
!                 free(conn->dbName);
!             conn->dbName = strdup(dbName);
!         }
!     }
! 
!     /*
!      * Insert remaining parameters into struct, overriding defaults
!      * (as well as any conflicting data from dbName taken as a conninfo).
  	 */
  	if (pghost && pghost[0] != '\0')
  	{
***************
*** 613,625 ****
  		conn->pgtty = strdup(pgtty);
  	}
  
- 	if (dbName && dbName[0] != '\0')
- 	{
- 		if (conn->dbName)
- 			free(conn->dbName);
- 		conn->dbName = strdup(dbName);
- 	}
- 
  	if (login && login[0] != '\0')
  	{
  		if (conn->pguser)
--- 633,638 ----
FAQ