mirror of https://github.com/postgres/postgres
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.9 KiB
82 lines
1.9 KiB
|
29 years ago
|
/******************************************************************************
|
||
|
|
These are user-defined functions that can be bound to a Postgres backend
|
||
|
|
and called by Postgres to execute SQL functions of the same name.
|
||
|
|
|
||
|
|
The calling format for these functions is defined by the CREATE FUNCTION
|
||
|
|
SQL statement that binds them to the backend.
|
||
|
|
*****************************************************************************/
|
||
|
|
|
||
|
30 years ago
|
#include <string.h>
|
||
|
|
#include <stdio.h>
|
||
|
29 years ago
|
#include "postgres.h" /* for char16, etc. */
|
||
|
|
#include "utils/palloc.h" /* for palloc */
|
||
|
|
#include "libpq-fe.h" /* for TUPLE */
|
||
|
|
#include "executor/executor.h" /* for GetAttributeByName() */
|
||
|
29 years ago
|
|
||
|
29 years ago
|
/* The following prototypes declare what we assume the user declares to
|
||
|
29 years ago
|
Postgres in his CREATE FUNCTION statement.
|
||
|
|
*/
|
||
|
|
|
||
|
29 years ago
|
int add_one(int arg);
|
||
|
29 years ago
|
char16 *concat16(char16 *arg1, char16 *arg2);
|
||
|
|
text *copytext(text *t);
|
||
|
29 years ago
|
|
||
|
29 years ago
|
bool
|
||
|
|
c_overpaid(TUPLE t, /* the current instance of EMP */
|
||
|
|
int4 limit);
|
||
|
29 years ago
|
|
||
|
|
|
||
|
30 years ago
|
|
||
|
|
int
|
||
|
|
add_one(int arg)
|
||
|
|
{
|
||
|
29 years ago
|
return (arg + 1);
|
||
|
30 years ago
|
}
|
||
|
|
|
||
|
28 years ago
|
char16 *
|
||
|
29 years ago
|
concat16(char16 *arg1, char16 *arg2)
|
||
|
30 years ago
|
{
|
||
|
29 years ago
|
char16 *new_c16 = (char16 *) palloc(sizeof(char16));
|
||
|
30 years ago
|
|
||
|
29 years ago
|
MemSet(new_c16, 0, sizeof(char16));
|
||
|
29 years ago
|
strncpy((char *) new_c16, (char *) arg1, 16);
|
||
|
|
return (char16 *) (strncat((char *) new_c16, (char *) arg2, 16));
|
||
|
30 years ago
|
}
|
||
|
|
|
||
|
28 years ago
|
text *
|
||
|
29 years ago
|
copytext(text *t)
|
||
|
30 years ago
|
{
|
||
|
29 years ago
|
|
||
|
|
/*
|
||
|
|
* VARSIZE is the total size of the struct in bytes.
|
||
|
|
*/
|
||
|
29 years ago
|
text *new_t = (text *) palloc(VARSIZE(t));
|
||
|
29 years ago
|
|
||
|
29 years ago
|
MemSet(new_t, 0, VARSIZE(t));
|
||
|
29 years ago
|
|
||
|
|
VARSIZE(new_t) = VARSIZE(t);
|
||
|
|
|
||
|
|
/*
|
||
|
|
* VARDATA is a pointer to the data region of the struct.
|
||
|
|
*/
|
||
|
|
memcpy((void *) VARDATA(new_t), /* destination */
|
||
|
|
(void *) VARDATA(t), /* source */
|
||
|
|
VARSIZE(t) - VARHDRSZ); /* how many bytes */
|
||
|
|
|
||
|
|
return (new_t);
|
||
|
30 years ago
|
}
|
||
|
|
|
||
|
|
bool
|
||
|
29 years ago
|
c_overpaid(TUPLE t, /* the current instance of EMP */
|
||
|
|
int4 limit)
|
||
|
30 years ago
|
{
|
||
|
29 years ago
|
bool isnull = false;
|
||
|
|
int4 salary;
|
||
|
30 years ago
|
|
||
|
29 years ago
|
salary = (int4) GetAttributeByName(t, "salary", &isnull);
|
||
|
30 years ago
|
|
||
|
29 years ago
|
if (isnull)
|
||
|
|
return (false);
|
||
|
|
return (salary > limit);
|
||
|
30 years ago
|
}
|