Skip to content

Commit

Permalink
allow vector data provider to record errors (currently only used in q…
Browse files Browse the repository at this point in the history
…uery

builder to report errors in where clauses from OGR and postgres provider).


git-svn-id: http://svn.osgeo.org/qgis/trunk@14935 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Dec 17, 2010
1 parent ff96fbd commit b78d855
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 8 deletions.
15 changes: 15 additions & 0 deletions python/core/qgsvectordataprovider.sip
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,19 @@ class QgsVectorDataProvider : QgsDataProvider

/** 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();
};
23 changes: 21 additions & 2 deletions src/app/qgsquerybuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,19 @@ void QgsQueryBuilder::test()
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." ) );
}
}

Expand All @@ -167,7 +175,18 @@ void QgsQueryBuilder::accept()
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;
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgsvectordataprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,24 @@ const QStringList &QgsVectorDataProvider::availableEncodings()
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;
23 changes: 23 additions & 0 deletions src/core/qgsvectordataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider

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 );

Expand All @@ -377,11 +393,18 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
/**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
1 change: 1 addition & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ bool QgsOgrProvider::setSubsetString( QString theSQL )

if ( !ogrLayer )
{
pushError( QString( "OGR[%1] error %2: %3" ).arg( CPLGetLastErrorType() ).arg( CPLGetLastErrorNo() ).arg( CPLGetLastErrorMsg() ) );
ogrLayer = prevLayer;
mSubsetString = prevSubsetString;
return false;
Expand Down
30 changes: 24 additions & 6 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1620,9 +1620,6 @@ QString QgsPostgresProvider::chooseViewColumn( const tableCols &cols )
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 );
Expand All @@ -1634,10 +1631,14 @@ bool QgsPostgresProvider::uniqueData( QString query, QString colName )

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
Expand Down Expand Up @@ -2779,6 +2780,23 @@ bool QgsPostgresProvider::setSubsetString( QString theSQL )

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;
Expand Down

0 comments on commit b78d855

Please sign in to comment.