1341 int gidfound = 0, i, j, ret, status;
1355 int r = asprintf(&(state->
table),
"__pgsql2shp%lu_tmp_table", (
long)getpid());
1359 res = PQexec(state->
conn, query);
1363 if (PQresultStatus(
res) != PGRES_COMMAND_OK)
1382 int r = asprintf(&query,
"SELECT a.attname, a.atttypid, "
1383 "a.atttypmod, a.attlen FROM "
1384 "pg_attribute a, pg_class c, pg_namespace n WHERE "
1385 "n.nspname = '%s' AND a.attrelid = c.oid AND "
1386 "n.oid = c.relnamespace AND "
1387 "a.atttypid != 0 AND "
1388 "a.attnum > 0 AND c.relname = '%s'", state->
schema, state->
table);
1393 int r = asprintf(&query,
"SELECT a.attname, a.atttypid, "
1394 "a.atttypmod, a.attlen FROM "
1395 "pg_attribute a, pg_class c WHERE "
1396 "a.attrelid = c.oid and a.attnum > 0 AND "
1397 "a.atttypid != 0 AND "
1398 "c.relname = '%s' AND "
1399 "pg_catalog.pg_table_is_visible(c.oid)", state->
table);
1403 LWDEBUGF(3,
"query is: %s\n", query);
1405 res = PQexec(state->
conn, query);
1408 if (PQresultStatus(
res) != PGRES_TUPLES_OK)
1415 if (!PQntuples(
res))
1431 if ( getenv(
"PGCLIENTENCODING") )
1462 for (i = 0; i < PQntuples(
res); i++)
1466 int pgfieldtype, pgtypmod, pgfieldlen;
1469 int dbffieldtype, dbffieldsize, dbffielddecs;
1472 pgfieldname = PQgetvalue(
res, i, 0);
1473 pgfieldtype = atoi(PQgetvalue(
res, i, 1));
1474 pgtypmod = atoi(PQgetvalue(
res, i, 2));
1475 pgfieldlen = atoi(PQgetvalue(
res, i, 3));
1506 if (!strcmp(pgfieldname,
"gid") )
1528 dbffieldname =
malloc(11);
1529 strncpy(dbffieldname, ptr, 10);
1530 dbffieldname[10] =
'\0';
1539 strncpy(dbffieldname, mapped, 10);
1540 dbffieldname[10] =
'\0';
1550 if (!strncasecmp(dbffieldname, state->
dbffieldnames[j], 10))
1552 sprintf(dbffieldname,
"%.7s_%.2d", ptr, abs(tmpint) % 100);
1562 for (nameit = 0; nameit < strlen(dbffieldname); nameit++)
1563 dbffieldname[nameit] = toupper(dbffieldname[nameit]);
1567 if (strcasecmp(dbffieldname, pgfieldname))
1569 snprintf(buf,
sizeof(buf),
_(
"Warning, field %s renamed to %s\n"), pgfieldname, dbffieldname);
1583 if (pgfieldtype == 21)
1596 else if (pgfieldtype == 23)
1609 else if (pgfieldtype == 20)
1630 else if (pgfieldtype == 700 || pgfieldtype == 701 || pgfieldtype == 1700)
1640 else if (pgfieldtype == 16)
1650 else if (pgfieldtype == 1082)
1660 else if (pgfieldtype == 1083 || pgfieldtype == 1266 || pgfieldtype == 1114 || pgfieldtype == 1184)
1673 secondsize = pgtypmod + 1;
1685 if (pgfieldtype == 1083)
1687 dbffieldsize = 8 + secondsize;
1690 else if (pgfieldtype == 1266)
1692 dbffieldsize = 8 + secondsize + 9;
1695 else if (pgfieldtype == 1114)
1697 dbffieldsize = 13 + 1 + 8 + secondsize;
1700 else if (pgfieldtype == 1184)
1702 dbffieldsize = 13 + 1 + 8 + secondsize + 9;
1712 else if (pgfieldtype == 2950)
1724 else if ((pgfieldtype == 1042 || pgfieldtype == 1043) && pgtypmod != -1)
1731 dbffieldsize = pgtypmod - 4;
1736 else if (dbffieldtype == -1)
1744 if (dbffieldsize == -1)
1762 snprintf(buf,
sizeof(buf),
_(
"Warning: values of field '%s' exceeding maximum dbf field width (%d) "
1774 LWDEBUGF(3,
"DBF FIELD_NAME: %s, SIZE: %d\n", dbffieldname, dbffieldsize);
1776 if (dbffieldtype != 9)
1779 if (
DBFAddField(state->
dbf, dbffieldname, dbffieldtype, dbffieldsize, dbffielddecs) == -1)
1781 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"Error: field %s of type %d could not be created."), dbffieldname, dbffieldtype);
1820 snprintf(buf,
sizeof(buf),
_(
"No geometry column found.\nThe DBF file will be created but not the shx or shp files.\n"));
1878 #ifdef WORDS_BIGENDIAN
1885 sprintf(buf,
"asbinary(%s::geometry, 'XDR') AS _geoX",
1895 sprintf(buf,
"asbinary(%s::geometry, 'NDR') AS _geoX",
1905 sprintf(buf,
" FROM \"%s\".\"%s\"", state->
schema, state->
table);
1909 sprintf(buf,
" FROM \"%s\"", state->
table);
1917 sprintf(buf,
" ORDER BY \"gid\"");
1930 res = PQexec(state->
conn,
"BEGIN");
1931 if (!
res || PQresultStatus(
res) != PGRES_COMMAND_OK)
1942 if (!
res || PQresultStatus(
res) != PGRES_COMMAND_OK)
int SHPAPI_CALL DBFAddField(DBFHandle psDBF, const char *pszFieldName, DBFFieldType eType, int nWidth, int nDecimals)
DBFHandle SHPAPI_CALL DBFCreateEx(const char *pszFilename, const char *pszCodePage)
void SHPAPI_CALL DBFSetWriteEndOfFileChar(DBFHandle psDBF, int bWriteFlag)
#define LWDEBUGF(level, msg,...)
static int getMaxFieldSize(PGconn *conn, char *schema, char *table, char *fname)
static int getTableInfo(SHPDUMPERSTATE *state)
char * quote_identifier(const char *s)
#define MAX_DBF_FIELD_SIZE
SHPHandle SHPAPI_CALL SHPCreate(const char *pszShapeFile, int nShapeType)
char * encoding2codepage(const char *encoding)
int colmap_read(const char *filename, colmap *map, char *errbuf, size_t errbuflen)
Read the content of filename into a symbol map.
const char * colmap_dbf_by_pg(colmap *map, const char *pgname)
char * column_map_filename
char message[SHPDUMPERMSGLEN]