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, tmpint++);
1575 for (j = 0; j < strlen(dbffieldname); j++)
1576 dbffieldname[j] = toupper(dbffieldname[j]);
1579 if (strcasecmp(dbffieldname, pgfieldname))
1581 if ( snprintf(buf, 256,
_(
"Warning, field %s renamed to %s\n"),
1582 pgfieldname, dbffieldname) >= 256 )
1598 if (pgfieldtype == 21)
1605 dbffieldtype = FTInteger;
1611 else if (pgfieldtype == 23)
1618 dbffieldtype = FTInteger;
1624 else if (pgfieldtype == 20)
1631 dbffieldtype = FTInteger;
1645 else if (pgfieldtype == 700 || pgfieldtype == 701 || pgfieldtype == 1700)
1647 dbffieldtype = FTDouble;
1655 else if (pgfieldtype == 16)
1657 dbffieldtype = FTLogical;
1665 else if (pgfieldtype == 1082)
1667 dbffieldtype = FTDate;
1675 else if (pgfieldtype == 1083 || pgfieldtype == 1266 || pgfieldtype == 1114 || pgfieldtype == 1184)
1688 secondsize = pgtypmod + 1;
1700 if (pgfieldtype == 1083)
1702 dbffieldsize = 8 + secondsize;
1705 else if (pgfieldtype == 1266)
1707 dbffieldsize = 8 + secondsize + 9;
1710 else if (pgfieldtype == 1114)
1712 dbffieldsize = 13 + 1 + 8 + secondsize;
1715 else if (pgfieldtype == 1184)
1717 dbffieldsize = 13 + 1 + 8 + secondsize + 9;
1720 dbffieldtype = FTString;
1727 else if (pgfieldtype == 2950)
1729 dbffieldtype = FTString;
1739 else if ((pgfieldtype == 1042 || pgfieldtype == 1043) && pgtypmod != -1)
1745 dbffieldtype = FTString;
1746 dbffieldsize = pgtypmod - 4;
1751 else if (dbffieldtype == -1)
1759 if (dbffieldsize == -1)
1770 dbffieldtype = FTString;
1777 snprintf(buf, 256,
_(
"Warning: values of field '%s' exceeding maximum dbf field width (%d) " 1786 LWDEBUGF(3,
"DBF FIELD_NAME: %s, SIZE: %d\n", dbffieldname, dbffieldsize);
1788 if (dbffieldtype != 9)
1791 if (
DBFAddField(state->
dbf, dbffieldname, dbffieldtype, dbffieldsize, dbffielddecs) == -1)
1793 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"Error: field %s of type %d could not be created."), dbffieldname, dbffieldtype);
1832 snprintf(buf, 256,
_(
"No geometry column found.\nThe DBF file will be created but not the shx or shp files.\n"));
1897 sprintf(buf,
"asbinary(%s::geometry, 'XDR') AS _geoX",
1909 sprintf(buf,
"asbinary(%s::geometry, 'NDR') AS _geoX",
1919 sprintf(buf,
" FROM \"%s\".\"%s\"", state->
schema, state->
table);
1923 sprintf(buf,
" FROM \"%s\"", state->
table);
1931 sprintf(buf,
" ORDER BY \"gid\"");
1944 res = PQexec(state->
conn,
"BEGIN");
1945 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
1956 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
1958 snprintf(state->
message,
SHPDUMPERMSGLEN,
_(
"Error executing main scan query: %s"), PQresultErrorMessage(res));
char * column_map_filename
int colmap_read(const char *filename, colmap *map, char *errbuf, size_t errbuflen)
Read the content of filename into a symbol map.
DBFHandle SHPAPI_CALL DBFCreateEx(const char *pszFilename, const char *pszCodePage)
SHPHandle SHPAPI_CALL SHPCreate(const char *pszShapeFile, int nShapeType)
static int getTableInfo(SHPDUMPERSTATE *state)
#define MAX_DBF_FIELD_SIZE
static int getMaxFieldSize(PGconn *conn, char *schema, char *table, char *fname)
int SHPAPI_CALL DBFAddField(DBFHandle psDBF, const char *pszFieldName, DBFFieldType eType, int nWidth, int nDecimals)
char * encoding2codepage(const char *encoding)
char * quote_identifier(const char *s)
char message[SHPDUMPERMSGLEN]
#define LWDEBUGF(level, msg,...)
const char * colmap_dbf_by_pg(colmap *map, const char *pgname)