-/* $PostgreSQL: pgsql/src/tutorial/funcs_new.c,v 1.8 2006/05/30 22:12:16 tgl Exp $ */
+/* src/tutorial/funcs_new.c */
/******************************************************************************
These are user-defined functions that can be bound to a Postgres backend
#include "postgres.h" /* general Postgres declarations */
-#include "fmgr.h" /* for argument/result macros */
#include "executor/executor.h" /* for GetAttributeByName() */
#include "utils/geo_decls.h" /* for point type */
-
PG_MODULE_MAGIC;
-/* These prototypes just prevent possible warnings from gcc. */
-
-Datum add_one(PG_FUNCTION_ARGS);
-Datum add_one_float8(PG_FUNCTION_ARGS);
-Datum makepoint(PG_FUNCTION_ARGS);
-Datum copytext(PG_FUNCTION_ARGS);
-Datum concat_text(PG_FUNCTION_ARGS);
-Datum c_overpaid(PG_FUNCTION_ARGS);
-
/* By Value */
Datum
copytext(PG_FUNCTION_ARGS)
{
- text *t = PG_GETARG_TEXT_P(0);
+ text *t = PG_GETARG_TEXT_PP(0);
/*
- * VARSIZE is the total size of the struct in bytes.
+ * 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.
*/
- text *new_t = (text *) palloc(VARSIZE(t));
+ text *new_t = (text *) palloc(VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
- VARATT_SIZEP(new_t) = VARSIZE(t);
+ SET_VARSIZE(new_t, VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
/*
- * VARDATA is a pointer to the data region of the struct.
+ * 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.
*/
- memcpy((void *) VARDATA(new_t), /* destination */
- (void *) VARDATA(t), /* source */
- VARSIZE(t) - VARHDRSZ); /* how many bytes */
+ memcpy((void *) VARDATA(new_t), /* destination */
+ (void *) VARDATA_ANY(t), /* source */
+ VARSIZE_ANY_EXHDR(t)); /* how many bytes */
PG_RETURN_TEXT_P(new_t);
}
Datum
concat_text(PG_FUNCTION_ARGS)
{
- text *arg1 = PG_GETARG_TEXT_P(0);
- text *arg2 = PG_GETARG_TEXT_P(1);
- int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
+ 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);
+ int32 new_text_size = arg1_size + arg2_size + VARHDRSZ;
text *new_text = (text *) palloc(new_text_size);
- memset((void *) new_text, 0, new_text_size);
- VARATT_SIZEP(new_text) = new_text_size;
- strncpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1) - VARHDRSZ);
- strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2) - VARHDRSZ);
+ SET_VARSIZE(new_text, new_text_size);
+ 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);
}