From: | Paul Jones <pbj(at)cmicdo(dot)com> |
---|---|
To: | "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org> |
Subject: | Code for user-defined type |
Date: | 2014-05-27 18:30:46 |
Message-ID: | 1401215446.45948.YahooMailNeo@web161702.mail.bf1.yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
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);
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));
}
From | Date | Subject | |
---|---|---|---|
Next Message | Vincent de Phily | 2014-05-27 18:31:38 | Re: Downtime-free 'alter table set tablespace' |
Previous Message | Louis Battuello | 2014-05-27 15:50:46 | Re: "Interrupt requested" in postgresql-DAY.log |