Skip to content

Commit

Permalink
improve deleteStyleFromDatabase signature
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrocha committed Jan 31, 2017
1 parent 6d6f288 commit 8e989c1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
/**
* Will delete the named style corresponding to style id provided from the database
*/
virtual void deleteStyleFromDatabase( const QString& styleId, QString &msgError /Out/ );
virtual bool deleteStyleFromDatabase( const QString& styleId, QString &msgError /Out/ );

/**
* Load a named style from file/local db/datasource db
Expand Down
8 changes: 4 additions & 4 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ typedef QString getStyleById_t(
QString& errCause
);

typedef void deleteStyleById_t(
typedef bool deleteStyleById_t(
const QString& uri,
QString styleID,
QString& errCause
Expand Down Expand Up @@ -4341,22 +4341,22 @@ QString QgsVectorLayer::getStyleFromDatabase( const QString& styleId, QString &m
return getStyleByIdMethod( mDataSource, styleId, msgError );
}

void QgsVectorLayer::deleteStyleFromDatabase( const QString& styleId, QString &msgError )
bool QgsVectorLayer::deleteStyleFromDatabase( const QString& styleId, QString &msgError )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return;
return false;
}
deleteStyleById_t* deleteStyleByIdMethod = reinterpret_cast< deleteStyleById_t * >( cast_to_fptr( myLib->resolve( "deleteStyleById" ) ) );

if ( !deleteStyleByIdMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey, QStringLiteral( "deleteStyleById" ) );
return;
return false;
}

return deleteStyleByIdMethod( mDataSource, styleId, msgError );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
/**
* Will delete the named style corresponding to style id provided from the database
*/
virtual void deleteStyleFromDatabase( const QString& styleId, QString &msgError );
virtual bool deleteStyleFromDatabase( const QString& styleId, QString &msgError );

/**
* Load a named style from file/local db/datasource db
Expand Down
12 changes: 9 additions & 3 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4632,14 +4632,16 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
return numberOfRelatedStyles;
}

QGISEXTERN void deleteStyleById( const QString &uri, QString styleId, QString &errCause )
QGISEXTERN bool deleteStyleById( const QString &uri, QString styleId, QString &errCause )
{
QgsDataSourceUri dsUri( uri );
bool deleted;

QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() );
deleted = false;
}
else
{
Expand All @@ -4653,11 +4655,15 @@ QGISEXTERN void deleteStyleById( const QString &uri, QString styleId, QString &e
.arg( result.PQresultStatus() ).arg( PGRES_COMMAND_OK ).arg( deleteStyleQuery ) );
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( deleteStyleQuery ) );
errCause = QObject::tr( "Error executing the delete query. The query was logged" );
deleted = false;
}
else
{
deleted = true;
}
}

conn->unref();

return deleted;
}

QGISEXTERN QString getStyleById( const QString& uri, QString styleId, QString& errCause )
Expand Down
6 changes: 4 additions & 2 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,12 @@ def testStyle(self):
self.assertTrue(qml.startswith('<!DOCTYPE qgis'), qml)
self.assertEqual(errmsg, "")

errmsg = vl.deleteStyleFromDatabase("101")
res, errmsg = vl.deleteStyleFromDatabase("100")
self.assertTrue(res)
self.assertEqual(errmsg, "")

errmsg = vl.deleteStyleFromDatabase("1")
res, errmsg = vl.deleteStyleFromDatabase("1")
self.assertTrue(res)
self.assertEqual(errmsg, "")

# table layer_styles does exit, but is now empty
Expand Down

0 comments on commit 8e989c1

Please sign in to comment.