Ticket #2497: ms_postgis_begin_to_connect.diff

File ms_postgis_begin_to_connect.diff, 7.2 KB (added by dfuhry, 16 years ago)

new version of patch, which doesn't clobber necessary query in msPOSTGISLayerGetShape

  • mappostgis.c

     
    166166    PQfinish((PGconn*) conn_handle);
    167167}
    168168
     169/* takes a connection and ensures that it is in a valid transactional state */
     170/* performing ROLLBACK and BEGIN on it if necessary */
     171int 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
    169217/*static int gBYTE_ORDER = 0;*/
    170218
    171219/* open up a connection to the postgresql database using the connection string in layer->connection */
     
    250298            return MS_FAILURE;
    251299        }
    252300
     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
    253307        msConnPoolRegister(layer, layerinfo->conn, msPOSTGISCloseConnection);
    254308
    255309        PQsetNoticeProcessor(layerinfo->conn, postresql_NOTICE_HANDLER, (void *) layer);
     
    486540    free(f_table_name);
    487541    free(columns_wanted);
    488542
    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 
    511543    /* set enable_seqscan=off not required (already done) */
    512544
    513545    if(layer->debug) {
     
    552584            PQclear(result);
    553585        }
    554586        layerinfo->query_result = NULL;
    555         PQreset(layerinfo->conn);
     587        if (msPOSTGISInitConnection(layerinfo->conn) != MS_SUCCESS)
     588        {
     589          return MS_FAILURE;
     590        }
    556591
    557592        free(error_message);
    558593        free(query_string_0_6);
     
    562597
    563598    PQclear(result);
    564599    layerinfo->query_result = NULL;
    565     PQreset(layerinfo->conn);
     600    msPOSTGISInitConnection(layerinfo->conn);
    566601
    567602    /* TODO Rename tmp2 to something meaningful */
    568603    tmp2 = (char *) malloc(149 + strlen(query_string_0_6) + strlen(error_message) + 1);
     
    643678          PQclear(layerinfo->query_result);
    644679        }
    645680        layerinfo->query_result = NULL;
    646         PQreset(layerinfo->conn);
     681        msPOSTGISInitConnection(layerinfo->conn);
    647682
    648683        return MS_FAILURE;
    649684    }
     
    12021237    free(geom_column_name);
    12031238    free(table_name);
    12041239
    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 
    12201240    query_result = PQexec(layerinfo->conn, query_str);
    12211241
    12221242    if(!query_result || PQresultStatus(query_result) != PGRES_COMMAND_OK) {
     
    12251245        if(query_result) {
    12261246          PQclear(query_result);
    12271247        }
    1228         PQreset(layerinfo->conn);
     1248        msPOSTGISInitConnection(layerinfo->conn);
    12291249
    12301250        free(query_str);
    12311251
     
    12411261        if(query_result) {
    12421262          PQclear(query_result);
    12431263        }
    1244         PQreset(layerinfo->conn);
     1264        msPOSTGISInitConnection(layerinfo->conn);
    12451265
    12461266        free(query_str);
    12471267
     
    13121332                PQclear(query_result);
    13131333
    13141334            query_result = PQexec(layerinfo->conn, "CLOSE mycursor2");
    1315             if(query_result) {
    1316                 PQclear(query_result);
    1317             }
    13181335
    1319             query_result = PQexec(layerinfo->conn, "ROLLBACK");
    1320             if(!query_result || PQresultStatus(query_result) != PGRES_COMMAND_OK) {
    1321                 msSetError(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                {
    13241341                    PQclear(query_result);
    13251342                }
    1326                 PQreset(layerinfo->conn);
    13271343
    1328                 msFreeShape(shape);
     1344                if (msPOSTGISInitConnection(layerinfo->conn) != MS_SUCCESS)
     1345                {
     1346                    return MS_FAILURE;
     1347                }
    13291348
     1349                msSetError(MS_QUERYERR, "Error executing POSTGIS CLOSE statement on query (which returned one or more tuples).", "msPOSTGISLayerGetShape()");
     1350
    13301351                return MS_FAILURE;
    13311352            }
    13321353
     
    13401361        PQclear(query_result);
    13411362
    13421363        query_result = PQexec(layerinfo->conn, "CLOSE mycursor2");
    1343         if(query_result) {
    1344             PQclear(query_result);
    1345         }
    13461364
    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            }
    13501371
    1351             if(query_result) {
    1352                 PQclear(query_result);
     1372            if (msPOSTGISInitConnection(layerinfo->conn) != MS_SUCCESS)
     1373            {
     1374              return MS_FAILURE;
    13531375            }
    1354             PQreset(layerinfo->conn);
    13551376
     1377            msSetError(MS_QUERYERR, "Error executing POSTGIS CLOSE statement on query (which returned zero tuples).", "msPOSTGISLayerGetShape()");
     1378
    13561379            return MS_FAILURE;
    13571380        }
    13581381