PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ update_table_chooser_from_database()

static void update_table_chooser_from_database ( )
static

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

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

References _, chooser_table_list_store, conn, 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().

Here is the call graph for this function:
Here is the caller graph for this function: