7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.31 1998/09/01 04:39:56 momjian Exp $
10
+ * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.32 1998/09/03 02:10:42 momjian Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -358,15 +358,15 @@ Pg_connect(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
358
358
conn = PQsetdb (pghost , pgport , pgoptions , pgtty , dbName );
359
359
}
360
360
361
- if (conn -> status == CONNECTION_OK )
361
+ if (PQstatus ( conn ) == CONNECTION_OK ) {
362
362
{
363
363
PgSetConnectionId (interp , conn );
364
364
return TCL_OK ;
365
365
}
366
366
else
367
367
{
368
- Tcl_AppendResult (interp , "Connection to database failed\n" , 0 );
369
- Tcl_AppendResult ( interp , conn -> errorMessage , 0 );
368
+ Tcl_AppendResult (interp , "Connection to database failed\n" ,
369
+ PQerrorMessage ( conn ) , 0 );
370
370
PQfinish (conn );
371
371
return TCL_ERROR ;
372
372
}
@@ -423,7 +423,6 @@ Pg_exec(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
423
423
Pg_ConnectionId * connid ;
424
424
PGconn * conn ;
425
425
PGresult * result ;
426
- int connStatus ;
427
426
428
427
if (argc != 3 )
429
428
{
@@ -442,7 +441,6 @@ Pg_exec(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
442
441
return TCL_ERROR ;
443
442
}
444
443
445
- connStatus = conn -> status ;
446
444
result = PQexec (conn , argv [2 ]);
447
445
448
446
/* Transfer any notify events from libpq to Tcl event queue. */
@@ -452,8 +450,8 @@ Pg_exec(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
452
450
{
453
451
int rId = PgSetResultId (interp , argv [1 ], result );
454
452
455
- if ( result -> resultStatus == PGRES_COPY_IN ||
456
- result -> resultStatus == PGRES_COPY_OUT )
453
+ ExecStatusType rStat = PQresultStatus ( result );
454
+ if ( rStat == PGRES_COPY_IN || rStat == PGRES_COPY_OUT )
457
455
{
458
456
connid -> res_copyStatus = RES_COPY_INPROGRESS ;
459
457
connid -> res_copy = rId ;
@@ -463,7 +461,7 @@ Pg_exec(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
463
461
else
464
462
{
465
463
/* error occurred during the query */
466
- Tcl_SetResult (interp , conn -> errorMessage , TCL_VOLATILE );
464
+ Tcl_SetResult (interp , PQerrorMessage ( conn ) , TCL_VOLATILE );
467
465
return TCL_ERROR ;
468
466
}
469
467
}
@@ -481,9 +479,12 @@ Pg_exec(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
481
479
-conn
482
480
the connection that produced the result
483
481
-assign arrayName
484
- assign the results to an array
485
- -assignbyidx arrayName
486
- assign the results to an array using the first field as a key
482
+ assign the results to an array, using subscripts of the form
483
+ (tupno,attributeName)
484
+ -assignbyidx arrayName ?appendstr?
485
+ assign the results to an array using the first field's value as a key.
486
+ All but the first field of each tuple are stored, using subscripts of the form
487
+ (field0value,attributeNameappendstr)
487
488
-numTuples
488
489
the number of tuples in the query
489
490
-attributes
@@ -509,6 +510,7 @@ Pg_result(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
509
510
int tupno ;
510
511
char * arrVar ;
511
512
char nameBuffer [256 ];
513
+ const char * appendstr ;
512
514
513
515
if (argc < 3 || argc > 5 )
514
516
{
@@ -564,8 +566,9 @@ Pg_result(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
564
566
565
567
/*
566
568
* this assignment assigns the table of result tuples into a giant
567
- * array with the name given in the argument, the indices of the
568
- * array or (tupno,attrName). Note we expect field names not to
569
+ * array with the name given in the argument.
570
+ * The indices of the array are of the form (tupno,attrName).
571
+ * Note we expect field names not to
569
572
* exceed a few dozen characters, so truncating to prevent buffer
570
573
* overflow shouldn't be a problem.
571
574
*/
@@ -589,28 +592,32 @@ Pg_result(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
589
592
}
590
593
else if (strcmp (opt , "-assignbyidx" ) == 0 )
591
594
{
592
- if (argc != 4 )
595
+ if (argc != 4 && argc != 5 )
593
596
{
594
- Tcl_AppendResult (interp , "-assignbyidx option must be followed by a variable name" , 0 );
597
+ Tcl_AppendResult (interp , "-assignbyidx option requires an array name and optionally an append string" , 0 );
595
598
return TCL_ERROR ;
596
599
}
597
600
arrVar = argv [3 ];
601
+ appendstr = (argc == 5 ) ? (const char * ) argv [4 ] : "" ;
598
602
599
603
/*
600
604
* this assignment assigns the table of result tuples into a giant
601
- * array with the name given in the argument, the indices of the
602
- * array or (tupno,attrName). Here, we still assume PQfname won't
603
- * exceed 200 characters, but we dare not make the same assumption
604
- * about the data in field 0.
605
+ * array with the name given in the argument. The indices of the array
606
+ * are of the form (field0Value,attrNameappendstr).
607
+ * Here, we still assume PQfname won't exceed 200 characters,
608
+ * but we dare not make the same assumption about the data in field 0
609
+ * nor the append string.
605
610
*/
606
611
for (tupno = 0 ; tupno < PQntuples (result ); tupno ++ )
607
612
{
608
613
const char * field0 = PQgetvalue (result , tupno , 0 );
609
- char * workspace = malloc (strlen (field0 ) + 210 );
614
+ char * workspace = malloc (strlen (field0 ) + strlen ( appendstr ) + 210 );
610
615
611
616
for (i = 1 ; i < PQnfields (result ); i ++ )
612
617
{
613
- sprintf (workspace , "%s,%.200s" , field0 , PQfname (result , i ));
618
+ sprintf (workspace , "%s,%.200s%s" , field0 , PQfname (result ,i ),
619
+ appendstr );
620
+ sprintf (workspace , "%s,%.200s" , field0 , PQfname (result ,i ));
614
621
if (Tcl_SetVar2 (interp , arrVar , workspace ,
615
622
PQgetvalue (result , tupno , i ),
616
623
TCL_LEAVE_ERR_MSG ) == NULL )
@@ -701,7 +708,7 @@ Pg_result(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
701
708
"\t-status\n" ,
702
709
"\t-conn\n" ,
703
710
"\t-assign arrayVarName\n" ,
704
- "\t-assignbyidx arrayVarName\n" ,
711
+ "\t-assignbyidx arrayVarName ?appendstr? \n" ,
705
712
"\t-numTuples\n" ,
706
713
"\t-numAttrs\n"
707
714
"\t-attributes\n"
@@ -1238,7 +1245,7 @@ Pg_select(ClientData cData, Tcl_Interp * interp, int argc, char **argv)
1238
1245
if ((result = PQexec (conn , argv [2 ])) == 0 )
1239
1246
{
1240
1247
/* error occurred during the query */
1241
- Tcl_SetResult (interp , conn -> errorMessage , TCL_STATIC );
1248
+ Tcl_SetResult (interp , PQerrorMessage ( conn ) , TCL_STATIC );
1242
1249
return TCL_ERROR ;
1243
1250
}
1244
1251
@@ -1406,11 +1413,10 @@ Pg_listen(ClientData cData, Tcl_Interp * interp, int argc, char *argv[])
1406
1413
ckfree (cmd );
1407
1414
/* Transfer any notify events from libpq to Tcl event queue. */
1408
1415
PgNotifyTransferEvents (connid );
1409
- if (! result || (result -> resultStatus != PGRES_COMMAND_OK ))
1416
+ if (PQresultStatus (result ) != PGRES_COMMAND_OK ) {
1410
1417
{
1411
1418
/* Error occurred during the execution of command */
1412
- if (result )
1413
- PQclear (result );
1419
+ PQclear (result );
1414
1420
ckfree (callback );
1415
1421
ckfree (caserelname );
1416
1422
Tcl_DeleteHashEntry (entry );
0 commit comments