2010-09-20 22:08:53 +02:00
|
|
|
/* src/tutorial/funcs.c */
|
2006-03-11 05:38:42 +01:00
|
|
|
|
1997-01-05 22:20:34 +01:00
|
|
|
/******************************************************************************
|
|
|
|
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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2000-08-25 01:32:43 +02:00
|
|
|
#include "postgres.h" /* general Postgres declarations */
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#include "executor/executor.h" /* for GetAttributeByName() */
|
1999-03-14 16:22:15 +01:00
|
|
|
#include "utils/geo_decls.h" /* for point type */
|
1997-01-05 22:20:34 +01:00
|
|
|
|
2006-05-31 22:58:09 +02:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-08-25 01:32:43 +02:00
|
|
|
/* By Value */
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_FUNCTION_INFO_V1(add_one);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
add_one(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2019-11-22 13:08:49 +01:00
|
|
|
int32 arg = PG_GETARG_INT32(0);
|
|
|
|
|
|
|
|
PG_RETURN_INT32(arg + 1);
|
2000-08-25 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* By Reference, Fixed Length */
|
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_FUNCTION_INFO_V1(add_one_float8);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
Datum
|
|
|
|
add_one_float8(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* The macros for FLOAT8 hide its pass-by-reference nature */
|
|
|
|
float8 arg = PG_GETARG_FLOAT8(0);
|
2000-08-25 01:32:43 +02:00
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_RETURN_FLOAT8(arg + 1.0);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_FUNCTION_INFO_V1(makepoint);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
makepoint(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2019-11-22 13:08:49 +01:00
|
|
|
Point *pointx = PG_GETARG_POINT_P(0);
|
|
|
|
Point *pointy = PG_GETARG_POINT_P(1);
|
2001-03-22 05:01:46 +01:00
|
|
|
Point *new_point = (Point *) palloc(sizeof(Point));
|
|
|
|
|
|
|
|
new_point->x = pointx->x;
|
|
|
|
new_point->y = pointy->y;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_RETURN_POINT_P(new_point);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-08-25 01:32:43 +02:00
|
|
|
/* By Reference, Variable Length */
|
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_FUNCTION_INFO_V1(copytext);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
copytext(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2019-11-22 13:08:49 +01:00
|
|
|
text *t = PG_GETARG_TEXT_PP(0);
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2019-11-22 13:08:49 +01:00
|
|
|
* VARSIZE_ANY_EXHDR is the size of the struct in bytes, minus the
|
|
|
|
* VARHDRSZ or VARHDRSZ_SHORT of its header. Construct the copy with a
|
|
|
|
* full-length header.
|
2001-03-22 05:01:46 +01:00
|
|
|
*/
|
2019-11-22 13:08:49 +01:00
|
|
|
text *new_t = (text *) palloc(VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
SET_VARSIZE(new_t, VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-22 13:08:49 +01:00
|
|
|
* VARDATA is a pointer to the data region of the new struct. The source
|
|
|
|
* could be a short datum, so retrieve its data through VARDATA_ANY.
|
2001-03-22 05:01:46 +01:00
|
|
|
*/
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
memcpy((void *) VARDATA(new_t), /* destination */
|
2019-11-22 13:08:49 +01:00
|
|
|
(void *) VARDATA_ANY(t), /* source */
|
|
|
|
VARSIZE_ANY_EXHDR(t)); /* how many bytes */
|
|
|
|
PG_RETURN_TEXT_P(new_t);
|
2000-08-25 01:32:43 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_FUNCTION_INFO_V1(concat_text);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
concat_text(PG_FUNCTION_ARGS)
|
2000-08-25 01:32:43 +02:00
|
|
|
{
|
2019-11-22 13:08:49 +01:00
|
|
|
text *arg1 = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *arg2 = PG_GETARG_TEXT_PP(1);
|
|
|
|
int32 arg1_size = VARSIZE_ANY_EXHDR(arg1);
|
|
|
|
int32 arg2_size = VARSIZE_ANY_EXHDR(arg2);
|
2006-09-27 18:19:42 +02:00
|
|
|
int32 new_text_size = arg1_size + arg2_size + VARHDRSZ;
|
2001-03-22 05:01:46 +01:00
|
|
|
text *new_text = (text *) palloc(new_text_size);
|
|
|
|
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(new_text, new_text_size);
|
2019-11-22 13:08:49 +01:00
|
|
|
memcpy(VARDATA(new_text), VARDATA_ANY(arg1), arg1_size);
|
|
|
|
memcpy(VARDATA(new_text) + arg1_size, VARDATA_ANY(arg2), arg2_size);
|
|
|
|
PG_RETURN_TEXT_P(new_text);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-08-25 01:32:43 +02:00
|
|
|
/* Composite types */
|
|
|
|
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_FUNCTION_INFO_V1(c_overpaid);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
c_overpaid(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2019-11-22 13:08:49 +01:00
|
|
|
HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
|
|
|
|
int32 limit = PG_GETARG_INT32(1);
|
2001-03-22 05:01:46 +01:00
|
|
|
bool isnull;
|
|
|
|
int32 salary;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
|
|
|
|
if (isnull)
|
2019-11-22 13:08:49 +01:00
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Alternatively, we might prefer to do PG_RETURN_NULL() for null salary
|
|
|
|
*/
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(salary > limit);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|