PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ ShpLoaderGenerateSQLRowStatement()

int ShpLoaderGenerateSQLRowStatement ( SHPLOADERSTATE state,
int  item,
char **  strrecord 
)

Definition at line 1516 of file shp2pgsql-core.c.

References _, shp_loader_state::col_names, shp_loader_state::config, DBFGetFieldCount(), DBFIsAttributeNULL(), DBFIsRecordDeleted(), DBFReadStringAttribute(), shp_loader_config::dump_format, shp_loader_config::encoding, escape_copy_string(), escape_insert_string(), free(), shp_loader_state::from_srid, GenerateLineStringGeometry(), GeneratePointGeometry(), GeneratePolygonGeometry(), shp_loader_config::geography, shp_loader_state::hDBFHandle, shp_loader_state::hSHPHandle, malloc(), MAXVALUELEN, shp_loader_state::message, SHPObject::nSHPType, shp_loader_config::null_policy, SHPObject::nVertices, POLICY_NULL_SKIP, shp_loader_config::readshape, window::res, shp_loader_config::schema, SHPDestroyObject(), SHPLOADERERR, SHPLOADERMSGLEN, SHPLOADEROK, SHPLOADERRECDELETED, SHPLOADERRECISNULL, SHPLOADERWARN, SHPReadObject(), SHPT_ARC, SHPT_ARCM, SHPT_ARCZ, SHPT_MULTIPOINT, SHPT_MULTIPOINTM, SHPT_MULTIPOINTZ, SHPT_POINT, SHPT_POINTM, SHPT_POINTZ, SHPT_POLYGON, SHPT_POLYGONM, SHPT_POLYGONZ, shp_loader_config::simple_geometries, stringbuffer_aprintf(), stringbuffer_clear(), stringbuffer_create(), stringbuffer_destroy(), stringbuffer_getstring(), shp_loader_config::table, shp_loader_state::to_srid, shp_loader_state::types, utf8(), UTF8_BAD_RESULT, UTF8_GOOD_RESULT, and UTF8_NO_RESULT.

Referenced by main(), and pgui_action_import().

