PostGIS  2.2.7dev-r@@SVN_REVISION@@
static void pgui_action_import ( GtkWidget *  widget,
gpointer  data 
)
static

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

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