Skip to content
Permalink
Browse files
allow vector data provider to record errors (currently only used in q…
…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 b78d8556a439d15355fa1769585e692770a83bea
@@ -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();
};
@@ -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." ) );
}
}

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

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

0 comments on commit b78d855

Please sign in to comment.