Re: Postgresql 9.5 and Shell scripts/variables vs. C programming/defining a value to be used - Mailing list pgsql-general

From John McKown
Subject Re: Postgresql 9.5 and Shell scripts/variables vs. C programming/defining a value to be used
Date
Msg-id CAAJSdji024ecwvCreTviPpO4KPGTw=AB=yPe1cE=NtvmTcKDMA@mail.gmail.com
Whole thread Raw
In response to Postgresql 9.5 and Shell scripts/variables vs. C programming/defining a value to be used  (Poul Kristensen <bcc5226@gmail.com>)
Responses Re: Postgresql 9.5 and Shell scripts/variables vs. C programming/defining a value to be used
List pgsql-general
On Mon, Nov 21, 2016 at 11:22 AM, Poul Kristensen <bcc5226@gmail.com> wrote:
Thank you for fast repons!

The $1 substitution below. I assume that it refers to "joe's place". But it is not very clear to me, how "joe's place" will appear instead of $1 when running. Where is it possiible to read more about this? There just is'nt much about substitution in C online. Any recommended books to buy?  


/* Here is our out-of-line parameter value */
    paramValues[0] = "joe's place";

    res = PQexecParams(conn,
                       "SELECT * FROM test1 WHERE t = $1",
                       1,       /* one param */
                       NULL,    /* let the backend deduce param type */
                       paramValues,
                       NULL,    /* don't need param lengths since text */
                       NULL,    /* default to all text params */
                       1);      /* ask for binary results */
}

/Poul



than I can do. But I just noticed a mistake in your code, or maybe just something left out. I would say:

char *value1 = "joe's place";
​​
char **paramV
​a​
lues = &value1;
​ /* closer match to the documentation's syntax */​

//char *paramValues[] = {"joe's place"}; /* same as above, different syntax */
//
//char *paramValues[1]; /* this looks to be missing */
//paramValues[0]="joe's place"; /* what you had */
res = PQexecParms(conn,
           "SELECT * FROM test1 WHERE t = $1",
           1, /* there is only 1 entry in paramValues array */
           paramValues, /* address of parameter value array */
           NULL, /* don't need param lengths since text */
           NULL, /* defaul to all text params */
           1); /* return all values as binary */

Well, you have an array of pointers to characters called paramValues. The $1 refers to whatever is pointed to by paramValues[0]​, which is a pointer to value1 which is a C "string". Basically in the second parameter, the command, the $n is used as a 1-based index into the paramValues[] array. This means that the actual C language array value is one less (since C arrays are 0-based). Which means that "$n" (n>=1) in the "command" string refers to value pointed to by paramValues[n-1]. The 3rd value, 1 in this case, tells PQexecParms how many entries there are in the paramValues[] array. I guess this is a type of validity check that the $n in the command string is not too large for the array.

Note: please keep the discussion on the list, not to me personally. It may be of help to others (or maybe not, I don't know.)

--
Heisenberg may have been here.


Maranatha! <><
John McKown

pgsql-general by date:

Previous
From: "Fran ..."
Date:
Subject: Re: Database migration to RDS issues permissions
Next
From: "David G. Johnston"
Date:
Subject: Re: variable value in array_to_string