PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ ShpDumperConnectDatabase()

int ShpDumperConnectDatabase ( SHPDUMPERSTATE state)

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

1254 {
1255  PGresult *res;
1256 
1257  char *connstring, *tmpvalue;
1258 
1259  /* Generate the PostgreSQL connection string */
1260  connstring = ShpDumperGetConnectionStringFromConn(state->config->conn);
1261 
1262  /* Connect to the database */
1263  state->conn = PQconnectdb(connstring);
1264  if (PQstatus(state->conn) == CONNECTION_BAD)
1265  {
1266  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQerrorMessage(state->conn));
1267  free(connstring);
1268  return SHPDUMPERERR;
1269  }
1270 
1271  /* Set datestyle to ISO */
1272  res = PQexec(state->conn, "SET DATESTYLE='ISO'");
1273  if (PQresultStatus(res) != PGRES_COMMAND_OK)
1274  {
1275  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQresultErrorMessage(res));
1276  PQclear(res);
1277  free(connstring);
1278  return SHPDUMPERERR;
1279  }
1280 
1281  PQclear(res);
1282 
1283  /* Retrieve PostGIS major version */
1284  res = PQexec(state->conn, "SELECT postgis_version()");
1285  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1286  {
1287  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQresultErrorMessage(res));
1288  PQclear(res);
1289  free(connstring);
1290  return SHPDUMPERERR;
1291  }
1292 
1293  tmpvalue = PQgetvalue(res, 0, 0);
1294  state->pgis_major_version = atoi(tmpvalue);
1295 
1296  PQclear(res);
1297 
1298  /* Find the OID for the geometry type */
1299  res = PQexec(state->conn, "SELECT oid FROM pg_type WHERE typname = 'geometry'");
1300  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1301  {
1302  snprintf(state->message, SHPDUMPERMSGLEN, _("Error looking up geometry oid: %s"), PQresultErrorMessage(res));
1303  PQclear(res);
1304  free(connstring);
1305  return SHPDUMPERERR;
1306  }
1307 
1308  if (PQntuples(res) > 0)
1309  {
1310  tmpvalue = PQgetvalue(res, 0, 0);
1311  state->geom_oid = atoi(tmpvalue);
1312  }
1313  else
1314  {
1315  snprintf(state->message, SHPDUMPERMSGLEN, _("Geometry type unknown (have you enabled postgis?)"));
1316  PQclear(res);
1317  free(connstring);
1318  return SHPDUMPERERR;
1319  }
1320 
1321  PQclear(res);
1322 
1323  /* Find the OID for the geography type */
1324  res = PQexec(state->conn, "SELECT oid FROM pg_type WHERE typname = 'geography'");
1325  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1326  {
1327  snprintf(state->message, SHPDUMPERMSGLEN, _("Error looking up geography oid: %s"), PQresultErrorMessage(res));
1328  PQclear(res);
1329  free(connstring);
1330  return SHPDUMPERERR;
1331  }
1332 
1333  if (PQntuples(res) > 0)
1334  {
1335  /* Old databases don't have a geography type, so don't fail if we don't find it */
1336  tmpvalue = PQgetvalue(res, 0, 0);
1337  state->geog_oid = atoi(tmpvalue);
1338  }
1339 
1340  PQclear(res);
1341 
1342  free(connstring);
1343 
1344  return SHPDUMPEROK;
1345 }
void free(void *)
tuple res
Definition: window.py:78
char * ShpDumperGetConnectionStringFromConn(SHPCONNECTIONCONFIG *conn)
#define SHPDUMPERMSGLEN
#define SHPDUMPEROK
#define SHPDUMPERERR
#define _(String)
Definition: shpcommon.h:24
SHPCONNECTIONCONFIG * conn
SHPDUMPERCONFIG * config
char message[SHPDUMPERMSGLEN]

References _, shp_dumper_state::config, shp_dumper_config::conn, shp_dumper_state::conn, free(), 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().

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