
#include "pg.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#if defined(_WIN32) || defined(_WIN64)
#	define U64FMT "%I64u"
	typedef unsigned __int64 myuint64_t;
#else
#	define U64FMT "%llu"
	typedef unsigned long long myuint64_t;
#endif

/*
  CREATE TABLE test_param
  (
    b "char",
    s smallint,
    i int,
    l bigint,
    t text,
    f float4,
    d float8,
    t2 text
  );
*/

int main(void)
{
	float f;
	double d = 0;
	char *text;
	unsigned char i8 = 0;
	unsigned short i16 = 0;
	unsigned int i32 = 0;
	myuint64_t i64;
	PGconn *conn;
	PGresult *res;
  PGparam *param;
	ExecStatusType status;
	char command[] = "INSERT INTO test_param VALUES "
		"($1, $2, $3, $4, $5, $6, $7, $8)";

  conn = PQconnectdb("hostaddr=127.0.0.1  user=postgres");
  if(PQstatus(conn) != CONNECTION_OK)
  {
    printf("connection failure\n");
		return 1;
	}

	/* clear test table */
	res = PQexec(conn, "DELETE FROM test_param");
	PQclear(res);

	/* creates a PGparam.  This can be used for multiple executions as
	 * long as PQparamReset() is called prior to putting param values.
	 */
	param = PQparamCreate();

	PQparamPutChar(param, UCHAR_MAX);
  PQparamPutInt2(param, USHRT_MAX);
  PQparamPutInt4(param, UINT_MAX);

	/* must pass a pointer to 64-bit value. frontend pq lib has no
	 * 64-bit type exposed.
	 */
	i64 = ULLONG_MAX;
  PQparamPutInt8(param, &i64);

	/* -1 means calculate the strlen */
  PQparamPutString(param, "This is a string", -1);
  PQparamPutFloat(param, 111.234f);
  PQparamPutDouble(param, 11111111.234567);

	/* SQL NULL value for text column */
	PQparamPutString(param, NULL, 0);

	/* execute the query using the provided params */
  PQparamExec(conn, command, param, 1, &res);

	/* check result */
  status = PQresultStatus(res);
  if(status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK)
		printf("%s", PQresultErrorMessage(res));
	PQclear(res);

	/* don't need this any more.  If it was needed, PGparamReset() would
	 * need to be called before using it again.
	 */
	PQparamClear(param);

	/* execute a query in binary format with no params.  NOTE: a PQparam
	 * without any values (a param count of 0) has the same effect as
	 * passing a NULL PGparam.  See PQparamReset(), which sets the
	 * internal param count to 0.
	 */
	PQparamExec(conn, "SELECT * FROM test_param", NULL, 1, &res);

  status = PQresultStatus(res);
  if(status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK)
  {
		printf("%s", PQresultErrorMessage(res));
		PQclear(res);
		return 1;
	}

	/* process results */
	PQgetchar(res, 0, 0, &i8);
	PQgetint2(res, 0, 1, &i16);
	PQgetint4(res, 0, 2, &i32);
	PQgetint8(res, 0, 3, &i64);
	text = PQgetvalue(res, 0, 4);
	PQgetfloat(res, 0, 5, &f);
	PQgetdouble(res, 0, 6, &d);

	printf("i8 = %d\ni16 = %d\ni32 = %u\ni64 = " U64FMT "\n"
		"str = '%s'\nfloat = %.3f\ndouble = %lf\n",
		i8, i16, i32, i64, text, f, d);

	if(PQgetisnull(res, 0, 7))
		printf("nullstr is NULL\n");
	else
		printf("nullstr WAS NOT NULL!!\n");

	PQclear(res);
	PQfinish(conn);
  return 0;
}


