aboutsummaryrefslogtreecommitdiff
path: root/src/tutorial/funcs_new.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-08-24 23:32:43 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-08-24 23:32:43 +0000
commitd9eb7d8fa1b841da4799b907ac2d97f8bcf5cf87 (patch)
treef5d2a75b43ffd38dbac59ad81a507fcc83373649 /src/tutorial/funcs_new.c
parent782c16c6a154e760bf1608d633488538cd52da93 (diff)
downloadpostgresql-d9eb7d8fa1b841da4799b907ac2d97f8bcf5cf87.tar.gz
postgresql-d9eb7d8fa1b841da4799b907ac2d97f8bcf5cf87.zip
Update funcs tutorial for new function manager.
Diffstat (limited to 'src/tutorial/funcs_new.c')
-rw-r--r--src/tutorial/funcs_new.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/tutorial/funcs_new.c b/src/tutorial/funcs_new.c
new file mode 100644
index 00000000000..0734e67a113
--- /dev/null
+++ b/src/tutorial/funcs_new.c
@@ -0,0 +1,116 @@
+/******************************************************************************
+ These are user-defined functions that can be bound to a Postgres backend
+ and called by Postgres to execute SQL functions of the same name.
+
+ The calling format for these functions is defined by the CREATE FUNCTION
+ SQL statement that binds them to the backend.
+
+ NOTE: this file shows examples of "new style" function call conventions.
+ See funcs.c for examples of "old style".
+*****************************************************************************/
+
+#include <string.h>
+
+#include "postgres.h" /* general Postgres declarations */
+
+#include "fmgr.h" /* for argument/result macros */
+#include "executor/executor.h" /* for GetAttributeByName() */
+#include "utils/geo_decls.h" /* for point type */
+
+
+/* These prototypes just prevent possible warnings from gcc. */
+
+Datum add_one(PG_FUNCTION_ARGS);
+Datum add_one_float8(PG_FUNCTION_ARGS);
+Datum makepoint(PG_FUNCTION_ARGS);
+Datum copytext(PG_FUNCTION_ARGS);
+Datum concat_text(PG_FUNCTION_ARGS);
+Datum c_overpaid(PG_FUNCTION_ARGS);
+
+
+/* By Value */
+
+Datum
+add_one(PG_FUNCTION_ARGS)
+{
+ int32 arg = PG_GETARG_INT32(0);
+
+ PG_RETURN_INT32(arg + 1);
+}
+
+/* By Reference, Fixed Length */
+
+Datum
+add_one_float8(PG_FUNCTION_ARGS)
+{
+ /* The macros for FLOAT8 hide its pass-by-reference nature */
+ float8 arg = PG_GETARG_FLOAT8(0);
+
+ PG_RETURN_FLOAT8(arg + 1.0);
+}
+
+Datum
+makepoint(PG_FUNCTION_ARGS)
+{
+ Point *pointx = PG_GETARG_POINT_P(0);
+ Point *pointy = PG_GETARG_POINT_P(1);
+ Point *new_point = (Point *) palloc(sizeof(Point));
+
+ new_point->x = pointx->x;
+ new_point->y = pointy->y;
+
+ PG_RETURN_POINT_P(new_point);
+}
+
+/* By Reference, Variable Length */
+
+Datum
+copytext(PG_FUNCTION_ARGS)
+{
+ text *t = PG_GETARG_TEXT_P(0);
+ /*
+ * VARSIZE is the total size of the struct in bytes.
+ */
+ text *new_t = (text *) palloc(VARSIZE(t));
+ VARATT_SIZEP(new_t) = VARSIZE(t);
+ /*
+ * VARDATA is a pointer to the data region of the struct.
+ */
+ memcpy((void *) VARDATA(new_t), /* destination */
+ (void *) VARDATA(t), /* source */
+ VARSIZE(t)-VARHDRSZ); /* how many bytes */
+ PG_RETURN_TEXT_P(new_t);
+}
+
+Datum
+concat_text(PG_FUNCTION_ARGS)
+{
+ text *arg1 = PG_GETARG_TEXT_P(0);
+ text *arg2 = PG_GETARG_TEXT_P(1);
+ int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
+ text *new_text = (text *) palloc(new_text_size);
+
+ memset((void *) new_text, 0, new_text_size);
+ VARATT_SIZEP(new_text) = new_text_size;
+ strncpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
+ strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
+ PG_RETURN_TEXT_P(new_text);
+}
+
+/* Composite types */
+
+Datum
+c_overpaid(PG_FUNCTION_ARGS)
+{
+ TupleTableSlot *t = (TupleTableSlot *) PG_GETARG_POINTER(0);
+ int32 limit = PG_GETARG_INT32(1);
+ bool isnull;
+ int32 salary;
+
+ salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
+ if (isnull)
+ PG_RETURN_BOOL(false);
+ /* Alternatively, we might prefer to do PG_RETURN_NULL() for null salary */
+
+ PG_RETURN_BOOL(salary > limit);
+}