Skip to content
Permalink
Browse files

improve deleteStyleFromDatabase signature

  • Loading branch information
jgrocha committed Jan 31, 2017
1 parent 6d6f288 commit 8e989c1e38fa75e279cf9982a5be72105e4585fc
@@ -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
@@ -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
@@ -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 );
@@ -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
@@ -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
{
@@ -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 )
@@ -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

0 comments on commit 8e989c1

Please sign in to comment.
You can’t perform that action at this time.