PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ pgui_action_import()

static void pgui_action_import ( GtkWidget *  widget,
gpointer  data 
)
static

Definition at line 1415 of file shp2pgsql-gui.c.

References _, shp_loader_state::config, connection_test(), shp_loader_config::createindex, dialog_progress, shp_loader_config::dump_format, FALSE, free(), shp_loader_config::geo_col, shp_loader_config::geography, GUIMSG_LINE_MAXLEN, import_file_list_store, IMPORT_POINTER_COLUMN, is_running, label_progress, malloc(), shp_loader_state::message, shp_loader_config::opt, pg_connection, shp_loader_state::pgdims, shp_loader_state::pgtype, pgui_copy_end(), pgui_copy_start(), pgui_copy_write(), pgui_exec(), pgui_logf(), pgui_raise_error_dialogue(), pgui_seterr(), progress, shp_loader_config::readshape, shp_loader_config::schema, shp_loader_config::shp_file, ShpDumperGetConnectionStringFromConn(), shp_loader_state::shpfiletype, ShpLoaderCreate(), ShpLoaderDestroy(), SHPLOADERERR, ShpLoaderGenerateSQLRowStatement(), ShpLoaderGetRecordCount(), ShpLoaderGetSQLCopyStatement(), ShpLoaderGetSQLFooter(), ShpLoaderGetSQLHeader(), SHPLOADEROK, ShpLoaderOpenShape(), SHPLOADERRECDELETED, SHPLOADERRECISNULL, SHPLOADERWARN, SHPTypeName(), shp_loader_config::simple_geometries, shp_loader_config::sr_id, shp_loader_config::table, TRUE, update_conn_ui_from_conn_config(), validate_remote_loader_columns(), and window_conn.

Referenced by pgui_create_main_window().

