Skip to content

Commit

Permalink
Add layer dependencies for virtual layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Mercier committed Jan 7, 2016
1 parent 020dac0 commit ba2e9df
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/providers/virtual/qgsvirtuallayerprovider.cpp
Expand Up @@ -600,6 +600,17 @@ QgsAttributeList QgsVirtualLayerProvider::pkAttributeIndexes()
return QgsAttributeList();
}

QSet<QString> QgsVirtualLayerProvider::layerDependencies() const
{
QSet<QString> deps;
foreach ( const QgsVirtualLayerDefinition::SourceLayer& l, mDefinition.sourceLayers() )
{
if ( l.isReferenced() )
deps << l.reference();
}
return deps;
}

/**
* Class factory to return a pointer to a newly created
* QgsSpatiaLiteProvider object
Expand Down
3 changes: 3 additions & 0 deletions src/providers/virtual/qgsvirtuallayerprovider.h
Expand Up @@ -89,6 +89,9 @@ class QgsVirtualLayerProvider: public QgsVectorDataProvider
/** Return list of indexes of fields that make up the primary key */
QgsAttributeList pkAttributeIndexes() override;

/** Get the list of layer ids on which this layer depends */
QSet<QString> layerDependencies() const override;

private:

// file on disk
Expand Down
39 changes: 39 additions & 0 deletions tests/src/python/test_provider_virtual.py
Expand Up @@ -30,6 +30,7 @@
QgsProviderRegistry,
QgsVirtualLayerDefinition,
QgsWKBTypes
QgsProject
)

from utilities import (unitTestDataPath,
Expand Down Expand Up @@ -664,5 +665,43 @@ def test_queryOnMemoryLayer(self):
ml.addFeatures([f3])
self.assertEqual(ml.featureCount(), vl.featureCount())

def test_ProjectDependencies(self):
# make a virtual layer with living references and save it to a project
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "france_parts", "ogr", False)
self.assertEqual(l1.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l1)

query = QUrl.toPercentEncoding("SELECT * FROM france_parts")
l2 = QgsVectorLayer("?query=%s" % query, "aa", "virtual", False)
self.assertEqual(l2.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l2)

self.assertEqual(len(l2.layerDependencies()), 1)
self.assertEqual(l2.layerDependencies()[0].startswith('france_parts'), True)

query = QUrl.toPercentEncoding("SELECT t1.objectid, t2.name_0 FROM france_parts as t1, aa as t2")
l3 = QgsVectorLayer("?query=%s" % query, "bb", "virtual", False)
self.assertEqual(l3.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l3)

self.assertEqual(len(l2.layerDependencies()), 1)
self.assertEqual(l2.layerDependencies()[0].startswith('france_parts'), True)

self.assertEqual(len(l3.layerDependencies()), 2)

temp = os.path.join(tempfile.gettempdir(), "qgstestproject.qgs")

QgsProject.instance().setFileName(temp)
QgsProject.instance().write()

QgsMapLayerRegistry.instance().removeMapLayers([l1, l2])
QgsProject.instance().clear()

QgsProject.instance().setFileName(temp)
QgsProject.instance().read()

# make sure the 3 layers are loaded back
self.assertEqual(len(QgsMapLayerRegistry.instance().mapLayers()), 3)

if __name__ == '__main__':
unittest.main()

0 comments on commit ba2e9df

Please sign in to comment.