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

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

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().

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

Here is the call graph for this function:

Here is the caller graph for this function: