1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-10 17:42:29 +03:00

Add binary I/O routines for a bunch more datatypes. Still a few to go,

but that was enough tedium for one day.  Along the way, move the few
support routines for types xid and cid into a more logical place.
This commit is contained in:
Tom Lane
2003-05-12 23:08:52 +00:00
parent b02832719c
commit 30f609484d
25 changed files with 1133 additions and 216 deletions

View File

@@ -1,7 +1,7 @@
#
# Makefile for utils/adt
#
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.54 2003/04/08 23:20:02 tgl Exp $
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.55 2003/05/12 23:08:50 tgl Exp $
#
subdir = src/backend/utils/adt
@@ -21,7 +21,7 @@ OBJS = acl.o arrayfuncs.o array_userfuncs.o arrayutils.o bool.o \
misc.o nabstime.o name.o not_in.o numeric.o numutils.o \
oid.o oracle_compat.o pseudotypes.o \
regexp.o regproc.o ruleutils.o selfuncs.o sets.o \
tid.o timestamp.o varbit.o varchar.o varlena.o version.o \
tid.o timestamp.o varbit.o varchar.o varlena.o version.o xid.o \
network.o mac.o inet_net_ntop.o inet_net_pton.o \
ri_triggers.o pg_lzcompress.o pg_locale.o formatting.o \
ascii.o quote.o pgstatfuncs.o encode.o

View File

