1339 int gidfound = 0, i, j, ret, status;
1354 sprintf(state->
table,
"__pgsql2shp%lu_tmp_table", (
long)getpid());
1359 res = PQexec(state->
conn, query);
1363 if (PQresultStatus(
res) != PGRES_COMMAND_OK)
1384 sprintf(query,
"SELECT a.attname, a.atttypid, "
1385 "a.atttypmod, a.attlen FROM "
1386 "pg_attribute a, pg_class c, pg_namespace n WHERE "
1387 "n.nspname = '%s' AND a.attrelid = c.oid AND "
1388 "n.oid = c.relnamespace AND "
1389 "a.atttypid != 0 AND "
1390 "a.attnum > 0 AND c.relname = '%s'", state->
schema, state->
table);
1396 sprintf(query,
"SELECT a.attname, a.atttypid, "
1397 "a.atttypmod, a.attlen FROM "
1398 "pg_attribute a, pg_class c WHERE "
1399 "a.attrelid = c.oid and a.attnum > 0 AND "
1400 "a.atttypid != 0 AND "
1401 "c.relname = '%s' AND "
1402 "pg_catalog.pg_table_is_visible(c.oid)", state->
table);
1405 LWDEBUGF(3,
"query is: %s\n", query);
1407 res = PQexec(state->
conn, query);
1410 if (PQresultStatus(
res) != PGRES_TUPLES_OK)
1417 if (!PQntuples(
res))
1433 if ( getenv(
"PGCLIENTENCODING") )
1461 for (i = 0; i < PQntuples(
res); i++)
1465 int pgfieldtype, pgtypmod, pgfieldlen;
1468 int dbffieldtype, dbffieldsize, dbffielddecs;
1471 pgfieldname = PQgetvalue(
res, i, 0);
1472 pgfieldtype = atoi(PQgetvalue(
res, i, 1));
1473 pgtypmod = atoi(PQgetvalue(
res, i, 2));
1474 pgfieldlen = atoi(PQgetvalue(
res, i, 3));
1505 if (!strcmp(pgfieldname,
"gid") )
1527 dbffieldname =
malloc(11);
1528 strncpy(dbffieldname, ptr, 10);
1529 dbffieldname[10] =
'\0';
1538 strncpy(dbffieldname, mapped, 10);
1539 dbffieldname[10] =
'\0';
1549 if (!strncasecmp(dbffieldname, state->
dbffieldnames[j], 10))
1551 sprintf(dbffieldname,
"%.7s_%.2d", ptr, abs(tmpint) % 100);
1561 for (nameit = 0; nameit < strlen(dbffieldname); nameit++)
1562 dbffieldname[nameit] = toupper(dbffieldname[nameit]);
1566 if (strcasecmp(dbffieldname, pgfieldname))
1568 if ( snprintf(buf, 256,
_(
"Warning, field %s renamed to %s\n"),
1569 pgfieldname, dbffieldname) >= 256 )
1585 if (pgfieldtype == 21)
1592 dbffieldtype = FTInteger;
1598 else if (pgfieldtype == 23)
1605 dbffieldtype = FTInteger;
1611 else if (pgfieldtype == 20)
1618 dbffieldtype = FTInteger;
1632 else if (pgfieldtype == 700 || pgfieldtype == 701 || pgfieldtype == 1700)
1634 dbffieldtype = FTDouble;
1642 else if (pgfieldtype == 16)
1644 dbffieldtype = FTLogical;
1652 else if (pgfieldtype == 1082)
1654 dbffieldtype = FTDate;
1662 else if (pgfieldtype == 1083 || pgfieldtype == 1266 || pgfieldtype == 1114 || pgfieldtype == 1184)
1675 secondsize = pgtypmod + 1;
1687 if (pgfieldtype == 1083)
1689 dbffieldsize = 8 + secondsize;
1692 else if (pgfieldtype == 1266)
1694 dbffieldsize = 8 + secondsize + 9;
1697 else if (pgfieldtype == 1114)
1699 dbffieldsize = 13 + 1 + 8 + secondsize;
1702 else if (pgfieldtype == 1184)
1704 dbffieldsize = 13 + 1 + 8 + secondsize + 9;
1707 dbffieldtype = FTString;
1714 else if (pgfieldtype == 2950)
1716 dbffieldtype = FTString;
1726 else if ((pgfieldtype == 1042 || pgfieldtype == 1043) && pgtypmod != -1)
1732 dbffieldtype = FTString;
1733 dbffieldsize = pgtypmod - 4;
1738 else if (dbffieldtype == -1)
1746 if (dbffieldsize == -1)
1757 dbffieldtype = FTString;
1764 snprintf(buf, 256,
_(
"Warning: values of field '%s' exceeding maximum dbf field width (%d) "
1773 LWDEBUGF(3,
"DBF FIELD_NAME: %s, SIZE: %d\n", dbffieldname, dbffieldsize);
1775 if (dbffieldtype != 9)
1778 if (
DBFAddField(state->
dbf, dbffieldname, dbffieldtype, dbffieldsize, dbffielddecs) == -1)
1780 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"Error: field %s of type %d could not be created."), dbffieldname, dbffieldtype);
1819 snprintf(buf, 256,
_(
"No geometry column found.\nThe DBF file will be created but not the shx or shp files.\n"));
1876 #ifdef WORDS_BIGENDIAN
1883 sprintf(buf,
"asbinary(%s::geometry, 'XDR') AS _geoX",
1893 sprintf(buf,
"asbinary(%s::geometry, 'NDR') AS _geoX",
1903 sprintf(buf,
" FROM \"%s\".\"%s\"", state->
schema, state->
table);
1907 sprintf(buf,
" FROM \"%s\"", state->
table);
1915 sprintf(buf,
" ORDER BY \"gid\"");
1928 res = PQexec(state->
conn,
"BEGIN");
1929 if (!
res || PQresultStatus(
res) != PGRES_COMMAND_OK)
1940 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)
#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]