1517 {
1518  SHPObject *obj = NULL;
1519  stringbuffer_t *sb;
1520  stringbuffer_t *sbwarn;
1521  char val[MAXVALUELEN];
1522  char *escval;
1523  char *geometry=NULL, *ret;
1524  char *utf8str;
1525  int res, i;
1526  int rv;
1527 
1528  /* Clear the stringbuffers */
1529  sbwarn = stringbuffer_create();
1530  stringbuffer_clear(sbwarn);
1531  sb = stringbuffer_create();
1532  stringbuffer_clear(sb);
1533 
1534  /* Skip deleted records */
1535  if (state->hDBFHandle && DBFIsRecordDeleted(state->hDBFHandle, item))
1536  {
1537  *strrecord = NULL;
1538  return SHPLOADERRECDELETED;
1539  }
1540 
1541  /* If we are reading the shapefile, open the specified record */
1542  if (state->config->readshape == 1)
1543  {
1544  obj = SHPReadObject(state->hSHPHandle, item);
1545  if (!obj)
1546  {
1547  snprintf(state->message, SHPLOADERMSGLEN, _("Error reading shape object %d"), item);
1548  return SHPLOADERERR;
1549  }
1550 
1551  /* If we are set to skip NULLs, return a NULL record status */
1552  if (state->config->null_policy == POLICY_NULL_SKIP && obj->nVertices == 0 )
1553  {
1554  SHPDestroyObject(obj);
1555 
1556  *strrecord = NULL;
1557  return SHPLOADERRECISNULL;
1558  }
1559  }
1560 
1561  /* If not in dump format, generate the INSERT string */
1562  if (!state->config->dump_format)
1563  {
1564  if (state->config->schema)
1565  {
1566  stringbuffer_aprintf(sb, "INSERT INTO \"%s\".\"%s\" %s VALUES (", state->config->schema,
1567  state->config->table, state->col_names);
1568  }
1569  else
1570  {
1571  stringbuffer_aprintf(sb, "INSERT INTO \"%s\" %s VALUES (", state->config->table,
1572  state->col_names);
1573  }
1574  }
1575 
1576 
1577  /* Read all of the attributes from the DBF file for this item */
1578  for (i = 0; i < DBFGetFieldCount(state->hDBFHandle); i++)
1579  {
1580  /* Special case for NULL attributes */
1581  if (DBFIsAttributeNULL(state->hDBFHandle, item, i))
1582  {
1583  if (state->config->dump_format)
1584  stringbuffer_aprintf(sb, "\\N");
1585  else
1586  stringbuffer_aprintf(sb, "NULL");
1587  }
1588  else
1589  {
1590  /* Attribute NOT NULL */
1591  switch (state->types[i])
1592  {
1593  case FTInteger:
1594  case FTDouble:
1595  rv = snprintf(val, MAXVALUELEN, "%s", DBFReadStringAttribute(state->hDBFHandle, item, i));
1596  if (rv >= MAXVALUELEN || rv == -1)
1597  {
1598  stringbuffer_aprintf(sbwarn, "Warning: field %d name truncated\n", i);
1599  val[MAXVALUELEN - 1] = '\0';
1600  }
1601 
1602  /* If the value is an empty string, change to 0 */
1603  if (val[0] == '\0')
1604  {
1605  val[0] = '0';
1606  val[1] = '\0';
1607  }
1608 
1609  /* If the value ends with just ".", remove the dot */
1610  if (val[strlen(val) - 1] == '.')
1611  val[strlen(val) - 1] = '\0';
1612  break;
1613 
1614  case FTString:
1615  case FTLogical:
1616  case FTDate:
1617  rv = snprintf(val, MAXVALUELEN, "%s", DBFReadStringAttribute(state->hDBFHandle, item, i));
1618  if (rv >= MAXVALUELEN || rv == -1)
1619  {
1620  stringbuffer_aprintf(sbwarn, "Warning: field %d name truncated\n", i);
1621  val[MAXVALUELEN - 1] = '\0';
1622  }
1623  break;
1624 
1625  default:
1626  snprintf(state->message, SHPLOADERMSGLEN, _("Error: field %d has invalid or unknown field type (%d)"), i, state->types[i]);
1627 
1628  /* clean up and return err */
1629  SHPDestroyObject(obj);
1630  stringbuffer_destroy(sbwarn);
1632  return SHPLOADERERR;
1633  }
1634 
1635  if (state->config->encoding)
1636  {
1637  char *encoding_msg = _("Try \"LATIN1\" (Western European), or one of the values described at http://www.postgresql.org/docs/current/static/multibyte.html.");
1638 
1639  rv = utf8(state->config->encoding, val, &utf8str);
1640 
1641  if (rv != UTF8_GOOD_RESULT)
1642  {
1643  if ( rv == UTF8_BAD_RESULT )
1644  snprintf(state->message, SHPLOADERMSGLEN, _("Unable to convert data value \"%s\" to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), utf8str, strerror(errno), state->config->encoding, encoding_msg);
1645  else if ( rv == UTF8_NO_RESULT )
1646  snprintf(state->message, SHPLOADERMSGLEN, _("Unable to convert data value to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), strerror(errno), state->config->encoding, encoding_msg);
1647  else
1648  snprintf(state->message, SHPLOADERMSGLEN, _("Unexpected return value from utf8()"));
1649 
1650  if ( rv == UTF8_BAD_RESULT )
1651  free(utf8str);
1652 
1653  /* clean up and return err */
1654  SHPDestroyObject(obj);
1655  stringbuffer_destroy(sbwarn);
1657  return SHPLOADERERR;
1658  }
1659  strncpy(val, utf8str, MAXVALUELEN);
1660  free(utf8str);
1661 
1662  }
1663 
1664  /* Escape attribute correctly according to dump format */
1665  if (state->config->dump_format)
1666  {
1667  escval = escape_copy_string(val);
1668  stringbuffer_aprintf(sb, "%s", escval);
1669  }
1670  else
1671  {
1672  escval = escape_insert_string(val);
1673  stringbuffer_aprintf(sb, "'%s'", escval);
1674  }
1675 
1676  /* Free the escaped version if required */
1677  if (val != escval)
1678  free(escval);
1679  }
1680 
1681  /* Only put in delimeter if not last field or a shape will follow */
1682  if (state->config->readshape == 1 || i < DBFGetFieldCount(state->hDBFHandle) - 1)
1683  {
1684  if (state->config->dump_format)
1685  stringbuffer_aprintf(sb, "\t");
1686  else
1687  stringbuffer_aprintf(sb, ",");
1688  }
1689 
1690  /* End of DBF attribute loop */
1691  }
1692 
1693 
1694  /* Add the shape attribute if we are reading it */
1695  if (state->config->readshape == 1)
1696  {
1697  /* Force the locale to C */
1698  char *oldlocale = setlocale(LC_NUMERIC, "C");
1699 
1700  /* Handle the case of a NULL shape */
1701  if (obj->nVertices == 0)
1702  {
1703  if (state->config->dump_format)
1704  stringbuffer_aprintf(sb, "\\N");
1705  else
1706  stringbuffer_aprintf(sb, "NULL");
1707  }
1708  else
1709  {
1710  /* Handle all other shape attributes */
1711  switch (obj->nSHPType)
1712  {
1713  case SHPT_POLYGON:
1714  case SHPT_POLYGONM:
1715  case SHPT_POLYGONZ:
1716  res = GeneratePolygonGeometry(state, obj, &geometry);
1717  break;
1718 
1719  case SHPT_POINT:
1720  case SHPT_POINTM:
1721  case SHPT_POINTZ:
1722  res = GeneratePointGeometry(state, obj, &geometry, 0);
1723  break;
1724 
1725  case SHPT_MULTIPOINT:
1726  case SHPT_MULTIPOINTM:
1727  case SHPT_MULTIPOINTZ:
1728  /* Force it to multi unless using -S */
1729  res = GeneratePointGeometry(state, obj, &geometry,
1730  state->config->simple_geometries ? 0 : 1);
1731  break;
1732 
1733  case SHPT_ARC:
1734  case SHPT_ARCM:
1735  case SHPT_ARCZ:
1736  res = GenerateLineStringGeometry(state, obj, &geometry);
1737  break;
1738 
1739  default:
1740  snprintf(state->message, SHPLOADERMSGLEN, _("Shape type is not supported, type id = %d"), obj->nSHPType);
1741  SHPDestroyObject(obj);
1742  stringbuffer_destroy(sbwarn);
1744 
1745  return SHPLOADERERR;
1746  }
1747  /* The default returns out of the function, so res will always have been set. */
1748  if (res != SHPLOADEROK)
1749  {
1750  /* Error message has already been set */
1751  SHPDestroyObject(obj);
1752  stringbuffer_destroy(sbwarn);
1754 
1755  return SHPLOADERERR;
1756  }
1757 
1758  /* Now generate the geometry string according to the current configuration */
1759  if (!state->config->dump_format)
1760  {
1761  if (state->to_srid != state->from_srid)
1762  {
1763  stringbuffer_aprintf(sb, "ST_Transform(");
1764  }
1765  stringbuffer_aprintf(sb, "'");
1766  }
1767 
1768  stringbuffer_aprintf(sb, "%s", geometry);
1769 
1770  if (!state->config->dump_format)
1771  {
1772  stringbuffer_aprintf(sb, "'");
1773 
1774  /* Close the ST_Transform if reprojecting. */
1775  if (state->to_srid != state->from_srid)
1776  {
1777  /* We need to add an explicit cast to geography/geometry to ensure that
1778  PostgreSQL doesn't get confused with the ST_Transform() raster
1779  function. */
1780  if (state->config->geography)
1781  stringbuffer_aprintf(sb, "::geometry, %d)::geography", state->to_srid);
1782  else
1783  stringbuffer_aprintf(sb, "::geometry, %d)", state->to_srid);
1784  }
1785  }
1786 
1787  free(geometry);
1788  }
1789 
1790  /* Tidy up everything */
1791  SHPDestroyObject(obj);
1792 
1793  setlocale(LC_NUMERIC, oldlocale);
1794  }
1795 
1796  /* Close the line correctly for dump/insert format */
1797  if (!state->config->dump_format)
1798  stringbuffer_aprintf(sb, ");");
1799 
1800 
1801  /* Copy the string buffer into a new string, destroying the string buffer */
1802  ret = (char *)malloc(strlen((char *)stringbuffer_getstring(sb)) + 1);
1803  strcpy(ret, (char *)stringbuffer_getstring(sb));
1805 
1806  *strrecord = ret;
1807 
1808  /* If any warnings occurred, set the returned message string and warning status */
1809  if (strlen((char *)stringbuffer_getstring(sbwarn)) > 0)
1810  {
1811  snprintf(state->message, SHPLOADERMSGLEN, "%s", stringbuffer_getstring(sbwarn));
1812  stringbuffer_destroy(sbwarn);
1813 
1814  return SHPLOADERWARN;
1815  }
1816  else
1817  {
1818  /* Everything went okay */
1819  stringbuffer_destroy(sbwarn);
1820 
1821  return SHPLOADEROK;
1822  }
1823 }
SHPLOADERCONFIG * config
#define SHPT_ARCM
Definition: shapefil.h:316
tuple res
Definition: window.py:78
DBFFieldType * types
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:35
#define SHPT_POLYGONM
Definition: shapefil.h:317
int nVertices
Definition: shapefil.h:348
#define SHPLOADERRECISNULL
#define _(String)
Definition: shpcommon.h:24
const char SHPAPI_CALL1 * DBFReadStringAttribute(DBFHandle psDBF, int iRecord, int iField){ return((const char *) DBFReadAttribute(psDBF, iRecord, iField, 'C')
#define UTF8_BAD_RESULT
#define SHPLOADERWARN
#define SHPT_MULTIPOINT
Definition: shapefil.h:310
#define POLICY_NULL_SKIP
#define SHPLOADERRECDELETED
#define SHPT_POLYGON
Definition: shapefil.h:309
void SHPAPI_CALL SHPDestroyObject(SHPObject *psObject)
Definition: shpopen.c:2182
#define SHPT_MULTIPOINTZ
Definition: shapefil.h:314
int GeneratePointGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry, int force_multi)
Generate an allocated geometry string for shapefile object obj using the state parameters if "force_m...
int GeneratePolygonGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry)
Generate an allocated geometry string for shapefile object obj using the state parameters.
int GenerateLineStringGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry)
Generate an allocated geometry string for shapefile object obj using the state parameters.
DBFHandle hDBFHandle
#define UTF8_GOOD_RESULT
#define SHPT_ARCZ
Definition: shapefil.h:312
char * escape_insert_string(char *str)
Escape input string suitable for INSERT.
int stringbuffer_aprintf(stringbuffer_t *s, const char *fmt,...)
Appends a formatted string to the current string buffer, using the format and argument list provided...
Definition: stringbuffer.c:253
int SHPAPI_CALL DBFGetFieldCount(DBFHandle psDBF)
Definition: dbfopen.c:1195
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
Definition: stringbuffer.c:90
SHPObject SHPAPI_CALL1 * SHPReadObject(SHPHandle hSHP, int iShape);int SHPAPI_CALL SHPWriteObject(SHPHandle hSHP, int iShape, SHPObject *psObject
char * escape_copy_string(char *str)
Escape input string suitable for COPY.
#define SHPLOADEROK
#define UTF8_NO_RESULT
#define SHPT_MULTIPOINTM
Definition: shapefil.h:318
#define SHPT_POINTZ
Definition: shapefil.h:311
SHPHandle hSHPHandle
#define SHPT_POINTM
Definition: shapefil.h:315
#define SHPT_POLYGONZ
Definition: shapefil.h:313
char message[SHPLOADERMSGLEN]
#define SHPT_POINT
Definition: shapefil.h:307
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:78
static int utf8(const char *fromcode, char *inputbuf, char **outputbuf)
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
Definition: stringbuffer.c:149
#define MAXVALUELEN
void free(void *)
void * malloc(YYSIZE_T)
int SHPAPI_CALL DBFIsAttributeNULL(DBFHandle psDBF, int iRecord, int iField)
Definition: dbfopen.c:1175
#define SHPLOADERERR
int nSHPType
Definition: shapefil.h:340
#define SHPT_ARC
Definition: shapefil.h:308
#define SHPLOADERMSGLEN
int SHPAPI_CALL DBFIsRecordDeleted(DBFHandle psDBF, int iShape)
Definition: dbfopen.c:1732
Here is the call graph for this function:
Here is the caller graph for this function: