PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ ShpDumperConnectDatabase()

int ShpDumperConnectDatabase ( SHPDUMPERSTATE state)

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

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