Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #6717 from pblottiere/remove_qgd
Do not save .qgd file alongside .qgs when it's not used
  • Loading branch information
pblottiere committed Apr 6, 2018
2 parents 5014d95 + ff1bc0b commit 1a515c9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsauxiliarystorage.sip.in
Expand Up @@ -345,6 +345,15 @@ Duplicates a table and its content.
static QString extension();
%Docstring
Returns the extension used for auxiliary databases.
%End

static bool exists( const QgsProject &project );
%Docstring
Returns true if the auxiliary database yet exists for a project, false otherwise.

:param project: The project for which the database is checked

.. versionadded:: 3.2
%End

};
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsauxiliarystorage.cpp
Expand Up @@ -647,6 +647,12 @@ QString QgsAuxiliaryStorage::extension()
return AS_EXTENSION;
}

bool QgsAuxiliaryStorage::exists( const QgsProject &project )
{
const QFileInfo fileinfo( filenameForProject( project ) );
return fileinfo.exists() && fileinfo.isFile();
}

bool QgsAuxiliaryStorage::exec( const QString &sql, sqlite3 *handler )
{
bool rc = false;
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsauxiliarystorage.h
Expand Up @@ -377,6 +377,15 @@ class CORE_EXPORT QgsAuxiliaryStorage
*/
static QString extension();

/**
* Returns true if the auxiliary database yet exists for a project, false otherwise.
*
* \param project The project for which the database is checked
*
* \since QGIS 3.2
*/
static bool exists( const QgsProject &project );

private:
spatialite_database_unique_ptr open( const QString &filename = QString() );
spatialite_database_unique_ptr open( const QgsProject &project );
Expand Down
8 changes: 7 additions & 1 deletion src/core/qgsproject.cpp
Expand Up @@ -2505,6 +2505,7 @@ void QgsProject::setTrustLayerMetadata( bool trust )
bool QgsProject::saveAuxiliaryStorage( const QString &filename )
{
const QMap<QString, QgsMapLayer *> layers = mapLayers();
bool empty = true;
for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
{
if ( it.value()->type() != QgsMapLayer::VectorLayer )
Expand All @@ -2514,10 +2515,15 @@ bool QgsProject::saveAuxiliaryStorage( const QString &filename )
if ( vl && vl->auxiliaryLayer() )
{
vl->auxiliaryLayer()->save();
empty &= vl->auxiliaryLayer()->auxiliaryFields().isEmpty();
}
}

if ( !filename.isEmpty() )
if ( !mAuxiliaryStorage->exists( *this ) && filename.isEmpty() && empty )
{
return true; // it's not an error
}
else if ( !filename.isEmpty() )
{
return mAuxiliaryStorage->saveAs( filename );
}
Expand Down
41 changes: 40 additions & 1 deletion tests/src/python/test_qgsauxiliarystorage.py
Expand Up @@ -40,7 +40,7 @@ def tmpPath():
f.close()
os.remove(f.fileName())

return f.fileName()
return f.fileName().replace('.', '_')


def createLayer():
Expand Down Expand Up @@ -405,6 +405,45 @@ def testCreateProperty(self):
afIndex = vl.fields().indexOf(afName)
self.assertEqual(index, afIndex)

def testQgdCreation(self):
# New project
p = QgsProject()
self.assertTrue(p.auxiliaryStorage().isValid())

# Save the project
path = tmpPath()
qgs = path + '.qgs'
self.assertTrue(p.write(qgs))
self.assertTrue(os.path.exists(qgs))

# Auxiliary storage is empty so .qgd file should not be saved
qgd = path + '.qgd'
self.assertFalse(os.path.exists(qgd))

# Add a vector layer and an auxiliary layer in the project
vl = createLayer()
self.assertTrue(vl.isValid())
p.addMapLayers([vl])

pkf = vl.fields().field(vl.fields().indexOf('pk'))
al = p.auxiliaryStorage().createAuxiliaryLayer(pkf, vl)
self.assertTrue(al.isValid())
vl.setAuxiliaryLayer(al)

# Add an auxiliary field to have a non empty auxiliary storage
pdef = QgsPropertyDefinition('propname', QgsPropertyDefinition.DataTypeNumeric, '', '', 'ut')
self.assertTrue(al.addAuxiliaryField(pdef))

# Save the project
newpath = tmpPath()
qgs = newpath + '.qgs'
self.assertTrue(p.write(qgs))
self.assertTrue(os.path.exists(qgs))

# Auxiliary storage is NOT empty so .qgd file should be saved now
qgd = newpath + '.qgd'
self.assertTrue(os.path.exists(qgd))


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

0 comments on commit 1a515c9

Please sign in to comment.