[QGIS Commit] r13374 - trunk/qgis/src/providers/postgres

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Apr 24 16:06:45 EDT 2010


Author: jef
Date: 2010-04-24 16:06:45 -0400 (Sat, 24 Apr 2010)
New Revision: 13374

Modified:
   trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
Log:
postgres provider: improve error handling

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2010-04-24 19:55:32 UTC (rev 13373)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2010-04-24 20:06:45 UTC (rev 13374)
@@ -2737,7 +2737,7 @@
 {
   QString prevWhere = sqlWhereClause;
 
-  sqlWhereClause = theSQL;
+  sqlWhereClause = theSQL.trimmed();
 
   if ( !mIsDbPrimaryKey && !uniqueData( mQuery, primaryKey ) )
   {
@@ -3207,7 +3207,8 @@
 
 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 )
   {
@@ -3215,46 +3216,42 @@
     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 )
-    {
-#ifdef QGISDEBUG
-      QString err = QString( "Query: %1 returned %2 [%3]" )
-                    .arg( query )
-                    .arg( errorStatus )
-                    .arg( QString::fromUtf8( PQresultErrorMessage( res ) ) );
-      QgsDebugMsgLevel( err, 3 );
-#endif
-      if ( openCursors )
-      {
-        PQexecNR( "ROLLBACK" );
+    QgsDebugMsgLevel( QString( "Query: %1 returned no result buffer" ).arg( query ), 3 );
+    return false;
+  }
 
-        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;
+  if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
+    return true;
 
-        PQexecNR( "BEGIN READ ONLY" );
-      }
-    }
-    return errorStatus == PGRES_COMMAND_OK;
-  }
-  else
+#ifdef QGISDEBUG
+  QString err = QString( "Query: %1 returned %2 [%3]" )
+    .arg( query )
+    .arg( errorStatus )
+    .arg( QString::fromUtf8( PQresultErrorMessage( res ) ) );
+  QgsDebugMsg( err );
+#endif
+  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;
 }
 



More information about the QGIS-commit mailing list