diff options
| author | Bruce Momjian | 1997-09-07 05:04:48 +0000 |
|---|---|---|
| committer | Bruce Momjian | 1997-09-07 05:04:48 +0000 |
| commit | 1ccd423235a48739d6f7a4d7889705b5f9ecc69b (patch) | |
| tree | 8001c4e839dfad8f29ceda7f8c5f5dbb8759b564 /src/test/examples | |
| parent | 8fecd4febf8357f3cc20383ed29ced484877d5ac (diff) | |
Massive commit to run PGINDENT on all *.c and *.h files.
Diffstat (limited to 'src/test/examples')
| -rw-r--r-- | src/test/examples/testlibpq.c | 213 | ||||
| -rw-r--r-- | src/test/examples/testlibpq2.c | 142 | ||||
| -rw-r--r-- | src/test/examples/testlibpq3.c | 263 | ||||
| -rw-r--r-- | src/test/examples/testlibpq4.c | 240 | ||||
| -rw-r--r-- | src/test/examples/testlo.c | 392 | ||||
| -rw-r--r-- | src/test/examples/testlo2.c | 378 |
6 files changed, 876 insertions, 752 deletions
diff --git a/src/test/examples/testlibpq.c b/src/test/examples/testlibpq.c index f3b9964416..97c1a1043e 100644 --- a/src/test/examples/testlibpq.c +++ b/src/test/examples/testlibpq.c @@ -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 */ - - 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 */ - dbName = "template1"; - - /* 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); - } + FILE *debug; + +#endif /* DEBUG */ + + 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 */ + dbName = "template1"; + + /* 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 */ - - /* 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); - - /* 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); - - 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"); - - /* 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"); - } - - 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); + 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); + + /* + * 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); + + 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"); + + /* 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"); + } + + 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); } - - diff --git a/src/test/examples/testlibpq2.c b/src/test/examples/testlibpq2.c index 801feec9bc..d38ea1e906 100644 --- a/src/test/examples/testlibpq2.c +++ b/src/test/examples/testlibpq2.c @@ -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; - - 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*/ - - /* 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); - } - - 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); - - 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); + char *pghost, + *pgport, + *pgoptions, + *pgtty; + char *dbName; + int nFields; + int i, + j; + + 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 */ + + /* 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); + } + + 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); + + 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); } - - diff --git a/src/test/examples/testlibpq3.c b/src/test/examples/testlibpq3.c index bab22b3b1b..16035585fe 100644 --- a/src/test/examples/testlibpq3.c +++ b/src/test/examples/testlibpq3.c @@ -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; - - 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 */ - - 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); - - /* 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); - - /* 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) + char *pghost, + *pgport, + *pgoptions, + *pgtty; + char *dbName; + int nFields; + int i, + j; + int i_fnum, + d_fnum, + p_fnum; + + 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 */ + + 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); + + /* 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); - 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); - } - - 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); -} - + 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); + } + + 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); + } + 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); + +} diff --git a/src/test/examples/testlibpq4.c b/src/test/examples/testlibpq4.c index 5e04097119..1d6c8f7885 100644 --- a/src/test/examples/testlibpq4.c +++ b/src/test/examples/testlibpq4.c @@ -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; - - 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]; - - - /* 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); - - conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2); - check_conn(conn2); - - /* 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); - - /* 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); - - 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"); - - /* 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"); - } - - 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); */ + char *pghost, + *pgport, + *pgoptions, + *pgtty; + char *dbName1, + dbName2; + char *tblName; + int nFields; + int i, + j; + + 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]; + + + /* + * 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); + + conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2); + check_conn(conn2); + + /* 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); + + /* + * 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); + + 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"); + + /* 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"); + } + + 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); */ } - - diff --git a/src/test/examples/testlo.c b/src/test/examples/testlo.c index c9324f8942..ef207e373d 100644 --- a/src/test/examples/testlo.c +++ b/src/test/examples/testlo.c @@ -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; - - /* - * 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); + 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"); } - } - - close(fd); - lo_close(conn, lobj_fd); - return lobjId; + 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; - - 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); + 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); + } + + 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; - - 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'; - - 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); + 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); + } + + lo_lseek(conn, lobj_fd, start, SEEK_SET); + buf = malloc(len + 1); + + 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; - - /* - * 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); + 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); + } } - } - lo_close(conn, lobj_fd); - close(fd); + lo_close(conn, lobj_fd); + close(fd); - return; + 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; - - 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]; - - /* - * 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)); - } + 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); + } + + database = argv[1]; + in_filename = argv[2]; + out_filename = argv[3]; + + /* + * 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)); + } /* - 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)); - } - - res = PQexec(conn, "end"); - PQclear(res); - PQfinish(conn); - exit(0); + 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); } diff --git a/src/test/examples/testlo2.c b/src/test/examples/testlo2.c index 36a5ce4fa4..f915a3ba3e 100644 --- a/src/test/examples/testlo2.c +++ b/src/test/examples/testlo2.c @@ -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; - - /* - * 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); + 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); } - } - - 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; - - 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); + 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); + } + + 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; - - 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'; - - 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); + 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); + } + + lo_lseek(conn, lobj_fd, start, SEEK_SET); + buf = malloc(len + 1); + + 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; - - /* - * 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); + 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); } - } - lo_close(conn, lobj_fd); - close(fd); + /* + * 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; + 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; - - 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]; - - /* - * 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); + 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); + } + + database = argv[1]; + in_filename = argv[2]; + out_filename = argv[3]; + + /* + * 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); /* - 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); } |
