PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ShpDumperConnectDatabase()

int ShpDumperConnectDatabase ( SHPDUMPERSTATE state)

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

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