#include "wkbtest.h"
create table wkbreader_test (interesting bool,comments varchar(100), the_geom geometry);
insert into wkbreader_test values ( false, 'simple point', 'POINT( 10 10)' );
insert into wkbreader_test values ( false, 'simple line' , 'LINESTRING(0 0, 10 0, 10 10, 0 10, 0 0)');
insert into wkbreader_test values( true, 'polygon w/ hole','POLYGON( (0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 7 5, 7 7 , 5 7 , 5 5))');
dont forget to set the port, dbname, host, and user in the
environment so the postgres connection will work
Also, change the "declare cursor" command so it returns the columns you want, and converts the geometry
into wkb.
*/
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
void dump_bytes( char *a, int numb)
{
int t;
for (t=0; t<numb; t++)
{
printf(" + Byte #%i has value %i (%x)\n",t,a[t],a[t]);
}
}
int find_WKB_typeid(PGconn *conn)
{
PGresult *dbresult;
char *num;
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "no connection to db\n");
exit_nicely(conn);
}
dbresult = PQexec(conn, "select OID from pg_type where typname = 'bytea';");
if (PQresultStatus(dbresult) != PGRES_TUPLES_OK)
{
fprintf(stderr, "couldnt execute query to find oid of geometry type");
exit_nicely(conn);
}
if ( PQntuples(dbresult) != 1)
{
fprintf(stderr, "query to find oid of geometry didnt return 1 row!");
exit_nicely(conn);
}
num = PQgetvalue(dbresult, 0, 0);
PQclear(dbresult);
return ( atoi(num) );
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int row,
field;
PGconn *conn;
PGresult *res;
int junk;
char *field_name;
int field_type;
int WKB_OID;
char conn_string[255];
bool *bool_val;
int *int_val;
float *float_val;
double *double_val;
char *char_val;
char *wkb_val;
char *table_name = "wkbreader_test";
char query_str[1000];
conn = PQconnectdb("");
* check to see that the backend connection was successfully made
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
WKB_OID = find_WKB_typeid(conn);
res = PQexec(conn, "BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "%s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
* should PQclear PGresult whenever it is no longer needed to avoid
* memory leaks
*/
PQclear(res);
* fetch rows from the pg_database, the system catalog of
* databases
*/
sprintf(query_str, "DECLARE mycursor BINARY CURSOR FOR select text(num), asbinary(the_geom,'ndr') as wkb from %s", table_name);
printf(query_str);
printf("\n");
res = PQexec(conn, query_str);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "DECLARE CURSOR command failed\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "FETCH ALL in mycursor");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
for (row=0; row< PQntuples(res); row++)
{
printf("------------------------------row %i --------------------------\n",row);
for (field =0 ; field< PQnfields(res); field++)
{
field_type =PQftype(res,field);
field_name = PQfname(res, field);
if (field_type ==16)
{
bool_val = (bool *) PQgetvalue(res, row, field);
if (*bool_val)
printf("%s: TRUE\n",field_name);
else
printf("%s: FALSE\n",field_name);
}
else if (field_type ==23 )
{
int_val = (int *) PQgetvalue(res, row, field);
printf("%s: %i\n",field_name,*int_val);
}
else if (field_type ==700 )
{
float_val = (float *) PQgetvalue(res, row, field);
printf("%s: %g\n",field_name,*float_val);
}
else if (field_type ==701 )
{
double_val = (double *) PQgetvalue(res, row, field);
printf("%s: %g\n",field_name,*double_val);
}
else if ( (field_type ==1043 ) || (field_type==25) )
{
char_val = (char *) PQgetvalue(res, row, field);
printf("%s: %s\n",field_name,char_val);
}
else if (field_type == WKB_OID )
{
char_val = (char *) PQgetvalue(res, row, field);
printf("%s: ", field_name);
decode_wkb(char_val, &junk);
printf("\n");
}
}
}
PQclear(res);
res = PQexec(conn, "CLOSE mycursor");
PQclear(res);
res = PQexec(conn, "COMMIT");
PQclear(res);
PQfinish(conn);
return 0;
}