Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
postgres provider: improve error handling
git-svn-id: http://svn.osgeo.org/qgis/trunk@13374 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Apr 24, 2010
1 parent d2b546a commit 7d4f628
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2737,7 +2737,7 @@ bool QgsPostgresProvider::setSubsetString( QString theSQL )
{
QString prevWhere = sqlWhereClause;

sqlWhereClause = theSQL;
sqlWhereClause = theSQL.trimmed();

if ( !mIsDbPrimaryKey && !uniqueData( mQuery, primaryKey ) )
{
Expand Down Expand Up @@ -3207,54 +3207,51 @@ bool QgsPostgresProvider::Conn::openCursor( QString cursorName, QString sql )

bool QgsPostgresProvider::Conn::closeCursor( QString cursorName )
{
bool res = PQexecNR( QString( "CLOSE %1" ).arg( cursorName ) );
if ( !PQexecNR( QString( "CLOSE %1" ).arg( cursorName ) ) )
return false;

if ( --openCursors == 0 )
{
QgsDebugMsg( "Committing read-only transaction" );
PQexecNR( "COMMIT" );
}

return res;
return true;
}

bool QgsPostgresProvider::Conn::PQexecNR( QString query )
{
Result res = ::PQexec( conn, query.toUtf8() );
if ( res )
if ( !res )
{
int errorStatus = PQresultStatus( res );
if ( errorStatus != PGRES_COMMAND_OK )
{
QgsDebugMsgLevel( QString( "Query: %1 returned no result buffer" ).arg( query ), 3 );
return false;
}

if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
return true;

#ifdef QGISDEBUG
QString err = QString( "Query: %1 returned %2 [%3]" )
.arg( query )
.arg( errorStatus )
.arg( QString::fromUtf8( PQresultErrorMessage( res ) ) );
QgsDebugMsgLevel( err, 3 );
QString err = QString( "Query: %1 returned %2 [%3]" )
.arg( query )
.arg( errorStatus )
.arg( QString::fromUtf8( PQresultErrorMessage( res ) ) );
QgsDebugMsg( err );
#endif
if ( openCursors )
{
PQexecNR( "ROLLBACK" );

QgsPostgresProvider::showMessageBox(
tr( "Query failed" ),
tr( "%1 cursor states lost.\nSQL: %2\nResult: %3 (%4)" )
.arg( openCursors )
.arg( query )
.arg( errorStatus )
.arg( QString::fromUtf8( PQresultErrorMessage( res ) ) ) );
openCursors = 0;

PQexecNR( "BEGIN READ ONLY" );
}
}
return errorStatus == PGRES_COMMAND_OK;
}
else
if ( openCursors )
{
QgsDebugMsgLevel( QString( "Query: %1 returned no result buffer" ).arg( query ), 3 );
QgsPostgresProvider::showMessageBox(
tr( "Query failed" ),
tr( "%1 cursor states lost.\nSQL: %2\nResult: %3 (%4)" )
.arg( openCursors )
.arg( query )
.arg( errorStatus )
.arg( QString::fromUtf8( PQresultErrorMessage( res ) ) ) );
openCursors = 0;
}

PQexecNR( "ROLLBACK" );

return false;
}

Expand Down

0 comments on commit 7d4f628

Please sign in to comment.