1416 {
1417  SHPLOADERCONFIG *loader_file_config;
1418  SHPLOADERSTATE *state;
1419  gint is_valid;
1420  gpointer gptr;
1421  GtkTreeIter iter;
1422  char *sql_form, *query, *connection_string, *progress_shapefile = NULL;
1423  char progress_text[GUIMSG_LINE_MAXLEN+1];
1424  PGresult *result;
1425 
1426  int ret, i = 0;
1427  char *header, *footer, *record;
1428 
1429  /* Get the first row of the import list */
1430  is_valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(import_file_list_store), &iter);
1431  if (!is_valid)
1432  {
1433  pgui_seterr(_("ERROR: You haven't specified any files to import"));
1435 
1436  return;
1437  }
1438 
1439  /* Firstly make sure that we can connect to the database - if we can't then there isn't much
1440  point doing anything else... */
1441  if (!connection_test())
1442  {
1443  pgui_seterr(_("Unable to connect to the database - please check your connection settings"));
1445 
1446  /* Open the connections UI for the user */
1448 
1449  gtk_widget_show_all(GTK_WIDGET(window_conn));
1450  return;
1451  }
1452 
1453  /* Let's open a single connection to the remote DB for the duration of the validation pass;
1454  note that we already know the connection string works, otherwise we would have bailed
1455  out earlier in the function */
1456  connection_string = ShpDumperGetConnectionStringFromConn(conn);
1457  pg_connection = PQconnectdb(connection_string);
1458 
1459  /* Setup the table/column type discovery query */
1460  sql_form = "SELECT a.attnum, a.attname AS field, t.typname AS type, a.attlen AS length, a.atttypmod AS precision FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n WHERE c.relname = '%s' AND n.nspname = '%s' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid AND c.relnamespace = n.oid ORDER BY a.attnum";
1461 
1462  /* Validation: we loop through each of the files in order to validate them as a separate pass */
1463  while (is_valid)
1464  {
1465  /* Grab the SHPLOADERCONFIG for this row */
1466  gtk_tree_model_get(GTK_TREE_MODEL(import_file_list_store), &iter, IMPORT_POINTER_COLUMN, &gptr, -1);
1467  loader_file_config = (SHPLOADERCONFIG *)gptr;
1468 
1469  /* For each entry, we execute a remote query in order to determine the column names
1470  and types for the remote table if they actually exist */
1471  query = malloc(strlen(sql_form) + strlen(loader_file_config->schema) + strlen(loader_file_config->table) + 1);
1472  sprintf(query, sql_form, loader_file_config->table, loader_file_config->schema);
1473  result = PQexec(pg_connection, query);
1474 
1475  /* Call the validation function with the SHPLOADERCONFIG and the result set */
1476  ret = validate_remote_loader_columns(loader_file_config, result);
1477  if (ret == SHPLOADERERR)
1478  {
1480 
1481  PQclear(result);
1482  free(query);
1483 
1484  return;
1485  }
1486 
1487  /* Free the SQL query */
1488  PQclear(result);
1489  free(query);
1490 
1491  /* Get next entry */
1492  is_valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(import_file_list_store), &iter);
1493  }
1494 
1495  /* Close our database connection */
1496  PQfinish(pg_connection);
1497 
1498 
1499  /* Once we've done the validation pass, now let's load the shapefile */
1500  is_valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(import_file_list_store), &iter);
1501  while (is_valid)
1502  {
1503  /* Grab the SHPLOADERCONFIG for this row */
1504  gtk_tree_model_get(GTK_TREE_MODEL(import_file_list_store), &iter, IMPORT_POINTER_COLUMN, &gptr, -1);
1505  loader_file_config = (SHPLOADERCONFIG *)gptr;
1506 
1507  pgui_logf("\n==============================");
1508  pgui_logf("Importing with configuration: %s, %s, %s, %s, mode=%c, dump=%d, simple=%d, geography=%d, index=%d, shape=%d, srid=%d", loader_file_config->table, loader_file_config->schema, loader_file_config->geo_col, loader_file_config->shp_file, loader_file_config->opt, loader_file_config->dump_format, loader_file_config->simple_geometries, loader_file_config->geography, loader_file_config->createindex, loader_file_config->readshape, loader_file_config->sr_id);
1509 
1510  /*
1511  * Loop through the items in the shapefile
1512  */
1513  is_running = TRUE;
1514 
1515  /* One connection per file, otherwise error handling becomes tricky... */
1516  pg_connection = PQconnectdb(connection_string);
1517 
1518  /* Disable the button to prevent multiple imports running at the same time */
1519  gtk_widget_set_sensitive(widget, FALSE);
1520 
1521  /* Allow GTK events to get a look in */
1522  while (gtk_events_pending())
1523  gtk_main_iteration();
1524 
1525  /* Create the shapefile state object */
1526  state = ShpLoaderCreate(loader_file_config);
1527 
1528  /* Open the shapefile */
1529  ret = ShpLoaderOpenShape(state);
1530  if (ret != SHPLOADEROK)
1531  {
1532  pgui_logf("%s", state->message);
1533 
1534  if (ret == SHPLOADERERR)
1535  goto import_cleanup;
1536  }
1537 
1538  /* For progress display, only show the "core" filename */
1539  for (i = strlen(loader_file_config->shp_file); i >= 0
1540  && loader_file_config->shp_file[i - 1] != '\\' && loader_file_config->shp_file[i - 1] != '/'; i--);
1541 
1542  progress_shapefile = malloc(strlen(loader_file_config->shp_file));
1543  strcpy(progress_shapefile, &loader_file_config->shp_file[i]);
1544 
1545  /* Display the progress dialog */
1546  snprintf(progress_text, GUIMSG_LINE_MAXLEN, _("Importing shapefile %s (%d records)..."), progress_shapefile, ShpLoaderGetRecordCount(state));
1547  progress_text[GUIMSG_LINE_MAXLEN] = '\0';
1548  gtk_label_set_text(GTK_LABEL(label_progress), progress_text);
1549  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), 0.0);
1550  gtk_widget_show_all(dialog_progress);
1551 
1552  /* If reading the whole shapefile, display its type */
1553  if (state->config->readshape)
1554  {
1555  pgui_logf("Shapefile type: %s", SHPTypeName(state->shpfiletype));
1556  pgui_logf("PostGIS type: %s[%d]", state->pgtype, state->pgdims);
1557  }
1558 
1559  /* Get the header */
1560  ret = ShpLoaderGetSQLHeader(state, &header);
1561  if (ret != SHPLOADEROK)
1562  {
1563  pgui_logf("%s", state->message);
1564 
1565  if (ret == SHPLOADERERR)
1566  goto import_cleanup;
1567  }
1568 
1569  /* Send the header to the remote server: if we are in COPY mode then the last
1570  statement will be a COPY and so will change connection mode */
1571  ret = pgui_exec(header);
1572  free(header);
1573 
1574  if (!ret)
1575  goto import_cleanup;
1576 
1577  /* If we are in prepare mode, we need to skip the actual load. */
1578  if (state->config->opt != 'p')
1579  {
1580  int numrecords = ShpLoaderGetRecordCount(state);
1581  int records_per_tick = (numrecords / 200) - 1;
1582 
1583  if ( records_per_tick < 1 )
1584  records_per_tick = 1;
1585 
1586  /* If we are in COPY (dump format) mode, output the COPY statement and enter COPY mode */
1587  if (state->config->dump_format)
1588  {
1589  ret = ShpLoaderGetSQLCopyStatement(state, &header);
1590 
1591  if (ret != SHPLOADEROK)
1592  {
1593  pgui_logf("%s", state->message);
1594 
1595  if (ret == SHPLOADERERR)
1596  goto import_cleanup;
1597  }
1598 
1599  /* Send the result to the remote server: this should put us in COPY mode */
1600  ret = pgui_copy_start(header);
1601  free(header);
1602 
1603  if (!ret)
1604  goto import_cleanup;
1605  }
1606 
1607  /* Main loop: iterate through all of the records and send them to stdout */
1608  for (i = 0; i < numrecords && is_running; i++)
1609  {
1610  ret = ShpLoaderGenerateSQLRowStatement(state, i, &record);
1611 
1612  switch (ret)
1613  {
1614  case SHPLOADEROK:
1615  /* Simply send the statement */
1616  if (state->config->dump_format)
1617  ret = pgui_copy_write(record);
1618  else
1619  ret = pgui_exec(record);
1620 
1621  /* Display a record number if we failed */
1622  if (!ret)
1623  pgui_logf(_("Import failed on record number %d"), i);
1624 
1625  free(record);
1626  break;
1627 
1628  case SHPLOADERERR:
1629  /* Display the error message then stop */
1630  pgui_logf("%s\n", state->message);
1631  goto import_cleanup;
1632  break;
1633 
1634  case SHPLOADERWARN:
1635  /* Display the warning, but continue */
1636  pgui_logf("%s\n", state->message);
1637 
1638  if (state->config->dump_format)
1639  ret = pgui_copy_write(record);
1640  else
1641  ret = pgui_exec(record);
1642 
1643  /* Display a record number if we failed */
1644  if (!ret)
1645  pgui_logf(_("Import failed on record number %d"), i);
1646 
1647  free(record);
1648  break;
1649 
1650  case SHPLOADERRECDELETED:
1651  /* Record is marked as deleted - ignore */
1652  break;
1653 
1654  case SHPLOADERRECISNULL:
1655  /* Record is NULL and should be ignored according to NULL policy */
1656  break;
1657  }
1658 
1659  /* Update the progress bar */
1660  if ( i % records_per_tick == 0 )
1661  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), (float)i / numrecords);
1662 
1663  /* Allow GTK events to get a look in */
1664  while (gtk_events_pending())
1665  gtk_main_iteration();
1666  }
1667 
1668  /* If we are in COPY (dump format) mode, leave COPY mode */
1669  if (state->config->dump_format)
1670  {
1671  if (! pgui_copy_end(0) )
1672  goto import_cleanup;
1673 
1674  result = PQgetResult(pg_connection);
1675  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1676  {
1677  pgui_logf(_("COPY failed with the following error: %s"), PQerrorMessage(pg_connection));
1678  ret = SHPLOADERERR;
1679  goto import_cleanup;
1680  }
1681  }
1682  } /* if (state->config->opt != 'p') */
1683 
1684  /* Only continue if we didn't abort part way through */
1685  if (is_running)
1686  {
1687  /* Get the footer */
1688  ret = ShpLoaderGetSQLFooter(state, &footer);
1689  if (ret != SHPLOADEROK)
1690  {
1691  pgui_logf("%s\n", state->message);
1692 
1693  if (ret == SHPLOADERERR)
1694  goto import_cleanup;
1695  }
1696 
1697  /* Just in case index creation takes a long time, update the progress text */
1698  if (state->config->createindex)
1699  {
1700  gtk_label_set_text(GTK_LABEL(label_progress), _("Creating spatial index..."));
1701 
1702  /* Allow GTK events to get a look in */
1703  while (gtk_events_pending())
1704  gtk_main_iteration();
1705  }
1706 
1707  /* Send the footer to the server */
1708  ret = pgui_exec(footer);
1709  free(footer);
1710 
1711  if (!ret)
1712  goto import_cleanup;
1713  }
1714 
1715 import_cleanup:
1716  /* Import has definitely stopped running */
1717  is_running = FALSE;
1718 
1719  /* Close the existing connection */
1720  PQfinish(pg_connection);
1721  pg_connection = NULL;
1722 
1723  /* If we didn't finish inserting all of the items (and we expected to), an error occurred */
1724  if ((state->config->opt != 'p' && i != ShpLoaderGetRecordCount(state)) || !ret)
1725  pgui_logf(_("Shapefile import failed."));
1726  else
1727  pgui_logf(_("Shapefile import completed."));
1728 
1729  /* Free the state object */
1730  ShpLoaderDestroy(state);
1731 
1732  /* Tidy up */
1733  if (progress_shapefile)
1734  free(progress_shapefile);
1735 
1736  /* Get next entry */
1737  is_valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(import_file_list_store), &iter);
1738  }
1739 
1740  /* Import has definitely finished */
1741  is_running = FALSE;
1742 
1743  /* Enable the button once again */
1744  gtk_widget_set_sensitive(widget, TRUE);
1745 
1746  /* Silly GTK bug means we have to hide and show the button for it to work again! */
1747  gtk_widget_hide(widget);
1748  gtk_widget_show(widget);
1749 
1750  /* Hide the progress dialog */
1751  gtk_widget_hide(dialog_progress);
1752 
1753  /* Allow GTK events to get a look in */
1754  while (gtk_events_pending())
1755  gtk_main_iteration();
1756 
1757  /* Tidy up */
1758  free(connection_string);
1759 
1760  return;
1761 }
static int connection_test(void)
SHPLOADERCONFIG * config
int ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter)
static int pgui_copy_write(const char *line)
int ShpLoaderOpenShape(SHPLOADERSTATE *state)
static int validate_remote_loader_columns(SHPLOADERCONFIG *config, PGresult *result)
#define SHPLOADERRECISNULL
#define _(String)
Definition: shpcommon.h:24
static int pgui_exec(const char *sql)
static void pgui_logf(const char *fmt,...)
#define SHPLOADERWARN
#define SHPLOADERRECDELETED
int ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
static GtkWidget * progress
static SHPCONNECTIONCONFIG * conn
void ShpLoaderDestroy(SHPLOADERSTATE *state)
static void pgui_seterr(const char *fmt,...)
static int pgui_copy_start(const char *sql)
int ShpLoaderGetRecordCount(SHPLOADERSTATE *state)
SHPLOADERSTATE * ShpLoaderCreate(SHPLOADERCONFIG *config)
static volatile int is_running
#define GUIMSG_LINE_MAXLEN
char * ShpDumperGetConnectionStringFromConn(SHPCONNECTIONCONFIG *conn)
#define SHPLOADEROK
const char SHPAPI_CALL1 * SHPTypeName(int nSHPType);const char SHPAPI_CALL1(*) SHPPartTypeName(int nPartType
Definition: shpopen.c:2092
GtkListStore * import_file_list_store
Definition: shp2pgsql-gui.c:56
char message[SHPLOADERMSGLEN]
#define FALSE
Definition: dbfopen.c:168
static void update_conn_ui_from_conn_config(void)
static GtkWidget * label_progress
void free(void *)
int ShpLoaderGetSQLCopyStatement(SHPLOADERSTATE *state, char **strheader)
void * malloc(YYSIZE_T)
int ShpLoaderGenerateSQLRowStatement(SHPLOADERSTATE *state, int item, char **strrecord)
static GtkWidget * window_conn
Definition: shp2pgsql-gui.c:98
static void pgui_raise_error_dialogue(void)
static int pgui_copy_end(const int rollback)
static PGconn * pg_connection
#define SHPLOADERERR
static GtkWidget * dialog_progress
#define TRUE
Definition: dbfopen.c:169
Here is the call graph for this function:
Here is the caller graph for this function: