Permalink
Browse files

- Update all Copyright notices.

- New (undocumented) functions r_version(), plr_set_rhome(),
  and plr_unset_rhome().
- Use Rembedded.h if available
- If R_HOME environment variable is not defined, attempt to find it using
  pkg-config. (idea from Dirk Eddelbuettel)
- In any case, create define for default R_HOME based on pkg-config. Use
  the default R_HOME during R interpreter init if the environment variable
  is unset. (idea from Dirk Eddelbuettel)
- Use PGDLLIMPORT instead of DLLIMPORT is it is defined.
- Switch to Rf_isVectorList instead of IS_LIST for spi_execp argument test.
  Prior to R-2.4.0, the latter allows bad arguments to get past, causing
  a segfault in an R internal type coersion function. (found by Steve Singer)
- New spi cursor manupulation functions (patch courtesy of Steve Singer).
- Force R interpreter non-interactive mode. Fixes some cases that previously
  appeared to be hung postgres backends in certain errors occured in R
  (R was actually waiting for user input). On some platforms this situation
  caused segfaults instead. (found by Jie Zhang)
- When a plr function source is empty, plr tries to find a function by the
  same name within the R interpreter environment. If the function could not
  be found, it would cause a hang or segfault. This was not easily trapped
  in the R interpreter. Now, build and compile the equivalent plr source.
  This allows the R interpreter to trap the error properly when the function
  does not exist.
- PG_VERSION_NUM if available. (patch courtesy of Neal Conway)
- Plug memory leak in POP_PLERRCONTEXT. (patch courtesy of Steve Singer)
  • Loading branch information...
1 parent 412947a commit d2d549579f3d0245012d691f709b2eceff2ddf78 jconway committed Sep 8, 2007
Showing with 499 additions and 33 deletions.
  1. +24 −1 Makefile
  2. +2 −2 README.plr
  3. +93 −5 doc/plr.sgml
  4. +49 −0 expected/plr.out
  5. +8 −1 pg_backend_support.c
  6. +1 −1 pg_conversion.c
  7. +200 −3 pg_rsupport.c
  8. +32 −1 pg_userfuncs.c
  9. +44 −10 plr.c
  10. +15 −9 plr.h
  11. +18 −0 plr.sql.in
  12. +13 −0 sql/plr.sql
