PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ ShpDumperConnectDatabase()

int ShpDumperConnectDatabase ( SHPDUMPERSTATE state)

Definition at line 1234 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().

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