PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ShpDumperConnectDatabase()

int ShpDumperConnectDatabase ( SHPDUMPERSTATE state)

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

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