PostGIS  2.1.10dev-r@@SVN_REVISION@@
int ShpDumperConnectDatabase ( SHPDUMPERSTATE state)

Definition at line 1323 of file pgsql2shp-core.c.

References _, shp_dumper_state::config, shp_dumper_config::conn, shp_dumper_state::conn, shp_dumper_state::geog_oid, shp_dumper_state::geom_oid, shp_dumper_state::message, shp_dumper_state::pgis_major_version, window::res, SHPDUMPERERR, ShpDumperGetConnectionStringFromConn(), SHPDUMPERMSGLEN, and SHPDUMPEROK.

Referenced by main(), and pgui_action_export().

1324 {
1325  PGresult *res;
1326 
1327  char *connstring, *tmpvalue;
1328 
1329  /* Generate the PostgreSQL connection string */
1330  connstring = ShpDumperGetConnectionStringFromConn(state->config->conn);
1331 
1332  /* Connect to the database */
1333  state->conn = PQconnectdb(connstring);
1334  if (PQstatus(state->conn) == CONNECTION_BAD)
1335  {
1336  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQerrorMessage(state->conn));
1337  free(connstring);
1338  return SHPDUMPERERR;
1339  }
1340 
1341  /* Set datestyle to ISO */
1342  res = PQexec(state->conn, "SET DATESTYLE='ISO'");
1343  if (PQresultStatus(res) != PGRES_COMMAND_OK)
1344  {
1345  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQresultErrorMessage(res));
1346  PQclear(res);
1347  free(connstring);
1348  return SHPDUMPERERR;
1349  }
1350 
1351  PQclear(res);
1352 
1353  /* Retrieve PostGIS major version */
1354  res = PQexec(state->conn, "SELECT postgis_version()");
1355  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1356  {
1357  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQresultErrorMessage(res));
1358  PQclear(res);
1359  free(connstring);
1360  return SHPDUMPERERR;
1361  }
1362 
1363  tmpvalue = PQgetvalue(res, 0, 0);
1364  state->pgis_major_version = atoi(tmpvalue);
1365 
1366  PQclear(res);
1367 
1368  /* Find the OID for the geometry type */
1369  res = PQexec(state->conn, "SELECT oid FROM pg_type WHERE typname = 'geometry'");
1370  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1371  {
1372  snprintf(state->message, SHPDUMPERMSGLEN, _("Error looking up geometry oid: %s"), PQresultErrorMessage(res));
1373  PQclear(res);
1374  free(connstring);
1375  return SHPDUMPERERR;
1376  }
1377 
1378  if (PQntuples(res) > 0)
1379  {
1380  tmpvalue = PQgetvalue(res, 0, 0);
1381  state->geom_oid = atoi(tmpvalue);
1382  }
1383  else
1384  {
1385  snprintf(state->message, SHPDUMPERMSGLEN, _("Geometry type unknown (have you enabled postgis?)"));
1386  PQclear(res);
1387  free(connstring);
1388  return SHPDUMPERERR;
1389  }
1390 
1391  PQclear(res);
1392 
1393  /* Find the OID for the geography type */
1394  res = PQexec(state->conn, "SELECT oid FROM pg_type WHERE typname = 'geography'");
1395  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1396  {
1397  snprintf(state->message, SHPDUMPERMSGLEN, _("Error looking up geography oid: %s"), PQresultErrorMessage(res));
1398  PQclear(res);
1399  free(connstring);
1400  return SHPDUMPERERR;
1401  }
1402 
1403  if (PQntuples(res) > 0)
1404  {
1405  /* Old databases don't have a geography type, so don't fail if we don't find it */
1406  tmpvalue = PQgetvalue(res, 0, 0);
1407  state->geog_oid = atoi(tmpvalue);
1408  }
1409 
1410  PQclear(res);
1411 
1412  free(connstring);
1413 
1414  return SHPDUMPEROK;
1415 }
tuple res
Definition: window.py:80
#define _(String)
Definition: shpcommon.h:23
SHPDUMPERCONFIG * config
#define SHPDUMPERERR
SHPCONNECTIONCONFIG * conn
char * ShpDumperGetConnectionStringFromConn(SHPCONNECTIONCONFIG *conn)
#define SHPDUMPERMSGLEN
#define SHPDUMPEROK
char message[SHPDUMPERMSGLEN]

Here is the call graph for this function:

Here is the caller graph for this function: