mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Massive commit to run PGINDENT on all *.c and *.h files.
This commit is contained in:
@ -1,118 +1,137 @@
|
||||
/*
|
||||
* testlibpq.c
|
||||
* Test the C version of LIBPQ, the POSTGRES frontend library.
|
||||
* Test the C version of LIBPQ, the POSTGRES frontend library.
|
||||
*
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "libpq-fe.h"
|
||||
|
||||
void
|
||||
exit_nicely(PGconn* conn)
|
||||
void
|
||||
exit_nicely(PGconn * conn)
|
||||
{
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
main()
|
||||
{
|
||||
char *pghost, *pgport, *pgoptions, *pgtty;
|
||||
char* dbName;
|
||||
int nFields;
|
||||
int i,j;
|
||||
char *pghost,
|
||||
*pgport,
|
||||
*pgoptions,
|
||||
*pgtty;
|
||||
char *dbName;
|
||||
int nFields;
|
||||
int i,
|
||||
j;
|
||||
|
||||
#ifdef DEBUG
|
||||
FILE *debug;
|
||||
#endif /* DEBUG */
|
||||
FILE *debug;
|
||||
|
||||
PGconn* conn;
|
||||
PGresult* res;
|
||||
#endif /* DEBUG */
|
||||
|
||||
/* begin, by setting the parameters for a backend connection
|
||||
if the parameters are null, then the system will try to use
|
||||
reasonable defaults by looking up environment variables
|
||||
or, failing that, using hardwired constants */
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
dbName = "template1";
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
/*
|
||||
* begin, by setting the parameters for a backend connection if the
|
||||
* parameters are null, then the system will try to use reasonable
|
||||
* defaults by looking up environment variables or, failing that,
|
||||
* using hardwired constants
|
||||
*/
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend
|
||||
* server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
dbName = "template1";
|
||||
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD) {
|
||||
fprintf(stderr,"Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr,"%s",PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD)
|
||||
{
|
||||
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
debug = fopen("/tmp/trace.out","w");
|
||||
PQtrace(conn, debug);
|
||||
#endif /* DEBUG */
|
||||
debug = fopen("/tmp/trace.out", "w");
|
||||
PQtrace(conn, debug);
|
||||
#endif /* DEBUG */
|
||||
|
||||
/* start a transaction block */
|
||||
res = PQexec(conn,"BEGIN");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"BEGIN command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
/* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
memory leaks */
|
||||
PQclear(res);
|
||||
/* start a transaction block */
|
||||
res = PQexec(conn, "BEGIN");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
fprintf(stderr, "BEGIN command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
/* fetch instances from the pg_database, the system catalog of databases*/
|
||||
res = PQexec(conn,"DECLARE myportal CURSOR FOR select * from pg_database");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"DECLARE CURSOR command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
PQclear(res);
|
||||
/*
|
||||
* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
* memory leaks
|
||||
*/
|
||||
PQclear(res);
|
||||
|
||||
res = PQexec(conn,"FETCH ALL in myportal");
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
||||
fprintf(stderr,"FETCH ALL command didn't return tuples properly\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
/* first, print out the attribute names */
|
||||
nFields = PQnfields(res);
|
||||
for (i=0; i < nFields; i++) {
|
||||
printf("%-15s",PQfname(res,i));
|
||||
}
|
||||
printf("\n\n");
|
||||
/*
|
||||
* fetch instances from the pg_database, the system catalog of
|
||||
* databases
|
||||
*/
|
||||
res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
fprintf(stderr, "DECLARE CURSOR command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
PQclear(res);
|
||||
|
||||
/* next, print out the instances */
|
||||
for (i=0; i < PQntuples(res); i++) {
|
||||
for (j=0 ; j < nFields; j++) {
|
||||
printf("%-15s", PQgetvalue(res,i,j));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
res = PQexec(conn, "FETCH ALL in myportal");
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
|
||||
/* close the portal */
|
||||
res = PQexec(conn, "CLOSE myportal");
|
||||
PQclear(res);
|
||||
/* first, print out the attribute names */
|
||||
nFields = PQnfields(res);
|
||||
for (i = 0; i < nFields; i++)
|
||||
{
|
||||
printf("%-15s", PQfname(res, i));
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
/* end the transaction */
|
||||
res = PQexec(conn, "END");
|
||||
PQclear(res);
|
||||
/* next, print out the instances */
|
||||
for (i = 0; i < PQntuples(res); i++)
|
||||
{
|
||||
for (j = 0; j < nFields; j++)
|
||||
{
|
||||
printf("%-15s", PQgetvalue(res, i, j));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
PQclear(res);
|
||||
|
||||
/* close the portal */
|
||||
res = PQexec(conn, "CLOSE myportal");
|
||||
PQclear(res);
|
||||
|
||||
/* end the transaction */
|
||||
res = PQexec(conn, "END");
|
||||
PQclear(res);
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
|
||||
#ifdef DEBUG
|
||||
fclose(debug);
|
||||
#endif /* DEBUG */
|
||||
fclose(debug);
|
||||
#endif /* DEBUG */
|
||||
|
||||
exit(0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* testlibpq2.c
|
||||
* Test of the asynchronous notification interface
|
||||
* Test of the asynchronous notification interface
|
||||
*
|
||||
populate a database with the following:
|
||||
|
||||
@ -21,73 +21,87 @@ INSERT INTO TBL1 values (10);
|
||||
#include <stdio.h>
|
||||
#include "libpq-fe.h"
|
||||
|
||||
void exit_nicely(PGconn* conn)
|
||||
void
|
||||
exit_nicely(PGconn * conn)
|
||||
{
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
main()
|
||||
{
|
||||
char *pghost, *pgport, *pgoptions, *pgtty;
|
||||
char* dbName;
|
||||
int nFields;
|
||||
int i,j;
|
||||
char *pghost,
|
||||
*pgport,
|
||||
*pgoptions,
|
||||
*pgtty;
|
||||
char *dbName;
|
||||
int nFields;
|
||||
int i,
|
||||
j;
|
||||
|
||||
PGconn* conn;
|
||||
PGresult* res;
|
||||
PGnotify* notify;
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
PGnotify *notify;
|
||||
|
||||
/* begin, by setting the parameters for a backend connection
|
||||
if the parameters are null, then the system will try to use
|
||||
reasonable defaults by looking up environment variables
|
||||
or, failing that, using hardwired constants */
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
dbName = getenv("USER"); /* change this to the name of your test database*/
|
||||
/*
|
||||
* begin, by setting the parameters for a backend connection if the
|
||||
* parameters are null, then the system will try to use reasonable
|
||||
* defaults by looking up environment variables or, failing that,
|
||||
* using hardwired constants
|
||||
*/
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend
|
||||
* server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
dbName = getenv("USER"); /* change this to the name of your test
|
||||
* database */
|
||||
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD) {
|
||||
fprintf(stderr,"Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr,"%s",PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD)
|
||||
{
|
||||
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
res = PQexec(conn, "LISTEN TBL2");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"LISTEN command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
/* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
memory leaks */
|
||||
PQclear(res);
|
||||
res = PQexec(conn, "LISTEN TBL2");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
fprintf(stderr, "LISTEN command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
/* async notification only come back as a result of a query*/
|
||||
/* we can send empty queries */
|
||||
res = PQexec(conn, " ");
|
||||
/* printf("res->status = %s\n", pgresStatus[PQresultStatus(res)]); */
|
||||
/* check for asynchronous returns */
|
||||
notify = PQnotifies(conn);
|
||||
if (notify) {
|
||||
fprintf(stderr,
|
||||
"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
|
||||
notify->relname, notify->be_pid);
|
||||
free(notify);
|
||||
break;
|
||||
}
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
/*
|
||||
* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
* memory leaks
|
||||
*/
|
||||
PQclear(res);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* async notification only come back as a result of a query */
|
||||
/* we can send empty queries */
|
||||
res = PQexec(conn, " ");
|
||||
/* printf("res->status = %s\n", pgresStatus[PQresultStatus(res)]); */
|
||||
/* check for asynchronous returns */
|
||||
notify = PQnotifies(conn);
|
||||
if (notify)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
|
||||
notify->relname, notify->be_pid);
|
||||
free(notify);
|
||||
break;
|
||||
}
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
/*
|
||||
* testlibpq3.c
|
||||
* Test the C version of LIBPQ, the POSTGRES frontend library.
|
||||
* tests the binary cursor interface
|
||||
* Test the C version of LIBPQ, the POSTGRES frontend library.
|
||||
* tests the binary cursor interface
|
||||
*
|
||||
*
|
||||
*
|
||||
populate a database by doing the following:
|
||||
|
||||
|
||||
CREATE TABLE test1 (i int4, d float4, p polygon);
|
||||
|
||||
INSERT INTO test1 values (1, 3.567, '(3.0, 4.0, 1.0, 2.0)'::polygon);
|
||||
@ -18,137 +18,162 @@ INSERT INTO test1 values (2, 89.05, '(4.0, 3.0, 2.0, 1.0)'::polygon);
|
||||
tuple 0: got
|
||||
i = (4 bytes) 1,
|
||||
d = (4 bytes) 3.567000,
|
||||
p = (4 bytes) 2 points boundbox = (hi=3.000000/4.000000, lo = 1.000000,2.000000)
|
||||
p = (4 bytes) 2 points boundbox = (hi=3.000000/4.000000, lo = 1.000000,2.000000)
|
||||
tuple 1: got
|
||||
i = (4 bytes) 2,
|
||||
d = (4 bytes) 89.050003,
|
||||
p = (4 bytes) 2 points boundbox = (hi=4.000000/3.000000, lo = 2.000000,1.000000)
|
||||
p = (4 bytes) 2 points boundbox = (hi=4.000000/3.000000, lo = 2.000000,1.000000)
|
||||
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "libpq-fe.h"
|
||||
#include "utils/geo-decls.h" /* for the POLYGON type */
|
||||
#include "utils/geo-decls.h" /* for the POLYGON type */
|
||||
|
||||
void exit_nicely(PGconn* conn)
|
||||
void
|
||||
exit_nicely(PGconn * conn)
|
||||
{
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
main()
|
||||
{
|
||||
char *pghost, *pgport, *pgoptions, *pgtty;
|
||||
char* dbName;
|
||||
int nFields;
|
||||
int i,j;
|
||||
int i_fnum, d_fnum, p_fnum;
|
||||
char *pghost,
|
||||
*pgport,
|
||||
*pgoptions,
|
||||
*pgtty;
|
||||
char *dbName;
|
||||
int nFields;
|
||||
int i,
|
||||
j;
|
||||
int i_fnum,
|
||||
d_fnum,
|
||||
p_fnum;
|
||||
|
||||
PGconn* conn;
|
||||
PGresult* res;
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
|
||||
/* begin, by setting the parameters for a backend connection
|
||||
if the parameters are null, then the system will try to use
|
||||
reasonable defaults by looking up environment variables
|
||||
or, failing that, using hardwired constants */
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
/*
|
||||
* begin, by setting the parameters for a backend connection if the
|
||||
* parameters are null, then the system will try to use reasonable
|
||||
* defaults by looking up environment variables or, failing that,
|
||||
* using hardwired constants
|
||||
*/
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend
|
||||
* server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
|
||||
dbName = getenv("USER"); /* change this to the name of your test database*/
|
||||
dbName = getenv("USER"); /* change this to the name of your test
|
||||
* database */
|
||||
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD) {
|
||||
fprintf(stderr,"Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr,"%s",PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD)
|
||||
{
|
||||
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
/* start a transaction block */
|
||||
res = PQexec(conn,"BEGIN");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"BEGIN command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
/* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
memory leaks */
|
||||
PQclear(res);
|
||||
/* start a transaction block */
|
||||
res = PQexec(conn, "BEGIN");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
fprintf(stderr, "BEGIN command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
/* fetch instances from the pg_database, the system catalog of databases*/
|
||||
res = PQexec(conn,"DECLARE mycursor BINARY CURSOR FOR select * from test1");
|
||||
if (res == NULL ||
|
||||
PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"DECLARE CURSOR command failed\n");
|
||||
if (res)
|
||||
/*
|
||||
* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
* memory leaks
|
||||
*/
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
PQclear(res);
|
||||
|
||||
res = PQexec(conn,"FETCH ALL in mycursor");
|
||||
if (res == NULL ||
|
||||
PQresultStatus(res) != PGRES_TUPLES_OK) {
|
||||
fprintf(stderr,"FETCH ALL command didn't return tuples properly\n");
|
||||
if (res)
|
||||
/*
|
||||
* fetch instances from the pg_database, the system catalog of
|
||||
* databases
|
||||
*/
|
||||
res = PQexec(conn, "DECLARE mycursor BINARY CURSOR FOR select * from test1");
|
||||
if (res == NULL ||
|
||||
PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
fprintf(stderr, "DECLARE CURSOR command failed\n");
|
||||
if (res)
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
i_fnum = PQfnumber(res,"i");
|
||||
d_fnum = PQfnumber(res,"d");
|
||||
p_fnum = PQfnumber(res,"p");
|
||||
|
||||
for (i=0;i<3;i++) {
|
||||
printf("type[%d] = %d, size[%d] = %d\n",
|
||||
i, PQftype(res,i),
|
||||
i, PQfsize(res,i));
|
||||
}
|
||||
for (i=0; i < PQntuples(res); i++) {
|
||||
int *ival;
|
||||
float *dval;
|
||||
int plen;
|
||||
POLYGON* pval;
|
||||
/* we hard-wire this to the 3 fields we know about */
|
||||
ival = (int*)PQgetvalue(res,i,i_fnum);
|
||||
dval = (float*)PQgetvalue(res,i,d_fnum);
|
||||
plen = PQgetlength(res,i,p_fnum);
|
||||
|
||||
/* plen doesn't include the length field so need to increment by VARHDSZ*/
|
||||
pval = (POLYGON*) malloc(plen + VARHDRSZ);
|
||||
pval->size = plen;
|
||||
memmove((char*)&pval->npts, PQgetvalue(res,i,p_fnum), plen);
|
||||
printf("tuple %d: got\n", i);
|
||||
printf(" i = (%d bytes) %d,\n",
|
||||
PQgetlength(res,i,i_fnum), *ival);
|
||||
printf(" d = (%d bytes) %f,\n",
|
||||
PQgetlength(res,i,d_fnum), *dval);
|
||||
printf(" p = (%d bytes) %d points \tboundbox = (hi=%f/%f, lo = %f,%f)\n",
|
||||
PQgetlength(res,i,d_fnum),
|
||||
pval->npts,
|
||||
pval->boundbox.xh,
|
||||
pval->boundbox.yh,
|
||||
pval->boundbox.xl,
|
||||
pval->boundbox.yl);
|
||||
}
|
||||
res = PQexec(conn, "FETCH ALL in mycursor");
|
||||
if (res == NULL ||
|
||||
PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
|
||||
if (res)
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
|
||||
/* close the portal */
|
||||
res = PQexec(conn, "CLOSE mycursor");
|
||||
PQclear(res);
|
||||
i_fnum = PQfnumber(res, "i");
|
||||
d_fnum = PQfnumber(res, "d");
|
||||
p_fnum = PQfnumber(res, "p");
|
||||
|
||||
/* end the transaction */
|
||||
res = PQexec(conn, "END");
|
||||
PQclear(res);
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
printf("type[%d] = %d, size[%d] = %d\n",
|
||||
i, PQftype(res, i),
|
||||
i, PQfsize(res, i));
|
||||
}
|
||||
for (i = 0; i < PQntuples(res); i++)
|
||||
{
|
||||
int *ival;
|
||||
float *dval;
|
||||
int plen;
|
||||
POLYGON *pval;
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
/* we hard-wire this to the 3 fields we know about */
|
||||
ival = (int *) PQgetvalue(res, i, i_fnum);
|
||||
dval = (float *) PQgetvalue(res, i, d_fnum);
|
||||
plen = PQgetlength(res, i, p_fnum);
|
||||
|
||||
/*
|
||||
* plen doesn't include the length field so need to increment by
|
||||
* VARHDSZ
|
||||
*/
|
||||
pval = (POLYGON *) malloc(plen + VARHDRSZ);
|
||||
pval->size = plen;
|
||||
memmove((char *) &pval->npts, PQgetvalue(res, i, p_fnum), plen);
|
||||
printf("tuple %d: got\n", i);
|
||||
printf(" i = (%d bytes) %d,\n",
|
||||
PQgetlength(res, i, i_fnum), *ival);
|
||||
printf(" d = (%d bytes) %f,\n",
|
||||
PQgetlength(res, i, d_fnum), *dval);
|
||||
printf(" p = (%d bytes) %d points \tboundbox = (hi=%f/%f, lo = %f,%f)\n",
|
||||
PQgetlength(res, i, d_fnum),
|
||||
pval->npts,
|
||||
pval->boundbox.xh,
|
||||
pval->boundbox.yh,
|
||||
pval->boundbox.xl,
|
||||
pval->boundbox.yl);
|
||||
}
|
||||
|
||||
PQclear(res);
|
||||
|
||||
/* close the portal */
|
||||
res = PQexec(conn, "CLOSE mycursor");
|
||||
PQclear(res);
|
||||
|
||||
/* end the transaction */
|
||||
res = PQexec(conn, "END");
|
||||
PQclear(res);
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* testlibpq4.c
|
||||
* this test programs shows to use LIBPQ to make multiple backend
|
||||
* this test programs shows to use LIBPQ to make multiple backend
|
||||
* connections
|
||||
*
|
||||
*
|
||||
@ -8,120 +8,142 @@
|
||||
#include <stdio.h>
|
||||
#include "libpq-fe.h"
|
||||
|
||||
void
|
||||
exit_nicely(PGconn* conn1, PGconn* conn2)
|
||||
void
|
||||
exit_nicely(PGconn * conn1, PGconn * conn2)
|
||||
{
|
||||
if (conn1)
|
||||
PQfinish(conn1);
|
||||
if (conn2)
|
||||
PQfinish(conn2);
|
||||
exit(1);
|
||||
if (conn1)
|
||||
PQfinish(conn1);
|
||||
if (conn2)
|
||||
PQfinish(conn2);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void check_conn(PGconn* conn)
|
||||
void
|
||||
check_conn(PGconn * conn)
|
||||
{
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD) {
|
||||
fprintf(stderr,"Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr,"%s",PQerrorMessage(conn));
|
||||
exit(1);
|
||||
}
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD)
|
||||
{
|
||||
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
{
|
||||
char *pghost, *pgport, *pgoptions, *pgtty;
|
||||
char* dbName1, dbName2;
|
||||
char* tblName;
|
||||
int nFields;
|
||||
int i,j;
|
||||
char *pghost,
|
||||
*pgport,
|
||||
*pgoptions,
|
||||
*pgtty;
|
||||
char *dbName1,
|
||||
dbName2;
|
||||
char *tblName;
|
||||
int nFields;
|
||||
int i,
|
||||
j;
|
||||
|
||||
PGconn* conn1, conn2;
|
||||
PGresult* res1, res2;
|
||||
PGconn *conn1,
|
||||
conn2;
|
||||
PGresult *res1,
|
||||
res2;
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr,"usage: %s tableName dbName1 dbName2\n",argv[0]);
|
||||
fprintf(stderr," compares two tables in two databases\n");
|
||||
exit(1);
|
||||
}
|
||||
tblName = argv[1];
|
||||
dbName1 = argv[2];
|
||||
dbName2 = argv[3];
|
||||
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "usage: %s tableName dbName1 dbName2\n", argv[0]);
|
||||
fprintf(stderr, " compares two tables in two databases\n");
|
||||
exit(1);
|
||||
}
|
||||
tblName = argv[1];
|
||||
dbName1 = argv[2];
|
||||
dbName2 = argv[3];
|
||||
|
||||
/* begin, by setting the parameters for a backend connection
|
||||
if the parameters are null, then the system will try to use
|
||||
reasonable defaults by looking up environment variables
|
||||
or, failing that, using hardwired constants */
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
|
||||
/* make a connection to the database */
|
||||
conn1 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName1);
|
||||
check_conn(conn1);
|
||||
/*
|
||||
* begin, by setting the parameters for a backend connection if the
|
||||
* parameters are null, then the system will try to use reasonable
|
||||
* defaults by looking up environment variables or, failing that,
|
||||
* using hardwired constants
|
||||
*/
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend
|
||||
* server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
|
||||
conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2);
|
||||
check_conn(conn2);
|
||||
/* make a connection to the database */
|
||||
conn1 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName1);
|
||||
check_conn(conn1);
|
||||
|
||||
/* start a transaction block */
|
||||
res1 = PQexec(conn1,"BEGIN");
|
||||
if (PQresultStatus(res1) != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"BEGIN command failed\n");
|
||||
PQclear(res1);
|
||||
exit_nicely(conn1,conn2);
|
||||
}
|
||||
/* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
memory leaks */
|
||||
PQclear(res1);
|
||||
conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2);
|
||||
check_conn(conn2);
|
||||
|
||||
/* fetch instances from the pg_database, the system catalog of databases*/
|
||||
res = PQexec(conn,"DECLARE myportal CURSOR FOR select * from pg_database");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"DECLARE CURSOR command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
PQclear(res);
|
||||
/* start a transaction block */
|
||||
res1 = PQexec(conn1, "BEGIN");
|
||||
if (PQresultStatus(res1) != PGRES_COMMAND_OK)
|
||||
{
|
||||
fprintf(stderr, "BEGIN command failed\n");
|
||||
PQclear(res1);
|
||||
exit_nicely(conn1, conn2);
|
||||
}
|
||||
|
||||
res = PQexec(conn,"FETCH ALL in myportal");
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
||||
fprintf(stderr,"FETCH ALL command didn't return tuples properly\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
/* first, print out the attribute names */
|
||||
nFields = PQnfields(res);
|
||||
for (i=0; i < nFields; i++) {
|
||||
printf("%-15s",PQfname(res,i));
|
||||
}
|
||||
printf("\n\n");
|
||||
/*
|
||||
* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
* memory leaks
|
||||
*/
|
||||
PQclear(res1);
|
||||
|
||||
/* next, print out the instances */
|
||||
for (i=0; i < PQntuples(res); i++) {
|
||||
for (j=0 ; j < nFields; j++) {
|
||||
printf("%-15s", PQgetvalue(res,i,j));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
/*
|
||||
* fetch instances from the pg_database, the system catalog of
|
||||
* databases
|
||||
*/
|
||||
res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
fprintf(stderr, "DECLARE CURSOR command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
PQclear(res);
|
||||
|
||||
PQclear(res);
|
||||
|
||||
/* close the portal */
|
||||
res = PQexec(conn, "CLOSE myportal");
|
||||
PQclear(res);
|
||||
res = PQexec(conn, "FETCH ALL in myportal");
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
/* end the transaction */
|
||||
res = PQexec(conn, "END");
|
||||
PQclear(res);
|
||||
/* first, print out the attribute names */
|
||||
nFields = PQnfields(res);
|
||||
for (i = 0; i < nFields; i++)
|
||||
{
|
||||
printf("%-15s", PQfname(res, i));
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
/* next, print out the instances */
|
||||
for (i = 0; i < PQntuples(res); i++)
|
||||
{
|
||||
for (j = 0; j < nFields; j++)
|
||||
{
|
||||
printf("%-15s", PQgetvalue(res, i, j));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* fclose(debug); */
|
||||
PQclear(res);
|
||||
|
||||
/* close the portal */
|
||||
res = PQexec(conn, "CLOSE myportal");
|
||||
PQclear(res);
|
||||
|
||||
/* end the transaction */
|
||||
res = PQexec(conn, "END");
|
||||
PQclear(res);
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
|
||||
/* fclose(debug); */
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* testlo.c--
|
||||
* test using large objects with libpq
|
||||
* test using large objects with libpq
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/test/examples/testlo.c,v 1.3 1997/08/12 22:55:19 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/test/examples/testlo.c,v 1.4 1997/09/07 05:04:04 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -21,225 +21,247 @@
|
||||
#include "libpq-fe.h"
|
||||
#include "libpq/libpq-fs.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
#define BUFSIZE 1024
|
||||
|
||||
/*
|
||||
* importFile -
|
||||
* import file "in_filename" into database as large object "lobjOid"
|
||||
* import file "in_filename" into database as large object "lobjOid"
|
||||
*
|
||||
*/
|
||||
Oid importFile(PGconn *conn, char *filename)
|
||||
Oid
|
||||
importFile(PGconn * conn, char *filename)
|
||||
{
|
||||
Oid lobjId;
|
||||
int lobj_fd;
|
||||
char buf[BUFSIZE];
|
||||
int nbytes, tmp;
|
||||
int fd;
|
||||
Oid lobjId;
|
||||
int lobj_fd;
|
||||
char buf[BUFSIZE];
|
||||
int nbytes,
|
||||
tmp;
|
||||
int fd;
|
||||
|
||||
/*
|
||||
* open the file to be read in
|
||||
*/
|
||||
fd = open(filename, O_RDONLY, 0666);
|
||||
if (fd < 0) { /* error */
|
||||
fprintf(stderr, "can't open unix file\"%s\"\n", filename);
|
||||
}
|
||||
|
||||
/*
|
||||
* create the large object
|
||||
*/
|
||||
lobjId = lo_creat(conn, INV_READ|INV_WRITE);
|
||||
if (lobjId == 0) {
|
||||
fprintf(stderr, "can't create large object");
|
||||
}
|
||||
|
||||
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
|
||||
/*
|
||||
* read in from the Unix file and write to the inversion file
|
||||
*/
|
||||
while ((nbytes = read(fd, buf, BUFSIZE)) > 0) {
|
||||
tmp = lo_write(conn, lobj_fd, buf, nbytes);
|
||||
if (tmp < nbytes) {
|
||||
fprintf(stderr, "error while reading \"%s\"", filename);
|
||||
/*
|
||||
* open the file to be read in
|
||||
*/
|
||||
fd = open(filename, O_RDONLY, 0666);
|
||||
if (fd < 0)
|
||||
{ /* error */
|
||||
fprintf(stderr, "can't open unix file\"%s\"\n", filename);
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
lo_close(conn, lobj_fd);
|
||||
|
||||
return lobjId;
|
||||
/*
|
||||
* create the large object
|
||||
*/
|
||||
lobjId = lo_creat(conn, INV_READ | INV_WRITE);
|
||||
if (lobjId == 0)
|
||||
{
|
||||
fprintf(stderr, "can't create large object");
|
||||
}
|
||||
|
||||
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
|
||||
|
||||
/*
|
||||
* read in from the Unix file and write to the inversion file
|
||||
*/
|
||||
while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
|
||||
{
|
||||
tmp = lo_write(conn, lobj_fd, buf, nbytes);
|
||||
if (tmp < nbytes)
|
||||
{
|
||||
fprintf(stderr, "error while reading \"%s\"", filename);
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
lo_close(conn, lobj_fd);
|
||||
|
||||
return lobjId;
|
||||
}
|
||||
|
||||
void pickout(PGconn *conn, Oid lobjId, int start, int len)
|
||||
void
|
||||
pickout(PGconn * conn, Oid lobjId, int start, int len)
|
||||
{
|
||||
int lobj_fd;
|
||||
char* buf;
|
||||
int nbytes;
|
||||
int nread;
|
||||
int lobj_fd;
|
||||
char *buf;
|
||||
int nbytes;
|
||||
int nread;
|
||||
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0) {
|
||||
fprintf(stderr,"can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0)
|
||||
{
|
||||
fprintf(stderr, "can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
|
||||
lo_lseek(conn, lobj_fd, start, SEEK_SET);
|
||||
buf = malloc(len+1);
|
||||
|
||||
nread = 0;
|
||||
while (len - nread > 0) {
|
||||
nbytes = lo_read(conn, lobj_fd, buf, len - nread);
|
||||
buf[nbytes] = '\0';
|
||||
fprintf(stderr,">>> %s", buf);
|
||||
nread += nbytes;
|
||||
}
|
||||
fprintf(stderr,"\n");
|
||||
lo_close(conn, lobj_fd);
|
||||
lo_lseek(conn, lobj_fd, start, SEEK_SET);
|
||||
buf = malloc(len + 1);
|
||||
|
||||
nread = 0;
|
||||
while (len - nread > 0)
|
||||
{
|
||||
nbytes = lo_read(conn, lobj_fd, buf, len - nread);
|
||||
buf[nbytes] = '\0';
|
||||
fprintf(stderr, ">>> %s", buf);
|
||||
nread += nbytes;
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
lo_close(conn, lobj_fd);
|
||||
}
|
||||
|
||||
void overwrite(PGconn *conn, Oid lobjId, int start, int len)
|
||||
void
|
||||
overwrite(PGconn * conn, Oid lobjId, int start, int len)
|
||||
{
|
||||
int lobj_fd;
|
||||
char* buf;
|
||||
int nbytes;
|
||||
int nwritten;
|
||||
int i;
|
||||
int lobj_fd;
|
||||
char *buf;
|
||||
int nbytes;
|
||||
int nwritten;
|
||||
int i;
|
||||
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0) {
|
||||
fprintf(stderr,"can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0)
|
||||
{
|
||||
fprintf(stderr, "can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
|
||||
lo_lseek(conn, lobj_fd, start, SEEK_SET);
|
||||
buf = malloc(len+1);
|
||||
|
||||
for (i=0;i<len;i++)
|
||||
buf[i] = 'X';
|
||||
buf[i] = '\0';
|
||||
lo_lseek(conn, lobj_fd, start, SEEK_SET);
|
||||
buf = malloc(len + 1);
|
||||
|
||||
nwritten = 0;
|
||||
while (len - nwritten > 0) {
|
||||
nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
|
||||
nwritten += nbytes;
|
||||
}
|
||||
fprintf(stderr,"\n");
|
||||
lo_close(conn, lobj_fd);
|
||||
for (i = 0; i < len; i++)
|
||||
buf[i] = 'X';
|
||||
buf[i] = '\0';
|
||||
|
||||
nwritten = 0;
|
||||
while (len - nwritten > 0)
|
||||
{
|
||||
nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
|
||||
nwritten += nbytes;
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
lo_close(conn, lobj_fd);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* exportFile -
|
||||
* export large object "lobjOid" to file "out_filename"
|
||||
* export large object "lobjOid" to file "out_filename"
|
||||
*
|
||||
*/
|
||||
void exportFile(PGconn *conn, Oid lobjId, char *filename)
|
||||
void
|
||||
exportFile(PGconn * conn, Oid lobjId, char *filename)
|
||||
{
|
||||
int lobj_fd;
|
||||
char buf[BUFSIZE];
|
||||
int nbytes, tmp;
|
||||
int fd;
|
||||
int lobj_fd;
|
||||
char buf[BUFSIZE];
|
||||
int nbytes,
|
||||
tmp;
|
||||
int fd;
|
||||
|
||||
/*
|
||||
* create an inversion "object"
|
||||
*/
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0) {
|
||||
fprintf(stderr,"can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
|
||||
/*
|
||||
* open the file to be written to
|
||||
*/
|
||||
fd = open(filename, O_CREAT|O_WRONLY, 0666);
|
||||
if (fd < 0) { /* error */
|
||||
fprintf(stderr, "can't open unix file\"%s\"",
|
||||
filename);
|
||||
}
|
||||
|
||||
/*
|
||||
* read in from the Unix file and write to the inversion file
|
||||
*/
|
||||
while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0) {
|
||||
tmp = write(fd, buf, nbytes);
|
||||
if (tmp < nbytes) {
|
||||
fprintf(stderr,"error while writing \"%s\"",
|
||||
filename);
|
||||
/*
|
||||
* create an inversion "object"
|
||||
*/
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0)
|
||||
{
|
||||
fprintf(stderr, "can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
}
|
||||
|
||||
lo_close(conn, lobj_fd);
|
||||
close(fd);
|
||||
/*
|
||||
* open the file to be written to
|
||||
*/
|
||||
fd = open(filename, O_CREAT | O_WRONLY, 0666);
|
||||
if (fd < 0)
|
||||
{ /* error */
|
||||
fprintf(stderr, "can't open unix file\"%s\"",
|
||||
filename);
|
||||
}
|
||||
|
||||
return;
|
||||
/*
|
||||
* read in from the Unix file and write to the inversion file
|
||||
*/
|
||||
while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
|
||||
{
|
||||
tmp = write(fd, buf, nbytes);
|
||||
if (tmp < nbytes)
|
||||
{
|
||||
fprintf(stderr, "error while writing \"%s\"",
|
||||
filename);
|
||||
}
|
||||
}
|
||||
|
||||
lo_close(conn, lobj_fd);
|
||||
close(fd);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
exit_nicely(PGconn* conn)
|
||||
void
|
||||
exit_nicely(PGconn * conn)
|
||||
{
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *in_filename, *out_filename;
|
||||
char *database;
|
||||
Oid lobjOid;
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
char *in_filename,
|
||||
*out_filename;
|
||||
char *database;
|
||||
Oid lobjOid;
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
|
||||
if (argc != 4) {
|
||||
fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
|
||||
argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
|
||||
argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
database = argv[1];
|
||||
in_filename = argv[2];
|
||||
out_filename = argv[3];
|
||||
database = argv[1];
|
||||
in_filename = argv[2];
|
||||
out_filename = argv[3];
|
||||
|
||||
/*
|
||||
* set up the connection
|
||||
*/
|
||||
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
|
||||
/*
|
||||
* set up the connection
|
||||
*/
|
||||
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
|
||||
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD) {
|
||||
fprintf(stderr,"Connection to database '%s' failed.\n", database);
|
||||
fprintf(stderr,"%s",PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
res = PQexec(conn, "begin");
|
||||
PQclear(res);
|
||||
printf("importing file \"%s\" ...\n", in_filename);
|
||||
/* lobjOid = importFile(conn, in_filename); */
|
||||
lobjOid = lo_import(conn, in_filename);
|
||||
if (lobjOid == 0)
|
||||
{
|
||||
fprintf(stderr,"%s\n",PQerrorMessage(conn));
|
||||
}
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD)
|
||||
{
|
||||
fprintf(stderr, "Connection to database '%s' failed.\n", database);
|
||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
res = PQexec(conn, "begin");
|
||||
PQclear(res);
|
||||
printf("importing file \"%s\" ...\n", in_filename);
|
||||
/* lobjOid = importFile(conn, in_filename); */
|
||||
lobjOid = lo_import(conn, in_filename);
|
||||
if (lobjOid == 0)
|
||||
{
|
||||
fprintf(stderr, "%s\n", PQerrorMessage(conn));
|
||||
}
|
||||
/*
|
||||
printf("\tas large object %d.\n", lobjOid);
|
||||
printf("\tas large object %d.\n", lobjOid);
|
||||
|
||||
printf("picking out bytes 1000-2000 of the large object\n");
|
||||
pickout(conn, lobjOid, 1000, 1000);
|
||||
printf("picking out bytes 1000-2000 of the large object\n");
|
||||
pickout(conn, lobjOid, 1000, 1000);
|
||||
|
||||
printf("overwriting bytes 1000-2000 of the large object with X's\n");
|
||||
overwrite(conn, lobjOid, 1000, 1000);
|
||||
printf("overwriting bytes 1000-2000 of the large object with X's\n");
|
||||
overwrite(conn, lobjOid, 1000, 1000);
|
||||
*/
|
||||
|
||||
printf("exporting large object to file \"%s\" ...\n", out_filename);
|
||||
/* exportFile(conn, lobjOid, out_filename); */
|
||||
if (!lo_export(conn, lobjOid,out_filename))
|
||||
{
|
||||
fprintf(stderr,"%s\n",PQerrorMessage(conn));
|
||||
}
|
||||
printf("exporting large object to file \"%s\" ...\n", out_filename);
|
||||
/* exportFile(conn, lobjOid, out_filename); */
|
||||
if (!lo_export(conn, lobjOid, out_filename))
|
||||
{
|
||||
fprintf(stderr, "%s\n", PQerrorMessage(conn));
|
||||
}
|
||||
|
||||
res = PQexec(conn, "end");
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
exit(0);
|
||||
res = PQexec(conn, "end");
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
exit(0);
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* lotest.c--
|
||||
* test using large objects with libpq
|
||||
* test using large objects with libpq
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/test/examples/Attic/testlo2.c,v 1.2 1997/08/12 22:55:21 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/test/examples/Attic/testlo2.c,v 1.3 1997/09/07 05:04:08 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -15,219 +15,241 @@
|
||||
#include "libpq-fe.h"
|
||||
#include "libpq/libpq-fs.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
#define BUFSIZE 1024
|
||||
|
||||
/*
|
||||
* importFile -
|
||||
* import file "in_filename" into database as large object "lobjOid"
|
||||
* import file "in_filename" into database as large object "lobjOid"
|
||||
*
|
||||
*/
|
||||
Oid importFile(PGconn *conn, char *filename)
|
||||
Oid
|
||||
importFile(PGconn * conn, char *filename)
|
||||
{
|
||||
Oid lobjId;
|
||||
int lobj_fd;
|
||||
char buf[BUFSIZE];
|
||||
int nbytes, tmp;
|
||||
int fd;
|
||||
Oid lobjId;
|
||||
int lobj_fd;
|
||||
char buf[BUFSIZE];
|
||||
int nbytes,
|
||||
tmp;
|
||||
int fd;
|
||||
|
||||
/*
|
||||
* open the file to be read in
|
||||
*/
|
||||
fd = open(filename, O_RDONLY, 0666);
|
||||
if (fd < 0) { /* error */
|
||||
fprintf(stderr, "can't open unix file\"%s\"\n", filename);
|
||||
}
|
||||
|
||||
/*
|
||||
* create the large object
|
||||
*/
|
||||
lobjId = lo_creat(conn, INV_READ|INV_WRITE);
|
||||
if (lobjId == 0) {
|
||||
fprintf(stderr, "can't create large object");
|
||||
}
|
||||
|
||||
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
|
||||
/*
|
||||
* read in from the Unix file and write to the inversion file
|
||||
*/
|
||||
while ((nbytes = read(fd, buf, BUFSIZE)) > 0) {
|
||||
tmp = lo_write(conn, lobj_fd, buf, nbytes);
|
||||
if (tmp < nbytes) {
|
||||
fprintf(stderr, "error while reading \"%s\"", filename);
|
||||
/*
|
||||
* open the file to be read in
|
||||
*/
|
||||
fd = open(filename, O_RDONLY, 0666);
|
||||
if (fd < 0)
|
||||
{ /* error */
|
||||
fprintf(stderr, "can't open unix file\"%s\"\n", filename);
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
lo_close(conn, lobj_fd);
|
||||
|
||||
return lobjId;
|
||||
/*
|
||||
* create the large object
|
||||
*/
|
||||
lobjId = lo_creat(conn, INV_READ | INV_WRITE);
|
||||
if (lobjId == 0)
|
||||
{
|
||||
fprintf(stderr, "can't create large object");
|
||||
}
|
||||
|
||||
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
|
||||
|
||||
/*
|
||||
* read in from the Unix file and write to the inversion file
|
||||
*/
|
||||
while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
|
||||
{
|
||||
tmp = lo_write(conn, lobj_fd, buf, nbytes);
|
||||
if (tmp < nbytes)
|
||||
{
|
||||
fprintf(stderr, "error while reading \"%s\"", filename);
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
lo_close(conn, lobj_fd);
|
||||
|
||||
return lobjId;
|
||||
}
|
||||
|
||||
void pickout(PGconn *conn, Oid lobjId, int start, int len)
|
||||
void
|
||||
pickout(PGconn * conn, Oid lobjId, int start, int len)
|
||||
{
|
||||
int lobj_fd;
|
||||
char* buf;
|
||||
int nbytes;
|
||||
int nread;
|
||||
int lobj_fd;
|
||||
char *buf;
|
||||
int nbytes;
|
||||
int nread;
|
||||
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0) {
|
||||
fprintf(stderr,"can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0)
|
||||
{
|
||||
fprintf(stderr, "can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
|
||||
lo_lseek(conn, lobj_fd, start, SEEK_SET);
|
||||
buf = malloc(len+1);
|
||||
|
||||
nread = 0;
|
||||
while (len - nread > 0) {
|
||||
nbytes = lo_read(conn, lobj_fd, buf, len - nread);
|
||||
buf[nbytes] = '\0';
|
||||
fprintf(stderr,">>> %s", buf);
|
||||
nread += nbytes;
|
||||
}
|
||||
fprintf(stderr,"\n");
|
||||
lo_close(conn, lobj_fd);
|
||||
lo_lseek(conn, lobj_fd, start, SEEK_SET);
|
||||
buf = malloc(len + 1);
|
||||
|
||||
nread = 0;
|
||||
while (len - nread > 0)
|
||||
{
|
||||
nbytes = lo_read(conn, lobj_fd, buf, len - nread);
|
||||
buf[nbytes] = '\0';
|
||||
fprintf(stderr, ">>> %s", buf);
|
||||
nread += nbytes;
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
lo_close(conn, lobj_fd);
|
||||
}
|
||||
|
||||
void overwrite(PGconn *conn, Oid lobjId, int start, int len)
|
||||
void
|
||||
overwrite(PGconn * conn, Oid lobjId, int start, int len)
|
||||
{
|
||||
int lobj_fd;
|
||||
char* buf;
|
||||
int nbytes;
|
||||
int nwritten;
|
||||
int i;
|
||||
int lobj_fd;
|
||||
char *buf;
|
||||
int nbytes;
|
||||
int nwritten;
|
||||
int i;
|
||||
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0) {
|
||||
fprintf(stderr,"can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0)
|
||||
{
|
||||
fprintf(stderr, "can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
|
||||
lo_lseek(conn, lobj_fd, start, SEEK_SET);
|
||||
buf = malloc(len+1);
|
||||
|
||||
for (i=0;i<len;i++)
|
||||
buf[i] = 'X';
|
||||
buf[i] = '\0';
|
||||
lo_lseek(conn, lobj_fd, start, SEEK_SET);
|
||||
buf = malloc(len + 1);
|
||||
|
||||
nwritten = 0;
|
||||
while (len - nwritten > 0) {
|
||||
nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
|
||||
nwritten += nbytes;
|
||||
}
|
||||
fprintf(stderr,"\n");
|
||||
lo_close(conn, lobj_fd);
|
||||
for (i = 0; i < len; i++)
|
||||
buf[i] = 'X';
|
||||
buf[i] = '\0';
|
||||
|
||||
nwritten = 0;
|
||||
while (len - nwritten > 0)
|
||||
{
|
||||
nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
|
||||
nwritten += nbytes;
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
lo_close(conn, lobj_fd);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* exportFile -
|
||||
* export large object "lobjOid" to file "out_filename"
|
||||
* export large object "lobjOid" to file "out_filename"
|
||||
*
|
||||
*/
|
||||
void exportFile(PGconn *conn, Oid lobjId, char *filename)
|
||||
void
|
||||
exportFile(PGconn * conn, Oid lobjId, char *filename)
|
||||
{
|
||||
int lobj_fd;
|
||||
char buf[BUFSIZE];
|
||||
int nbytes, tmp;
|
||||
int fd;
|
||||
int lobj_fd;
|
||||
char buf[BUFSIZE];
|
||||
int nbytes,
|
||||
tmp;
|
||||
int fd;
|
||||
|
||||
/*
|
||||
* create an inversion "object"
|
||||
*/
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0) {
|
||||
fprintf(stderr,"can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
|
||||
/*
|
||||
* open the file to be written to
|
||||
*/
|
||||
fd = open(filename, O_CREAT|O_WRONLY, 0666);
|
||||
if (fd < 0) { /* error */
|
||||
fprintf(stderr, "can't open unix file\"%s\"",
|
||||
filename);
|
||||
}
|
||||
|
||||
/*
|
||||
* read in from the Unix file and write to the inversion file
|
||||
*/
|
||||
while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0) {
|
||||
tmp = write(fd, buf, nbytes);
|
||||
if (tmp < nbytes) {
|
||||
fprintf(stderr,"error while writing \"%s\"",
|
||||
filename);
|
||||
/*
|
||||
* create an inversion "object"
|
||||
*/
|
||||
lobj_fd = lo_open(conn, lobjId, INV_READ);
|
||||
if (lobj_fd < 0)
|
||||
{
|
||||
fprintf(stderr, "can't open large object %d",
|
||||
lobjId);
|
||||
}
|
||||
}
|
||||
|
||||
lo_close(conn, lobj_fd);
|
||||
close(fd);
|
||||
/*
|
||||
* open the file to be written to
|
||||
*/
|
||||
fd = open(filename, O_CREAT | O_WRONLY, 0666);
|
||||
if (fd < 0)
|
||||
{ /* error */
|
||||
fprintf(stderr, "can't open unix file\"%s\"",
|
||||
filename);
|
||||
}
|
||||
|
||||
return;
|
||||
/*
|
||||
* read in from the Unix file and write to the inversion file
|
||||
*/
|
||||
while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
|
||||
{
|
||||
tmp = write(fd, buf, nbytes);
|
||||
if (tmp < nbytes)
|
||||
{
|
||||
fprintf(stderr, "error while writing \"%s\"",
|
||||
filename);
|
||||
}
|
||||
}
|
||||
|
||||
lo_close(conn, lobj_fd);
|
||||
close(fd);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
exit_nicely(PGconn* conn)
|
||||
void
|
||||
exit_nicely(PGconn * conn)
|
||||
{
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *in_filename, *out_filename;
|
||||
char *database;
|
||||
Oid lobjOid;
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
char *in_filename,
|
||||
*out_filename;
|
||||
char *database;
|
||||
Oid lobjOid;
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
|
||||
if (argc != 4) {
|
||||
fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
|
||||
argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
|
||||
argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
database = argv[1];
|
||||
in_filename = argv[2];
|
||||
out_filename = argv[3];
|
||||
database = argv[1];
|
||||
in_filename = argv[2];
|
||||
out_filename = argv[3];
|
||||
|
||||
/*
|
||||
* set up the connection
|
||||
*/
|
||||
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
|
||||
/*
|
||||
* set up the connection
|
||||
*/
|
||||
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
|
||||
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD) {
|
||||
fprintf(stderr,"Connection to database '%s' failed.\n", database);
|
||||
fprintf(stderr,"%s",PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
res = PQexec(conn, "begin");
|
||||
PQclear(res);
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD)
|
||||
{
|
||||
fprintf(stderr, "Connection to database '%s' failed.\n", database);
|
||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
printf("importing file \"%s\" ...\n", in_filename);
|
||||
/* lobjOid = importFile(conn, in_filename); */
|
||||
lobjOid = lo_import(conn, in_filename);
|
||||
res = PQexec(conn, "begin");
|
||||
PQclear(res);
|
||||
|
||||
printf("importing file \"%s\" ...\n", in_filename);
|
||||
/* lobjOid = importFile(conn, in_filename); */
|
||||
lobjOid = lo_import(conn, in_filename);
|
||||
/*
|
||||
printf("\tas large object %d.\n", lobjOid);
|
||||
printf("\tas large object %d.\n", lobjOid);
|
||||
|
||||
printf("picking out bytes 1000-2000 of the large object\n");
|
||||
pickout(conn, lobjOid, 1000, 1000);
|
||||
printf("picking out bytes 1000-2000 of the large object\n");
|
||||
pickout(conn, lobjOid, 1000, 1000);
|
||||
|
||||
printf("overwriting bytes 1000-2000 of the large object with X's\n");
|
||||
overwrite(conn, lobjOid, 1000, 1000);
|
||||
printf("overwriting bytes 1000-2000 of the large object with X's\n");
|
||||
overwrite(conn, lobjOid, 1000, 1000);
|
||||
*/
|
||||
|
||||
printf("exporting large object to file \"%s\" ...\n", out_filename);
|
||||
/* exportFile(conn, lobjOid, out_filename); */
|
||||
lo_export(conn, lobjOid,out_filename);
|
||||
printf("exporting large object to file \"%s\" ...\n", out_filename);
|
||||
/* exportFile(conn, lobjOid, out_filename); */
|
||||
lo_export(conn, lobjOid, out_filename);
|
||||
|
||||
res = PQexec(conn, "end");
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
exit(0);
|
||||
res = PQexec(conn, "end");
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
exit(0);
|
||||
}
|
||||
|
@ -1,283 +1,300 @@
|
||||
/*
|
||||
* $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.9 1997/08/19 21:40:56 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.10 1997/09/07 05:04:10 momjian Exp $
|
||||
*/
|
||||
|
||||
#include <float.h> /* faked on sunos */
|
||||
#include <float.h> /* faked on sunos */
|
||||
#include <stdio.h>
|
||||
#include <string.h> /* for memset() */
|
||||
#include <string.h> /* for memset() */
|
||||
|
||||
#include <postgres.h>
|
||||
|
||||
#include "utils/geo_decls.h" /* includes <math.h> */
|
||||
#include "executor/executor.h" /* For GetAttributeByName */
|
||||
#include "executor/executor.h" /* For GetAttributeByName */
|
||||
|
||||
#define P_MAXDIG 12
|
||||
#define LDELIM '('
|
||||
#define RDELIM ')'
|
||||
#define DELIM ','
|
||||
#define LDELIM '('
|
||||
#define RDELIM ')'
|
||||
#define DELIM ','
|
||||
|
||||
typedef void *TUPLE;
|
||||
typedef void *TUPLE;
|
||||
|
||||
extern double *regress_dist_ptpath (Point *pt, PATH *path);
|
||||
extern double *regress_path_dist (PATH *p1, PATH *p2);
|
||||
extern PATH *poly2path (POLYGON *poly);
|
||||
extern Point *interpt_pp (PATH *p1, PATH *p2);
|
||||
extern void regress_lseg_construct (LSEG *lseg, Point *pt1, Point *pt2);
|
||||
extern char overpaid (TUPLE tuple);
|
||||
extern int boxarea (BOX *box);
|
||||
extern char *reverse_c16 (char *string);
|
||||
extern double *regress_dist_ptpath(Point * pt, PATH * path);
|
||||
extern double *regress_path_dist(PATH * p1, PATH * p2);
|
||||
extern PATH *poly2path(POLYGON * poly);
|
||||
extern Point *interpt_pp(PATH * p1, PATH * p2);
|
||||
extern void regress_lseg_construct(LSEG * lseg, Point * pt1, Point * pt2);
|
||||
extern char overpaid(TUPLE tuple);
|
||||
extern int boxarea(BOX * box);
|
||||
extern char *reverse_c16(char *string);
|
||||
|
||||
/*
|
||||
** Distance from a point to a path
|
||||
** Distance from a point to a path
|
||||
*/
|
||||
double *
|
||||
double *
|
||||
regress_dist_ptpath(pt, path)
|
||||
Point *pt;
|
||||
PATH *path;
|
||||
Point *pt;
|
||||
PATH *path;
|
||||
{
|
||||
double *result;
|
||||
double *tmp;
|
||||
int i;
|
||||
LSEG lseg;
|
||||
double *result;
|
||||
double *tmp;
|
||||
int i;
|
||||
LSEG lseg;
|
||||
|
||||
switch (path->npts) {
|
||||
case 0:
|
||||
result = PALLOCTYPE(double);
|
||||
*result = Abs((double) DBL_MAX); /* +infinity */
|
||||
break;
|
||||
case 1:
|
||||
result = point_distance(pt, &path->p[0]);
|
||||
break;
|
||||
default:
|
||||
/*
|
||||
* the distance from a point to a path is the smallest distance
|
||||
* from the point to any of its constituent segments.
|
||||
*/
|
||||
Assert(path->npts > 1);
|
||||
result = PALLOCTYPE(double);
|
||||
for (i = 0; i < path->npts - 1; ++i) {
|
||||
regress_lseg_construct(&lseg, &path->p[i], &path->p[i+1]);
|
||||
tmp = dist_ps(pt, &lseg);
|
||||
if (i == 0 || *tmp < *result)
|
||||
*result = *tmp;
|
||||
PFREE(tmp);
|
||||
switch (path->npts)
|
||||
{
|
||||
case 0:
|
||||
result = PALLOCTYPE(double);
|
||||
*result = Abs((double) DBL_MAX); /* +infinity */
|
||||
break;
|
||||
case 1:
|
||||
result = point_distance(pt, &path->p[0]);
|
||||
break;
|
||||
default:
|
||||
|
||||
/*
|
||||
* the distance from a point to a path is the smallest distance
|
||||
* from the point to any of its constituent segments.
|
||||
*/
|
||||
Assert(path->npts > 1);
|
||||
result = PALLOCTYPE(double);
|
||||
for (i = 0; i < path->npts - 1; ++i)
|
||||
{
|
||||
regress_lseg_construct(&lseg, &path->p[i], &path->p[i + 1]);
|
||||
tmp = dist_ps(pt, &lseg);
|
||||
if (i == 0 || *tmp < *result)
|
||||
*result = *tmp;
|
||||
PFREE(tmp);
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return(result);
|
||||
return (result);
|
||||
}
|
||||
|
||||
/* this essentially does a cartesian product of the lsegs in the
|
||||
two paths, and finds the min distance between any two lsegs */
|
||||
double *
|
||||
double *
|
||||
regress_path_dist(p1, p2)
|
||||
PATH *p1;
|
||||
PATH *p2;
|
||||
PATH *p1;
|
||||
PATH *p2;
|
||||
{
|
||||
double *min, *tmp;
|
||||
int i,j;
|
||||
LSEG seg1, seg2;
|
||||
double *min,
|
||||
*tmp;
|
||||
int i,
|
||||
j;
|
||||
LSEG seg1,
|
||||
seg2;
|
||||
|
||||
regress_lseg_construct(&seg1, &p1->p[0], &p1->p[1]);
|
||||
regress_lseg_construct(&seg2, &p2->p[0], &p2->p[1]);
|
||||
min = lseg_distance(&seg1, &seg2);
|
||||
regress_lseg_construct(&seg1, &p1->p[0], &p1->p[1]);
|
||||
regress_lseg_construct(&seg2, &p2->p[0], &p2->p[1]);
|
||||
min = lseg_distance(&seg1, &seg2);
|
||||
|
||||
for (i = 0; i < p1->npts - 1; i++)
|
||||
for (j = 0; j < p2->npts - 1; j++)
|
||||
{
|
||||
regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i+1]);
|
||||
regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j+1]);
|
||||
for (i = 0; i < p1->npts - 1; i++)
|
||||
for (j = 0; j < p2->npts - 1; j++)
|
||||
{
|
||||
regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
|
||||
regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
|
||||
|
||||
if (*min < *(tmp = lseg_distance(&seg1, &seg2)))
|
||||
*min = *tmp;
|
||||
PFREE(tmp);
|
||||
}
|
||||
if (*min < *(tmp = lseg_distance(&seg1, &seg2)))
|
||||
*min = *tmp;
|
||||
PFREE(tmp);
|
||||
}
|
||||
|
||||
return(min);
|
||||
return (min);
|
||||
}
|
||||
|
||||
PATH *
|
||||
PATH *
|
||||
poly2path(poly)
|
||||
POLYGON *poly;
|
||||
POLYGON *poly;
|
||||
{
|
||||
int i;
|
||||
char *output = (char *)PALLOC(2*(P_MAXDIG + 1)*poly->npts + 64);
|
||||
char buf[2*(P_MAXDIG)+20];
|
||||
int i;
|
||||
char *output = (char *) PALLOC(2 * (P_MAXDIG + 1) * poly->npts + 64);
|
||||
char buf[2 * (P_MAXDIG) + 20];
|
||||
|
||||
sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
|
||||
sprintf(output, "(1, %*d", P_MAXDIG, poly->npts);
|
||||
|
||||
for (i=0; i<poly->npts; i++)
|
||||
{
|
||||
sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
|
||||
strcat(output, buf);
|
||||
}
|
||||
for (i = 0; i < poly->npts; i++)
|
||||
{
|
||||
sprintf(buf, ",%*g,%*g", P_MAXDIG, poly->p[i].x, P_MAXDIG, poly->p[i].y);
|
||||
strcat(output, buf);
|
||||
}
|
||||
|
||||
sprintf(buf, "%c", RDELIM);
|
||||
strcat(output, buf);
|
||||
return(path_in(output));
|
||||
sprintf(buf, "%c", RDELIM);
|
||||
strcat(output, buf);
|
||||
return (path_in(output));
|
||||
}
|
||||
|
||||
/* return the point where two paths intersect. Assumes that they do. */
|
||||
Point *
|
||||
interpt_pp(p1,p2)
|
||||
PATH *p1;
|
||||
PATH *p2;
|
||||
/* return the point where two paths intersect. Assumes that they do. */
|
||||
Point *
|
||||
interpt_pp(p1, p2)
|
||||
PATH *p1;
|
||||
PATH *p2;
|
||||
{
|
||||
|
||||
Point *retval;
|
||||
int i,j;
|
||||
LSEG seg1, seg2;
|
||||
#if FALSE
|
||||
LINE *ln;
|
||||
#endif
|
||||
bool found; /* We've found the intersection */
|
||||
|
||||
found = false; /* Haven't found it yet */
|
||||
|
||||
for (i = 0; i < p1->npts - 1 && !found; i++)
|
||||
for (j = 0; j < p2->npts - 1 && !found; j++)
|
||||
{
|
||||
regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i+1]);
|
||||
regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j+1]);
|
||||
if (lseg_intersect(&seg1, &seg2)) found = true;
|
||||
}
|
||||
Point *retval;
|
||||
int i,
|
||||
j;
|
||||
LSEG seg1,
|
||||
seg2;
|
||||
|
||||
#if FALSE
|
||||
ln = line_construct_pp(&seg2.p[0], &seg2.p[1]);
|
||||
retval = interpt_sl(&seg1, ln);
|
||||
LINE *ln;
|
||||
|
||||
#endif
|
||||
retval = lseg_interpt( &seg1, &seg2);
|
||||
|
||||
return(retval);
|
||||
bool found; /* We've found the intersection */
|
||||
|
||||
found = false; /* Haven't found it yet */
|
||||
|
||||
for (i = 0; i < p1->npts - 1 && !found; i++)
|
||||
for (j = 0; j < p2->npts - 1 && !found; j++)
|
||||
{
|
||||
regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i + 1]);
|
||||
regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j + 1]);
|
||||
if (lseg_intersect(&seg1, &seg2))
|
||||
found = true;
|
||||
}
|
||||
|
||||
#if FALSE
|
||||
ln = line_construct_pp(&seg2.p[0], &seg2.p[1]);
|
||||
retval = interpt_sl(&seg1, ln);
|
||||
#endif
|
||||
retval = lseg_interpt(&seg1, &seg2);
|
||||
|
||||
return (retval);
|
||||
}
|
||||
|
||||
|
||||
/* like lseg_construct, but assume space already allocated */
|
||||
void
|
||||
void
|
||||
regress_lseg_construct(lseg, pt1, pt2)
|
||||
LSEG *lseg;
|
||||
Point *pt1;
|
||||
Point *pt2;
|
||||
LSEG *lseg;
|
||||
Point *pt1;
|
||||
Point *pt2;
|
||||
{
|
||||
lseg->p[0].x = pt1->x;
|
||||
lseg->p[0].y = pt1->y;
|
||||
lseg->p[1].x = pt2->x;
|
||||
lseg->p[1].y = pt2->y;
|
||||
lseg->m = point_sl(pt1, pt2);
|
||||
lseg->p[0].x = pt1->x;
|
||||
lseg->p[0].y = pt1->y;
|
||||
lseg->p[1].x = pt2->x;
|
||||
lseg->p[1].y = pt2->y;
|
||||
lseg->m = point_sl(pt1, pt2);
|
||||
}
|
||||
|
||||
|
||||
char overpaid(tuple)
|
||||
TUPLE tuple;
|
||||
char
|
||||
overpaid(tuple)
|
||||
TUPLE tuple;
|
||||
{
|
||||
bool isnull;
|
||||
long salary;
|
||||
bool isnull;
|
||||
long salary;
|
||||
|
||||
salary = (long)GetAttributeByName(tuple, "salary", &isnull);
|
||||
return(salary > 699);
|
||||
salary = (long) GetAttributeByName(tuple, "salary", &isnull);
|
||||
return (salary > 699);
|
||||
}
|
||||
|
||||
/* New type "widget"
|
||||
* This used to be "circle", but I added circle to builtins,
|
||||
* so needed to make sure the names do not collide. - tgl 97/04/21
|
||||
* so needed to make sure the names do not collide. - tgl 97/04/21
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Point center;
|
||||
double radius;
|
||||
} WIDGET;
|
||||
typedef struct
|
||||
{
|
||||
Point center;
|
||||
double radius;
|
||||
} WIDGET;
|
||||
|
||||
WIDGET *widget_in (char *str);
|
||||
char *widget_out (WIDGET *widget);
|
||||
int pt_in_widget (Point *point, WIDGET *widget);
|
||||
WIDGET *widget_in(char *str);
|
||||
char *widget_out(WIDGET * widget);
|
||||
int pt_in_widget(Point * point, WIDGET * widget);
|
||||
|
||||
#define NARGS 3
|
||||
|
||||
WIDGET *
|
||||
WIDGET *
|
||||
widget_in(str)
|
||||
char *str;
|
||||
char *str;
|
||||
{
|
||||
char *p, *coord[NARGS], buf2[1000];
|
||||
int i;
|
||||
WIDGET *result;
|
||||
char *p,
|
||||
*coord[NARGS],
|
||||
buf2[1000];
|
||||
int i;
|
||||
WIDGET *result;
|
||||
|
||||
if (str == NULL)
|
||||
return(NULL);
|
||||
return (NULL);
|
||||
for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
|
||||
if (*p == ',' || (*p == LDELIM && !i))
|
||||
coord[i++] = p + 1;
|
||||
if (i < NARGS - 1)
|
||||
return(NULL);
|
||||
return (NULL);
|
||||
result = (WIDGET *) palloc(sizeof(WIDGET));
|
||||
result->center.x = atof(coord[0]);
|
||||
result->center.y = atof(coord[1]);
|
||||
result->radius = atof(coord[2]);
|
||||
|
||||
sprintf(buf2, "widget_in: read (%f, %f, %f)\n", result->center.x,
|
||||
result->center.y,result->radius);
|
||||
return(result);
|
||||
result->center.y, result->radius);
|
||||
return (result);
|
||||
}
|
||||
|
||||
char *
|
||||
char *
|
||||
widget_out(widget)
|
||||
WIDGET *widget;
|
||||
WIDGET *widget;
|
||||
{
|
||||
char *result;
|
||||
char *result;
|
||||
|
||||
if (widget == NULL)
|
||||
return(NULL);
|
||||
if (widget == NULL)
|
||||
return (NULL);
|
||||
|
||||
result = (char *) palloc(60);
|
||||
sprintf(result, "(%g,%g,%g)",
|
||||
widget->center.x, widget->center.y, widget->radius);
|
||||
return(result);
|
||||
result = (char *) palloc(60);
|
||||
sprintf(result, "(%g,%g,%g)",
|
||||
widget->center.x, widget->center.y, widget->radius);
|
||||
return (result);
|
||||
}
|
||||
|
||||
int
|
||||
int
|
||||
pt_in_widget(point, widget)
|
||||
Point *point;
|
||||
WIDGET *widget;
|
||||
Point *point;
|
||||
WIDGET *widget;
|
||||
{
|
||||
extern double point_dt();
|
||||
|
||||
return( point_dt(point, &widget->center) < widget->radius );
|
||||
return (point_dt(point, &widget->center) < widget->radius);
|
||||
}
|
||||
|
||||
#define ABS(X) ((X) > 0 ? (X) : -(X))
|
||||
|
||||
int
|
||||
int
|
||||
boxarea(box)
|
||||
|
||||
BOX *box;
|
||||
BOX *box;
|
||||
|
||||
{
|
||||
int width, height;
|
||||
int width,
|
||||
height;
|
||||
|
||||
width = ABS(box->high.x - box->low.x);
|
||||
width = ABS(box->high.x - box->low.x);
|
||||
height = ABS(box->high.y - box->low.y);
|
||||
return (width * height);
|
||||
}
|
||||
|
||||
char *
|
||||
char *
|
||||
reverse_c16(string)
|
||||
char *string;
|
||||
char *string;
|
||||
{
|
||||
register i;
|
||||
int len;
|
||||
char *new_string;
|
||||
register i;
|
||||
int len;
|
||||
char *new_string;
|
||||
|
||||
if (!(new_string = palloc(16))) {
|
||||
fprintf(stderr, "reverse_c16: palloc failed\n");
|
||||
return(NULL);
|
||||
}
|
||||
memset(new_string, 0, 16);
|
||||
for (i = 0; i < 16 && string[i]; ++i)
|
||||
;
|
||||
if (i == 16 || !string[i])
|
||||
--i;
|
||||
len = i;
|
||||
for (; i >= 0; --i)
|
||||
new_string[len-i] = string[i];
|
||||
return(new_string);
|
||||
if (!(new_string = palloc(16)))
|
||||
{
|
||||
fprintf(stderr, "reverse_c16: palloc failed\n");
|
||||
return (NULL);
|
||||
}
|
||||
memset(new_string, 0, 16);
|
||||
for (i = 0; i < 16 && string[i]; ++i)
|
||||
;
|
||||
if (i == 16 || !string[i])
|
||||
--i;
|
||||
len = i;
|
||||
for (; i >= 0; --i)
|
||||
new_string[len - i] = string[i];
|
||||
return (new_string);
|
||||
}
|
||||
|
Reference in New Issue
Block a user