-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_hello.c
More file actions
45 lines (35 loc) · 990 Bytes
/
Copy pathpg_hello.c
File metadata and controls
45 lines (35 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "postgres.h"
#include "fmgr.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
Datum hello( PG_FUNCTION_ARGS );
PG_FUNCTION_INFO_V1( hello );
Datum
hello( PG_FUNCTION_ARGS )
{
// variable declarations
char greet[] = "Hello, ";
text *towhom;
int greetlen;
int towhomlen;
text *greeting;
// Get arguments. If we declare our function as STRICT, then
// this check is superfluous.
if( PG_ARGISNULL(0) ) {
PG_RETURN_NULL();
}
towhom = PG_GETARG_TEXT_P(0);
// Calculate string sizes.
greetlen = strlen(greet);
towhomlen = VARSIZE(towhom) - VARHDRSZ;
// Allocate memory and set data structure size.
greeting = (text *)palloc( greetlen + towhomlen );
SET_VARSIZE(greeting, greetlen + towhomlen + VARHDRSZ);
// Construct greeting string.
strncpy( VARDATA(greeting), greet, greetlen );
strncpy( VARDATA(greeting) + greetlen,
VARDATA(towhom),
towhomlen );
PG_RETURN_TEXT_P( greeting );
}