Header And Logo

PostgreSQL
| The world's most advanced open source database.

funcs.c

Go to the documentation of this file.
00001 /* src/tutorial/funcs.c */
00002 
00003 /******************************************************************************
00004   These are user-defined functions that can be bound to a Postgres backend
00005   and called by Postgres to execute SQL functions of the same name.
00006 
00007   The calling format for these functions is defined by the CREATE FUNCTION
00008   SQL statement that binds them to the backend.
00009 
00010   NOTE: this file shows examples of "old style" function call conventions.
00011   See funcs_new.c for examples of "new style".
00012 *****************************************************************************/
00013 
00014 #include "postgres.h"           /* general Postgres declarations */
00015 
00016 #include "executor/executor.h"  /* for GetAttributeByName() */
00017 #include "utils/geo_decls.h"    /* for point type */
00018 
00019 PG_MODULE_MAGIC;
00020 
00021 
00022 /* These prototypes just prevent possible warnings from gcc. */
00023 
00024 int         add_one(int arg);
00025 float8     *add_one_float8(float8 *arg);
00026 Point      *makepoint(Point *pointx, Point *pointy);
00027 text       *copytext(text *t);
00028 text       *concat_text(text *arg1, text *arg2);
00029 bool c_overpaid(HeapTupleHeader t,  /* the current instance of EMP */
00030            int32 limit);
00031 
00032 
00033 /* By Value */
00034 
00035 int
00036 add_one(int arg)
00037 {
00038     return arg + 1;
00039 }
00040 
00041 /* By Reference, Fixed Length */
00042 
00043 float8 *
00044 add_one_float8(float8 *arg)
00045 {
00046     float8     *result = (float8 *) palloc(sizeof(float8));
00047 
00048     *result = *arg + 1.0;
00049 
00050     return result;
00051 }
00052 
00053 Point *
00054 makepoint(Point *pointx, Point *pointy)
00055 {
00056     Point      *new_point = (Point *) palloc(sizeof(Point));
00057 
00058     new_point->x = pointx->x;
00059     new_point->y = pointy->y;
00060 
00061     return new_point;
00062 }
00063 
00064 /* By Reference, Variable Length */
00065 
00066 text *
00067 copytext(text *t)
00068 {
00069     /*
00070      * VARSIZE is the total size of the struct in bytes.
00071      */
00072     text       *new_t = (text *) palloc(VARSIZE(t));
00073 
00074     SET_VARSIZE(new_t, VARSIZE(t));
00075 
00076     /*
00077      * VARDATA is a pointer to the data region of the struct.
00078      */
00079     memcpy((void *) VARDATA(new_t),     /* destination */
00080            (void *) VARDATA(t), /* source */
00081            VARSIZE(t) - VARHDRSZ);      /* how many bytes */
00082     return new_t;
00083 }
00084 
00085 text *
00086 concat_text(text *arg1, text *arg2)
00087 {
00088     int32       arg1_size = VARSIZE(arg1) - VARHDRSZ;
00089     int32       arg2_size = VARSIZE(arg2) - VARHDRSZ;
00090     int32       new_text_size = arg1_size + arg2_size + VARHDRSZ;
00091     text       *new_text = (text *) palloc(new_text_size);
00092 
00093     SET_VARSIZE(new_text, new_text_size);
00094     memcpy(VARDATA(new_text), VARDATA(arg1), arg1_size);
00095     memcpy(VARDATA(new_text) + arg1_size, VARDATA(arg2), arg2_size);
00096     return new_text;
00097 }
00098 
00099 /* Composite types */
00100 
00101 bool
00102 c_overpaid(HeapTupleHeader t,   /* the current instance of EMP */
00103            int32 limit)
00104 {
00105     bool        isnull;
00106     int32       salary;
00107 
00108     salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
00109     if (isnull)
00110         return false;
00111     return salary > limit;
00112 }