~ubuntu-branches/debian/sid/postgresql-9.3/sid

« back to all changes in this revision

Viewing changes to src/tutorial/funcs.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-05-08 05:39:52 UTC
  • Revision ID: package-import@ubuntu.com-20130508053952-1j7uilp7mjtrvq8q
Tags: upstream-9.3~beta1
ImportĀ upstreamĀ versionĀ 9.3~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* src/tutorial/funcs.c */
 
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 "old style" function call conventions.
 
11
  See funcs_new.c for examples of "new 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
PG_MODULE_MAGIC;
 
20
 
 
21
 
 
22
/* These prototypes just prevent possible warnings from gcc. */
 
23
 
 
24
int                     add_one(int arg);
 
25
float8     *add_one_float8(float8 *arg);
 
26
Point      *makepoint(Point *pointx, Point *pointy);
 
27
text       *copytext(text *t);
 
28
text       *concat_text(text *arg1, text *arg2);
 
29
bool c_overpaid(HeapTupleHeader t,      /* the current instance of EMP */
 
30
                   int32 limit);
 
31
 
 
32
 
 
33
/* By Value */
 
34
 
 
35
int
 
36
add_one(int arg)
 
37
{
 
38
        return arg + 1;
 
39
}
 
40
 
 
41
/* By Reference, Fixed Length */
 
42
 
 
43
float8 *
 
44
add_one_float8(float8 *arg)
 
45
{
 
46
        float8     *result = (float8 *) palloc(sizeof(float8));
 
47
 
 
48
        *result = *arg + 1.0;
 
49
 
 
50
        return result;
 
51
}
 
52
 
 
53
Point *
 
54
makepoint(Point *pointx, Point *pointy)
 
55
{
 
56
        Point      *new_point = (Point *) palloc(sizeof(Point));
 
57
 
 
58
        new_point->x = pointx->x;
 
59
        new_point->y = pointy->y;
 
60
 
 
61
        return new_point;
 
62
}
 
63
 
 
64
/* By Reference, Variable Length */
 
65
 
 
66
text *
 
67
copytext(text *t)
 
68
{
 
69
        /*
 
70
         * VARSIZE is the total size of the struct in bytes.
 
71
         */
 
72
        text       *new_t = (text *) palloc(VARSIZE(t));
 
73
 
 
74
        SET_VARSIZE(new_t, VARSIZE(t));
 
75
 
 
76
        /*
 
77
         * VARDATA is a pointer to the data region of the struct.
 
78
         */
 
79
        memcpy((void *) VARDATA(new_t),         /* destination */
 
80
                   (void *) VARDATA(t), /* source */
 
81
                   VARSIZE(t) - VARHDRSZ);              /* how many bytes */
 
82
        return new_t;
 
83
}
 
84
 
 
85
text *
 
86
concat_text(text *arg1, text *arg2)
 
87
{
 
88
        int32           arg1_size = VARSIZE(arg1) - VARHDRSZ;
 
89
        int32           arg2_size = VARSIZE(arg2) - VARHDRSZ;
 
90
        int32           new_text_size = arg1_size + arg2_size + VARHDRSZ;
 
91
        text       *new_text = (text *) palloc(new_text_size);
 
92
 
 
93
        SET_VARSIZE(new_text, new_text_size);
 
94
        memcpy(VARDATA(new_text), VARDATA(arg1), arg1_size);
 
95
        memcpy(VARDATA(new_text) + arg1_size, VARDATA(arg2), arg2_size);
 
96
        return new_text;
 
97
}
 
98
 
 
99
/* Composite types */
 
100
 
 
101
bool
 
102
c_overpaid(HeapTupleHeader t,   /* the current instance of EMP */
 
103
                   int32 limit)
 
104
{
 
105
        bool            isnull;
 
106
        int32           salary;
 
107
 
 
108
        salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
 
109
        if (isnull)
 
110
                return false;
 
111
        return salary > limit;
 
112
}