#include <string.h>
#include <libpq-fe.h>
#include <libpq/libpq-fs.h>

static char *conn_string =
	"dbname=test user=gonzalo password=password hostaddr=127.0.0.1";

int
main ()
{
	PGconn *cnc;
	PGresult *res;
	int oid, fd;
	char *the_string = "The String";
	char *other_str = "            ";
	int written;
	int result = 0;

	cnc = PQconnectdb (conn_string);
	if (PQstatus (cnc) != CONNECTION_OK) {
		printf ("Error connecting: %s\n", PQerrorMessage (cnc));
		return -1;
	}

	res = PQexec (cnc, "begin");
	PQclear(res);

	oid = lo_creat (cnc, INV_READ | INV_WRITE); /* Don't care if only one is set */
	fd = lo_open (cnc, oid, INV_READ);
	if (fd < 0) {
		printf ("Error opening BLOB: %s\n", PQerrorMessage (cnc));
		return -1;
	}
	
	written = lo_write (cnc, fd, the_string, strlen (the_string));
	if (written >= 0) {
		printf ("ERROR: I was able to write %d bytes.\n", written);
		result = 1;
	} else {
		printf ("It worked! %s\n", PQerrorMessage (cnc));
	}

	lo_close (cnc, fd);
	lo_unlink (cnc, oid);

	res = PQexec (cnc, "end");
	PQclear (res);
	PQfinish (cnc);

	return result;
}

