Skip to content
Permalink
Browse files

make deleteStyleFromDatabase consistent with other vectorlayer's methods

  • Loading branch information
jgrocha committed Jan 30, 2017
1 parent a4637a6 commit 6d6f288102b3186a7e06b3c6103527e4aa0ab9cd
@@ -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 );

/**
@@ -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
@@ -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() )
{
@@ -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 );
@@ -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 );

/**
@@ -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,
@@ -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 )
@@ -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
@@ -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 );

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

0 comments on commit 6d6f288

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