Skip to content

Commit 9371e62

Browse files
author
jef
committedDec 17, 2010
allow vector data provider to record errors (currently only used in query
builder to report errors in where clauses from OGR and postgres provider). git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14935 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent d5a55a9 commit 9371e62

File tree

6 files changed

+104
-8
lines changed

6 files changed

+104
-8
lines changed
 

‎python/core/qgsvectordataprovider.sip

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,19 @@ class QgsVectorDataProvider : QgsDataProvider
299299

300300
/** Returns a list of available encodings */
301301
static const QStringList &availableEncodings();
302+
303+
/* provider has errors to report
304+
* @note added in 1.7
305+
*/
306+
bool hasErrors();
307+
308+
/* clear recorded errors
309+
* @note added in 1.7
310+
*/
311+
void clearErrors();
312+
313+
/* get recorded errors
314+
* @note added in 1.7
315+
*/
316+
QStringList errors();
302317
};

‎src/app/qgsquerybuilder.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,19 @@ void QgsQueryBuilder::test()
151151
tr( "Query Result" ),
152152
tr( "The where clause returned %n row(s).", "returned test rows", mLayer->featureCount() ) );
153153
}
154+
else if ( mLayer->dataProvider()->hasErrors() )
155+
{
156+
QMessageBox::warning( this,
157+
tr( "Query Failed" ),
158+
tr( "An error occurred when executing the query." )
159+
+ tr( "\nThe data provider said:\n%1" ).arg( mLayer->dataProvider()->errors().join( "\n" ) ) );
160+
mLayer->dataProvider()->clearErrors();
161+
}
154162
else
155163
{
156164
QMessageBox::warning( this,
157165
tr( "Query Failed" ),
158-
tr( "An error occurred when executing the query" ) );
166+
tr( "An error occurred when executing the query." ) );
159167
}
160168
}
161169

@@ -167,7 +175,18 @@ void QgsQueryBuilder::accept()
167175
if ( !mLayer->setSubsetString( txtSQL->toPlainText() ) )
168176
{
169177
//error in query - show the problem
170-
QMessageBox::warning( this, tr( "Error in Query" ), tr( "The subset string could not be set" ) );
178+
if ( mLayer->dataProvider()->hasErrors() )
179+
{
180+
QMessageBox::warning( this,
181+
tr( "Query Failed" ),
182+
tr( "An error occurred when executing the query." )
183+
+ tr( "\nThe data provider said:\n%1" ).arg( mLayer->dataProvider()->errors().join( "\n" ) ) );
184+
mLayer->dataProvider()->clearErrors();
185+
}
186+
else
187+
{
188+
QMessageBox::warning( this, tr( "Error in Query" ), tr( "The subset string could not be set" ) );
189+
}
171190
return;
172191
}
173192
}

‎src/core/qgsvectordataprovider.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,24 @@ const QStringList &QgsVectorDataProvider::availableEncodings()
491491
return smEncodings;
492492
}
493493

494+
void QgsVectorDataProvider::clearErrors()
495+
{
496+
mErrors.clear();
497+
}
498+
499+
bool QgsVectorDataProvider::hasErrors()
500+
{
501+
return !mErrors.isEmpty();
502+
}
503+
504+
QStringList QgsVectorDataProvider::errors()
505+
{
506+
return mErrors;
507+
}
508+
509+
void QgsVectorDataProvider::pushError( QString msg )
510+
{
511+
mErrors << msg;
512+
}
513+
494514
QStringList QgsVectorDataProvider::smEncodings;

‎src/core/qgsvectordataprovider.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,22 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
353353

354354
static const QStringList &availableEncodings();
355355

356+
/* provider has errors to report
357+
* @note added in 1.7
358+
*/
359+
bool hasErrors();
360+
361+
/* clear recorded errors
362+
* @note added in 1.7
363+
*/
364+
void clearErrors();
365+
366+
/* get recorded errors
367+
* @note added in 1.7
368+
*/
369+
QStringList errors();
370+
371+
356372
protected:
357373
QVariant convertValue( QVariant::Type type, QString value );
358374

@@ -377,11 +393,18 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
377393
/**The names of the providers native types*/
378394
QList< NativeType > mNativeTypes;
379395

396+
void pushError( QString msg );
397+
380398
private:
381399
/** old notation **/
382400
QMap<QString, QVariant::Type> mOldTypeList;
383401

402+
// list of errors
403+
QStringList mErrors;
404+
405+
384406
static QStringList smEncodings;
407+
385408
};
386409

387410
#endif

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ bool QgsOgrProvider::setSubsetString( QString theSQL )
240240

241241
if ( !ogrLayer )
242242
{
243+
pushError( QString( "OGR[%1] error %2: %3" ).arg( CPLGetLastErrorType() ).arg( CPLGetLastErrorNo() ).arg( CPLGetLastErrorMsg() ) );
243244
ogrLayer = prevLayer;
244245
mSubsetString = prevSubsetString;
245246
return false;

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,9 +1620,6 @@ QString QgsPostgresProvider::chooseViewColumn( const tableCols &cols )
16201620
bool QgsPostgresProvider::uniqueData( QString query, QString colName )
16211621
{
16221622
// Check to see if the given column contains unique data
1623-
1624-
bool isUnique = false;
1625-
16261623
QString sql = QString( "select count(distinct %1)=count(%1) from %2" )
16271624
.arg( quotedIdentifier( colName ) )
16281625
.arg( mQuery );
@@ -1634,10 +1631,14 @@ bool QgsPostgresProvider::uniqueData( QString query, QString colName )
16341631

16351632
Result unique = connectionRO->PQexec( sql );
16361633

1637-
if ( PQntuples( unique ) == 1 && QString::fromUtf8( PQgetvalue( unique, 0, 0 ) ).startsWith( "t" ) )
1638-
isUnique = true;
1634+
if ( PQresultStatus( unique ) != PGRES_TUPLES_OK )
1635+
{
1636+
pushError( QString::fromUtf8( PQresultErrorMessage( unique ) ) );
1637+
return false;
1638+
}
16391639

1640-
return isUnique;
1640+
return PQntuples( unique ) == 1
1641+
&& QString::fromUtf8( PQgetvalue( unique, 0, 0 ) ).startsWith( "t" );
16411642
}
16421643

16431644
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 @@ bool QgsPostgresProvider::setSubsetString( QString theSQL )
27792780

27802781
sqlWhereClause = theSQL.trimmed();
27812782

2783+
QString sql = QString( "select * from %1" ).arg( mQuery );
2784+
2785+
if ( !sqlWhereClause.isEmpty() )
2786+
{
2787+
sql += QString( " where %1" ).arg( sqlWhereClause );
2788+
}
2789+
2790+
sql += " limit 0";
2791+
2792+
Result res = connectionRO->PQexec( sql );
2793+
if ( PQresultStatus( res ) != PGRES_COMMAND_OK && PQresultStatus( res ) != PGRES_TUPLES_OK )
2794+
{
2795+
pushError( QString::fromUtf8( PQresultErrorMessage( res ) ) );
2796+
sqlWhereClause = prevWhere;
2797+
return false;
2798+
}
2799+
27822800
if ( !mIsDbPrimaryKey && !uniqueData( mQuery, primaryKey ) )
27832801
{
27842802
sqlWhereClause = prevWhere;

0 commit comments

Comments
 (0)