Hello
2014-05-27 20:30 GMT+02:00 Paul Jones <p...@cmicdo.com>: > I have written a user-defined type that allows direct import and printing > of > DB2 timestamps.It does correctly import and export DB2 timestamps, > butI'm wondering ifsomeone could tell me if I made anymistakes in > the C code, particularly w.r.t. memory leaks or non-portableconstructs. > > > I'm doing this on 9.3.4. > > Thanks, > PJ > > > ------------------ SQL --------------------------- > > > CREATE TYPE db2tstz; > > CREATE FUNCTION db2tstzin(cstring) RETURNS db2tstz > AS '/home/paul/src/pgproj/types/db2tstz.so', 'db2tstzin' > LANGUAGE C STRICT; > > CREATE FUNCTION db2tstzout(db2tstz) RETURNS cstring > AS '/home/paul/src/pgproj/types/db2tstz.so', 'db2tstzout' > LANGUAGE C STRICT; > > CREATE TYPE db2tstz ( > INPUT = db2tstzin, > OUTPUT = db2tstzout, > LIKE = timestamptz > ); > > CREATE TABLE foo > ( > aaa INT, > bbb DB2TSTZ > ); > > > INSERT INTO foo VALUES (1, '2013-10-03-17.22.18.823581'), > (2, '2010-04-06-13.16.47.174372'); > > ---------------- C Code ---------------- > > #include "postgres.h" > #include <string.h> > #include "fmgr.h" > #include "utils/formatting.h" > #include "utils/datetime.h" > > #ifdef PG_MODULE_MAGIC > PG_MODULE_MAGIC; > #endif > > /* by value */ > > PG_FUNCTION_INFO_V1(db2tstzin); > > Datum > db2tstzin(PG_FUNCTION_ARGS) > { > char *date_txt = PG_GETARG_CSTRING(0); > Timestamp tsp; > > tsp = DirectFunctionCall2(to_timestamp, > cstring_to_text(date_txt), > cstring_to_text("YYYY-MM-DD-HH24.MI.SS.US")); > > PG_RETURN_DATUM(tsp); > } > > PG_FUNCTION_INFO_V1(db2tstzout); > > There is one issue DirectFunctionCall takes a parameters converted to Datum and returns Datum You should to use a macros XGetDatum and DatumGetX In this case newDate = DatumGetTimestamp(DirectFunctionCall2(to_timestamp, CStringGetDatum(date_txt), CStringGetDatum(cstring_to_text(nls_date_format)))); PG_RETURN_TIMESTAMP(newDate); There is inconsistency in types - Timestamp and Timestamptz - > Datum > db2tstzout(PG_FUNCTION_ARGS) > { > TimestampTz tsp = PG_GETARG_TIMESTAMPTZ(0); > text *result; > > result = (text *)DirectFunctionCall2(timestamp_to_char, > (int)&tsp, > cstring_to_text("YYYY-MM-DD-HH24.MI.SS.US")); > > PG_RETURN_CSTRING(text_to_cstring(result)); > } > > Regards Pavel > > -- > Sent via pgsql-general mailing list (pgsql-general@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-general >