Ticket #2497: ms_postgis_begin_to_connect.diff
| File ms_postgis_begin_to_connect.diff, 7.2 KB (added by , 16 years ago) |
|---|
-
mappostgis.c
166 166 PQfinish((PGconn*) conn_handle); 167 167 } 168 168 169 /* takes a connection and ensures that it is in a valid transactional state */ 170 /* performing ROLLBACK and BEGIN on it if necessary */ 171 int msPOSTGISInitConnection(PGconn *conn) 172 { 173 // if connection is in CONNECTION_BAD status, PQreset() it 174 if (PQstatus(conn) == CONNECTION_BAD) 175 { 176 PQreset(conn); 177 if (PQstatus(conn) == CONNECTION_BAD) 178 { 179 msSetError(MS_QUERYERR, "Database connection reports status CONNECTION_BAD even after attempt to PQreset() it.", "msPOSTGISInitConnection()"); 180 return MS_FAILURE; 181 } 182 } 183 184 if (PQtransactionStatus(conn) == PQTRANS_INERROR) // idle, in a failed transaction block 185 { 186 PGresult *rb_res = PQexec(conn, "ROLLBACK"); 187 if (!rb_res || PQresultStatus(rb_res) != PGRES_COMMAND_OK) { 188 msSetError(MS_QUERYERR, "Error executing POSTGIS ROLLBACK statement.", "msPOSTGISInitConnection()"); 189 190 if(rb_res) { 191 PQclear(rb_res); 192 } 193 194 return MS_FAILURE; 195 } 196 PQclear(rb_res); 197 } 198 199 if (PQtransactionStatus(conn) == PQTRANS_IDLE) // idle, but not in a transaction block 200 { 201 PGresult *beg_res = PQexec(conn, "BEGIN"); 202 if (!beg_res || PQresultStatus(beg_res) != PGRES_COMMAND_OK) { 203 msSetError(MS_QUERYERR, "Error executing POSTGIS BEGIN statement.", "msPOSTGISInitConnection()"); 204 205 if(beg_res) { 206 PQclear(beg_res); 207 } 208 209 return MS_FAILURE; 210 } 211 PQclear(beg_res); 212 } 213 214 return MS_SUCCESS; 215 } 216 169 217 /*static int gBYTE_ORDER = 0;*/ 170 218 171 219 /* open up a connection to the postgresql database using the connection string in layer->connection */ … … 250 298 return MS_FAILURE; 251 299 } 252 300 301 /* start a transaction, since it's required by all subsequent (DECLARE CURSOR) queries */ 302 if (msPOSTGISInitConnection(layerinfo->conn) != MS_SUCCESS) 303 { 304 return MS_FAILURE; 305 } 306 253 307 msConnPoolRegister(layer, layerinfo->conn, msPOSTGISCloseConnection); 254 308 255 309 PQsetNoticeProcessor(layerinfo->conn, postresql_NOTICE_HANDLER, (void *) layer); … … 486 540 free(f_table_name); 487 541 free(columns_wanted); 488 542 489 /* start transaction required by cursor */490 491 result = PQexec(layerinfo->conn, "BEGIN");492 if(!result || PQresultStatus(result) != PGRES_COMMAND_OK) {493 msSetError(MS_QUERYERR, "Error executing POSTGIS BEGIN statement.", "prepare_database()");494 495 if(result) {496 PQclear(result);497 }498 if(layerinfo->query_result) {499 PQclear(layerinfo->query_result);500 }501 layerinfo->query_result = NULL;502 PQreset(layerinfo->conn);503 504 free(query_string_0_6);505 506 return MS_FAILURE; /* totally screwed */507 }508 509 PQclear(result);510 511 543 /* set enable_seqscan=off not required (already done) */ 512 544 513 545 if(layer->debug) { … … 552 584 PQclear(result); 553 585 } 554 586 layerinfo->query_result = NULL; 555 PQreset(layerinfo->conn); 587 if (msPOSTGISInitConnection(layerinfo->conn) != MS_SUCCESS) 588 { 589 return MS_FAILURE; 590 } 556 591 557 592 free(error_message); 558 593 free(query_string_0_6); … … 562 597 563 598 PQclear(result); 564 599 layerinfo->query_result = NULL; 565 PQreset(layerinfo->conn);600 msPOSTGISInitConnection(layerinfo->conn); 566 601 567 602 /* TODO Rename tmp2 to something meaningful */ 568 603 tmp2 = (char *) malloc(149 + strlen(query_string_0_6) + strlen(error_message) + 1); … … 643 678 PQclear(layerinfo->query_result); 644 679 } 645 680 layerinfo->query_result = NULL; 646 PQreset(layerinfo->conn);681 msPOSTGISInitConnection(layerinfo->conn); 647 682 648 683 return MS_FAILURE; 649 684 } … … 1202 1237 free(geom_column_name); 1203 1238 free(table_name); 1204 1239 1205 query_result = PQexec(layerinfo->conn, "BEGIN");1206 if(!query_result || PQresultStatus(query_result) != PGRES_COMMAND_OK) {1207 msSetError(MS_QUERYERR, "Error executing POSTGIS BEGIN statement.", "msPOSTGISLayerGetShape()");1208 1209 if(query_result) {1210 PQclear(query_result);1211 }1212 PQreset(layerinfo->conn);1213 1214 free(query_str);1215 1216 return MS_FAILURE;1217 }1218 PQclear(query_result);1219 1220 1240 query_result = PQexec(layerinfo->conn, query_str); 1221 1241 1222 1242 if(!query_result || PQresultStatus(query_result) != PGRES_COMMAND_OK) { … … 1225 1245 if(query_result) { 1226 1246 PQclear(query_result); 1227 1247 } 1228 PQreset(layerinfo->conn);1248 msPOSTGISInitConnection(layerinfo->conn); 1229 1249 1230 1250 free(query_str); 1231 1251 … … 1241 1261 if(query_result) { 1242 1262 PQclear(query_result); 1243 1263 } 1244 PQreset(layerinfo->conn);1264 msPOSTGISInitConnection(layerinfo->conn); 1245 1265 1246 1266 free(query_str); 1247 1267 … … 1312 1332 PQclear(query_result); 1313 1333 1314 1334 query_result = PQexec(layerinfo->conn, "CLOSE mycursor2"); 1315 if(query_result) {1316 PQclear(query_result);1317 }1318 1335 1319 query_result = PQexec(layerinfo->conn, "ROLLBACK");1320 if(!query_result || PQresultStatus(query_result) != PGRES_COMMAND_OK){1321 ms SetError(MS_QUERYERR, "Error executing POSTGIS BEGIN statement.", "msPOSTGISLayerGetShape()");1322 1323 if(query_result){1336 if(!query_result || PQresultStatus(query_result) != PGRES_COMMAND_OK) 1337 { 1338 msFreeShape(shape); 1339 if (query_result) 1340 { 1324 1341 PQclear(query_result); 1325 1342 } 1326 PQreset(layerinfo->conn);1327 1343 1328 msFreeShape(shape); 1344 if (msPOSTGISInitConnection(layerinfo->conn) != MS_SUCCESS) 1345 { 1346 return MS_FAILURE; 1347 } 1329 1348 1349 msSetError(MS_QUERYERR, "Error executing POSTGIS CLOSE statement on query (which returned one or more tuples).", "msPOSTGISLayerGetShape()"); 1350 1330 1351 return MS_FAILURE; 1331 1352 } 1332 1353 … … 1340 1361 PQclear(query_result); 1341 1362 1342 1363 query_result = PQexec(layerinfo->conn, "CLOSE mycursor2"); 1343 if(query_result) {1344 PQclear(query_result);1345 }1346 1364 1347 query_result = PQexec(layerinfo->conn, "ROLLBACK"); 1348 if(!query_result || PQresultStatus(query_result) != PGRES_COMMAND_OK) { 1349 msSetError(MS_QUERYERR, "Error executing POSTGIS BEGIN statement.", "msPOSTGISLayerGetShape()"); 1365 if (!query_result || PQresultStatus(query_result) != PGRES_COMMAND_OK) 1366 { 1367 if (query_result) 1368 { 1369 PQclear(query_result); 1370 } 1350 1371 1351 if(query_result) { 1352 PQclear(query_result); 1372 if (msPOSTGISInitConnection(layerinfo->conn) != MS_SUCCESS) 1373 { 1374 return MS_FAILURE; 1353 1375 } 1354 PQreset(layerinfo->conn);1355 1376 1377 msSetError(MS_QUERYERR, "Error executing POSTGIS CLOSE statement on query (which returned zero tuples).", "msPOSTGISLayerGetShape()"); 1378 1356 1379 return MS_FAILURE; 1357 1380 } 1358 1381
