Skip to content
Permalink
Browse files

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 1a515c923d05419adf68a86e27c9a0fc8330e059
@@ -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

};
@@ -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;
@@ -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 );
@@ -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 )
@@ -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 );
}
@@ -40,7 +40,7 @@ def tmpPath():
f.close()
os.remove(f.fileName())

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


def createLayer():
@@ -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.
You can’t perform that action at this time.