PostGIS  2.5.0beta2dev-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  val[MAXVALUELEN-1] = '\0';
1661  free(utf8str);
1662 
1663  }
1664 
1665  /* Escape attribute correctly according to dump format */
1666  if (state->config->dump_format)
1667  {
1668  escval = escape_copy_string(val);
1669  stringbuffer_aprintf(sb, "%s", escval);
1670  }
1671  else
1672  {
1673  escval = escape_insert_string(val);
1674  stringbuffer_aprintf(sb, "'%s'", escval);
1675  }
1676 
1677  /* Free the escaped version if required */
1678  if (val != escval)
1679  free(escval);
1680  }
1681 
1682  /* Only put in delimeter if not last field or a shape will follow */
1683  if (state->config->readshape == 1 || i < DBFGetFieldCount(state->hDBFHandle) - 1)
1684  {
1685  if (state->config->dump_format)
1686  stringbuffer_aprintf(sb, "\t");
1687  else
1688  stringbuffer_aprintf(sb, ",");
1689  }
1690 
1691  /* End of DBF attribute loop */
1692  }
1693 
1694 
1695  /* Add the shape attribute if we are reading it */
1696  if (state->config->readshape == 1)
1697  {
1698  /* Force the locale to C */
1699  char *oldlocale = setlocale(LC_NUMERIC, "C");
1700 
1701  /* Handle the case of a NULL shape */
1702  if (obj->nVertices == 0)
1703  {
1704  if (state->config->dump_format)
1705  stringbuffer_aprintf(sb, "\\N");
1706  else
1707  stringbuffer_aprintf(sb, "NULL");
1708  }
1709  else
1710  {
1711  /* Handle all other shape attributes */
1712  switch (obj->nSHPType)
1713  {
1714  case SHPT_POLYGON:
1715  case SHPT_POLYGONM:
1716  case SHPT_POLYGONZ:
1717  res = GeneratePolygonGeometry(state, obj, &geometry);
1718  break;
1719 
1720  case SHPT_POINT:
1721  case SHPT_POINTM:
1722  case SHPT_POINTZ:
1723  res = GeneratePointGeometry(state, obj, &geometry, 0);
1724  break;
1725 
1726  case SHPT_MULTIPOINT:
1727  case SHPT_MULTIPOINTM:
1728  case SHPT_MULTIPOINTZ:
1729  /* Force it to multi unless using -S */
1730  res = GeneratePointGeometry(state, obj, &geometry,
1731  state->config->simple_geometries ? 0 : 1);
1732  break;
1733 
1734  case SHPT_ARC:
1735  case SHPT_ARCM:
1736  case SHPT_ARCZ:
1737  res = GenerateLineStringGeometry(state, obj, &geometry);
1738  break;
1739 
1740  default:
1741  snprintf(state->message, SHPLOADERMSGLEN, _("Shape type is not supported, type id = %d"), obj->nSHPType);
1742  SHPDestroyObject(obj);
1743  stringbuffer_destroy(sbwarn);
1745 
1746  return SHPLOADERERR;
1747  }
1748  /* The default returns out of the function, so res will always have been set. */
1749  if (res != SHPLOADEROK)
1750  {
1751  /* Error message has already been set */
1752  SHPDestroyObject(obj);
1753  stringbuffer_destroy(sbwarn);
1755 
1756  return SHPLOADERERR;
1757  }
1758 
1759  /* Now generate the geometry string according to the current configuration */
1760  if (!state->config->dump_format)
1761  {
1762  if (state->to_srid != state->from_srid)
1763  {
1764  stringbuffer_aprintf(sb, "ST_Transform(");
1765  }
1766  stringbuffer_aprintf(sb, "'");
1767  }
1768 
1769  stringbuffer_aprintf(sb, "%s", geometry);
1770 
1771  if (!state->config->dump_format)
1772  {
1773  stringbuffer_aprintf(sb, "'");
1774 
1775  /* Close the ST_Transform if reprojecting. */
1776  if (state->to_srid != state->from_srid)
1777  {
1778  /* We need to add an explicit cast to geography/geometry to ensure that
1779  PostgreSQL doesn't get confused with the ST_Transform() raster
1780  function. */
1781  if (state->config->geography)
1782  stringbuffer_aprintf(sb, "::geometry, %d)::geography", state->to_srid);
1783  else
1784  stringbuffer_aprintf(sb, "::geometry, %d)", state->to_srid);
1785  }
1786  }
1787 
1788  free(geometry);
1789  }
1790 
1791  /* Tidy up everything */
1792  SHPDestroyObject(obj);
1793 
1794  setlocale(LC_NUMERIC, oldlocale);
1795  }
1796 
1797  /* Close the line correctly for dump/insert format */
1798  if (!state->config->dump_format)
1799  stringbuffer_aprintf(sb, ");");
1800 
1801 
1802  /* Copy the string buffer into a new string, destroying the string buffer */
1803  ret = (char *)malloc(strlen((char *)stringbuffer_getstring(sb)) + 1);
1804  strcpy(ret, (char *)stringbuffer_getstring(sb));
1806 
1807  *strrecord = ret;
1808 
1809  /* If any warnings occurred, set the returned message string and warning status */
1810  if (strlen((char *)stringbuffer_getstring(sbwarn)) > 0)
1811  {
1812  snprintf(state->message, SHPLOADERMSGLEN, "%s", stringbuffer_getstring(sbwarn));
1813  stringbuffer_destroy(sbwarn);
1814 
1815  return SHPLOADERWARN;
1816  }
1817  else
1818  {
1819  /* Everything went okay */
1820  stringbuffer_destroy(sbwarn);
1821 
1822  return SHPLOADEROK;
1823  }
1824 }
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: