*
**********************************************************************
* $Log$
+ * Revision 1.40 2003/12/27 13:30:23 strk
+ * Added schema specification support
+ *
* Revision 1.39 2003/12/19 18:55:46 strk
* substituted setenv() calls with putenv() for Solaris support
*
/* Global data */
PGconn *conn;
int rowbuflen;
-char *geo_col_name, *table, *shp_file;
+char *geo_col_name, *table, *shp_file, *schema;
int type_ary[256];
char *main_scan_query;
DBFHandle dbf;
int initShapefile(char *shp_file, PGresult *res);
int initialize(void);
int getGeometryOID(PGconn *conn);
-int getGeometryType(char *table, char *geo_col_name);
+int getGeometryType(char *schema, char *table, char *geo_col_name);
SHPObject *create_lines(char *str,int shape_id, SHPHandle shp,int dims);
SHPObject *create_multilines(char *str,int shape_id, SHPHandle shp,int dims);
SHPObject *create_points(char *str,int shape_id, SHPHandle shp,int dims);
geotype=-1;
shape_creator = NULL;
table = NULL;
+ schema = NULL;
geo_col_name = NULL;
shp_file = NULL;
main_scan_query = NULL;
} else {
sprintf(query, "DECLARE cur CURSOR FOR %s", main_scan_query);
}
-fprintf(stderr, "MAINSCAN: %s\n", main_scan_query);
+//fprintf(stderr, "MAINSCAN: %s\n", main_scan_query);
res = PQexec(conn, query);
free(query);
if ( ! res || PQresultStatus(res) != PGRES_COMMAND_OK ) {
* Return 0 on unknown or unsupported geometry type
*/
int
-getGeometryType(char *table, char *geo_col_name)
+getGeometryType(char *schema, char *table, char *geo_col_name)
{
char query[1024];
PGresult *res;
char *geo_str; // the geometry type string
//get what kind of Geometry type is in the table
- sprintf(query, "SELECT DISTINCT geometrytype(\"%s\") FROM \"%s\" WHERE NOT geometrytype(\"%s\") IS NULL", geo_col_name, table, geo_col_name);
+ if ( schema )
+ {
+ sprintf(query, "SELECT DISTINCT geometrytype(\"%s\") "
+ "FROM \"%s\".\"%s\" WHERE NOT geometrytype(\"%s\") "
+ "IS NULL", geo_col_name, schema, table, geo_col_name);
+ }
+ else
+ {
+ sprintf(query, "SELECT DISTINCT geometrytype(\"%s\") "
+ "FROM \"%s\" WHERE NOT geometrytype(\"%s\") IS NULL",
+ geo_col_name, table, geo_col_name);
+ }
//printf("\n\n-->%s\n\n",query);
res = PQexec(conn, query);
void
usage(status)
{
- printf("USAGE: pgsql2shp [<options>] <database> <table>\n");
+ printf("USAGE: pgsql2shp [<options>] <database> [<schema>.]<table>\n");
printf("\n");
printf("OPTIONS:\n");
printf(" -d Set the dump file to 3 dimensions, if this option is not used\n");
int parse_commandline(int ARGC, char **ARGV)
{
int c, curindex;
- char buf[256];
+ char buf[256], *ptr;
buf[255] = '\0'; // just in case...
putenv(strdup(buf));
}else if(curindex == 1){
table = ARGV[optind];
+ if ( (ptr=strchr(table, '.')) )
+ {
+ *ptr = '\0';
+ schema = table;
+ table = ptr+1;
+ }
}
curindex++;
}
int geom_fld = -1;
char *mainscan_flds[256];
int mainscan_nflds=0;
+ int size;
/* Query user attributes name, type and size */
- query = (char *)malloc(sizeof(table)+256);
+
+ size = strlen(table);
+ if ( schema ) size += strlen(schema);
+ size += 256;
+
+ query = (char *)malloc(size);
if ( ! query ) return 0; // out of virtual memory
- sprintf(query, "SELECT a.attname, a.atttypid, a.attlen FROM "
+
+ if ( schema )
+ {
+ sprintf(query, "SELECT a.attname, a.atttypid, a.attlen FROM "
+ "pg_attribute a, pg_class c, pg_namespace n WHERE "
+ "n.nspname = '%s' AND a.attrelid = c.oid AND "
+ "a.attnum > 0 AND c.relname = '%s'", schema, table);
+ }
+ else
+ {
+ sprintf(query, "SELECT a.attname, a.atttypid, a.attlen FROM "
"pg_attribute a, pg_class c WHERE "
"a.attrelid = c.oid and a.attnum > 0 AND "
"c.relname = '%s'", table);
+ }
+
/* Exec query */
//fprintf(stderr, "Attribute query:\n%s\n", query);
return 0;
}
+
/* Get geometry oid */
geo_oid = getGeometryOID(conn);
if ( geo_oid == -1 )
* We now create the appropriate shape (shp) file.
* And set the shape creator function.
*/
- geotype = getGeometryType(table, geo_col_name);
+ geotype = getGeometryType(schema, table, geo_col_name);
if ( geotype == -1 ) return 0;
switch (geotype)
strcat(main_scan_query, buf);
}
- sprintf(buf, " FROM \"%s\"", table);
+
+ if ( schema )
+ {
+ sprintf(buf, " FROM \"%s\".\"%s\"", schema, table);
+ }
+ else
+ {
+ sprintf(buf, " FROM \"%s\"", table);
+ }
+
strcat(main_scan_query, buf);
PQclear(res);