@@ -8,13 +8,14 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/bool.c,v 1.26 2002/06/20 20:29:36 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/bool.c,v 1.27 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
/*****************************************************************************
@@ -94,6 +95,36 @@ boolout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* boolrecv - converts external binary format to bool
*
* The external representation is one byte. Any nonzero value is taken
* as "true".
*/
Datum
boolrecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
int ext;
ext = pq_getmsgbyte(buf);
PG_RETURN_BOOL((ext != 0) ? true : false);
}
/*
* boolsend - converts bool to binary format
*/
Datum
boolsend(PG_FUNCTION_ARGS)
{
bool arg1 = PG_GETARG_BOOL(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendbyte(&buf, arg1 ? 1 : 0);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/*****************************************************************************
* PUBLIC ROUTINES *

View File

@@ -1,20 +1,21 @@
/*-------------------------------------------------------------------------
*
* char.c
* Functions for the built-in type "char".
* Functions for the built-in type "cid" (what's that doing here?)
* Functions for the built-in type "char" (not to be confused with
* bpchar, which is the SQL CHAR(n) type).
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.34 2003/03/11 21:01:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/char.c,v 1.35 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
/*****************************************************************************
@@ -52,6 +53,35 @@ charout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* charrecv - converts external binary format to char
*
* The external representation is one byte, with no character set
* conversion. This is somewhat dubious, perhaps, but in many
* cases people use char for a 1-byte binary type.
*/
Datum
charrecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_CHAR(pq_getmsgbyte(buf));
}
/*
* charsend - converts char to binary format
*/
Datum
charsend(PG_FUNCTION_ARGS)
{
char arg1 = PG_GETARG_CHAR(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendbyte(&buf, arg1);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
@@ -196,51 +226,3 @@ char_text(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(result);
}
/*****************************************************************************
* USER I/O ROUTINES *
*****************************************************************************/
/*
* cidin - converts CommandId to internal representation.
*/
Datum
cidin(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);
CommandId c;
c = atoi(s);
/* XXX assume that CommandId is 32 bits... */
PG_RETURN_INT32((int32) c);
}
/*
* cidout - converts a cid to external representation.
*/
Datum
cidout(PG_FUNCTION_ARGS)
{
/* XXX assume that CommandId is 32 bits... */
CommandId c = PG_GETARG_INT32(0);
char *result = (char *) palloc(16);
sprintf(result, "%u", (unsigned int) c);
PG_RETURN_CSTRING(result);
}
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/
Datum
cideq(PG_FUNCTION_ARGS)
{
/* XXX assume that CommandId is 32 bits... */
CommandId arg1 = PG_GETARG_INT32(0);
CommandId arg2 = PG_GETARG_INT32(1);
PG_RETURN_BOOL(arg1 == arg2);
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.81 2003/04/08 17:02:04 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.82 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -21,6 +21,7 @@
#include <float.h>
#include "access/hash.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/date.h"
@@ -117,6 +118,32 @@ date_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* date_recv - converts external binary format to date
*/
Datum
date_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_DATEADT((DateADT) pq_getmsgint(buf, sizeof(DateADT)));
}
/*
* date_send - converts date to binary format
*/
Datum
date_send(PG_FUNCTION_ARGS)
{
DateADT date = PG_GETARG_DATEADT(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, date, sizeof(date));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
Datum
date_eq(PG_FUNCTION_ARGS)
{
@@ -594,6 +621,43 @@ time_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* time_recv - converts external binary format to time
*
* We make no attempt to provide compatibility between int and float
* time representations ...
*/
Datum
time_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
#ifdef HAVE_INT64_TIMESTAMP
PG_RETURN_TIMEADT((TimeADT) pq_getmsgint64(buf));
#else
PG_RETURN_TIMEADT((TimeADT) pq_getmsgfloat8(buf));
#endif
}
/*
* time_send - converts time to binary format
*/
Datum
time_send(PG_FUNCTION_ARGS)
{
TimeADT time = PG_GETARG_TIMEADT(0);
StringInfoData buf;
pq_begintypsend(&buf);
#ifdef HAVE_INT64_TIMESTAMP
pq_sendint64(&buf, time);
#else
pq_sendfloat8(&buf, time);
#endif
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* time_scale()
* Adjust time type for specified scale factor.
* Used by PostgreSQL type system to stuff columns.
@@ -1349,6 +1413,47 @@ timetz_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* timetz_recv - converts external binary format to timetz
*/
Datum
timetz_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
TimeTzADT *time;
time = (TimeTzADT *) palloc(sizeof(TimeTzADT));
#ifdef HAVE_INT64_TIMESTAMP
time->time = pq_getmsgint64(buf);
#else
time->time = pq_getmsgfloat8(buf);
#endif
time->zone = pq_getmsgint(buf, sizeof(time->zone));
PG_RETURN_TIMETZADT_P(time);
}
/*
* timetz_send - converts timetz to binary format
*/
Datum
timetz_send(PG_FUNCTION_ARGS)
{
TimeTzADT *time = PG_GETARG_TIMETZADT_P(0);
StringInfoData buf;
pq_begintypsend(&buf);
#ifdef HAVE_INT64_TIMESTAMP
pq_sendint64(&buf, time->time);
#else
pq_sendfloat8(&buf, time->time);
#endif
pq_sendint(&buf, time->zone, sizeof(time->zone));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* timetz2tm()
* Convert TIME WITH TIME ZONE data type to POSIX time structure.
*/

View File

@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.107 2003/05/04 04:30:15 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.108 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -23,6 +23,7 @@
#include <limits.h>
#include "access/xact.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "utils/builtins.h"
@@ -347,11 +348,11 @@ tm2abstime(struct tm * tm, int tz)
}
/* nabstimein()
/* abstimein()
* Decode date/time string and return abstime.
*/
Datum
nabstimein(PG_FUNCTION_ARGS)
abstimein(PG_FUNCTION_ARGS)
{
char *str = PG_GETARG_CSTRING(0);
AbsoluteTime result;
@@ -409,11 +410,11 @@ nabstimein(PG_FUNCTION_ARGS)
}
/* nabstimeout()
/* abstimeout()
* Given an AbsoluteTime return the English text version of the date
*/
Datum
nabstimeout(PG_FUNCTION_ARGS)
abstimeout(PG_FUNCTION_ARGS)
{
AbsoluteTime time = PG_GETARG_ABSOLUTETIME(0);
char *result;
@@ -450,6 +451,31 @@ nabstimeout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* abstimerecv - converts external binary format to abstime
*/
Datum
abstimerecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_ABSOLUTETIME((AbsoluteTime) pq_getmsgint(buf, sizeof(AbsoluteTime)));
}
/*
* abstimesend - converts abstime to binary format
*/
Datum
abstimesend(PG_FUNCTION_ARGS)
{
AbsoluteTime time = PG_GETARG_ABSOLUTETIME(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, time, sizeof(time));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* abstime_finite()
*/
@@ -751,7 +777,6 @@ reltimein(PG_FUNCTION_ARGS)
PG_RETURN_RELATIVETIME(result);
}
/*
* reltimeout - converts the internal format to a reltime string
*/
@@ -771,6 +796,31 @@ reltimeout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* reltimerecv - converts external binary format to reltime
*/
Datum
reltimerecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_RELATIVETIME((RelativeTime) pq_getmsgint(buf, sizeof(RelativeTime)));
}
/*
* reltimesend - converts reltime to binary format
*/
Datum
reltimesend(PG_FUNCTION_ARGS)
{
RelativeTime time = PG_GETARG_RELATIVETIME(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, time, sizeof(time));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
static void
reltime2tm(RelativeTime time, struct tm * tm)
@@ -817,7 +867,6 @@ tintervalin(PG_FUNCTION_ARGS)
/*
* tintervalout - converts an internal interval format to a string
*
*/
Datum
tintervalout(PG_FUNCTION_ARGS)
@@ -832,12 +881,12 @@ tintervalout(PG_FUNCTION_ARGS)
strcat(i_str, INVALID_INTERVAL_STR);
else
{
p = DatumGetCString(DirectFunctionCall1(nabstimeout,
p = DatumGetCString(DirectFunctionCall1(abstimeout,
AbsoluteTimeGetDatum(interval->data[0])));
strcat(i_str, p);
pfree(p);
strcat(i_str, "\" \"");
p = DatumGetCString(DirectFunctionCall1(nabstimeout,
p = DatumGetCString(DirectFunctionCall1(abstimeout,
AbsoluteTimeGetDatum(interval->data[1])));
strcat(i_str, p);
pfree(p);
@@ -846,6 +895,43 @@ tintervalout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(i_str);
}
/*
* tintervalrecv - converts external binary format to tinterval
*/
Datum
tintervalrecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
TimeInterval interval;
interval = (TimeInterval) palloc(sizeof(TimeIntervalData));
interval->status = pq_getmsgint(buf, sizeof(interval->status));
if (!(interval->status == T_INTERVAL_INVAL ||
interval->status == T_INTERVAL_VALID))
elog(ERROR, "Invalid status in external tinterval");
interval->data[0] = pq_getmsgint(buf, sizeof(interval->data[0]));
interval->data[1] = pq_getmsgint(buf, sizeof(interval->data[1]));
PG_RETURN_TIMEINTERVAL(interval);
}
/*
* tintervalsend - converts tinterval to binary format
*/
Datum
tintervalsend(PG_FUNCTION_ARGS)
{
TimeInterval interval = PG_GETARG_TIMEINTERVAL(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, interval->status, sizeof(interval->status));
pq_sendint(&buf, interval->data[0], sizeof(interval->data[0]));
pq_sendint(&buf, interval->data[1], sizeof(interval->data[1]));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/*****************************************************************************
* PUBLIC ROUTINES *
@@ -1558,7 +1644,7 @@ istinterval(char *i_string,
p1++;
}
/* get the first date */
*i_start = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
*i_start = DatumGetAbsoluteTime(DirectFunctionCall1(abstimein,
CStringGetDatum(p)));
/* rechange NULL at the end of the first date to a "'" */
*p1 = '"';
@@ -1586,7 +1672,7 @@ istinterval(char *i_string,
p1++;
}
/* get the second date */
*i_end = DatumGetAbsoluteTime(DirectFunctionCall1(nabstimein,
*i_end = DatumGetAbsoluteTime(DirectFunctionCall1(abstimein,
CStringGetDatum(p)));
/* rechange NULL at the end of the first date to a ''' */
*p1 = '"';

View File

@@ -14,7 +14,7 @@
* Copyright (c) 1998-2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.60 2003/04/21 00:22:24 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.61 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -28,6 +28,7 @@
#include <errno.h>
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/int8.h"
@@ -370,6 +371,79 @@ numeric_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(str);
}
/*
* numeric_recv - converts external binary format to numeric
*
* External format is a sequence of int16's:
* ndigits, weight, sign, dscale, NumericDigits.
*/
Datum
numeric_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
NumericVar value;
Numeric res;
int len,
i;
init_var(&value);
len = (uint16) pq_getmsgint(buf, sizeof(uint16));
if (len < 0 || len > NUMERIC_MAX_PRECISION + NUMERIC_MAX_RESULT_SCALE)
elog(ERROR, "Invalid length in external numeric");
alloc_var(&value, len);
value.weight = (int16) pq_getmsgint(buf, sizeof(int16));
value.sign = (uint16) pq_getmsgint(buf, sizeof(uint16));
if (!(value.sign == NUMERIC_POS ||
value.sign == NUMERIC_NEG ||
value.sign == NUMERIC_NAN))
elog(ERROR, "Invalid sign in external numeric");
value.dscale = (uint16) pq_getmsgint(buf, sizeof(uint16));
for (i = 0; i < len; i++)
{
NumericDigit d = pq_getmsgint(buf, sizeof(NumericDigit));
if (d < 0 || d >= NBASE)
elog(ERROR, "Invalid digit in external numeric");
value.digits[i] = d;
}
res = make_result(&value);
free_var(&value);
PG_RETURN_NUMERIC(res);
}
/*
* numeric_send - converts numeric to binary format
*/
Datum
numeric_send(PG_FUNCTION_ARGS)
{
Numeric num = PG_GETARG_NUMERIC(0);
NumericVar x;
StringInfoData buf;
int i;
init_var(&x);
set_var_from_num(num, &x);
pq_begintypsend(&buf);
pq_sendint(&buf, x.ndigits, sizeof(int16));
pq_sendint(&buf, x.weight, sizeof(int16));
pq_sendint(&buf, x.sign, sizeof(int16));
pq_sendint(&buf, x.dscale, sizeof(int16));
for (i = 0; i < x.ndigits; i++)
pq_sendint(&buf, x.digits[i], sizeof(NumericDigit));
free_var(&x);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/*
* numeric() -

View File

@@ -13,7 +13,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.76 2002/09/18 21:35:23 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/regproc.c,v 1.77 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -200,6 +200,26 @@ regprocout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* regprocrecv - converts external binary format to regproc
*/
Datum
regprocrecv(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidrecv, so share code */
return oidrecv(fcinfo);
}
/*
* regprocsend - converts regproc to binary format
*/
Datum
regprocsend(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidsend, so share code */
return oidsend(fcinfo);
}
/*
* regprocedurein - converts "proname(args)" to proc OID
@@ -343,6 +363,26 @@ regprocedureout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* regprocedurerecv - converts external binary format to regprocedure
*/
Datum
regprocedurerecv(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidrecv, so share code */
return oidrecv(fcinfo);
}
/*
* regproceduresend - converts regprocedure to binary format
*/
Datum
regproceduresend(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidsend, so share code */
return oidsend(fcinfo);
}
/*
* regoperin - converts "oprname" to operator OID
@@ -506,6 +546,26 @@ regoperout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* regoperrecv - converts external binary format to regoper
*/
Datum
regoperrecv(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidrecv, so share code */
return oidrecv(fcinfo);
}
/*
* regopersend - converts regoper to binary format
*/
Datum
regopersend(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidsend, so share code */
return oidsend(fcinfo);
}
/*
* regoperatorin - converts "oprname(args)" to operator OID
@@ -666,6 +726,26 @@ regoperatorout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* regoperatorrecv - converts external binary format to regoperator
*/
Datum
regoperatorrecv(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidrecv, so share code */
return oidrecv(fcinfo);
}
/*
* regoperatorsend - converts regoperator to binary format
*/
Datum
regoperatorsend(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidsend, so share code */
return oidsend(fcinfo);
}
/*
* regclassin - converts "classname" to class OID
@@ -804,6 +884,26 @@ regclassout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* regclassrecv - converts external binary format to regclass
*/
Datum
regclassrecv(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidrecv, so share code */
return oidrecv(fcinfo);
}
/*
* regclasssend - converts regclass to binary format
*/
Datum
regclasssend(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidsend, so share code */
return oidsend(fcinfo);
}
/*
* regtypein - converts "typename" to type OID
@@ -936,6 +1036,26 @@ regtypeout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* regtyperecv - converts external binary format to regtype
*/
Datum
regtyperecv(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidrecv, so share code */
return oidrecv(fcinfo);
}
/*
* regtypesend - converts regtype to binary format
*/
Datum
regtypesend(PG_FUNCTION_ARGS)
{
/* Exactly the same as oidsend, so share code */
return oidsend(fcinfo);
}
/*
* Given a C string, parse it into a qualified-name list.

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.36 2002/09/04 20:31:29 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/tid.c,v 1.37 2003/05/12 23:08:50 tgl Exp $
*
* NOTES
* input routine largely stolen from boxin().
@@ -24,8 +24,10 @@
#include "access/heapam.h"
#include "catalog/namespace.h"
#include "utils/builtins.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
#define ItemPointerGetDatum(X) PointerGetDatum(X)
@@ -91,13 +93,11 @@ tidout(PG_FUNCTION_ARGS)
BlockNumber blockNumber;
OffsetNumber offsetNumber;
char buf[32];
static char *invalidTid = "()";
if (!ItemPointerIsValid(itemPtr))
PG_RETURN_CSTRING(pstrdup(invalidTid));
PG_RETURN_CSTRING(pstrdup("()"));
blockId = &(itemPtr->ip_blkid);
blockNumber = BlockIdGetBlockNumber(blockId);
offsetNumber = itemPtr->ip_posid;
@@ -106,6 +106,49 @@ tidout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(pstrdup(buf));
}
/*
* tidrecv - converts external binary format to tid
*/
Datum
tidrecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
ItemPointer result;
BlockNumber blockNumber;
OffsetNumber offsetNumber;
blockNumber = pq_getmsgint(buf, sizeof(blockNumber));
offsetNumber = pq_getmsgint(buf, sizeof(offsetNumber));
result = (ItemPointer) palloc(sizeof(ItemPointerData));
ItemPointerSet(result, blockNumber, offsetNumber);
PG_RETURN_ITEMPOINTER(result);
}
/*
* tidsend - converts tid to binary format
*/
Datum
tidsend(PG_FUNCTION_ARGS)
{
ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
BlockId blockId;
BlockNumber blockNumber;
OffsetNumber offsetNumber;
StringInfoData buf;
blockId = &(itemPtr->ip_blkid);
blockNumber = BlockIdGetBlockNumber(blockId);
offsetNumber = itemPtr->ip_posid;
pq_begintypsend(&buf);
pq_sendint(&buf, blockNumber, sizeof(blockNumber));
pq_sendint(&buf, offsetNumber, sizeof(offsetNumber));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/*****************************************************************************
* PUBLIC ROUTINES *
*****************************************************************************/

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.83 2003/04/07 15:04:03 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.84 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -24,6 +24,7 @@
#include "access/hash.h"
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "utils/array.h"
#include "utils/builtins.h"
@@ -142,6 +143,43 @@ timestamp_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* timestamp_recv - converts external binary format to timestamp
*
* We make no attempt to provide compatibility between int and float
* timestamp representations ...
*/
Datum
timestamp_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
#ifdef HAVE_INT64_TIMESTAMP
PG_RETURN_TIMESTAMP((Timestamp) pq_getmsgint64(buf));
#else
PG_RETURN_TIMESTAMP((Timestamp) pq_getmsgfloat8(buf));
#endif
}
/*
* timestamp_send - converts timestamp to binary format
*/
Datum
timestamp_send(PG_FUNCTION_ARGS)
{
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
StringInfoData buf;
pq_begintypsend(&buf);
#ifdef HAVE_INT64_TIMESTAMP
pq_sendint64(&buf, timestamp);
#else
pq_sendfloat8(&buf, timestamp);
#endif
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* timestamp_scale()
* Adjust time type for specified scale factor.
* Used by PostgreSQL type system to stuff columns.
@@ -300,7 +338,7 @@ timestamptz_in(PG_FUNCTION_ARGS)
Datum
timestamptz_out(PG_FUNCTION_ARGS)
{
TimestampTz dt = PG_GETARG_TIMESTAMP(0);
TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0);
char *result;
int tz;
struct tm tt,
@@ -320,6 +358,43 @@ timestamptz_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* timestamptz_recv - converts external binary format to timestamptz
*
* We make no attempt to provide compatibility between int and float
* timestamp representations ...
*/
Datum
timestamptz_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
#ifdef HAVE_INT64_TIMESTAMP
PG_RETURN_TIMESTAMPTZ((TimestampTz) pq_getmsgint64(buf));
#else
PG_RETURN_TIMESTAMPTZ((TimestampTz) pq_getmsgfloat8(buf));
#endif
}
/*
* timestamptz_send - converts timestamptz to binary format
*/
Datum
timestamptz_send(PG_FUNCTION_ARGS)
{
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
StringInfoData buf;
pq_begintypsend(&buf);
#ifdef HAVE_INT64_TIMESTAMP
pq_sendint64(&buf, timestamp);
#else
pq_sendfloat8(&buf, timestamp);
#endif
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* timestamptz_scale()
* Adjust time type for specified scale factor.
* Used by PostgreSQL type system to stuff columns.
@@ -327,7 +402,7 @@ timestamptz_out(PG_FUNCTION_ARGS)
Datum
timestamptz_scale(PG_FUNCTION_ARGS)
{
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
int32 typmod = PG_GETARG_INT32(1);
TimestampTz result;
@@ -423,6 +498,47 @@ interval_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* interval_recv - converts external binary format to interval
*/
Datum
interval_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
Interval *interval;
interval = (Interval *) palloc(sizeof(Interval));
#ifdef HAVE_INT64_TIMESTAMP
interval->time = pq_getmsgint64(buf);
#else
interval->time = pq_getmsgfloat8(buf);
#endif
interval->month = pq_getmsgint(buf, sizeof(interval->month));
PG_RETURN_INTERVAL_P(interval);
}
/*
* interval_send - converts interval to binary format
*/
Datum
interval_send(PG_FUNCTION_ARGS)
{
Interval *interval = PG_GETARG_INTERVAL_P(0);
StringInfoData buf;
pq_begintypsend(&buf);
#ifdef HAVE_INT64_TIMESTAMP
pq_sendint64(&buf, interval->time);
#else
pq_sendfloat8(&buf, interval->time);
#endif
pq_sendint(&buf, interval->month, sizeof(interval->month));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* interval_scale()
* Adjust interval type for specified fields.
* Used by PostgreSQL type system to stuff columns.
@@ -1594,7 +1710,7 @@ timestamp_mi_span(PG_FUNCTION_ARGS)
Datum
timestamptz_pl_span(PG_FUNCTION_ARGS)
{
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
Interval *span = PG_GETARG_INTERVAL_P(1);
TimestampTz result;
int tz;
@@ -1651,7 +1767,7 @@ timestamptz_pl_span(PG_FUNCTION_ARGS)
Datum
timestamptz_mi_span(PG_FUNCTION_ARGS)
{
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
Interval *span = PG_GETARG_INTERVAL_P(1);
Interval tspan;
@@ -2094,8 +2210,8 @@ timestamp_age(PG_FUNCTION_ARGS)
Datum
timestamptz_age(PG_FUNCTION_ARGS)
{
TimestampTz dt1 = PG_GETARG_TIMESTAMP(0);
TimestampTz dt2 = PG_GETARG_TIMESTAMP(1);
TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
Interval *result;
fsec_t fsec,
fsec1,
@@ -2463,7 +2579,7 @@ Datum
timestamptz_trunc(PG_FUNCTION_ARGS)
{
text *units = PG_GETARG_TEXT_P(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMP(1);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
TimestampTz result;
int tz;
int type,
@@ -2918,7 +3034,7 @@ Datum
timestamptz_part(PG_FUNCTION_ARGS)
{
text *units = PG_GETARG_TEXT_P(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMP(1);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
float8 result;
int tz;
int type,
@@ -3349,7 +3465,7 @@ timestamp_timestamptz(PG_FUNCTION_ARGS)
Datum
timestamptz_timestamp(PG_FUNCTION_ARGS)
{
TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
Timestamp result;
struct tm tt,
*tm = &tt;
@@ -3379,7 +3495,7 @@ Datum
timestamptz_zone(PG_FUNCTION_ARGS)
{
text *zone = PG_GETARG_TEXT_P(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMP(1);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
Timestamp result;
int tz;
@@ -3428,7 +3544,7 @@ Datum
timestamptz_izone(PG_FUNCTION_ARGS)
{
Interval *zone = PG_GETARG_INTERVAL_P(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMP(1);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
Timestamp result;
int tz;

View File

@@ -9,7 +9,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.29 2002/11/13 00:39:47 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.30 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -17,6 +17,7 @@
#include "postgres.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "utils/array.h"
#include "utils/fmgroids.h"
#include "utils/memutils.h"
@@ -204,6 +205,26 @@ bit_out(PG_FUNCTION_ARGS)
#endif
}
/*
* bit_recv - converts external binary format to bit
*/
Datum
bit_recv(PG_FUNCTION_ARGS)
{
/* Exactly the same as varbit_recv, so share code */
return varbit_recv(fcinfo);
}
/*
* bit_send - converts bit to binary format
*/
Datum
bit_send(PG_FUNCTION_ARGS)
{
/* Exactly the same as varbit_send, so share code */
return varbit_send(fcinfo);
}
/* bit()
* Converts a bit() type to a specific internal length.
* len is the bitlength specified in the column definition.
@@ -407,6 +428,58 @@ varbit_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* varbit_recv - converts external binary format to varbit
*
* External format is the bitlen as an int32, then the byte array.
*/
Datum
varbit_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
VarBit *result;
int len,
bitlen;
int ipad;
bits8 mask;
bitlen = pq_getmsgint(buf, sizeof(int32));
if (bitlen < 0)
elog(ERROR, "Invalid length in external bit string");
len = VARBITTOTALLEN(bitlen);
result = (VarBit *) palloc(len);
VARATT_SIZEP(result) = len;
VARBITLEN(result) = bitlen;
pq_copymsgbytes(buf, (char *) VARBITS(result), VARBITBYTES(result));
/* Make sure last byte is zero-padded if needed */
ipad = VARBITPAD(result);
if (ipad > 0)
{
mask = BITMASK << ipad;
*(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
}
PG_RETURN_VARBIT_P(result);
}
/*
* varbit_send - converts varbit to binary format
*/
Datum
varbit_send(PG_FUNCTION_ARGS)
{
VarBit *s = PG_GETARG_VARBIT_P(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, VARBITLEN(s), sizeof(int32));
pq_sendbytes(&buf, VARBITS(s), VARBITBYTES(s));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* varbit()
* Converts a varbit() type to a specific internal length.
* len is the maximum bitlength specified in the column definition.

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.95 2002/09/18 21:35:23 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varchar.c,v 1.96 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -163,6 +163,26 @@ bpcharout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* bpcharrecv - converts external binary format to bpchar
*/
Datum
bpcharrecv(PG_FUNCTION_ARGS)
{
/* Exactly the same as textrecv, so share code */
return textrecv(fcinfo);
}
/*
* bpcharsend - converts bpchar to binary format
*/
Datum
bpcharsend(PG_FUNCTION_ARGS)
{
/* Exactly the same as textsend, so share code */
return textsend(fcinfo);
}
/*
* Converts a CHARACTER type to the specified size.
@@ -405,6 +425,26 @@ varcharout(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(result);
}
/*
* varcharrecv - converts external binary format to varchar
*/
Datum
varcharrecv(PG_FUNCTION_ARGS)
{
/* Exactly the same as textrecv, so share code */
return textrecv(fcinfo);
}
/*
* varcharsend - converts varchar to binary format
*/
Datum
varcharsend(PG_FUNCTION_ARGS)
{
/* Exactly the same as textsend, so share code */
return textsend(fcinfo);
}
/*
* Converts a VARCHAR type to the specified size.

169
src/backend/utils/adt/xid.c Normal file
View File

@@ -0,0 +1,169 @@
/*-------------------------------------------------------------------------
*
* xid.c
* POSTGRES transaction identifier and command identifier datatypes.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/xid.c,v 1.1 2003/05/12 23:08:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <limits.h>
#include "access/xact.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#define PG_GETARG_TRANSACTIONID(n) DatumGetTransactionId(PG_GETARG_DATUM(n))
#define PG_RETURN_TRANSACTIONID(x) return TransactionIdGetDatum(x)
#define PG_GETARG_COMMANDID(n) DatumGetCommandId(PG_GETARG_DATUM(n))
#define PG_RETURN_COMMANDID(x) return CommandIdGetDatum(x)
Datum
xidin(PG_FUNCTION_ARGS)
{
char *str = PG_GETARG_CSTRING(0);
PG_RETURN_TRANSACTIONID((TransactionId) strtoul(str, NULL, 0));
}
Datum
xidout(PG_FUNCTION_ARGS)
{
TransactionId transactionId = PG_GETARG_TRANSACTIONID(0);
/* maximum 32 bit unsigned integer representation takes 10 chars */
char *str = palloc(11);
snprintf(str, 11, "%lu", (unsigned long) transactionId);
PG_RETURN_CSTRING(str);
}
/*
* xidrecv - converts external binary format to xid
*/
Datum
xidrecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_TRANSACTIONID((TransactionId) pq_getmsgint(buf, sizeof(TransactionId)));
}
/*
* xidsend - converts xid to binary format
*/
Datum
xidsend(PG_FUNCTION_ARGS)
{
TransactionId arg1 = PG_GETARG_TRANSACTIONID(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, arg1, sizeof(arg1));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/*
* xideq - are two xids equal?
*/
Datum
xideq(PG_FUNCTION_ARGS)
{
TransactionId xid1 = PG_GETARG_TRANSACTIONID(0);
TransactionId xid2 = PG_GETARG_TRANSACTIONID(1);
PG_RETURN_BOOL(TransactionIdEquals(xid1, xid2));
}
/*
* xid_age - compute age of an XID (relative to current xact)
*/
Datum
xid_age(PG_FUNCTION_ARGS)
{
TransactionId xid = PG_GETARG_TRANSACTIONID(0);
TransactionId now = GetCurrentTransactionId();
/* Permanent XIDs are always infinitely old */
if (!TransactionIdIsNormal(xid))
PG_RETURN_INT32(INT_MAX);
PG_RETURN_INT32((int32) (now - xid));
}
/*****************************************************************************
* COMMAND IDENTIFIER ROUTINES *
*****************************************************************************/
/*
* cidin - converts CommandId to internal representation.
*/
Datum
cidin(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);
CommandId c;
c = atoi(s);
PG_RETURN_COMMANDID(c);
}
/*
* cidout - converts a cid to external representation.
*/
Datum
cidout(PG_FUNCTION_ARGS)
{
CommandId c = PG_GETARG_COMMANDID(0);
char *result = (char *) palloc(16);
snprintf(result, 16, "%u", (unsigned int) c);
PG_RETURN_CSTRING(result);
}
/*
* cidrecv - converts external binary format to cid
*/
Datum
cidrecv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_COMMANDID((CommandId) pq_getmsgint(buf, sizeof(CommandId)));
}
/*
* cidsend - converts cid to binary format
*/
Datum
cidsend(PG_FUNCTION_ARGS)
{
CommandId arg1 = PG_GETARG_COMMANDID(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, arg1, sizeof(arg1));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
Datum
cideq(PG_FUNCTION_ARGS)
{
CommandId arg1 = PG_GETARG_COMMANDID(0);
CommandId arg2 = PG_GETARG_COMMANDID(1);
PG_RETURN_BOOL(arg1 == arg2);
}