pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Brijesh Shrivastav <Bshrivastav@esri.com>
To: 'Tom Lane' <tgl@sss.pgh.pa.us>
To: Brijesh Shrivastav <Bshrivastav@esri.com>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: libpq binary transfer of the numeric data type -
Date: Mon, 2 Aug 2004 17:29:26 -0700
Message-ID: <491DC5F3D279CD4EB4B157DDD62237F404E27FEF@zipwire.esri.com> (raw)

This is the part I am not clear. It seems the numeric_recv function
read int16s as you descibed below but returns Numeric structure.
What do we get from PQgetvalue(), a Numeric object or a series of 
int16s followed by array of numeric digits as sent in numeric_send() 
routine?


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)
		ereport(ERROR,
	
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
				 errmsg("invalid length in external
\"numeric\" value")));

	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))
		ereport(ERROR,
	
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
				 errmsg("invalid sign in external
\"numeric\" value")));

	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)
			ereport(ERROR,
	
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
					 errmsg("invalid digit in external
\"numeric\" value")));
		value.digits[i] = d;
	}

	res = make_result(&value);
	free_var(&value);

	PG_RETURN_NUMERIC(res);
}

warm regards,
Brijesh
-----Original Message-----
From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Sent: Monday, August 02, 2004 4:35 PM
To: Brijesh Shrivastav
Cc: pgsql-interfaces@postgresql.org
Subject: Re: [INTERFACES] libpq binary transfer of the numeric data type
- II 


Brijesh Shrivastav <Bshrivastav@esri.com> writes:
> Has anyone had success with fetching numeric data in binary format?
> I tried to follow Tom's advice below and try to reverse engineer from 
> recv/send function. It seemes the end structure that user will 
> get is of Numeric type (see struct below)

No, it isn't.  What you get is a series of int16 fields:

	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));

Note that the "digits" are base-10000 digits.

			regards, tom lane



view thread (9+ messages)

Message-ID: <491DC5F3D279CD4EB4B157DDD62237F404E27FEF@zipwire.esri.com>
Permalink:  ../491DC5F3D279CD4EB4B157DDD62237F404E27FEF@zipwire.esri.com/
Also on:    postgresql.org/message-id/491DC5F3D279CD4EB4B157DDD62237F404E27FEF@zipwire.esri.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: Bshrivastav@esri.com, tgl@sss.pgh.pa.us
  Subject: Re: libpq binary transfer of the numeric data type -
  In-Reply-To: <491DC5F3D279CD4EB4B157DDD62237F404E27FEF@zipwire.esri.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox