1 /******************************************************************************
2 This file contains routines that can be bound to a Postgres backend and
3 called by the backend in the process of processing queries. The calling
4 format for these routines is dictated by Postgres architecture.
5 ******************************************************************************/
15 /* These prototypes declare the requirements that Postgres places on these
16 user written functions.
18 Complex *complex_in(char *str);
19 char *complex_out(Complex * complex);
20 Complex *complex_add(Complex * a, Complex * b);
21 bool complex_abs_lt(Complex * a, Complex * b);
22 bool complex_abs_le(Complex * a, Complex * b);
23 bool complex_abs_eq(Complex * a, Complex * b);
24 bool complex_abs_ge(Complex * a, Complex * b);
25 bool complex_abs_gt(Complex * a, Complex * b);
26 int4 complex_abs_cmp(Complex * a, Complex * b);
29 /*****************************************************************************
30 * Input/Output functions
31 *****************************************************************************/
40 if (sscanf(str, " ( %lf , %lf )", &x, &y) != 2)
42 elog(ERROR, "complex_in: error in parsing \"%s\"", str);
45 result = (Complex *) palloc(sizeof(Complex));
52 * You might have noticed a slight inconsistency between the following
53 * declaration and the SQL definition:
54 * CREATE FUNCTION complex_out(opaque) RETURNS opaque ...
55 * The reason is that the argument pass into complex_out is really just a
56 * pointer. POSTGRES thinks all output functions are:
57 * char *out_func(char *);
60 complex_out(Complex * complex)
67 result = (char *) palloc(60);
68 sprintf(result, "(%g,%g)", complex->x, complex->y);
72 /*****************************************************************************
74 *****************************************************************************/
77 complex_add(Complex * a, Complex * b)
81 result = (Complex *) palloc(sizeof(Complex));
82 result->x = a->x + b->x;
83 result->y = a->y + b->y;
88 /*****************************************************************************
89 * Operator class for defining B-tree index
90 *****************************************************************************/
92 #define Mag(c) ((c)->x*(c)->x + (c)->y*(c)->y)
95 complex_abs_lt(Complex * a, Complex * b)
104 complex_abs_le(Complex * a, Complex * b)
106 double amag = Mag(a),
113 complex_abs_eq(Complex * a, Complex * b)
115 double amag = Mag(a),
122 complex_abs_ge(Complex * a, Complex * b)
124 double amag = Mag(a),
131 complex_abs_gt(Complex * a, Complex * b)
133 double amag = Mag(a),
140 complex_abs_cmp(Complex * a, Complex * b)
142 double amag = Mag(a),
147 else if (amag > bmag)
153 /*****************************************************************************
155 *****************************************************************************/
158 * You should always test your code separately. Trust me, using POSTGRES to
159 * debug your C function will be very painful and unproductive. In case of
160 * POSTGRES crashing, it is impossible to tell whether the bug is in your
161 * code or POSTGRES's.
163 void test_main(void);
170 a = complex_in("(4.01, 3.77 )");
171 printf("a = %s\n", complex_out(a));
172 b = complex_in("(1.0,2.0)");
173 printf("b = %s\n", complex_out(b));
174 printf("a + b = %s\n", complex_out(complex_add(a, b)));
175 printf("a < b = %d\n", complex_abs_lt(a, b));
176 printf("a <= b = %d\n", complex_abs_le(a, b));
177 printf("a = b = %d\n", complex_abs_eq(a, b));
178 printf("a >= b = %d\n", complex_abs_ge(a, b));
179 printf("a > b = %d\n", complex_abs_gt(a, b));