]> granicus.if.org Git - postgresql/blobdiff - src/tutorial/funcs.c
Update copyright for 2006. Update scripts.
[postgresql] / src / tutorial / funcs.c
index bb0efacb64e5a00392e360373c2c39eb38750b48..a988ad431b2d2c8457380d6cdf3688a920cef6e2 100644 (file)
@@ -4,27 +4,29 @@
 
   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 <string.h>
-#include <stdio.h>
-#include "postgres.h"                  /* for variable length type */
-#include "utils/palloc.h"              /* for palloc */
+#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);
-
-bool c_overpaid(TupleTableSlot *t,     /* the current instance of EMP */
-                  int4 limit);
+text      *concat_text(text *arg1, text *arg2);
+bool c_overpaid(HeapTupleHeader t,     /* the current instance of EMP */
+                  int32 limit);
 
 
+/* By Value */
 
 int
 add_one(int arg)
@@ -32,6 +34,18 @@ add_one(int arg)
        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)
 {
@@ -43,18 +57,17 @@ makepoint(Point *pointx, Point *pointy)
        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));
 
-       MemSet(new_t, 0, VARSIZE(t));
-
-       VARSIZE(new_t) = VARSIZE(t);
+       VARATT_SIZEP(new_t) = VARSIZE(t);
 
        /*
         * VARDATA is a pointer to the data region of the struct.
@@ -62,19 +75,32 @@ copytext(text *t)
        memcpy((void *) VARDATA(new_t),         /* destination */
                   (void *) VARDATA(t), /* source */
                   VARSIZE(t) - VARHDRSZ);              /* how many bytes */
-
        return new_t;
 }
 
-bool
-c_overpaid(TupleTableSlot *t,  /* the current instance of EMP */
-                  int4 limit)
+text *
+concat_text(text *arg1, text *arg2)
 {
-       bool            isnull = false;
-       int4            salary;
+       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 */
 
-       salary = (int4) GetAttributeByName(t, "salary", &isnull);
+bool
+c_overpaid(HeapTupleHeader t,  /* the current instance of EMP */
+                  int32 limit)
+{
+       bool            isnull;
+       int32           salary;
 
+       salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
        if (isnull)
                return false;
        return salary > limit;