Skip to content

Commit

Permalink
make deleteStyleFromDatabase consistent with other vectorlayer's methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrocha committed Jan 30, 2017
1 parent a4637a6 commit 6d6f288
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 28 deletions.
5 changes: 0 additions & 5 deletions python/core/qgsvectordataprovider.sip
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,6 @@ class QgsVectorDataProvider : QgsDataProvider
*/
virtual bool isDeleteStyleFromDBSupported() const;

/**
* Deletes an existing style from the provider
*/
virtual void deleteStyleById( const QString& uri, QString styleId, QString& errCause );

static QVariant convertValue( QVariant::Type type, const QString& value );

/**
Expand Down
5 changes: 5 additions & 0 deletions python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
*/
virtual QString getStyleFromDatabase( const QString& styleId, QString &msgError /Out/ );

/**
* Will delete the named style corresponding to style id provided from the database
*/
virtual void deleteStyleFromDatabase( const QString& styleId, QString &msgError /Out/ );

/**
* Load a named style from file/local db/datasource db
* @param theURI the URI of the style or the URI of the layer
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsloadstylefromdbdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ void QgsLoadStyleFromDBDialog::deleteStyleFromDB()
return;

uri = mLayer->dataProvider()->dataSourceUri();
mLayer->dataProvider()->deleteStyleById( uri, mSelectedStyleId, msgError );
// mLayer->dataProvider()->deleteStyleById( uri, mSelectedStyleId, msgError );
mLayer->deleteStyleFromDatabase( mSelectedStyleId, msgError );

if ( !msgError.isNull() )
{
Expand Down
8 changes: 0 additions & 8 deletions src/core/qgsvectordataprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,14 +700,6 @@ bool QgsVectorDataProvider::isDeleteStyleFromDBSupported() const
return false;
}

void QgsVectorDataProvider::deleteStyleById( const QString& uri, QString styleId, QString& errCause ) const
{
Q_UNUSED( uri );
Q_UNUSED( styleId );
Q_UNUSED( errCause );
return;
}

void QgsVectorDataProvider::pushError( const QString& msg ) const
{
QgsDebugMsg( msg );
Expand Down
5 changes: 0 additions & 5 deletions src/core/qgsvectordataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,6 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
*/
virtual bool isDeleteStyleFromDBSupported() const;

/**
* Deletes an existing style from the provider
*/
virtual void deleteStyleById( const QString& uri, QString styleId, QString& errCause ) const;

static QVariant convertValue( QVariant::Type type, const QString& value );

/**
Expand Down
27 changes: 27 additions & 0 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ typedef QString getStyleById_t(
QString& errCause
);

typedef void deleteStyleById_t(
const QString& uri,
QString styleID,
QString& errCause
);

QgsVectorLayer::QgsVectorLayer( const QString& vectorLayerPath,
const QString& baseName,
const QString& providerKey,
Expand Down Expand Up @@ -4335,6 +4341,27 @@ QString QgsVectorLayer::getStyleFromDatabase( const QString& styleId, QString &m
return getStyleByIdMethod( mDataSource, styleId, msgError );
}

void 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;
}
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 deleteStyleByIdMethod( mDataSource, styleId, msgError );
}


void QgsVectorLayer::saveStyleToDatabase( const QString& name, const QString& description,
bool useAsDefault, const QString& uiFileContent, QString &msgError )
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
virtual QString getStyleFromDatabase( const QString& styleId, QString &msgError );

/**
* Will delete the named style corresponding to style id provided from the database
*/
virtual void deleteStyleFromDatabase( const QString& styleId, QString &msgError );

/**
* Load a named style from file/local db/datasource db
* @param theURI the URI of the style or the URI of the layer
Expand Down
5 changes: 4 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4632,7 +4632,7 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
return numberOfRelatedStyles;
}

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

Expand All @@ -4655,6 +4655,9 @@ void QgsPostgresProvider::deleteStyleById( const QString &uri, QString styleId,
errCause = QObject::tr( "Error executing the delete query. The query was logged" );
}
}

conn->unref();

}

QGISEXTERN QString getStyleById( const QString& uri, QString styleId, QString& errCause )
Expand Down
1 change: 0 additions & 1 deletion src/providers/postgres/qgspostgresprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
bool isValid() const override;
virtual bool isSaveAndLoadStyleToDBSupported() const override { return true; }
virtual bool isDeleteStyleFromDBSupported() const override { return true; }
void deleteStyleById( const QString& uri, QString styleId, QString& errCause ) const override;
QgsAttributeList attributeIndexes() const override;
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
QString defaultValueClause( int fieldId ) const override;
Expand Down
17 changes: 10 additions & 7 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,19 +663,20 @@ def testStyle(self):
status = vl.loadNamedStyle(mFilePath)
self.assertTrue(status)

errorMsg = vl.saveStyleToDatabase("name", "description", False, "")
errorMsg = vl.saveStyleToDatabase("by day", "faded greens and elegant patterns", False, "")
self.assertEqual(errorMsg, "")

qml, errmsg = vl.getStyleFromDatabase("not_existing")
# the style id should be "1", not "by day"
qml, errmsg = vl.getStyleFromDatabase("by day")
self.assertEqual(qml, "")
self.assertNotEqual(errmsg, "")

related_count, idlist, namelist, desclist, errmsg = vl.listStylesInDatabase()
self.assertEqual(related_count, 1)
self.assertEqual(errmsg, "")
self.assertEqual(idlist, ['1'])
self.assertEqual(namelist, ['name'])
self.assertEqual(desclist, ['description'])
self.assertEqual(idlist, ["1"])
self.assertEqual(namelist, ["by day"])
self.assertEqual(desclist, ["faded greens and elegant patterns"])

qml, errmsg = vl.getStyleFromDatabase("100")
self.assertEqual(qml, "")
Expand All @@ -685,8 +686,10 @@ def testStyle(self):
self.assertTrue(qml.startswith('<!DOCTYPE qgis'), qml)
self.assertEqual(errmsg, "")

uri = vl.dataProvider().dataSourceUri()
vl.dataProvider().deleteStyleById(uri, "1", errmsg)
errmsg = vl.deleteStyleFromDatabase("101")
self.assertEqual(errmsg, "")

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

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

0 comments on commit 6d6f288

Please sign in to comment.