PostGIS  2.1.10dev-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 1244 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, 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().

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

Here is the call graph for this function:

Here is the caller graph for this function: