77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.5 1996/10/30 06:18:39 scrappy Exp $
10+ * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.6 1996/11/09 10:39:41 scrappy Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -209,6 +209,44 @@ tcl_value (char *value)
209209
210210#endif
211211
212+ /**********************************
213+ * pg_conndefaults
214+
215+ syntax:
216+ pg_conndefaults
217+
218+ the return result is a list describing the possible options and their
219+ current default values for a call to pg_connect with the new -conninfo
220+ syntax. Each entry in the list is a sublist of the format:
221+
222+ {optname label dispchar dispsize value}
223+
224+ **********************************/
225+
226+ int
227+ Pg_conndefaults (ClientData cData ,Tcl_Interp * interp ,int argc ,char * argv [])
228+ {
229+ PQconninfoOption * option ;
230+ char buf [8192 ];
231+
232+ Tcl_ResetResult (interp );
233+ for (option = PQconndefaults ();option -> keyword != NULL ;option ++ ) {
234+ if (option -> val == NULL ) {
235+ option -> val = "" ;
236+ }
237+ sprintf (buf ,"{%s} {%s} {%s} %d {%s}" ,
238+ option -> keyword ,
239+ option -> label ,
240+ option -> dispchar ,
241+ option -> dispsize ,
242+ option -> val );
243+ Tcl_AppendElement (interp ,buf );
244+ }
245+
246+ return TCL_OK ;
247+ }
248+
249+
212250/**********************************
213251 * pg_connect
214252 make a connection to a backend.
@@ -235,55 +273,73 @@ Pg_connect(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
235273
236274if (argc == 1 ) {
237275Tcl_AppendResult (interp ,"pg_connect: database name missing\n" ,0 );
238- Tcl_AppendResult (interp ,"pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]" ,0 );
276+ Tcl_AppendResult (interp ,"pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]\n" ,0 );
277+ Tcl_AppendResult (interp ,"pg_connect -conninfo <conninfo-string>" ,0 );
239278return TCL_ERROR ;
240279
241280 }
242- if (argc > 2 ) {
243- /* parse for pg environment settings */
244- i = 2 ;
245- while (i + 1 < argc ) {
246- if (strcmp (argv [i ],"-host" )== 0 ) {
247- pghost = argv [i + 1 ];
248- i += 2 ;
249- }
250- else
251- if (strcmp (argv [i ],"-port" )== 0 ) {
252- pgport = argv [i + 1 ];
281+
282+ if (!strcmp ("-conninfo" ,argv [1 ])) {
283+ /*
284+ * Establish a connection using the new PQconnectdb() interface
285+ */
286+ if (argc != 3 ) {
287+ Tcl_AppendResult (interp ,"pg_connect: syntax error\n" ,0 );
288+ Tcl_AppendResult (interp ,"pg_connect -conninfo <conninfo-string>" ,0 );
289+ return TCL_ERROR ;
290+ }
291+ conn = PQconnectdb (argv [2 ]);
292+ }else {
293+ /*
294+ * Establish a connection using the old PQsetdb() interface
295+ */
296+ if (argc > 2 ) {
297+ /* parse for pg environment settings */
298+ i = 2 ;
299+ while (i + 1 < argc ) {
300+ if (strcmp (argv [i ],"-host" )== 0 ) {
301+ pghost = argv [i + 1 ];
253302i += 2 ;
254303}
255304else
256- if (strcmp (argv [i ],"-tty " )== 0 ) {
257- pgtty = argv [i + 1 ];
305+ if (strcmp (argv [i ],"-port " )== 0 ) {
306+ pgport = argv [i + 1 ];
258307i += 2 ;
259308 }
260- else if (strcmp (argv [i ],"-options" )== 0 ) {
261- pgoptions = argv [i + 1 ];
262- i += 2 ;
263- }
264- else {
265- Tcl_AppendResult (interp ,"Bad option to pg_connect : \n" ,
266- argv [i ],0 );
267- Tcl_AppendResult (interp ,"pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]" ,0 );
268- return TCL_ERROR ;
269- }
270- }/* while */
271- if ((i %2 != 0 )|| i != argc ) {
272- Tcl_AppendResult (interp ,"wrong # of arguments to pg_connect\n" ,argv [i ],0 );
273- Tcl_AppendResult (interp ,"pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]" ,0 );
274- return TCL_ERROR ;
309+ else
310+ if (strcmp (argv [i ],"-tty" )== 0 ) {
311+ pgtty = argv [i + 1 ];
312+ i += 2 ;
313+ }
314+ else if (strcmp (argv [i ],"-options" )== 0 ) {
315+ pgoptions = argv [i + 1 ];
316+ i += 2 ;
317+ }
318+ else {
319+ Tcl_AppendResult (interp ,"Bad option to pg_connect : \n" ,
320+ argv [i ],0 );
321+ Tcl_AppendResult (interp ,"pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]" ,0 );
322+ return TCL_ERROR ;
323+ }
324+ }/* while */
325+ if ((i %2 != 0 )|| i != argc ) {
326+ Tcl_AppendResult (interp ,"wrong # of arguments to pg_connect\n" ,argv [i ],0 );
327+ Tcl_AppendResult (interp ,"pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]" ,0 );
328+ return TCL_ERROR ;
329+ }
275330}
331+ dbName = argv [1 ];
332+ conn = PQsetdb (pghost ,pgport ,pgoptions ,pgtty ,dbName );
276333 }
277- dbName = argv [1 ];
278334
279- conn = PQsetdb (pghost ,pgport ,pgoptions ,pgtty ,dbName );
280335if (conn -> status == CONNECTION_OK ) {
281336PgSetConnectionId (cd ,interp -> result ,conn );
282337return TCL_OK ;
283338 }
284339else {
285- Tcl_AppendResult (interp ,"Connection to" , dbName , " failed\n" ,0 );
340+ Tcl_AppendResult (interp ,"Connection todatabase failed\n" ,0 );
286341Tcl_AppendResult (interp ,conn -> errorMessage ,0 );
342+ PQfinish (conn );
287343return TCL_ERROR ;
288344 }
289345}