7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -209,6 +209,44 @@ tcl_value (char *value)
209
209
210
210
#endif
211
211
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
+
212
250
/**********************************
213
251
* pg_connect
214
252
make a connection to a backend.
@@ -235,55 +273,73 @@ Pg_connect(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
235
273
236
274
if (argc == 1 ) {
237
275
Tcl_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 );
239
278
return TCL_ERROR ;
240
279
241
280
}
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 ];
253
302
i += 2 ;
254
303
}
255
304
else
256
- if (strcmp (argv [i ], "-tty " ) == 0 ) {
257
- pgtty = argv [i + 1 ];
305
+ if (strcmp (argv [i ], "-port " ) == 0 ) {
306
+ pgport = argv [i + 1 ];
258
307
i += 2 ;
259
308
}
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
+ }
275
330
}
331
+ dbName = argv [1 ];
332
+ conn = PQsetdb (pghost , pgport , pgoptions , pgtty , dbName );
276
333
}
277
- dbName = argv [1 ];
278
334
279
- conn = PQsetdb (pghost , pgport , pgoptions , pgtty , dbName );
280
335
if (conn -> status == CONNECTION_OK ) {
281
336
PgSetConnectionId (cd , interp -> result , conn );
282
337
return TCL_OK ;
283
338
}
284
339
else {
285
- Tcl_AppendResult (interp , "Connection to " , dbName , " failed\n" , 0 );
340
+ Tcl_AppendResult (interp , "Connection to database failed\n" , 0 );
286
341
Tcl_AppendResult (interp , conn -> errorMessage , 0 );
342
+ PQfinish (conn );
287
343
return TCL_ERROR ;
288
344
}
289
345
}
0 commit comments