Changeset 11225

Show
Ignore:
Timestamp:
04/08/07 10:51:34 (2 years ago)
Author:
mloskot
Message:

Ported fix of cleaning query results in PG driver (Ticket #1514). It was submitted to the trunk with r10931.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.4/gdal/ogr/ogrsf_frmts/pg/ogr_pg.h

    r10646 r11225  
    315315}; 
    316316 
    317  
    318317#endif /* ndef _OGR_PG_H_INCLUDED */ 
    319318 
    320  
  • branches/1.4/gdal/ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp

    r10875 r11225  
    2929 
    3030#include "ogr_pg.h" 
     31#include "ogrpgutility.h" 
    3132#include "cpl_conv.h" 
    3233#include "cpl_string.h" 
     
    167168    if( strstr(pszNewName, "dbname=") != NULL ) 
    168169    { 
    169         int     i; 
    170  
    171170        pszDBName = CPLStrdup( strstr(pszNewName, "dbname=") + 7 ); 
    172171 
    173         for( i = 0; pszDBName[i] != '\0'; i++ ) 
     172        for( int i = 0; pszDBName[i] != '\0'; i++ ) 
    174173        { 
    175174            if( pszDBName[i] == ' ' ) 
     
    192191/*      so we will get the value of the gist indexes.                   */ 
    193192/* -------------------------------------------------------------------- */ 
    194     PGresult            *hResult
     193    PGresult    *hResult = NULL
    195194 
    196195    hResult = PQexec(hPGConn, "BEGIN"); 
     
    198197    if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) 
    199198    { 
    200         PQclear( hResult ); 
     199        OGRPGClearResult( hResult ); 
     200        CPLAssert(NULL == hResult); 
    201201 
    202202        hResult = PQexec(hPGConn, 
     
    211211    } 
    212212    else 
     213    { 
    213214        nGeometryOID = (Oid) 0; 
    214  
    215     if( hResult ) 
    216         PQclear( hResult ); 
     215    } 
     216 
     217    OGRPGClearResult( hResult ); 
     218    CPLAssert(NULL == hResult); /* Test if safe PQclear has not been broken */ 
    217219 
    218220    // find out postgis version. 
     
    279281                    PQgetvalue(hResult,0,0) ); 
    280282        } 
    281         PQclear(hResult); 
     283        OGRPGClearResult(hResult); 
    282284 
    283285 
     
    295297            hResult = PQexec(hPGConn, "SET ENABLE_SEQSCAN = ON"); 
    296298        } 
    297         PQclear( hResult ); 
     299        OGRPGClearResult( hResult ); 
    298300    } 
    299301 
    300302    hResult = PQexec(hPGConn, "COMMIT"); 
    301     PQclear( hResult ); 
     303    OGRPGClearResult( hResult ); 
    302304 
    303305/* -------------------------------------------------------------------- */ 
     
    308310    if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) 
    309311    { 
    310         PQclear( hResult ); 
     312        OGRPGClearResult( hResult ); 
    311313 
    312314        if ( bHavePostGIS ) 
     
    325327    if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) 
    326328    { 
    327         PQclear( hResult ); 
     329        OGRPGClearResult( hResult ); 
    328330        hResult = PQexec(hPGConn, "FETCH ALL in mycursor" ); 
    329331    } 
     
    331333    if( !hResult || PQresultStatus(hResult) != PGRES_TUPLES_OK ) 
    332334    { 
     335        OGRPGClearResult( hResult ); 
     336 
    333337        CPLError( CE_Failure, CPLE_AppDefined, 
    334338                  "%s", PQerrorMessage(hPGConn) ); 
     
    361365/*      Cleanup                                                         */ 
    362366/* -------------------------------------------------------------------- */ 
    363     PQclear( hResult ); 
     367    OGRPGClearResult( hResult ); 
    364368 
    365369    hResult = PQexec(hPGConn, "CLOSE mycursor"); 
    366     PQclear( hResult ); 
     370    OGRPGClearResult( hResult ); 
    367371 
    368372    hResult = PQexec(hPGConn, "COMMIT"); 
    369     PQclear( hResult ); 
     373    OGRPGClearResult( hResult ); 
    370374 
    371375/* -------------------------------------------------------------------- */ 
     
    448452 
    449453    hResult = PQexec(hPGConn, "BEGIN"); 
    450     PQclear( hResult ); 
     454    OGRPGClearResult( hResult ); 
    451455 
    452456    if( bHavePostGIS ) 
     
    459463 
    460464        hResult = PQexec( hPGConn, szCommand ); 
    461         PQclear( hResult ); 
     465        OGRPGClearResult( hResult ); 
    462466    } 
    463467 
     
    465469    CPLDebug( "OGR_PG", "PGexec(%s)", szCommand ); 
    466470    hResult = PQexec( hPGConn, szCommand ); 
    467     PQclear( hResult ); 
     471    OGRPGClearResult( hResult ); 
    468472 
    469473    hResult = PQexec(hPGConn, "COMMIT"); 
    470     PQclear( hResult ); 
     474    OGRPGClearResult( hResult ); 
    471475 
    472476    return OGRERR_NONE; 
     
    535539      { 
    536540          pszSchemaName = CPLStrdup(PQgetvalue(hResult,0,0)); 
    537           PQclear( hResult ); 
    538541      } 
     542      OGRPGClearResult( hResult ); 
    539543    } 
    540544 
     
    605609/* -------------------------------------------------------------------- */ 
    606610    hResult = PQexec(hPGConn, "BEGIN"); 
    607     PQclear( hResult ); 
    608  
    609  
     611    OGRPGClearResult( hResult ); 
    610612 
    611613    if( !bHavePostGIS ) 
     
    633635        CPLFree( pszLayerName ); 
    634636        CPLFree( pszSchemaName ); 
    635         PQclear( hResult ); 
     637 
     638        OGRPGClearResult( hResult ); 
    636639        hResult = PQexec( hPGConn, "ROLLBACK" ); 
    637         PQclear( hResult ); 
     640        OGRPGClearResult( hResult ); 
    638641        return NULL; 
    639642    } 
    640643 
    641     PQclear( hResult ); 
     644    OGRPGClearResult( hResult ); 
    642645 
    643646/* -------------------------------------------------------------------- */ 
     
    669672        CPLDebug( "OGR_PG", "PQexec(%s)", szCommand ); 
    670673        hResult = PQexec(hPGConn, szCommand); 
    671         PQclear( hResult ); 
     674        OGRPGClearResult( hResult ); 
    672675 
    673676        switch( wkbFlatten(eType) ) 
     
    725728            CPLFree( pszSchemaName ); 
    726729 
    727             PQclear( hResult ); 
     730            OGRPGClearResult( hResult ); 
    728731 
    729732            hResult = PQexec(hPGConn, "ROLLBACK"); 
    730             PQclear( hResult ); 
     733            OGRPGClearResult( hResult ); 
    731734 
    732735            return NULL; 
    733736        } 
    734737 
    735         PQclear( hResult ); 
     738        OGRPGClearResult( hResult ); 
    736739    } 
    737740 
     
    740743/* -------------------------------------------------------------------- */ 
    741744    hResult = PQexec(hPGConn, "COMMIT"); 
    742     PQclear( hResult ); 
     745    OGRPGClearResult( hResult ); 
    743746 
    744747/* -------------------------------------------------------------------- */ 
     
    883886/*      Try looking up in spatial_ref_sys table.                        */ 
    884887/* -------------------------------------------------------------------- */ 
    885     PGresult        *hResult
     888    PGresult        *hResult = NULL
    886889    char            szCommand[1024]; 
    887890    OGRSpatialReference *poSRS = NULL; 
     
    910913    } 
    911914 
    912     PQclear( hResult ); 
     915    OGRPGClearResult( hResult ); 
    913916    SoftCommit(); 
    914917 
     
    936939 
    937940{ 
    938     PGresult            *hResult
     941    PGresult            *hResult = NULL
    939942    char                szCommand[10000]; 
    940943    char                *pszWKT = NULL; 
    941     int                 nSRSId
     944    int                 nSRSId = -1
    942945 
    943946    if( poSRS == NULL ) 
     
    957960/* -------------------------------------------------------------------- */ 
    958961    hResult = PQexec(hPGConn, "BEGIN"); 
     962    OGRPGClearResult( hResult ); 
    959963 
    960964    sprintf( szCommand, 
     
    971975        && PQntuples(hResult) > 0 ) 
    972976    { 
     977        // TODO: mloskot - replace with strtol() function. 
     978        // atoi does not provide any level of diagnostics. 
     979        // What about adding CPLAtoi() ?  
     980 
    973981        nSRSId = atoi(PQgetvalue( hResult, 0, 0 )); 
    974982 
    975         PQclear( hResult ); 
     983        OGRPGClearResult( hResult ); 
    976984 
    977985        hResult = PQexec(hPGConn, "COMMIT"); 
    978         PQclear( hResult ); 
     986        OGRPGClearResult( hResult ); 
    979987 
    980988        return nSRSId; 
     
    991999 
    9921000    hResult = PQexec(hPGConn, "COMMIT"); 
    993     PQclear( hResult ); 
     1001    OGRPGClearResult( hResult ); 
    9941002 
    9951003    if( bTableMissing ) 
     
    10031011/* -------------------------------------------------------------------- */ 
    10041012    hResult = PQexec(hPGConn, "BEGIN"); 
    1005     PQclear( hResult ); 
     1013    OGRPGClearResult( hResult ); 
    10061014 
    10071015    hResult = PQexec(hPGConn, "SELECT MAX(srid) FROM spatial_ref_sys" ); 
     
    10101018    { 
    10111019        nSRSId = atoi(PQgetvalue(hResult,0,0)) + 1; 
    1012         PQclear( hResult ); 
     1020        OGRPGClearResult( hResult ); 
    10131021    } 
    10141022    else 
     1023    { 
    10151024        nSRSId = 1; 
     1025    } 
    10161026 
    10171027/* -------------------------------------------------------------------- */ 
     
    10431053 
    10441054    hResult = PQexec(hPGConn, szCommand ); 
    1045     PQclear( hResult ); 
     1055    OGRPGClearResult( hResult ); 
    10461056 
    10471057    hResult = PQexec(hPGConn, "COMMIT"); 
    1048     PQclear( hResult ); 
     1058    OGRPGClearResult( hResult ); 
    10491059 
    10501060    return nSRSId; 
     
    10661076    if( nSoftTransactionLevel == 1 ) 
    10671077    { 
    1068         PGresult            *hResult
    1069         PGconn          *hPGConn = GetPGConn(); 
     1078        PGresult    *hResult = NULL
     1079        PGconn      *hPGConn = GetPGConn(); 
    10701080 
    10711081        //CPLDebug( "OGR_PG", "BEGIN Transaction" ); 
     
    10741084        if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK ) 
    10751085        { 
     1086            OGRPGClearResult( hResult ); 
     1087 
    10761088            CPLDebug( "OGR_PG", "BEGIN Transaction failed:\n%s", 
    10771089                      PQerrorMessage( hPGConn ) ); 
     
    10791091        } 
    10801092 
    1081         PQclear( hResult ); 
     1093        OGRPGClearResult( hResult ); 
    10821094    } 
    10831095 
     
    11071119    if( nSoftTransactionLevel == 0 ) 
    11081120    { 
    1109         PGresult            *hResult
    1110         PGconn          *hPGConn = GetPGConn(); 
     1121        PGresult    *hResult = NULL
     1122        PGconn      *hPGConn = GetPGConn(); 
    11111123 
    11121124        //CPLDebug( "OGR_PG", "COMMIT Transaction" ); 
     
    11151127        if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK ) 
    11161128        { 
     1129            OGRPGClearResult( hResult ); 
     1130 
    11171131            CPLDebug( "OGR_PG", "COMMIT Transaction failed:\n%s", 
    11181132                      PQerrorMessage( hPGConn ) ); 
    11191133            return OGRERR_FAILURE; 
    11201134        } 
    1121  
    1122         PQclear( hResult ); 
     1135         
     1136        OGRPGClearResult( hResult ); 
    11231137    } 
    11241138 
     
    11441158    nSoftTransactionLevel = 0; 
    11451159 
    1146     PGresult            *hResult
    1147     PGconn              *hPGConn = GetPGConn(); 
     1160    PGresult    *hResult = NULL
     1161    PGconn      *hPGConn = GetPGConn(); 
    11481162 
    11491163    hResult = PQexec(hPGConn, "ROLLBACK"); 
    11501164 
    11511165    if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK ) 
     1166    { 
     1167        OGRPGClearResult( hResult ); 
     1168 
    11521169        return OGRERR_FAILURE; 
    1153  
    1154     PQclear( hResult ); 
     1170    } 
     1171 
     1172    OGRPGClearResult( hResult ); 
    11551173 
    11561174    return OGRERR_NONE; 
     
    12111229    { 
    12121230        const char *pszLayerName = pszSQLCommand + 9; 
    1213         int iLayer; 
    12141231 
    12151232        while( *pszLayerName == ' ' ) 
    12161233            pszLayerName++; 
    12171234         
    1218         for( iLayer = 0; iLayer < nLayers; iLayer++ ) 
     1235        for( int iLayer = 0; iLayer < nLayers; iLayer++ ) 
    12191236        { 
    12201237            if( EQUAL(papoLayers[iLayer]->GetLayerDefn()->GetName(),  
     
    12311248/*      Execute the statement.                                          */ 
    12321249/* -------------------------------------------------------------------- */ 
    1233     PGresult            *hResult = NULL; 
     1250    PGresult    *hResult = NULL; 
    12341251 
    12351252    FlushSoftTransaction(); 
     
    12681285    } 
    12691286 
    1270     if( hResult ) 
    1271         PQclear( hResult ); 
     1287    OGRPGClearResult( hResult ); 
    12721288 
    12731289    FlushSoftTransaction(); 
     
    12941310{ 
    12951311    char    *pszSafeName = CPLStrdup( pszSrcName ); 
    1296     int     i; 
    1297  
    1298     for( i = 0; pszSafeName[i] != '\0'; i++ ) 
     1312 
     1313    for( int i = 0; pszSafeName[i] != '\0'; i++ ) 
    12991314    { 
    13001315        pszSafeName[i] = (char) tolower( pszSafeName[i] ); 
  • branches/1.4/gdal/ogr/ogrsf_frmts/pg/ogrpglayer.cpp

    r10874 r11225  
    3030 ****************************************************************************/ 
    3131 
     32#include "ogr_pg.h" 
     33#include "ogrpgutility.h" 
    3234#include "cpl_conv.h" 
    33 #include "ogr_pg.h" 
    3435#include "cpl_string.h" 
    3536 
     
    124125    if( hCursorResult != NULL ) 
    125126    { 
    126         PQclear( hCursorResult ); 
     127        OGRPGClearResult( hCursorResult ); 
    127128 
    128129        if( bCursorActive ) 
     
    131132 
    132133            hCursorResult = PQexec(hPGConn, szCommand); 
    133             PQclear( hCursorResult ); 
     134            OGRPGClearResult( hCursorResult ); 
    134135        } 
    135136 
     
    525526 
    526527        hCursorResult = PQexec(hPGConn, osCommand ); 
    527         PQclear( hCursorResult ); 
     528        OGRPGClearResult( hCursorResult ); 
    528529 
    529530        osCommand.Printf( "FETCH %d in %s", CURSOR_PAGE, pszCursorName ); 
     
    541542        || PQresultStatus(hCursorResult) != PGRES_TUPLES_OK ) 
    542543    { 
     544        CPLDebug( "OGR_PG", "PQclear() on an error condition"); 
     545 
     546        OGRPGClearResult( hCursorResult ); 
     547 
    543548        iNextShapeId = MAX(1,iNextShapeId); 
    544549        return NULL; 
     
    551556        && bCursorActive ) 
    552557    { 
    553         PQclear( hCursorResult ); 
     558        OGRPGClearResult( hCursorResult ); 
    554559         
    555560        osCommand.Printf( "FETCH %d in %s", CURSOR_PAGE, pszCursorName ); 
     
    565570    if( nResultOffset >= PQntuples(hCursorResult) ) 
    566571    { 
    567         PQclear( hCursorResult ); 
     572        OGRPGClearResult( hCursorResult ); 
    568573 
    569574        if( bCursorActive ) 
     
    572577 
    573578            hCursorResult = PQexec(hPGConn, osCommand); 
    574             PQclear( hCursorResult ); 
     579            OGRPGClearResult( hCursorResult ); 
    575580        } 
    576581 
     
    604609 
    605610{ 
    606     /* This should be implemented! */ 
     611    /* 
     612     * TODO: This should be implemented! 
     613     * See related Bug 1445 
     614     * http://bugzilla.remotesensing.org/show_bug.cgi?id=1445 
     615     */ 
    607616 
    608617    return NULL; 
  • branches/1.4/gdal/ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp

    r10874 r11225  
    2828 ****************************************************************************/ 
    2929 
     30#include "ogr_pg.h" 
     31#include "ogrpgutility.h" 
    3032#include "cpl_conv.h" 
    3133#include "cpl_string.h" 
    3234#include "cpl_error.h" 
    33 #include "ogr_pg.h" 
    3435 
    3536CPL_CVSID("$Id$"); 
     
    131132        osCurrentSchema = PQgetvalue(hResult,0,0); 
    132133 
    133         PQclear( hResult ); 
     134        OGRPGClearResult( hResult ); 
    134135    } 
    135136 
     
    166167    if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) 
    167168    { 
    168         PQclear( hResult ); 
     169        OGRPGClearResult( hResult ); 
    169170 
    170171        CPLString osSchemaClause; 
     
    189190    if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) 
    190191    { 
    191         PQclear( hResult ); 
     192        OGRPGClearResult( hResult ); 
    192193        hResult = PQexec(hPGConn, "FETCH ALL in mycursor" ); 
    193194    } 
     
    195196    if( !hResult || PQresultStatus(hResult) != PGRES_TUPLES_OK ) 
    196197    { 
     198        OGRPGClearResult( hResult ); 
     199 
    197200        CPLError( CE_Failure, CPLE_AppDefined, 
    198201                  "%s", PQerrorMessage(hPGConn) ); 
     
    202205    if( PQntuples(hResult) == 0 ) 
    203206    { 
    204         PQclear( hResult ); 
     207        OGRPGClearResult( hResult ); 
     208 
    205209        hResult = PQexec(hPGConn, "CLOSE mycursor"); 
    206         PQclear( hResult ); 
     210        OGRPGClearResult( hResult ); 
    207211 
    208212        hResult = PQexec(hPGConn, "COMMIT"); 
    209         PQclear( hResult ); 
     213        OGRPGClearResult( hResult ); 
    210214 
    211215        CPLError( CE_Failure, CPLE_AppDefined, 
     
    238242    for( iRecord = 0; iRecord < PQntuples(hResult); iRecord++ ) 
    239243    { 
    240         const char      *pszType, *pszFormatType; 
     244        const char      *pszType = NULL; 
     245        const char      *pszFormatType = NULL; 
    241246        OGRFieldDefn    oField( PQgetvalue( hResult, iRecord, 0 ), OFTString); 
    242247 
     
    353358    } 
    354359 
    355     PQclear( hResult ); 
     360    OGRPGClearResult( hResult ); 
    356361 
    357362    hResult = PQexec(hPGConn, "CLOSE mycursor"); 
    358     PQclear( hResult ); 
     363    OGRPGClearResult( hResult ); 
    359364 
    360365    hResult = PQexec(hPGConn, "COMMIT"); 
    361     PQclear( hResult ); 
     366    OGRPGClearResult( hResult ); 
    362367 
    363368    // get layer geometry type (for PostGIS dataset) 
     
    369374 
    370375        hResult = PQexec(hPGConn,osCommand); 
     376 
    371377        if ( hResult && PQntuples(hResult) == 1 && !PQgetisnull(hResult,0,0) ) 
    372378        { 
     
    400406 
    401407            poDefn->SetGeomType( nGeomType ); 
    402  
    403             PQclear( hResult ); 
    404         } 
     408        } 
     409 
     410        OGRPGClearResult( hResult ); 
    405411    } 
    406412 
     
    519525 
    520526{ 
    521     int         i, nSize; 
    522     char        *pszFieldList; 
     527    int     i = 0; 
     528    int     nSize = 0; 
     529    char    *pszFieldList = NULL; 
    523530 
    524531    nSize = 25; 
     
    609616 
    610617{ 
    611     PGconn              *hPGConn = poDS->GetPGConn(); 
    612     PGresult            *hResult
    613     CPLString            osCommand; 
     618    PGconn      *hPGConn = poDS->GetPGConn(); 
     619    PGresult    *hResult = NULL
     620    CPLString   osCommand; 
    614621 
    615622/* -------------------------------------------------------------------- */ 
     
    634641 
    635642/* -------------------------------------------------------------------- */ 
    636 /*      Execute the insert.                                             */ 
     643/*      Execute the delete.                                             */ 
    637644/* -------------------------------------------------------------------- */ 
    638645    OGRErr eErr; 
     
    652659                  PQerrorMessage(hPGConn) ); 
    653660 
    654         PQclear( hResult ); 
     661        OGRPGClearResult( hResult ); 
    655662 
    656663        poDS->SoftRollback(); 
    657  
    658         return OGRERR_FAILURE; 
    659     } 
    660  
    661     return poDS->SoftCommit(); 
     664        eErr = OGRERR_FAILURE; 
     665    } 
     666    else 
     667    { 
     668        OGRPGClearResult( hResult ); 
     669 
     670        eErr = poDS->SoftCommit(); 
     671    } 
     672 
     673    return eErr; 
    662674} 
    663675 
     
    986998                  PQerrorMessage(hPGConn), osCommand.c_str() ); 
    987999 
    988         PQclear( hResult ); 
     1000        OGRPGClearResult( hResult ); 
    9891001 
    9901002        poDS->SoftRollback(); 
     
    9991011    printf( "nNewOID = %d\n", (int) nNewOID ); 
    10001012#endif 
    1001     PQclear( hResult ); 
     1013 
     1014    OGRPGClearResult( hResult ); 
    10021015 
    10031016    return poDS->SoftCommit(); 
     
    12441257{ 
    12451258    PGconn              *hPGConn = poDS->GetPGConn(); 
    1246     PGresult            *hResult
     1259    PGresult            *hResult = NULL
    12471260    CPLString           osCommand; 
    12481261    char                szFieldType[256]; 
     
    13371350    poDS->FlushSoftTransaction(); 
    13381351    hResult = PQexec(hPGConn, "BEGIN"); 
    1339     PQclear( hResult ); 
     1352    OGRPGClearResult( hResult ); 
    13401353 
    13411354    osCommand.Printf( "ALTER TABLE %s ADD COLUMN \"%s\" %s", 
     
    13491362                  PQerrorMessage(hPGConn) ); 
    13501363 
    1351         PQclear( hResult ); 
     1364        OGRPGClearResult( hResult ); 
     1365 
    13521366        hResult = PQexec( hPGConn, "ROLLBACK" ); 
    1353         PQclear( hResult ); 
     1367        OGRPGClearResult( hResult ); 
    13541368 
    13551369        return OGRERR_FAILURE; 
    13561370    } 
    13571371 
    1358     PQclear( hResult ); 
     1372    OGRPGClearResult( hResult ); 
    13591373 
    13601374    hResult = PQexec(hPGConn, "COMMIT"); 
    1361     PQclear( hResult ); 
     1375    OGRPGClearResult( hResult ); 
    13621376 
    13631377    poFeatureDefn->AddFieldDefn( &oField ); 
     
    13851399/* -------------------------------------------------------------------- */ 
    13861400    OGRFeature  *poFeature = NULL; 
    1387     PGresult    *hResult
     1401    PGresult    *hResult = NULL
    13881402    PGconn      *hPGConn = poDS->GetPGConn(); 
    13891403    char        *pszFieldList = BuildFields(); 
     
    14051419    if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) 
    14061420    { 
    1407         PQclear( hResult ); 
     1421        OGRPGClearResult( hResult ); 
     1422 
    14081423        hResult = PQexec(hPGConn, "FETCH ALL in getfeaturecursor" ); 
    14091424 
     
    14191434/*      Cleanup                                                         */ 
    14201435/* -------------------------------------------------------------------- */ 
    1421     PQclear( hResult ); 
     1436    OGRPGClearResult( hResult ); 
    14221437 
    14231438    hResult = PQexec(hPGConn, "CLOSE getfeaturecursor"); 
    1424     PQclear( hResult ); 
     1439    OGRPGClearResult( hResult ); 
    14251440 
    14261441    poDS->FlushSoftTransaction(); 
    1427  
    14281442 
    14291443    return poFeature; 
     
    14561470/* -------------------------------------------------------------------- */ 
    14571471    PGconn              *hPGConn = poDS->GetPGConn(); 
    1458     PGresult            *hResult
     1472    PGresult            *hResult = NULL
    14591473    CPLString           osCommand; 
    14601474    int                 nCount = 0; 
     
    14621476    poDS->FlushSoftTransaction(); 
    14631477    hResult = PQexec(hPGConn, "BEGIN"); 
    1464     PQclear( hResult ); 
     1478    OGRPGClearResult( hResult ); 
    14651479 
    14661480    osCommand.Printf( 
     
    14741488 
    14751489    hResult = PQexec(hPGConn, osCommand); 
    1476     PQclear( hResult ); 
     1490    OGRPGClearResult( hResult ); 
    14771491 
    14781492    hResult = PQexec(hPGConn, "FETCH ALL in countCursor"); 
     
    14811495    else 
    14821496        CPLDebug( "OGR_PG", "%s; failed.", osCommand.c_str() ); 
    1483     PQclear( hResult ); 
     1497    OGRPGClearResult( hResult ); 
    14841498 
    14851499    hResult = PQexec(hPGConn, "CLOSE countCursor"); 
    1486     PQclear( hResult ); 
     1500    OGRPGClearResult( hResult ); 
    14871501 
    14881502    hResult = PQexec(hPGConn, "COMMIT"); 
    1489     PQclear( hResult ); 
     1503    OGRPGClearResult( hResult ); 
    14901504 
    14911505    return nCount; 
     
    15051519    if( nSRSId == -2 ) 
    15061520    { 
    1507         PGconn          *hPGConn = poDS->GetPGConn(); 
    1508         PGresult        *hResult
    1509         char            szCommand[1024]; 
     1521        PGconn      *hPGConn = poDS->GetPGConn(); 
     1522        PGresult    *hResult = NULL
     1523        char        szCommand[1024]; 
    15101524 
    15111525        nSRSId = -1; 
     
    15271541        else // I think perhaps an older version used f_schema_name. 
    15281542        { 
    1529             PQclear( hResult ); 
     1543            OGRPGClearResult( hResult ); 
     1544 
    15301545            poDS->SoftCommit(); 
    15311546            poDS->SoftStartTransaction(); 
     1547 
    15321548            sprintf( szCommand, 
    15331549                     "SELECT srid FROM geometry_columns " 
     
    15431559        } 
    15441560 
    1545         PQclear( hResult ); 
     1561        OGRPGClearResult( hResult ); 
    15461562 
    15471563        poDS->SoftCommit(); 
     
    15651581    if ( bHasPostGISGeometry ) 
    15661582    { 
    1567         PGconn          *hPGConn = poDS->GetPGConn(); 
    1568         PGresult        *hResult
    1569         CPLString       osCommand; 
     1583        PGconn      *hPGConn = poDS->GetPGConn(); 
     1584        PGresult    *hResult = NULL
     1585        CPLString   osCommand; 
    15701586 
    15711587        osCommand.Printf( "SELECT Extent(\"%s\") FROM %s",  
     
    15751591        if( ! hResult || PQresultStatus(hResult) != PGRES_TUPLES_OK || PQgetisnull(hResult,0,0) ) 
    15761592        { 
    1577             PQclear( hResult ); 
     1593            OGRPGClearResult( hResult ); 
    15781594            CPLDebug("OGR_PG","Unable to get extent by PostGIS. Using standard OGRLayer method."); 
    15791595            return OGRPGLayer::GetExtent( psExtent, bForce ); 
     
    15971613                      "Bad extent representation: '%s'", pszBox); 
    15981614            CSLDestroy(papszTokens); 
    1599             PQclear(hResult); 
     1615 
     1616            OGRPGClearResult( hResult ); 
    16001617            return OGRERR_FAILURE; 
    16011618        } 
     
    16131630 
    16141631        CSLDestroy(papszTokens); 
    1615  
    1616         PQclear( hResult ); 
     1632        OGRPGClearResult( hResult ); 
     1633 
    16171634        return OGRERR_NONE; 
    16181635    } 
     
    16571674        bCopyActive = TRUE; 
    16581675 
    1659     PQclear( hResult ); 
     1676    OGRPGClearResult( hResult ); 
    16601677    CPLFree( pszCommand ); 
    16611678 
     
    16811698 
    16821699    bCopyActive = FALSE; 
    1683  
    16841700 
    16851701    /* This is for postgresql 7.4 and higher */ 
     
    17221738    } 
    17231739 
    1724     PQclear(hResult); 
     1740    OGRPGClearResult( hResult ); 
    17251741 
    17261742    bUseCopy = USE_COPY_UNSET; 
     
    17351751char *OGRPGTableLayer::BuildCopyFields() 
    17361752{ 
    1737     int         i, nSize; 
    1738     char        *pszFieldList; 
     1753    int     i = 0; 
     1754    int     nSize = 0; 
     1755    char    *pszFieldList; 
    17391756         
    17401757    nSize = 25;