PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ ShpDumperConnectDatabase()

int ShpDumperConnectDatabase ( SHPDUMPERSTATE state)

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

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