Skip to content

Commit 17b5bd3

Browse files
committed
From: Anton de Wet <adw@obsidian.co.za>
Subject: [HACKERS] Small patch to pgtclCmds.c Hi I have made the following small change to the extensions I made to pgtclCmds.c quite a while ago. At the moment there is a -assignbyidx option to pg_result assigning the returned tuples to an array by using the 1st field of the select statement as the key to the array. eg "select name,age from vitalstatistics" will result in an array with myarray(peter) = 32 myarray(paul) = 45 Often I need to have a pseudo-multi dimentional array eg. "select name,age from vitalstatistics where occupation='plummer' I would like to be able to generate an array newarray(peter,overpaid) = 32 So to add a arbitrary string to the key value I have extended pg_result $res -assignbyidx $arrayname to have an optional argument pg_result $res -assignbyidx $arrayname $appendstr So that that string is appended to the key value.
1 parent 5b1311a commit 17b5bd3

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/interfaces/libpgtcl/pgtclCmds.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.12 1997/01/23 19:47:18 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.13 1997/04/02 18:16:49 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -437,8 +437,10 @@ Pg_exec(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
437437
the connection that produced the result
438438
-assign arrayName
439439
assign the results to an array
440-
-assignbyidx arrayName
440+
-assignbyidx arrayName ?appendstr?
441441
assign the results to an array using the first field as a key
442+
optional appendstr append that string to the key name. Usefull for
443+
creating pseudo-multi dimentional arrays in tcl.
442444
-numTuples
443445
the number of tuples in the query
444446
-attributes
@@ -462,9 +464,10 @@ Pg_result(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
462464
int tupno;
463465
char prearrayInd[MAX_MESSAGE_LEN];
464466
char arrayInd[MAX_MESSAGE_LEN];
467+
char *appendstr;
465468
char *arrVar;
466469

467-
if (argc != 3 && argc != 4) {
470+
if (argc != 3 && argc != 4 && argc != 5) {
468471
Tcl_AppendResult(interp, "Wrong # of arguments\n",0);
469472
goto Pg_result_errReturn;
470473
}
@@ -523,18 +526,24 @@ Pg_result(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
523526
return TCL_OK;
524527
}
525528
else if (strcmp(opt, "-assignbyidx") == 0) {
526-
if (argc != 4) {
527-
Tcl_AppendResult(interp, "-assignbyidx option must be followed by a variable name",0);
529+
if (argc !=4 && argc != 5) {
530+
Tcl_AppendResult(interp, "-assignbyidx requires the array name and takes one optional argument as an append string",0);
528531
return TCL_ERROR;
529532
}
530533
arrVar = argv[3];
531534
/* this assignment assigns the table of result tuples into a giant
532535
array with the name given in the argument,
533536
the indices of the array or (tupno,attrName)*/
537+
if (argc == 5) {
538+
appendstr = argv[4];
539+
} else {
540+
appendstr = "";
541+
}
534542
for (tupno = 0; tupno<PQntuples(result); tupno++) {
535543
sprintf(prearrayInd,"%s",PQgetvalue(result,tupno,0));
536544
for (i=1;i<PQnfields(result);i++) {
537-
sprintf(arrayInd, "%s,%s", prearrayInd, PQfname(result,i));
545+
sprintf(arrayInd, "%s,%s%s", prearrayInd, PQfname(result,i),
546+
appendstr);
538547
Tcl_SetVar2(interp, arrVar, arrayInd,
539548
PQgetvalue(result,tupno,i),
540549
TCL_LEAVE_ERR_MSG);
@@ -604,7 +613,7 @@ Pg_result(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
604613
"\t-status\n",
605614
"\t-conn\n",
606615
"\t-assign arrayVarName\n",
607-
"\t-assignbyidx arrayVarName\n",
616+
"\t-assignbyidx arrayVarName ?appendstr?\n",
608617
"\t-numTuples\n",
609618
"\t-attributes\n"
610619
"\t-lAttributes\n"

0 commit comments

Comments
 (0)