PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ShpDumperConnectDatabase()

int ShpDumperConnectDatabase ( SHPDUMPERSTATE state)

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

1214 {
1215  PGresult *res;
1216  char *tmpvalue;
1217 
1218  /* Generate the PostgreSQL connection string */
1219  char *connstring = ShpDumperGetConnectionStringFromConn(state->config->conn);
1220 
1221  /* Connect to the database */
1222  state->conn = PQconnectdb(connstring);
1223  if (PQstatus(state->conn) == CONNECTION_BAD)
1224  {
1225  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQerrorMessage(state->conn));
1226  free(connstring);
1227  return SHPDUMPERERR;
1228  }
1229 
1230  /* Set datestyle to ISO */
1231  res = PQexec(state->conn, "SET DATESTYLE='ISO'");
1232  if (PQresultStatus(res) != PGRES_COMMAND_OK)
1233  {
1234  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQresultErrorMessage(res));
1235  PQclear(res);
1236  free(connstring);
1237  return SHPDUMPERERR;
1238  }
1239 
1240  PQclear(res);
1241 
1242  /* Retrieve PostGIS major version */
1243  res = PQexec(state->conn, "SELECT postgis_version()");
1244  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1245  {
1246  snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQresultErrorMessage(res));
1247  PQclear(res);
1248  free(connstring);
1249  return SHPDUMPERERR;
1250  }
1251 
1252  tmpvalue = PQgetvalue(res, 0, 0);
1253  state->pgis_major_version = atoi(tmpvalue);
1254 
1255  PQclear(res);
1256 
1257  /* Find the OID for the geometry type */
1258  res = PQexec(state->conn, "SELECT oid FROM pg_type WHERE typname = 'geometry'");
1259  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1260  {
1261  snprintf(state->message, SHPDUMPERMSGLEN, _("Error looking up geometry oid: %s"), PQresultErrorMessage(res));
1262  PQclear(res);
1263  free(connstring);
1264  return SHPDUMPERERR;
1265  }
1266 
1267  if (PQntuples(res) > 0)
1268  {
1269  tmpvalue = PQgetvalue(res, 0, 0);
1270  state->geom_oid = atoi(tmpvalue);
1271  }
1272  else
1273  {
1274  snprintf(state->message, SHPDUMPERMSGLEN, _("Geometry type unknown (have you enabled postgis?)"));
1275  PQclear(res);
1276  free(connstring);
1277  return SHPDUMPERERR;
1278  }
1279 
1280  PQclear(res);
1281 
1282  /* Find the OID for the geography type */
1283  res = PQexec(state->conn, "SELECT oid FROM pg_type WHERE typname = 'geography'");
1284  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1285  {
1286  snprintf(state->message, SHPDUMPERMSGLEN, _("Error looking up geography oid: %s"), PQresultErrorMessage(res));
1287  PQclear(res);
1288  free(connstring);
1289  return SHPDUMPERERR;
1290  }
1291 
1292  if (PQntuples(res) > 0)
1293  {
1294  /* Old databases don't have a geography type, so don't fail if we don't find it */
1295  tmpvalue = PQgetvalue(res, 0, 0);
1296  state->geog_oid = atoi(tmpvalue);
1297  }
1298 
1299  PQclear(res);
1300 
1301  free(connstring);
1302 
1303  return SHPDUMPEROK;
1304 }
void free(void *)
tuple res
Definition: window.py:79
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: