[PATCH 2/4] Add tests for "COPY ... TO FUNCTION ..."

From: Daniel Farina <dfarina(at)truviso(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Daniel Farina <dfarina(at)truviso(dot)com>
Subject: [PATCH 2/4] Add tests for "COPY ... TO FUNCTION ..."
Date: 2009-11-23 21:34:40
Message-ID: 1259012082-6196-3-git-send-email-dfarina@truviso.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Signed-off-by: Daniel Farina <dfarina(at)truviso(dot)com>
---
src/test/regress/input/copy.source | 38 +++++++++++++++++++
src/test/regress/output/copy.source | 69 +++++++++++++++++++++++++++++++++++
src/test/regress/regress.c | 56 ++++++++++++++++++++++++++++
3 files changed, 163 insertions(+), 0 deletions(-)

diff --git a/src/test/regress/input/copy.source b/src/test/regress/input/copy.source
index 376329d..e5dcd62 100644
--- a/src/test/regress/input/copy.source
+++ b/src/test/regress/input/copy.source
@@ -107,3 +107,41 @@ this is just a line full of junk that would error out if parsed

copy copytest3 to stdout csv header;

+
+-- test copy to function
+
+CREATE FUNCTION copyto_setup_state ()
+ RETURNS void
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+
+CREATE FUNCTION copyto_function (internal)
+ RETURNS void
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+
+CREATE FUNCTION copyto_yield_len ()
+ RETURNS int4
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+
+CREATE FUNCTION copyto_yield_text ()
+ RETURNS text
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+
+CREATE FUNCTION copyto_free ()
+ RETURNS void
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+
+select copyto_setup_state();
+copy copytest to function copyto_function;
+select copyto_yield_len();
+select copyto_yield_text();
+select copyto_free();
+
+select copyto_setup_state();
+copy binary copytest to function copyto_function;
+select copyto_yield_len();
+select copyto_free();
diff --git a/src/test/regress/output/copy.source b/src/test/regress/output/copy.source
index 5a88d6e..74ea935 100644
--- a/src/test/regress/output/copy.source
+++ b/src/test/regress/output/copy.source
@@ -71,3 +71,72 @@ copy copytest3 to stdout csv header;
c1,"col with , comma","col with "" quote"
1,a,1
2,b,2
+-- test copy to function
+CREATE FUNCTION copyto_setup_state ()
+ RETURNS void
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+CREATE FUNCTION copyto_function (internal)
+ RETURNS void
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+CREATE FUNCTION copyto_yield_len ()
+ RETURNS int4
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+CREATE FUNCTION copyto_yield_text ()
+ RETURNS text
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+CREATE FUNCTION copyto_free ()
+ RETURNS void
+ AS '@libdir@/regress(at)DLSUFFIX@'
+ LANGUAGE C;
+select copyto_setup_state();
+ copyto_setup_state
+--------------------
+
+(1 row)
+
+copy copytest to function copyto_function;
+select copyto_yield_len();
+ copyto_yield_len
+------------------
+ 76
+(1 row)
+
+select copyto_yield_text();
+ copyto_yield_text
+-------------------------------------------
+ DOS abc\r\ndef 1 +
+ Unix abc\ndef 2 +
+ Mac abc\rdef 3 +
+ esc\\ape a\\r\\\r\\\n\\nb 4+
+
+(1 row)
+
+select copyto_free();
+ copyto_free
+-------------
+
+(1 row)
+
+select copyto_setup_state();
+ copyto_setup_state
+--------------------
+
+(1 row)
+
+copy binary copytest to function copyto_function;
+select copyto_yield_len();
+ copyto_yield_len
+------------------
+ 142
+(1 row)
+
+select copyto_free();
+ copyto_free
+-------------
+
+(1 row)
+
diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c
index 0e94e68..a96a085 100644
--- a/src/test/regress/regress.c
+++ b/src/test/regress/regress.c
@@ -16,6 +16,7 @@
#include "executor/spi.h"
#include "utils/builtins.h"
#include "utils/geo_decls.h"
+#include "utils/memutils.h"


#define P_MAXDIG 12
@@ -34,6 +35,11 @@ extern char *reverse_name(char *string);
extern int oldstyle_length(int n, text *t);
extern Datum int44in(PG_FUNCTION_ARGS);
extern Datum int44out(PG_FUNCTION_ARGS);
+extern Datum copyto_free(PG_FUNCTION_ARGS);
+extern Datum copyto_function(PG_FUNCTION_ARGS);
+extern Datum copyto_setup_state(PG_FUNCTION_ARGS);
+extern Datum copyto_yield_len(PG_FUNCTION_ARGS);
+extern Datum copyto_yield_text(PG_FUNCTION_ARGS);

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
@@ -737,3 +743,53 @@ int44out(PG_FUNCTION_ARGS)
*--walk = '\0';
PG_RETURN_CSTRING(result);
}
+
+/*
+ * copyto testing
+ */
+static StringInfo global_buf;
+
+PG_FUNCTION_INFO_V1(copyto_setup_state);
+
+Datum
+copyto_setup_state(PG_FUNCTION_ARGS)
+{
+ MemoryContext oldcxt = MemoryContextSwitchTo(TopMemoryContext);
+ global_buf = makeStringInfo();
+ MemoryContextSwitchTo(oldcxt);
+ PG_RETURN_VOID();
+}
+
+PG_FUNCTION_INFO_V1(copyto_function);
+
+Datum
+copyto_function(PG_FUNCTION_ARGS)
+{
+ StringInfo copybuf = (void *) PG_GETARG_POINTER(0);
+ appendBinaryStringInfo(global_buf, copybuf->data, copybuf->len);
+ PG_RETURN_VOID();
+}
+
+PG_FUNCTION_INFO_V1(copyto_yield_len);
+
+Datum
+copyto_yield_len(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_INT32(global_buf->len);
+}
+
+PG_FUNCTION_INFO_V1(copyto_yield_text);
+
+Datum
+copyto_yield_text(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_DATUM(DirectFunctionCall1(textin,
+ CStringGetDatum(global_buf->data)));
+}
+
+Datum
+copyto_free(PG_FUNCTION_ARGS)
+{
+ pfree(global_buf);
+ PG_RETURN_VOID();
+}
--
1.6.5.3

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Farina 2009-11-23 21:34:41 [PATCH 3/4] Add dblink functions for use with COPY ... TO FUNCTION ...
Previous Message Daniel Farina 2009-11-23 21:34:39 [PATCH 1/4] Add "COPY ... TO FUNCTION ..." support