aboutsummaryrefslogtreecommitdiff
path: root/src/tutorial/funcs.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.c
parent782c16c6a154e760bf1608d633488538cd52da93 (diff)
downloadpostgresql-d9eb7d8fa1b841da4799b907ac2d97f8bcf5cf87.tar.gz
postgresql-d9eb7d8fa1b841da4799b907ac2d97f8bcf5cf87.zip
Update funcs tutorial for new function manager.
Diffstat (limited to 'src/tutorial/funcs.c')
-rw-r--r--src/tutorial/funcs.c105
1 files changed, 67 insertions, 38 deletions
diff --git a/src/tutorial/funcs.c b/src/tutorial/funcs.c
index 5cb8d95c736..621503a421c 100644
--- a/src/tutorial/funcs.c
+++ b/src/tutorial/funcs.c
@@ -4,75 +4,104 @@
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 "old style" function call conventions.
+ See funcs_new.c for examples of "new style".
*****************************************************************************/
-#include "postgres.h" /* for variable length type */
+#include <string.h>
+
+#include "postgres.h" /* general Postgres declarations */
+
#include "executor/executor.h" /* for GetAttributeByName() */
#include "utils/geo_decls.h" /* for point type */
-/* The following prototypes declare what we assume the user declares to
- Postgres in his CREATE FUNCTION statement.
-*/
+
+/* These prototypes just prevent possible warnings from gcc. */
int add_one(int arg);
+float8 *add_one_float8(float8 *arg);
Point *makepoint(Point *pointx, Point *pointy);
text *copytext(text *t);
-
+text *concat_text(text *arg1, text *arg2);
bool c_overpaid(TupleTableSlot *t, /* the current instance of EMP */
- int4 limit);
-
+ int32 limit);
+/* By Value */
+
int
add_one(int arg)
{
- return arg + 1;
+ return arg + 1;
+}
+
+/* By Reference, Fixed Length */
+
+float8 *
+add_one_float8(float8 *arg)
+{
+ float8 *result = (float8 *) palloc(sizeof(float8));
+
+ *result = *arg + 1.0;
+
+ return result;
}
Point *
makepoint(Point *pointx, Point *pointy)
{
- Point *new_point = (Point *) palloc(sizeof(Point));
+ Point *new_point = (Point *) palloc(sizeof(Point));
- new_point->x = pointx->x;
- new_point->y = pointy->y;
-
- return new_point;
+ new_point->x = pointx->x;
+ new_point->y = pointy->y;
+
+ return new_point;
}
+/* By Reference, Variable Length */
+
text *
copytext(text *t)
{
+ /*
+ * 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 */
+ return new_t;
+}
- /*
- * VARSIZE is the total size of the struct in bytes.
- */
- text *new_t = (text *) palloc(VARSIZE(t));
-
- MemSet(new_t, 0, VARSIZE(t));
-
- VARSIZE(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 */
-
- return new_t;
+text *
+concat_text(text *arg1, text *arg2)
+{
+ 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);
+ return new_text;
}
+/* Composite types */
+
bool
c_overpaid(TupleTableSlot *t, /* the current instance of EMP */
- int4 limit)
+ int32 limit)
{
- bool isnull = false;
- int4 salary;
-
- salary = (int4) GetAttributeByName(t, "salary", &isnull);
+ bool isnull;
+ int32 salary;
- if (isnull)
- return false;
- return salary > limit;
+ salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
+ if (isnull)
+ return (false);
+ return salary > limit;
}