PostGIS  2.5.0dev-r@@SVN_REVISION@@
int ShpLoaderGetSQLHeader ( SHPLOADERSTATE state,
char **  strheader 
)

TODO: if the table has more then one geometry column the DROP TABLE call will leave spurious records in geometry_columns.

If the geometry column in the table being dropped does not match 'the_geom' or the name specified with -g an error is returned by DropGeometryColumn.

The table to be dropped might not exist.

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

References _, shp_loader_state::config, shp_loader_config::encoding, shp_loader_state::field_names, shp_loader_state::geo_col, shp_loader_config::geography, shp_loader_config::idxtablespace, malloc(), shp_loader_state::message, shp_loader_state::num_fields, shp_loader_config::opt, shp_loader_state::pgdims, shp_loader_state::pgfieldtypes, shp_loader_state::pgtype, shp_loader_config::readshape, shp_loader_config::schema, SHPLOADERERR, SHPLOADERMSGLEN, SHPLOADEROK, SRID_UNKNOWN, stringbuffer_aprintf(), stringbuffer_clear(), stringbuffer_create(), stringbuffer_destroy(), stringbuffer_getstring(), shp_loader_config::table, shp_loader_config::tablespace, shp_loader_state::to_srid, shp_loader_state::types, shp_loader_config::usetransaction, and shp_loader_state::widths.

Referenced by main(), and pgui_action_import().