View
@@ -1,8 +1,20 @@
# location of R library
+
+ifdef R_HOME
r_libdir1x = ${R_HOME}/bin
r_libdir2x = ${R_HOME}/lib
# location of R includes
r_includespec = ${R_HOME}/include
+else
+R_HOME := $(shell pkg-config --variable=rhome libR)
+r_libdir1x := $(shell pkg-config --variable=rlibdir libR)
+r_libdir2x := $(shell pkg-config --variable=rlibdir libR)
+r_includespec := $(shell pkg-config --variable=rincludedir libR)
+endif
+
+rhomedef := $(shell pkg-config --variable=rhome libR)
+
+ifneq (,${R_HOME})
MODULE_big := plr
PG_CPPFLAGS += -I$(r_includespec)
@@ -55,6 +67,7 @@ ifneq (,$(findstring yes, $(shared_libr)$(allow_nonpic_in_shlib)))
override CPPFLAGS := -I$(srcdir) -I$(r_includespec) $(CPPFLAGS)
override CPPFLAGS += -DPKGLIBDIR=\"$(pkglibdir)\" -DDLSUFFIX=\"$(DLSUFFIX)\"
+override CPPFLAGS += -DR_HOME_DEFAULT=\"$(rhomedef)\"
else # can't build
@@ -65,4 +78,14 @@ all:
echo "*** the documentation for details."; \
echo ""
-endif # can't build
+endif # can't build - cannot find libR
+
+else # can't build - no R_HOME
+
+all:
+ @echo ""; \
+ echo "*** Cannot build PL/R because R_HOME cannot be found." ; \
+ echo "*** Refer to the documentation for details."; \
+ echo ""
+
+endif
View
@@ -2,7 +2,7 @@
* PL/R - PostgreSQL support for R as a
* procedural language (PL)
*
- * Copyright (c) 2003-2006 by Joseph E. Conway
+ * Copyright (c) 2003-2007 by Joseph E. Conway
* ALL RIGHTS RESERVED
*
* Joe Conway <mail@joeconway.com>
@@ -30,7 +30,7 @@
*
*/
-See http://www.joeconway.com/plr/ for release notes
+See http://www.joeconway.com/plr/ for release notes and latest docs
Notes:
- R headers are required. Download and install R prior to building
View
@@ -3,7 +3,7 @@
<title>PL/R User's Guide - R Procedural Language</title>
<bookinfo>
<copyright>
- <year>2003-2006</year>
+ <year>2003-2007</year>
<holder>Joseph E Conway</holder>
</copyright>
</bookinfo>
@@ -62,8 +62,24 @@
commands (starting from <literal>/path/to/plr</literal>):
<programlisting>
cd plr
- make -f Makefile.pgxs
- make -f Makefile.pgxs install
+ USE_PGXS=1 make
+ USE_PGXS=1 make install
+ </programlisting>
+ </para>
+
+ <para>
+ Win32 - adjust paths according to your own setup, and be sure to restart
+ the PostgreSQL service after changing:
+ <programlisting>
+ In Windows environment:
+ R_HOME=C:\Progra~1\R\R-2.5.0
+ Path=%PATH%;C:\Progra~1\R\R-2.5.0\bin
+
+ In MSYS:
+ export R_HOME=/c/progra~1/R/R-2.5.0
+ export PATH=$PATH:/c/progra~1/PostgreSQL/8.2/bin
+ USE_PGXS=1 make
+ USE_PGXS=1 make install
</programlisting>
</para>
@@ -112,8 +128,9 @@ CREATE LANGUAGE plr HANDLER plr_call_handler;
<para>
Additionally, libR must be findable by your runtime linker.
On Linux, this involves adding an entry in /etc/ld.so.conf for the
- location of libR (typically $R_HOME/bin), and then running ldconfig.
- Refer to <literal>man ldconfig</literal> or its equivalent for your system.
+ location of libR (typically $R_HOME/bin or $R_HOME/lib), and then
+ running ldconfig. Refer to <literal>man ldconfig</literal> or its
+ equivalent for your system.
</para>
</tip>
@@ -790,6 +807,77 @@ select * from test_spi_execp('sp') as t(typeid oid, typename name);
</varlistentry>
<varlistentry>
+ <term>
+ <function>pg.spi.cursor_open</function>(
+ <type> character</type> <replaceable>cursor_name</replaceable>,
+ <type>external pointer</type> <replaceable>saved_plan</replaceable>,
+ <type>variable list</type> <replaceable>value_list</replaceable>)
+ </term>
+ <listitem>
+ <para>
+ Opens a cursor identified by cursor_name. The cursor can then be used to scroll through
+ the results of a query plan previously prepared by pg.spi.prepare. Any arguments to
+ the plan should be specified in argvalues similar to pg.spi.execp. Only read-only cursors
+ are supported at the moment.
+ <programlisting>
+plan <- pg.spi.prepare('SELECT * FROM pg_class');
+cursor_obj <- pg.spi.cursor_open('my_cursor',plan);
+ </programlisting>
+ </para>
+ <para>
+ Returns a cursor object that be be passed to <function>pg.spi.cursor_fetch</function>
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>
+ <function>pg.spi.cursor_fetch</function>(
+ <type>external pointer</type> <replaceable>cursor</replaceable>,
+ <type>boolean</type> <replaceable>forward</replaceable>,
+ <type>integer</type> <replaceable>rows</replaceable>)
+ </term>
+ <listitem>
+ <para>
+ Fetches rows from the cursor object previosuly returned by <function>pg.spi.cursor_open
+ </function>. If forward is TRUE then the cursor is moved forward to
+ fetch at most the number of rows required by the rows parameter. If forward is
+ FALSE then the cursor is moved backrwards at most the number of rows specified.
+ </para>
+ <para>
+ rows indicates the maximum number of rows that should be returned.
+ </para>
+ <para>
+ <programlisting>
+plan <- pg.spi.prepare('SELECT * FROM pg_class');
+cursor_obj <- pg.spi.cursor_open('my_cursor',plan);
+data <- pg.spi.cursor_forward(cursor_obj,TRUE,10);
+ </programlisting>
+ </para>
+ <para>
+ Returns a data frame containing the results.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>
+ <function>pg.spi.cursor_close</function>(
+ <type>external pointer</type><replaceable>cursor</replaceable>)
+ </term>
+ <listitem>
+ <para>
+ Closes a cursor previously opened by <function>pg.spi.cursor_open</function>
+ <programlisting>
+plan <- pg.spi.prepare('SELECT * FROM pg_class');
+cursor_obj <- pg.spi.cursor_open('my_cursor',plan);
+pg.spi.cursor_close(cursor_obj);
+ </programlisting>
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><function>pg.spi.lastoid</function>()</term>
<listitem>
<para>
View
@@ -819,3 +819,52 @@ select count(*) from foo;
(1 row)
drop trigger footrig on foo;
+-- Test cursors: creating, scrolling forward, closing
+CREATE OR REPLACE FUNCTION cursor_fetch_test(integer,boolean) RETURNS SETOF integer AS 'plan<-pg.spi.prepare("SELECT * FROM generate_series(1,10)"); cursor<-pg.spi.cursor_open("curs",plan); dat<-pg.spi.cursor_fetch(cursor,arg2,arg1); pg.spi.cursor_close(cursor); return (dat);' language 'plr';
+SELECT * FROM cursor_fetch_test(1,true);
+ cursor_fetch_test
+-------------------
+ 1
+(1 row)
+
+SELECT * FROM cursor_fetch_test(2,true);
+ cursor_fetch_test
+-------------------
+ 1
+ 2
+(2 rows)
+
+SELECT * FROM cursor_fetch_test(20,true);
+ cursor_fetch_test
+-------------------
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+(10 rows)
+
+--Test cursors: scrolling backwards
+CREATE OR REPLACE FUNCTION cursor_direction_test() RETURNS SETOF integer AS'plan<-pg.spi.prepare("SELECT * FROM generate_series(1,10)"); cursor<-pg.spi.cursor_open("curs",plan); dat<-pg.spi.cursor_fetch(cursor,TRUE,as.integer(3)); dat2<-pg.spi.cursor_fetch(cursor,FALSE,as.integer(3)); pg.spi.cursor_close(cursor); return (dat2);' language 'plr';
+SELECT * FROM cursor_direction_test();
+ cursor_direction_test
+-----------------------
+ 2
+ 1
+(2 rows)
+
+--Test cursors: Passing arguments to a plan
+CREATE OR REPLACE FUNCTION cursor_fetch_test_arg(integer) RETURNS SETOF integer AS 'plan<-pg.spi.prepare("SELECT * FROM generate_series(1,$1)",c(INT4OID)); cursor<-pg.spi.cursor_open("curs",plan,list(arg1)); dat<-pg.spi.cursor_fetch(cursor,TRUE,arg1); pg.spi.cursor_close(cursor); return (dat);' language 'plr';
+SELECT * FROM cursor_fetch_test_arg(3);
+ cursor_fetch_test_arg
+-----------------------
+ 1
+ 2
+ 3
+(3 rows)
+
View
@@ -2,7 +2,7 @@
* PL/R - PostgreSQL support for R as a
* procedural language (PL)
*
- * Copyright (c) 2003-2006 by Joseph E. Conway
+ * Copyright (c) 2003-2007 by Joseph E. Conway
* ALL RIGHTS RESERVED
*
* Joe Conway <mail@joeconway.com>
@@ -36,10 +36,17 @@
#include "optimizer/clauses.h"
#include "utils/memutils.h"
+#ifdef PGDLLIMPORT
+/* GUC variable */
+extern PGDLLIMPORT char *Dynamic_library_path;
+/* Postgres global */
+extern PGDLLIMPORT char pkglib_path[];
+#else
/* GUC variable */
extern DLLIMPORT char *Dynamic_library_path;
/* Postgres global */
extern DLLIMPORT char pkglib_path[];
+#endif /* PGDLLIMPORT */
/* compiled function hash table */
extern HTAB *plr_HashTable;
View
@@ -2,7 +2,7 @@
* PL/R - PostgreSQL support for R as a
* procedural language (PL)
*
- * Copyright (c) 2003-2006 by Joseph E. Conway
+ * Copyright (c) 2003-2007 by Joseph E. Conway
* ALL RIGHTS RESERVED
*
* Joe Conway <mail@joeconway.com>
Oops, something went wrong.

0 comments on commit d2d5495

Please sign in to comment.