4
>Database Access and Support Functions</TITLE
7
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
12
TITLE="PL/R User's Guide - R Procedural Language"
13
HREF="index.html"><LINK
15
TITLE="Using Global Data"
16
HREF="plr-global-data.html"><LINK
18
TITLE="PostgreSQL Support Functions"
19
HREF="plr-pgsql-support-funcs.html"><LINK
22
HREF="stylesheet.css"><META
24
CONTENT="2003-08-25T17:44:28"></HEAD
35
SUMMARY="Header navigation table"
44
>PL/R User's Guide - R Procedural Language</TH
52
HREF="plr-global-data.html"
66
HREF="plr-pgsql-support-funcs.html"
79
NAME="PLR-SPI-RSUPPORT-FUNCS"
81
>Chapter 6. Database Access and Support Functions</H1
83
> The following commands are available to access the database from
84
the body of a PL/R procedure, or in support thereof:
107
> Execute an SQL query given as a string. An error in the query
108
causes an error to be raised. Otherwise, the command's return value
109
is the number of rows processed for <TT
120
or zero if the query is a utility statement. If the query is a
124
> statement, the values of the selected columns
125
are placed in an R data.frame with the target column names used as
126
the frame column names. However, non-numeric columns <SPAN
133
> converted to factors. If you want all non-numeric
134
columns converted to factors, a convenience function <TT
137
> (described below) is provided.
140
> If a field of a SELECT result is NULL, the target variable for it
146
CLASS="PROGRAMLISTING"
147
>create or replace function test_spi_tup(text) returns record as '
151
select * from test_spi_tup('select oid, NULL::text as nullcol,
152
typname from pg_type where typname = ''oid'' or typname = ''text''')
153
as t(typeid oid, nullcol text, typename name);
154
typeid | nullcol | typename
155
--------+---------+----------
161
The NULL values were passed to R as <SPAN
165
PostgreSQL they were converted back to NULL.
193
> Prepares and saves a query plan for later execution. The saved plan
194
will be retained for the life of the current backend.
197
> The query may use <I
201
placeholders for values to be supplied whenever the plan is actually
202
executed. In the query string, refer to arguments by the symbols
210
arguments, the values of the argument types must be given as a vector.
220
if the query has no arguments. The argument types must be identified
221
by the type Oids, shown in pg_type. Global variables are provided for
222
this use. They are named according to the convention TYPENAMEOID, where
223
the actual name of the type, in all capitals, is substituted for
224
TYPENAME. A support function, <TT
228
used to list the predefined Global variables:
230
CLASS="PROGRAMLISTING"
231
>select * from r_typenames();
233
-----------------+---------
252
> The return value from <TT
256
to be used in subsequent calls to <TT
273
>external pointer</TT
291
> Execute a query previously prepared with <TT
301
pointer returned by <TT
305
references arguments, a <TT
311
be supplied: this is an R list of actual values for the plan arguments.
312
It must be the same length as the argument <TT
318
> previously given to <TT
331
if the query has no arguments. The following illustrates the use of
339
with and without query arguments:
341
CLASS="PROGRAMLISTING"
342
>create or replace function test_spi_prep(text) returns text as '
343
sp <<- pg.spi.prepare(arg1, c(NAMEOID, NAMEOID));
347
select test_spi_prep('select oid, typname from pg_type
348
where typname = $1 or typname = $2');
354
create or replace function test_spi_execp(text, text, text) returns record as '
355
pg.spi.execp(pg.reval(arg1), list(arg2,arg3))
358
select * from test_spi_execp('sp','oid','text') as t(typeid oid, typename name);
365
create or replace function test_spi_prep(text) returns text as '
366
sp <<- pg.spi.prepare(arg1, NA);
370
select test_spi_prep('select oid, typname from pg_type
371
where typname = ''bytea'' or typname = ''text''');
377
create or replace function test_spi_execp(text) returns setof record as '
378
pg.spi.execp(pg.reval(arg1), NA)
381
select * from test_spi_execp('sp') as t(typeid oid, typename name);
388
create or replace function test_spi_prep(text) returns text as '
389
sp <<- pg.spi.prepare(arg1);
393
select test_spi_prep('select oid, typname from pg_type
394
where typname = ''bytea'' or typname = ''text''');
400
create or replace function test_spi_execp(text) returns setof record as '
401
pg.spi.execp(pg.reval(arg1))
404
select * from test_spi_execp('sp') as t(typeid oid, typename name);
414
> NULL arguments should be passed as individual <TT
426
> Except for the way in which the query and its arguments are specified,
444
> Returns the OID of the row inserted by the last query executed via
452
if that query was a single-row INSERT. (If not, you get zero.)
471
> Duplicates all occurrences of single quote and backslash characters
472
in the given string. This may be used to safely quote strings
473
that are to be inserted into SQL queries given to
499
> Return the given string suitably quoted to be used as an identifier
500
in an SQL query string. Quotes are added only if necessary (i.e., if
501
the string contains non-identifier characters or would be case-folded).
502
Embedded quotes are properly doubled. This may be used to safely quote
503
strings that are to be inserted into SQL queries given to
541
> Emit a PostgreSQL <TT
551
> also raises an error condition:
552
further execution of the function is abandoned, and the current
553
transaction is aborted.
575
> as input, and converts all
576
non-numeric columns to <TT
579
>s. This may be useful
580
for data.frames produced by <TT
587
>, because the PL/R conversion
605
SUMMARY="Footer navigation table"
616
HREF="plr-global-data.html"
634
HREF="plr-pgsql-support-funcs.html"
644
>Using Global Data</TD
654
>PostgreSQL Support Functions</TD
b'\\ No newline at end of file'