1265 {
1266  stringbuffer_t *sb;
1267  char *ret;
1268  int j;
1269 
1270  /* Create the stringbuffer containing the header; we use this API as it's easier
1271  for handling string resizing during append */
1272  sb = stringbuffer_create();
1273  stringbuffer_clear(sb);
1274 
1275  /* Set the client encoding if required */
1276  if (state->config->encoding)
1277  {
1278  stringbuffer_aprintf(sb, "SET CLIENT_ENCODING TO UTF8;\n");
1279  }
1280 
1281  /* Use SQL-standard string escaping rather than PostgreSQL standard */
1282  stringbuffer_aprintf(sb, "SET STANDARD_CONFORMING_STRINGS TO ON;\n");
1283 
1284  /* Drop table if requested */
1285  if (state->config->opt == 'd')
1286  {
1298  if (state->config->schema)
1299  {
1300  if (state->config->readshape == 1 && (! state->config->geography) )
1301  {
1302  stringbuffer_aprintf(sb, "SELECT DropGeometryColumn('%s','%s','%s');\n",
1303  state->config->schema, state->config->table, state->geo_col);
1304  }
1305 
1306  stringbuffer_aprintf(sb, "DROP TABLE IF EXISTS \"%s\".\"%s\";\n", state->config->schema,
1307  state->config->table);
1308  }
1309  else
1310  {
1311  if (state->config->readshape == 1 && (! state->config->geography) )
1312  {
1313  stringbuffer_aprintf(sb, "SELECT DropGeometryColumn('','%s','%s');\n",
1314  state->config->table, state->geo_col);
1315  }
1316 
1317  stringbuffer_aprintf(sb, "DROP TABLE IF EXISTS \"%s\";\n", state->config->table);
1318  }
1319  }
1320 
1321  /* Start of transaction if we are using one */
1322  if (state->config->usetransaction)
1323  {
1324  stringbuffer_aprintf(sb, "BEGIN;\n");
1325  }
1326 
1327  /* If not in 'append' mode create the spatial table */
1328  if (state->config->opt != 'a')
1329  {
1330  /*
1331  * Create a table for inserting the shapes into with appropriate
1332  * columns and types
1333  */
1334  if (state->config->schema)
1335  {
1336  stringbuffer_aprintf(sb, "CREATE TABLE \"%s\".\"%s\" (gid serial",
1337  state->config->schema, state->config->table);
1338  }
1339  else
1340  {
1341  stringbuffer_aprintf(sb, "CREATE TABLE \"%s\" (gid serial", state->config->table);
1342  }
1343 
1344  /* Generate the field types based upon the shapefile information */
1345  for (j = 0; j < state->num_fields; j++)
1346  {
1347  stringbuffer_aprintf(sb, ",\n\"%s\" ", state->field_names[j]);
1348 
1349  /* First output the raw field type string */
1350  stringbuffer_aprintf(sb, "%s", state->pgfieldtypes[j]);
1351 
1352  /* Some types do have typmods though... */
1353  if (!strcmp("varchar", state->pgfieldtypes[j]))
1354  stringbuffer_aprintf(sb, "(%d)", state->widths[j]);
1355 
1356  if (!strcmp("numeric", state->pgfieldtypes[j]))
1357  {
1358  /* Doubles we just allow PostgreSQL to auto-detect the size */
1359  if (state->types[j] != FTDouble)
1360  stringbuffer_aprintf(sb, "(%d,0)", state->widths[j]);
1361  }
1362  }
1363 
1364  /* Add the geography column directly to the table definition, we don't
1365  need to do an AddGeometryColumn() call. */
1366  if (state->config->readshape == 1 && state->config->geography)
1367  {
1368  char *dimschar;
1369 
1370  if (state->pgdims == 4)
1371  dimschar = "ZM";
1372  else
1373  dimschar = "";
1374 
1375  if (state->to_srid != SRID_UNKNOWN && state->to_srid != 4326)
1376  {
1377  snprintf(state->message, SHPLOADERMSGLEN, _("Invalid SRID for geography type: %d"), state->to_srid);
1379  return SHPLOADERERR;
1380  }
1381  stringbuffer_aprintf(sb, ",\n\"%s\" geography(%s%s,%d)", state->geo_col, state->pgtype, dimschar, 4326);
1382  }
1383  stringbuffer_aprintf(sb, ")");
1384 
1385  /* Tablespace is optional. */
1386  if (state->config->tablespace != NULL)
1387  {
1388  stringbuffer_aprintf(sb, " TABLESPACE \"%s\"", state->config->tablespace);
1389  }
1390  stringbuffer_aprintf(sb, ";\n");
1391 
1392  /* Create the primary key. This is done separately because the index for the PK needs
1393  * to be in the correct tablespace. */
1394 
1395  /* TODO: Currently PostgreSQL does not allow specifying an index to use for a PK (so you get
1396  * a default one called table_pkey) and it does not provide a way to create a PK index
1397  * in a specific tablespace. So as a hacky solution we create the PK, then move the
1398  * index to the correct tablespace. Eventually this should be:
1399  * CREATE INDEX table_pkey on table(gid) TABLESPACE tblspc;
1400  * ALTER TABLE table ADD PRIMARY KEY (gid) USING INDEX table_pkey;
1401  * A patch has apparently been submitted to PostgreSQL to enable this syntax, see this thread:
1402  * http://archives.postgresql.org/pgsql-hackers/2011-01/msg01405.php */
1403  stringbuffer_aprintf(sb, "ALTER TABLE ");
1404 
1405  /* Schema is optional, include if present. */
1406  if (state->config->schema)
1407  {
1408  stringbuffer_aprintf(sb, "\"%s\".",state->config->schema);
1409  }
1410  stringbuffer_aprintf(sb, "\"%s\" ADD PRIMARY KEY (gid);\n", state->config->table);
1411 
1412  /* Tablespace is optional for the index. */
1413  if (state->config->idxtablespace != NULL)
1414  {
1415  stringbuffer_aprintf(sb, "ALTER INDEX ");
1416  if (state->config->schema)
1417  {
1418  stringbuffer_aprintf(sb, "\"%s\".",state->config->schema);
1419  }
1420 
1421  /* WARNING: We're assuming the default "table_pkey" name for the primary
1422  * key index. PostgreSQL may use "table_pkey1" or similar in the
1423  * case of a name conflict, so you may need to edit the produced
1424  * SQL in this rare case. */
1425  stringbuffer_aprintf(sb, "\"%s_pkey\" SET TABLESPACE \"%s\";\n",
1426  state->config->table, state->config->idxtablespace);
1427  }
1428 
1429  /* Create the geometry column with an addgeometry call */
1430  if (state->config->readshape == 1 && (!state->config->geography))
1431  {
1432  /* If they didn't specify a target SRID, see if they specified a source SRID. */
1433  int srid = state->to_srid;
1434  if (state->config->schema)
1435  {
1436  stringbuffer_aprintf(sb, "SELECT AddGeometryColumn('%s','%s','%s','%d',",
1437  state->config->schema, state->config->table, state->geo_col, srid);
1438  }
1439  else
1440  {
1441  stringbuffer_aprintf(sb, "SELECT AddGeometryColumn('','%s','%s','%d',",
1442  state->config->table, state->geo_col, srid);
1443  }
1444 
1445  stringbuffer_aprintf(sb, "'%s',%d);\n", state->pgtype, state->pgdims);
1446  }
1447  }
1448 
1449  /* Copy the string buffer into a new string, destroying the string buffer */
1450  ret = (char *)malloc(strlen((char *)stringbuffer_getstring(sb)) + 1);
1451  strcpy(ret, (char *)stringbuffer_getstring(sb));
1453 
1454  *strheader = ret;
1455 
1456  return SHPLOADEROK;
1457 }
SHPLOADERCONFIG * config
DBFFieldType * types
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:35
#define _(String)
Definition: shpcommon.h:24
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
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
Definition: stringbuffer.c:90
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
#define SHPLOADEROK
char message[SHPLOADERMSGLEN]
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:78
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
Definition: stringbuffer.c:149
void * malloc(YYSIZE_T)
#define SHPLOADERERR
#define SHPLOADERMSGLEN

Here is the call graph for this function:

Here is the caller graph for this function: