]> granicus.if.org Git - postgresql/blob - src/tutorial/funcs_new.c
Remove 576 references of include files that were not needed.
[postgresql] / src / tutorial / funcs_new.c
1 /* $PostgreSQL: pgsql/src/tutorial/funcs_new.c,v 1.11 2006/07/14 14:52:27 momjian Exp $ */
2
3 /******************************************************************************
4   These are user-defined functions that can be bound to a Postgres backend
5   and called by Postgres to execute SQL functions of the same name.
6
7   The calling format for these functions is defined by the CREATE FUNCTION
8   SQL statement that binds them to the backend.
9
10   NOTE: this file shows examples of "new style" function call conventions.
11   See funcs.c for examples of "old style".
12 *****************************************************************************/
13
14 #include "postgres.h"                   /* general Postgres declarations */
15
16 #include "executor/executor.h"  /* for GetAttributeByName() */
17 #include "utils/geo_decls.h"    /* for point type */
18
19
20 PG_MODULE_MAGIC;
21
22 /* These prototypes just prevent possible warnings from gcc. */
23
24 Datum           add_one(PG_FUNCTION_ARGS);
25 Datum           add_one_float8(PG_FUNCTION_ARGS);
26 Datum           makepoint(PG_FUNCTION_ARGS);
27 Datum           copytext(PG_FUNCTION_ARGS);
28 Datum           concat_text(PG_FUNCTION_ARGS);
29 Datum           c_overpaid(PG_FUNCTION_ARGS);
30
31
32 /* By Value */
33
34 PG_FUNCTION_INFO_V1(add_one);
35
36 Datum
37 add_one(PG_FUNCTION_ARGS)
38 {
39         int32           arg = PG_GETARG_INT32(0);
40
41         PG_RETURN_INT32(arg + 1);
42 }
43
44 /* By Reference, Fixed Length */
45
46 PG_FUNCTION_INFO_V1(add_one_float8);
47
48 Datum
49 add_one_float8(PG_FUNCTION_ARGS)
50 {
51         /* The macros for FLOAT8 hide its pass-by-reference nature */
52         float8          arg = PG_GETARG_FLOAT8(0);
53
54         PG_RETURN_FLOAT8(arg + 1.0);
55 }
56
57 PG_FUNCTION_INFO_V1(makepoint);
58
59 Datum
60 makepoint(PG_FUNCTION_ARGS)
61 {
62         Point      *pointx = PG_GETARG_POINT_P(0);
63         Point      *pointy = PG_GETARG_POINT_P(1);
64         Point      *new_point = (Point *) palloc(sizeof(Point));
65
66         new_point->x = pointx->x;
67         new_point->y = pointy->y;
68
69         PG_RETURN_POINT_P(new_point);
70 }
71
72 /* By Reference, Variable Length */
73
74 PG_FUNCTION_INFO_V1(copytext);
75
76 Datum
77 copytext(PG_FUNCTION_ARGS)
78 {
79         text       *t = PG_GETARG_TEXT_P(0);
80
81         /*
82          * VARSIZE is the total size of the struct in bytes.
83          */
84         text       *new_t = (text *) palloc(VARSIZE(t));
85
86         VARATT_SIZEP(new_t) = VARSIZE(t);
87
88         /*
89          * VARDATA is a pointer to the data region of the struct.
90          */
91         memcpy((void *) VARDATA(new_t),         /* destination */
92                    (void *) VARDATA(t), /* source */
93                    VARSIZE(t) - VARHDRSZ);              /* how many bytes */
94         PG_RETURN_TEXT_P(new_t);
95 }
96
97 PG_FUNCTION_INFO_V1(concat_text);
98
99 Datum
100 concat_text(PG_FUNCTION_ARGS)
101 {
102         text       *arg1 = PG_GETARG_TEXT_P(0);
103         text       *arg2 = PG_GETARG_TEXT_P(1);
104         int32           new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
105         text       *new_text = (text *) palloc(new_text_size);
106
107         memset((void *) new_text, 0, new_text_size);
108         VARATT_SIZEP(new_text) = new_text_size;
109         strncpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1) - VARHDRSZ);
110         strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2) - VARHDRSZ);
111         PG_RETURN_TEXT_P(new_text);
112 }
113
114 /* Composite types */
115
116 PG_FUNCTION_INFO_V1(c_overpaid);
117
118 Datum
119 c_overpaid(PG_FUNCTION_ARGS)
120 {
121         HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
122         int32           limit = PG_GETARG_INT32(1);
123         bool            isnull;
124         int32           salary;
125
126         salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
127         if (isnull)
128                 PG_RETURN_BOOL(false);
129
130         /*
131          * Alternatively, we might prefer to do PG_RETURN_NULL() for null salary
132          */
133
134         PG_RETURN_BOOL(salary > limit);
135 }