PostGIS  2.2.7dev-r@@SVN_REVISION@@
static void update_table_chooser_from_database ( )
static

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

References _, chooser_table_list_store, free(), malloc(), pg_connection, ShpDumperGetConnectionStringFromConn(), TABLECHOOSER_GEO_COLUMN, TABLECHOOSER_GEO_LISTSTORE_COLUMN, TABLECHOOSER_GEOCOL_COMBO_COLUMNS, TABLECHOOSER_GEOCOL_COMBO_TEXT, TABLECHOOSER_HASGEO_COLUMN, TABLECHOOSER_SCHEMA_COLUMN, and TABLECHOOSER_TABLE_COLUMN.

Referenced by pgui_action_open_table_dialog().

668 {
669  PGresult *result, *geocol_result;
670  GtkTreeIter iter, geocol_iter;
671  GtkListStore *dumper_geocol_combo_list;
672  char *connection_string, *sql_form, *query, *schema, *table, *geocol_query, *geocol_name=NULL;
673  int hasgeo, i, j;
674 
675  /* Open a connection to the database */
676  connection_string = ShpDumperGetConnectionStringFromConn(conn);
677  pg_connection = PQconnectdb(connection_string);
678 
679  /* Here we find a list of all tables/views that not in a pg_* schema (or information_schema) and
680  we return the schema name, table name and whether or not the table/view contains any geo
681  columns */
682  query = "SELECT tableoids.oid, n.nspname, tableoids.relname, COALESCE((SELECT 1 from pg_attribute WHERE attrelid = tableoids.oid AND atttypid IN (SELECT oid FROM pg_type WHERE typname in ('geometry', 'geography')) LIMIT 1), 0) hasgeo FROM (SELECT c.oid, c.relname, c.relnamespace FROM pg_class c WHERE c.relkind IN ('r', 'v') AND c.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname NOT ILIKE 'pg_%' AND nspname <> 'information_schema')) tableoids, pg_namespace n WHERE tableoids.relnamespace = n.oid ORDER BY n.nspname, tableoids.relname";
683 
684  result = PQexec(pg_connection, query);
685 
686  /* Free any existing entries in the model */
687  gtk_list_store_clear(chooser_table_list_store);
688 
689  /* Now insert one row for each query result */
690  for (i = 0; i < PQntuples(result); i++)
691  {
692  gtk_list_store_insert_before(chooser_table_list_store, &iter, NULL);
693 
694  /* Look up the geo columns; if there are none then we set the field to (None). If we have just one
695  column then we set the column name directly. If we have more than one then we create a combo
696  dropdown containing the column names */
697  schema = PQgetvalue(result, i, PQfnumber(result, "nspname"));
698  table = PQgetvalue(result, i, PQfnumber(result, "relname"));
699 
700  sql_form = "SELECT n.nspname, c.relname, a.attname FROM pg_class c, pg_namespace n, pg_attribute a WHERE c.relnamespace = n.oid AND n.nspname = '%s' AND c.relname = '%s' AND a.attrelid = c.oid AND a.atttypid IN (SELECT oid FROM pg_type WHERE typname in ('geometry', 'geography'))";
701 
702  geocol_query = malloc(strlen(sql_form) + strlen(schema) + strlen(table) + 1);
703  sprintf(geocol_query, sql_form, schema, table);
704 
705  geocol_result = PQexec(pg_connection, geocol_query);
706 
707  /* Create a combo list loaded with the column names. Note that while we create the model and load
708  the data here, we don't actually display the geo column in this dialog. Instead we build the
709  list here so that we can pass to the export table list store when creating a new entry. This
710  is to ensure that the export table list model can directly represent a SHPDUMPERCONFIG. */
711  dumper_geocol_combo_list = gtk_list_store_new(TABLECHOOSER_GEOCOL_COMBO_COLUMNS, G_TYPE_STRING);
712 
713  if (PQntuples(geocol_result) > 0)
714  {
715  /* Load the columns into the list store */
716  for (j = 0; j < PQntuples(geocol_result); j++)
717  {
718  geocol_name = PQgetvalue(geocol_result, j, PQfnumber(geocol_result, "attname"));
719 
720  gtk_list_store_insert_before(dumper_geocol_combo_list, &geocol_iter, (GtkTreeIter *)TABLECHOOSER_GEOCOL_COMBO_TEXT);
721  gtk_list_store_set(dumper_geocol_combo_list, &geocol_iter,
722  TABLECHOOSER_GEOCOL_COMBO_TEXT, geocol_name,
723  -1);
724  }
725  }
726  else
727  {
728  /* Add a "default" entry */
729  geocol_name = NULL;
730 
731  gtk_list_store_insert_before(dumper_geocol_combo_list, &geocol_iter, (GtkTreeIter *)TABLECHOOSER_GEOCOL_COMBO_TEXT);
732  gtk_list_store_set(dumper_geocol_combo_list, &geocol_iter,
734  -1);
735  }
736 
737  /* Free the query result */
738  PQclear(geocol_result);
739 
740  /* Free the query string */
741  free(geocol_query);
742 
743  /* Set the list store data */
744  hasgeo = atoi(PQgetvalue(result, i, PQfnumber(result, "hasgeo")));
745  gtk_list_store_set(chooser_table_list_store, &iter,
748  TABLECHOOSER_GEO_LISTSTORE_COLUMN, dumper_geocol_combo_list,
749  TABLECHOOSER_GEO_COLUMN, geocol_name,
751  -1);
752  }
753 
754  /* Clear up the result set */
755  PQclear(result);
756 
757  /* Close the existing connection */
758  PQfinish(pg_connection);
759  pg_connection = NULL;
760 
761  return;
762 }
#define _(String)
Definition: shpcommon.h:24
static SHPCONNECTIONCONFIG * conn
char * ShpDumperGetConnectionStringFromConn(SHPCONNECTIONCONFIG *conn)
void free(void *)
void * malloc(YYSIZE_T)
static PGconn * pg_connection
GtkListStore * chooser_table_list_store

Here is the call graph for this function:

Here is the caller graph for this function: