[QGIS Commit] r14935 - in trunk/qgis: python/core src/app src/core src/providers/ogr src/providers/postgres

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri Dec 17 10:00:18 EST 2010


Author: jef
Date: 2010-12-17 07:00:17 -0800 (Fri, 17 Dec 2010)
New Revision: 14935

Modified:
   trunk/qgis/python/core/qgsvectordataprovider.sip
   trunk/qgis/src/app/qgsquerybuilder.cpp
   trunk/qgis/src/core/qgsvectordataprovider.cpp
   trunk/qgis/src/core/qgsvectordataprovider.h
   trunk/qgis/src/providers/ogr/qgsogrprovider.cpp
   trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
Log:
allow vector data provider to record errors (currently only used in query
builder to report errors in where clauses from OGR and postgres provider).


Modified: trunk/qgis/python/core/qgsvectordataprovider.sip
===================================================================
--- trunk/qgis/python/core/qgsvectordataprovider.sip	2010-12-16 23:26:56 UTC (rev 14934)
+++ trunk/qgis/python/core/qgsvectordataprovider.sip	2010-12-17 15:00:17 UTC (rev 14935)
@@ -299,4 +299,19 @@
 
       /** Returns a list of available encodings */
       static const QStringList &availableEncodings();
+
+      /* provider has errors to report
+       * @note added in 1.7
+       */
+      bool hasErrors();
+
+      /* clear recorded errors 
+       * @note added in 1.7
+       */
+      void clearErrors();
+
+      /* get recorded errors
+       * @note added in 1.7
+       */
+      QStringList errors();
 };

Modified: trunk/qgis/src/app/qgsquerybuilder.cpp
===================================================================
--- trunk/qgis/src/app/qgsquerybuilder.cpp	2010-12-16 23:26:56 UTC (rev 14934)
+++ trunk/qgis/src/app/qgsquerybuilder.cpp	2010-12-17 15:00:17 UTC (rev 14935)
@@ -151,11 +151,19 @@
                               tr( "Query Result" ),
                               tr( "The where clause returned %n row(s).", "returned test rows", mLayer->featureCount() ) );
   }
+  else if ( mLayer->dataProvider()->hasErrors() )
+  {
+    QMessageBox::warning( this,
+                          tr( "Query Failed" ),
+                          tr( "An error occurred when executing the query." )
+                          + tr( "\nThe data provider said:\n%1" ).arg( mLayer->dataProvider()->errors().join( "\n" ) ) );
+    mLayer->dataProvider()->clearErrors();
+  }
   else
   {
     QMessageBox::warning( this,
                           tr( "Query Failed" ),
-                          tr( "An error occurred when executing the query" ) );
+                          tr( "An error occurred when executing the query." ) );
   }
 }
 
@@ -167,7 +175,18 @@
     if ( !mLayer->setSubsetString( txtSQL->toPlainText() ) )
     {
       //error in query - show the problem
-      QMessageBox::warning( this, tr( "Error in Query" ), tr( "The subset string could not be set" ) );
+      if ( mLayer->dataProvider()->hasErrors() )
+      {
+        QMessageBox::warning( this,
+                              tr( "Query Failed" ),
+                              tr( "An error occurred when executing the query." )
+                              + tr( "\nThe data provider said:\n%1" ).arg( mLayer->dataProvider()->errors().join( "\n" ) ) );
+        mLayer->dataProvider()->clearErrors();
+      }
+      else
+      {
+        QMessageBox::warning( this, tr( "Error in Query" ), tr( "The subset string could not be set" ) );
+      }
       return;
     }
   }

Modified: trunk/qgis/src/core/qgsvectordataprovider.cpp
===================================================================
--- trunk/qgis/src/core/qgsvectordataprovider.cpp	2010-12-16 23:26:56 UTC (rev 14934)
+++ trunk/qgis/src/core/qgsvectordataprovider.cpp	2010-12-17 15:00:17 UTC (rev 14935)
@@ -491,4 +491,24 @@
   return smEncodings;
 }
 
+void QgsVectorDataProvider::clearErrors()
+{
+  mErrors.clear();
+}
+
+bool QgsVectorDataProvider::hasErrors()
+{
+  return !mErrors.isEmpty();
+}
+
+QStringList QgsVectorDataProvider::errors()
+{
+  return mErrors;
+}
+
+void QgsVectorDataProvider::pushError( QString msg )
+{
+  mErrors << msg;
+}
+
 QStringList QgsVectorDataProvider::smEncodings;

Modified: trunk/qgis/src/core/qgsvectordataprovider.h
===================================================================
--- trunk/qgis/src/core/qgsvectordataprovider.h	2010-12-16 23:26:56 UTC (rev 14934)
+++ trunk/qgis/src/core/qgsvectordataprovider.h	2010-12-17 15:00:17 UTC (rev 14935)
@@ -353,6 +353,22 @@
 
     static const QStringList &availableEncodings();
 
+    /* provider has errors to report
+     * @note added in 1.7
+     */
+    bool hasErrors();
+
+    /* clear recorded errors
+     * @note added in 1.7
+     */
+    void clearErrors();
+
+    /* get recorded errors
+     * @note added in 1.7
+     */
+    QStringList errors();
+
+
   protected:
     QVariant convertValue( QVariant::Type type, QString value );
 
@@ -377,11 +393,18 @@
     /**The names of the providers native types*/
     QList< NativeType > mNativeTypes;
 
+    void pushError( QString msg );
+
   private:
     /** old notation **/
     QMap<QString, QVariant::Type> mOldTypeList;
 
+    // list of errors
+    QStringList mErrors;
+
+
     static QStringList smEncodings;
+
 };
 
 #endif

Modified: trunk/qgis/src/providers/ogr/qgsogrprovider.cpp
===================================================================
--- trunk/qgis/src/providers/ogr/qgsogrprovider.cpp	2010-12-16 23:26:56 UTC (rev 14934)
+++ trunk/qgis/src/providers/ogr/qgsogrprovider.cpp	2010-12-17 15:00:17 UTC (rev 14935)
@@ -240,6 +240,7 @@
 
     if ( !ogrLayer )
     {
+      pushError( QString( "OGR[%1] error %2: %3" ).arg( CPLGetLastErrorType() ).arg( CPLGetLastErrorNo() ).arg( CPLGetLastErrorMsg() ) );
       ogrLayer = prevLayer;
       mSubsetString = prevSubsetString;
       return false;

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2010-12-16 23:26:56 UTC (rev 14934)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2010-12-17 15:00:17 UTC (rev 14935)
@@ -1620,9 +1620,6 @@
 bool QgsPostgresProvider::uniqueData( QString query, QString colName )
 {
   // Check to see if the given column contains unique data
-
-  bool isUnique = false;
-
   QString sql = QString( "select count(distinct %1)=count(%1) from %2" )
                 .arg( quotedIdentifier( colName ) )
                 .arg( mQuery );
@@ -1634,10 +1631,14 @@
 
   Result unique = connectionRO->PQexec( sql );
 
-  if ( PQntuples( unique ) == 1 && QString::fromUtf8( PQgetvalue( unique, 0, 0 ) ).startsWith( "t" ) )
-    isUnique = true;
+  if ( PQresultStatus( unique ) != PGRES_TUPLES_OK )
+  {
+    pushError( QString::fromUtf8( PQresultErrorMessage( unique ) ) );
+    return false;
+  }
 
-  return isUnique;
+  return PQntuples( unique ) == 1
+         && QString::fromUtf8( PQgetvalue( unique, 0, 0 ) ).startsWith( "t" );
 }
 
 int QgsPostgresProvider::SRCFromViewColumn( const QString& ns, const QString& relname, const QString& attname_table, const QString& attname_view, const QString& viewDefinition, SRC& result ) const
@@ -2779,6 +2780,23 @@
 
   sqlWhereClause = theSQL.trimmed();
 
+  QString sql = QString( "select * from %1" ).arg( mQuery );
+
+  if ( !sqlWhereClause.isEmpty() )
+  {
+    sql += QString( " where %1" ).arg( sqlWhereClause );
+  }
+
+  sql += " limit 0";
+
+  Result res = connectionRO->PQexec( sql );
+  if ( PQresultStatus( res ) != PGRES_COMMAND_OK && PQresultStatus( res ) != PGRES_TUPLES_OK )
+  {
+    pushError( QString::fromUtf8( PQresultErrorMessage( res ) ) );
+    sqlWhereClause = prevWhere;
+    return false;
+  }
+
   if ( !mIsDbPrimaryKey && !uniqueData( mQuery, primaryKey ) )
   {
     sqlWhereClause = prevWhere;



More information about the QGIS-commit mailing list