PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ ShpLoaderGenerateSQLRowStatement()

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

Definition at line 1508 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().

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