1356 int gidfound = 0, i, j, ret, status;
1371 sprintf(state->
table,
"__pgsql2shp%lu_tmp_table", (
long)getpid());
1376 res = PQexec(state->
conn, query);
1380 if (PQresultStatus(
res) != PGRES_COMMAND_OK)
1401 sprintf(query,
"SELECT a.attname, a.atttypid, "
1402 "a.atttypmod, a.attlen FROM "
1403 "pg_attribute a, pg_class c, pg_namespace n WHERE "
1404 "n.nspname = '%s' AND a.attrelid = c.oid AND "
1405 "n.oid = c.relnamespace AND "
1406 "a.atttypid != 0 AND "
1407 "a.attnum > 0 AND c.relname = '%s'", state->
schema, state->
table);
1413 sprintf(query,
"SELECT a.attname, a.atttypid, "
1414 "a.atttypmod, a.attlen FROM "
1415 "pg_attribute a, pg_class c WHERE "
1416 "a.attrelid = c.oid and a.attnum > 0 AND "
1417 "a.atttypid != 0 AND "
1418 "c.relname = '%s' AND "
1419 "pg_catalog.pg_table_is_visible(c.oid)", state->
table);
1422 LWDEBUGF(3,
"query is: %s\n", query);
1424 res = PQexec(state->
conn, query);
1427 if (PQresultStatus(
res) != PGRES_TUPLES_OK)
1434 if (!PQntuples(
res))
1450 if ( getenv(
"PGCLIENTENCODING") )
1478 for (i = 0; i < PQntuples(
res); i++)
1482 int pgfieldtype, pgtypmod, pgfieldlen;
1485 int dbffieldtype, dbffieldsize, dbffielddecs;
1488 pgfieldname = PQgetvalue(
res, i, 0);
1489 pgfieldtype = atoi(PQgetvalue(
res, i, 1));
1490 pgtypmod = atoi(PQgetvalue(
res, i, 2));
1491 pgfieldlen = atoi(PQgetvalue(
res, i, 3));
1522 if (!strcmp(pgfieldname,
"gid") )
1544 dbffieldname =
malloc(11);
1545 strncpy(dbffieldname, ptr, 10);
1546 dbffieldname[10] =
'\0';
1555 strncpy(dbffieldname, mapped, 10);
1556 dbffieldname[10] =
'\0';
1566 if (!strncasecmp(dbffieldname, state->
dbffieldnames[j], 10))
1568 sprintf(dbffieldname,
"%.7s_%.2d", ptr, abs(tmpint) % 100);
1578 for (nameit = 0; nameit < strlen(dbffieldname); nameit++)
1579 dbffieldname[nameit] = toupper(dbffieldname[nameit]);
1583 if (strcasecmp(dbffieldname, pgfieldname))
1585 if ( snprintf(buf, 256,
_(
"Warning, field %s renamed to %s\n"),
1586 pgfieldname, dbffieldname) >= 256 )
1602 if (pgfieldtype == 21)
1609 dbffieldtype = FTInteger;
1615 else if (pgfieldtype == 23)
1622 dbffieldtype = FTInteger;
1628 else if (pgfieldtype == 20)
1635 dbffieldtype = FTInteger;
1649 else if (pgfieldtype == 700 || pgfieldtype == 701 || pgfieldtype == 1700)
1651 dbffieldtype = FTDouble;
1659 else if (pgfieldtype == 16)
1661 dbffieldtype = FTLogical;
1669 else if (pgfieldtype == 1082)
1671 dbffieldtype = FTDate;
1679 else if (pgfieldtype == 1083 || pgfieldtype == 1266 || pgfieldtype == 1114 || pgfieldtype == 1184)
1692 secondsize = pgtypmod + 1;
1704 if (pgfieldtype == 1083)
1706 dbffieldsize = 8 + secondsize;
1709 else if (pgfieldtype == 1266)
1711 dbffieldsize = 8 + secondsize + 9;
1714 else if (pgfieldtype == 1114)
1716 dbffieldsize = 13 + 1 + 8 + secondsize;
1719 else if (pgfieldtype == 1184)
1721 dbffieldsize = 13 + 1 + 8 + secondsize + 9;
1724 dbffieldtype = FTString;
1731 else if (pgfieldtype == 2950)
1733 dbffieldtype = FTString;
1743 else if ((pgfieldtype == 1042 || pgfieldtype == 1043) && pgtypmod != -1)
1749 dbffieldtype = FTString;
1750 dbffieldsize = pgtypmod - 4;
1755 else if (dbffieldtype == -1)
1763 if (dbffieldsize == -1)
1774 dbffieldtype = FTString;
1781 snprintf(buf, 256,
_(
"Warning: values of field '%s' exceeding maximum dbf field width (%d) "
1790 LWDEBUGF(3,
"DBF FIELD_NAME: %s, SIZE: %d\n", dbffieldname, dbffieldsize);
1792 if (dbffieldtype != 9)
1795 if (
DBFAddField(state->
dbf, dbffieldname, dbffieldtype, dbffieldsize, dbffielddecs) == -1)
1797 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"Error: field %s of type %d could not be created."), dbffieldname, dbffieldtype);
1836 snprintf(buf, 256,
_(
"No geometry column found.\nThe DBF file will be created but not the shx or shp files.\n"));
1901 sprintf(buf,
"asbinary(%s::geometry, 'XDR') AS _geoX",
1913 sprintf(buf,
"asbinary(%s::geometry, 'NDR') AS _geoX",
1923 sprintf(buf,
" FROM \"%s\".\"%s\"", state->
schema, state->
table);
1927 sprintf(buf,
" FROM \"%s\"", state->
table);
1935 sprintf(buf,
" ORDER BY \"gid\"");
1948 res = PQexec(state->
conn,
"BEGIN");
1949 if (!
res || PQresultStatus(
res) != PGRES_COMMAND_OK)
1960 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]