mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-18 18:44:06 +08:00
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.
This commit is contained in:
parent
5b1311acfb
commit
17b5bd33e4
@ -7,7 +7,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.12 1997/01/23 19:47:18 scrappy Exp $
|
* $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.13 1997/04/02 18:16:49 scrappy Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -437,8 +437,10 @@ Pg_exec(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
|
|||||||
the connection that produced the result
|
the connection that produced the result
|
||||||
-assign arrayName
|
-assign arrayName
|
||||||
assign the results to an array
|
assign the results to an array
|
||||||
-assignbyidx arrayName
|
-assignbyidx arrayName ?appendstr?
|
||||||
assign the results to an array using the first field as a key
|
assign the results to an array using the first field as a key
|
||||||
|
optional appendstr append that string to the key name. Usefull for
|
||||||
|
creating pseudo-multi dimentional arrays in tcl.
|
||||||
-numTuples
|
-numTuples
|
||||||
the number of tuples in the query
|
the number of tuples in the query
|
||||||
-attributes
|
-attributes
|
||||||
@ -462,9 +464,10 @@ Pg_result(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
|
|||||||
int tupno;
|
int tupno;
|
||||||
char prearrayInd[MAX_MESSAGE_LEN];
|
char prearrayInd[MAX_MESSAGE_LEN];
|
||||||
char arrayInd[MAX_MESSAGE_LEN];
|
char arrayInd[MAX_MESSAGE_LEN];
|
||||||
|
char *appendstr;
|
||||||
char *arrVar;
|
char *arrVar;
|
||||||
|
|
||||||
if (argc != 3 && argc != 4) {
|
if (argc != 3 && argc != 4 && argc != 5) {
|
||||||
Tcl_AppendResult(interp, "Wrong # of arguments\n",0);
|
Tcl_AppendResult(interp, "Wrong # of arguments\n",0);
|
||||||
goto Pg_result_errReturn;
|
goto Pg_result_errReturn;
|
||||||
}
|
}
|
||||||
@ -523,18 +526,24 @@ Pg_result(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
|
|||||||
return TCL_OK;
|
return TCL_OK;
|
||||||
}
|
}
|
||||||
else if (strcmp(opt, "-assignbyidx") == 0) {
|
else if (strcmp(opt, "-assignbyidx") == 0) {
|
||||||
if (argc != 4) {
|
if (argc !=4 && argc != 5) {
|
||||||
Tcl_AppendResult(interp, "-assignbyidx option must be followed by a variable name",0);
|
Tcl_AppendResult(interp, "-assignbyidx requires the array name and takes one optional argument as an append string",0);
|
||||||
return TCL_ERROR;
|
return TCL_ERROR;
|
||||||
}
|
}
|
||||||
arrVar = argv[3];
|
arrVar = argv[3];
|
||||||
/* this assignment assigns the table of result tuples into a giant
|
/* this assignment assigns the table of result tuples into a giant
|
||||||
array with the name given in the argument,
|
array with the name given in the argument,
|
||||||
the indices of the array or (tupno,attrName)*/
|
the indices of the array or (tupno,attrName)*/
|
||||||
|
if (argc == 5) {
|
||||||
|
appendstr = argv[4];
|
||||||
|
} else {
|
||||||
|
appendstr = "";
|
||||||
|
}
|
||||||
for (tupno = 0; tupno<PQntuples(result); tupno++) {
|
for (tupno = 0; tupno<PQntuples(result); tupno++) {
|
||||||
sprintf(prearrayInd,"%s",PQgetvalue(result,tupno,0));
|
sprintf(prearrayInd,"%s",PQgetvalue(result,tupno,0));
|
||||||
for (i=1;i<PQnfields(result);i++) {
|
for (i=1;i<PQnfields(result);i++) {
|
||||||
sprintf(arrayInd, "%s,%s", prearrayInd, PQfname(result,i));
|
sprintf(arrayInd, "%s,%s%s", prearrayInd, PQfname(result,i),
|
||||||
|
appendstr);
|
||||||
Tcl_SetVar2(interp, arrVar, arrayInd,
|
Tcl_SetVar2(interp, arrVar, arrayInd,
|
||||||
PQgetvalue(result,tupno,i),
|
PQgetvalue(result,tupno,i),
|
||||||
TCL_LEAVE_ERR_MSG);
|
TCL_LEAVE_ERR_MSG);
|
||||||
@ -604,7 +613,7 @@ Pg_result(ClientData cData, Tcl_Interp *interp, int argc, char* argv[])
|
|||||||
"\t-status\n",
|
"\t-status\n",
|
||||||
"\t-conn\n",
|
"\t-conn\n",
|
||||||
"\t-assign arrayVarName\n",
|
"\t-assign arrayVarName\n",
|
||||||
"\t-assignbyidx arrayVarName\n",
|
"\t-assignbyidx arrayVarName ?appendstr?\n",
|
||||||
"\t-numTuples\n",
|
"\t-numTuples\n",
|
||||||
"\t-attributes\n"
|
"\t-attributes\n"
|
||||||
"\t-lAttributes\n"
|
"\t-lAttributes\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user