]> Untitled Git - articles.git/blob
2e6074381b42af6b9b876dc715093967384efd9a
[articles.git] /
1 #include "postgres.h"
2 #include "fmgr.h"
3
4 PG_FUNCTION_INFO_V1(my_factorial);
5
6 Datum
7 my_factorial(PG_FUNCTION_ARGS)
8 {
9         int32 res, n = PG_GETARG_INT32(0);
10         Datum d;
11
12         if (n < 0)
13                 elog(ERROR, "Так нельзя!");
14
15         if (n == 0)
16                 PG_RETURN_INT32(1);
17
18         d = DirectFunctionCall1(my_factorial, Int32GetDatum(n - 1));
19
20         res = DatumGetInt32(d) * n;
21
22         PG_RETURN_INT32(res);
23 }
24