agora inbox for pgsql-interfaces@postgresql.org  
help / color / mirror / Atom feed
From: Neeraj Rai <rneeraj@yahoo.com>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: - pgaql long value corrupted using htons
Date: Thu, 23 May 2013 04:53:44 -0700 (PDT)
Message-ID: <1369310024.8114.YahooMailNeo@web164904.mail.bf1.yahoo.com> (raw)
In-Reply-To: <1369266903.47851.YahooMailNeo@web164904.mail.bf1.yahoo.com>
References: <1369185968.87888.YahooMailNeo@web164906.mail.bf1.yahoo.com>
	<1369241530.69063.YahooMailAndroidMobile@web164901.mail.bf1.yahoo.com>
	<1369266733.72536.YahooMailNeo@web164902.mail.bf1.yahoo.com>
	<1369266903.47851.YahooMailNeo@web164904.mail.bf1.yahoo.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-general>

1. I am having trouble writing long values via c interface .testlibqp-dt.c  is attached.
. 2 is being inserted as 8589934592.


qsf=> select * from t1;
   curdate   |  strf  |        intf        |         tm_new         | txnid 
------------+--------+--------------------+------------------------+-------
1999-01-10 | hello2 |8589934592 | 1999-01-08 04:05:06-05 |     3

I tried htobe64 but that didn't help either.
Any advise would be appreciated.


2. I am using string for data and timestamp and it seems to work.

I found functions like TimestampTzGetDatum,time_t_to_timestamptz in the code 
But it seems they are not exposed to interface.
I have the date time in unix format (via time and gettimeofday). I would prefer to pass those values
in binary, rather than string format. Is there any option to do this ? Or is it easily addable?

thanks
Neeraj

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Attachments:

  [text/x-csrc] testlibpq-dt.c (3.1K, ../1369310024.8114.YahooMailNeo@web164904.mail.bf1.yahoo.com/3-testlibpq-dt.c)
  download | inline:
/*
CREATE table t1
(
curdate                 date,
strf                    varchar(10),
intf                    bigint,
tm_new                  timestamptz,
txnid                   smallint
);
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>

#include "libpq-fe.h"

static void
exit_nicely(PGconn *conn)
{
	PQfinish(conn);
	exit(1);
}

typedef struct
{
  int 		curdate;
  char		strf[8];
  long		intf;
  int		tm_new;
  short		txnid;
} t1data;

int
main(int argc, char **argv)
{
  char		*user;
  char		*pass;
  char		*dbname;
  char		*host = NULL;
  char		*port = NULL;
  PGconn	*conn;
  PGresult	*res;

  char			insertStmt[512];
  int			result;
  const char* 	stmtName = "t1insert";
  const char*	paramValue[5];
  int			paramLength[5];
  int			paramFormat[5];

  t1data mydata;
  char	date[16];
  char  tm_new[32];
  int ii=0;

  if (argc < 3)
	{
	  fprintf(stderr, "usage: dbname user pass [host][port] \n");
	  exit(1);
	}
  dbname    = argv[1];
  user	    = argv[2];
  pass	    = argv[3];
  if (argc > 3)	host = argv[4];
  if (argc > 4)	port = argv[5];

  /* Make a connection to the database */
  conn = PQsetdbLogin(host, port, NULL, NULL, dbname, user, pass);

  /* Check to see that the backend connection was successfully made */
  if (PQstatus(conn) != CONNECTION_OK)
	{
	  fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
	  exit_nicely(conn);
	}
  fprintf(stdout, "Connected to database : %s %s\n", user, dbname);

  // prepare insert statement with unique key
  strcpy(insertStmt, "INSERT INTO t1 values ($1, $2, $3, $4, $5);");

  // let the backend prepare statement (it is parsed/planned only once)
  res = PQprepare (conn, stmtName, insertStmt, 1, NULL);
  result = PQresultStatus(res);
  if (result != PGRES_COMMAND_OK)
	{
	  fprintf(stderr, "PQprepare failed: %s : (%d) %s\n", insertStmt, result,PQerrorMessage(conn));
	  PQclear(res);
	  exit_nicely(conn);
	}
  fprintf(stdout, "PQprepare done : %s=%s\n", stmtName, insertStmt);
  PQclear(res);

  // maybe multiple calls to insert data
  strcpy(date, "19990110");
  strcpy(mydata.strf, "hello2");
  mydata.intf = htonl((uint32_t)2);
  strcpy(tm_new, "1999-01-08 04:05:06 EST");
  mydata.txnid = htons(3);

  // set values
  paramValue[0] = date;
  paramValue[1] = mydata.strf;
  paramValue[2] = (char*)&mydata.intf;
  paramValue[3] = tm_new;
  paramValue[4] = (char*)&mydata.txnid;

  // set lengths
  paramLength[0] = 8;
  paramLength[1] = 6;
  paramLength[2] = sizeof(mydata.intf);
  paramLength[3] = strlen(tm_new);
  paramLength[4] = sizeof(mydata.txnid);

  // set format
  paramFormat[0] = 0;
  paramFormat[1] = 0;
  paramFormat[2] = 1;
  paramFormat[3] = 0;
  paramFormat[4] = 1;

  res = PQexecPrepared(conn, stmtName, 5, paramValue, paramLength, paramFormat, 0);
  result = PQresultStatus(res);
  if (result != PGRES_COMMAND_OK)
	{
	  fprintf(stderr, "exec failed: %s : (%d) %s\n", insertStmt, result,PQerrorMessage(conn));
	  PQclear(res);
	  exit_nicely(conn);
	}
  fprintf(stdout, "exec done : %s, $1=(%d)\n", stmtName, ii);

  PQclear(res);

  /* close the connection to the database and cleanup */
  PQfinish(conn);

  return 0;
}

view thread (3+ messages)

Message-ID: <1369310024.8114.YahooMailNeo@web164904.mail.bf1.yahoo.com>
Permalink:  ../1369310024.8114.YahooMailNeo@web164904.mail.bf1.yahoo.com/
Also on:    postgresql.org/message-id/1369310024.8114.YahooMailNeo@web164904.mail.bf1.yahoo.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: rneeraj@yahoo.com, pgsql-general@postgresql.org
  Subject: Re: - pgaql long value corrupted using htons
  In-Reply-To: <1369310024.8114.YahooMailNeo@web164904.mail.bf1.yahoo.com>

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

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