From f1ecbb08e466a1091bc3df1341460c8ff94ba51d Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Mon, 21 Jan 2013 16:14:25 -0500 Subject: [PATCH] Tie hexkbw handling to in/out functions --- libpc/pc_api.h | 2 +- libpc/pc_point.c | 4 ++-- pgsql/pc_inout.c | 18 +++++++++++++++++- pgsql/pc_pgsql.c | 12 +++++++++++- pgsql/pc_pgsql.h | 4 ++-- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/libpc/pc_api.h b/libpc/pc_api.h index 8c10dc7..d5e3606 100644 --- a/libpc/pc_api.h +++ b/libpc/pc_api.h @@ -222,7 +222,7 @@ uint32_t pc_schema_is_valid(const PCSCHEMA *s); /** Create a new PCPOINT */ PCPOINT* pc_point_make(const PCSCHEMA *s); /** Create a new readonly PCPOINT on top of a data buffer */ -PCPOINT* pc_point_from_data(const PCSCHEMA *s, uint8_t *data); +PCPOINT* pc_point_from_data(const PCSCHEMA *s, const uint8_t *data); /** Create a new read/write PCPOINT on top of a data buffer */ PCPOINT* pc_point_from_data_rw(const PCSCHEMA *s, uint8_t *data); /** Create a new read/write PCPOINT from a double array */ diff --git a/libpc/pc_point.c b/libpc/pc_point.c index f55e1c9..790fe3a 100644 --- a/libpc/pc_point.c +++ b/libpc/pc_point.c @@ -42,7 +42,7 @@ pc_point_make(const PCSCHEMA *s) }; PCPOINT * -pc_point_from_data(const PCSCHEMA *s, uint8_t *data) +pc_point_from_data(const PCSCHEMA *s, const uint8_t *data) { size_t sz; PCPOINT *pt; @@ -56,7 +56,7 @@ pc_point_from_data(const PCSCHEMA *s, uint8_t *data) /* Reference the external data */ pt = pcalloc(sizeof(PCPOINT)); - pt->data = data; + pt->data = (uint8_t*)data; /* Set up basic info */ pt->schema = s; diff --git a/pgsql/pc_inout.c b/pgsql/pc_inout.c index fa4d3ed..a51369e 100644 --- a/pgsql/pc_inout.c +++ b/pgsql/pc_inout.c @@ -27,7 +27,7 @@ Datum pcpoint_in(PG_FUNCTION_ARGS) /* Empty string. */ if ( str[0] == '\0' ) { - ereport(ERROR,(errmsg("parse error - invalid pcpoint"))); + ereport(ERROR,(errmsg("pcpoint parse error - empty string"))); } /* Binary or text form? Let's find out. */ @@ -46,6 +46,22 @@ Datum pcpoint_in(PG_FUNCTION_ARGS) PG_RETURN_POINTER(serpt); } +PG_FUNCTION_INFO_V1(pcpoint_out); +Datum pcpoint_out(PG_FUNCTION_ARGS) +{ + PCPOINT *pcpt = NULL; + SERIALIZED_POINT *serpt = NULL; + uint8_t *wkb = NULL; + size_t wkb_size = 0; + char *hexwkb = NULL; + + serpt = (SERIALIZED_POINT*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + pcpt = pc_point_deserialize(serpt); + wkb = wkb_from_point(pcpt, &wkb_size); + hexwkb = hexbytes_from_bytes(wkb, wkb_size); + PG_RETURN_CSTRING(hexwkb); +} + PG_FUNCTION_INFO_V1(PC_SchemaIsValid); Datum PC_SchemaIsValid(PG_FUNCTION_ARGS) diff --git a/pgsql/pc_pgsql.c b/pgsql/pc_pgsql.c index f7bdb19..7937788 100644 --- a/pgsql/pc_pgsql.c +++ b/pgsql/pc_pgsql.c @@ -379,7 +379,7 @@ pc_schema_get_by_id(uint32_t pcid) */ SERIALIZED_POINT * -pc_point_serialize(PCPOINT *pcpt) +pc_point_serialize(const PCPOINT *pcpt) { size_t serpt_size = sizeof(SERIALIZED_POINT) - 1 + pcpt->schema->size; SERIALIZED_POINT *serpt = palloc(serpt_size); @@ -389,3 +389,13 @@ pc_point_serialize(PCPOINT *pcpt) return serpt; } +PCPOINT * +pc_point_deserialize(const SERIALIZED_POINT *serpt) +{ + PCPOINT *pcpt; + PCSCHEMA *schema = pc_schema_get_by_id(serpt->pcid); + pcpt = pc_point_from_data(schema, serpt->data); + return pcpt; +} + + diff --git a/pgsql/pc_pgsql.h b/pgsql/pc_pgsql.h index 8967565..81fb611 100644 --- a/pgsql/pc_pgsql.h +++ b/pgsql/pc_pgsql.h @@ -36,10 +36,10 @@ SERIALIZED_PATCH; PCSCHEMA* pc_schema_get_by_id(uint32_t pcid); /** Turn a PCPOINT into a byte buffer suitable for saving in PgSQL */ -SERIALIZED_POINT* pc_point_serialize(PCPOINT *pcpt); +SERIALIZED_POINT* pc_point_serialize(const PCPOINT *pcpt); /** Turn a byte buffer into a PCPOINT for processing */ -PCPOINT* pc_point_deserialize(SERIALIZED_POINT *serpt); +PCPOINT* pc_point_deserialize(const SERIALIZED_POINT *serpt); /** Returns 1 for little (NDR) and 0 for big (XDR) */ char machine_endian(void);