Commit 4e911c84 authored by Tom Lane's avatar Tom Lane

Fix SPI example to reflect new-style calling convention for textout().

parent b1065f8f
...@@ -2691,7 +2691,7 @@ are invisible to the query scan. For example, in query ...@@ -2691,7 +2691,7 @@ are invisible to the query scan. For example, in query
INSERT INTO a SELECT * FROM a INSERT INTO a SELECT * FROM a
tuples inserted are invisible for SELECT' scan. In effect, this tuples inserted are invisible for SELECT's scan. In effect, this
duplicates the database table within itself (subject to unique index duplicates the database table within itself (subject to unique index
rules, of course) without recursing. rules, of course) without recursing.
</Para> </Para>
...@@ -2708,7 +2708,7 @@ of Q) or after Q is done. ...@@ -2708,7 +2708,7 @@ of Q) or after Q is done.
<Para> <Para>
This example of SPI usage demonstrates the visibility rule. This example of SPI usage demonstrates the visibility rule.
There are more complex examples in in src/test/regress/regress.c and There are more complex examples in src/test/regress/regress.c and
in contrib/spi. in contrib/spi.
</Para> </Para>
...@@ -2719,47 +2719,54 @@ query using SPI_exec and returns the number of tuples for which the query ...@@ -2719,47 +2719,54 @@ query using SPI_exec and returns the number of tuples for which the query
executed: executed:
<ProgramListing> <ProgramListing>
#include "executor/spi.h" /* this is what you need to work with SPI */ #include "executor/spi.h" /* this is what you need to work with SPI */
int execq(text *sql, int cnt); int execq(text *sql, int cnt);
int int
execq(text *sql, int cnt) execq(text *sql, int cnt)
{ {
int ret; char *query;
int proc = 0; int ret;
int proc;
SPI_connect();
/* Convert given TEXT object to a C string */
ret = SPI_exec(textout(sql), cnt); query = DatumGetCString(DirectFunctionCall1(textout,
PointerGetDatum(sql)));
proc = SPI_processed;
/* SPI_connect();
* If this is SELECT and some tuple(s) fetched -
* returns tuples to the caller via elog (NOTICE). ret = SPI_exec(query, cnt);
*/
if ( ret == SPI_OK_SELECT && SPI_processed > 0 ) proc = SPI_processed;
{ /*
TupleDesc tupdesc = SPI_tuptable->tupdesc; * If this is SELECT and some tuple(s) fetched -
SPITupleTable *tuptable = SPI_tuptable; * returns tuples to the caller via elog (NOTICE).
char buf[8192]; */
int i; if ( ret == SPI_OK_SELECT && SPI_processed > 0 )
{
for (ret = 0; ret < proc; ret++) TupleDesc tupdesc = SPI_tuptable->tupdesc;
{ SPITupleTable *tuptable = SPI_tuptable;
HeapTuple tuple = tuptable->vals[ret]; char buf[8192];
int i,j;
for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
sprintf(buf + strlen (buf), " %s%s", for (j = 0; j < proc; j++)
SPI_getvalue(tuple, tupdesc, i), {
(i == tupdesc->natts) ? " " : " |"); HeapTuple tuple = tuptable->vals[j];
elog (NOTICE, "EXECQ: %s", buf);
} for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
} sprintf(buf + strlen (buf), " %s%s",
SPI_getvalue(tuple, tupdesc, i),
SPI_finish(); (i == tupdesc->natts) ? " " : " |");
elog (NOTICE, "EXECQ: %s", buf);
return (proc); }
}
SPI_finish();
pfree(query);
return (proc);
} }
</ProgramListing> </ProgramListing>
</Para> </Para>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment