Skip to content

Commit

Permalink
Release spatialite context when a spatialite_database_unique_ptr scop…
Browse files Browse the repository at this point in the history
…es out
  • Loading branch information
m-kuhn committed Nov 15, 2017
1 parent 5c12430 commit 2aed700
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
25 changes: 18 additions & 7 deletions src/core/qgsspatialiteutils.cpp
Expand Up @@ -17,15 +17,23 @@


#include "qgsspatialiteutils.h"
#include "qgslogger.h"

#include <sqlite3.h>
#include <spatialite.h>

int spatialite_database_unique_ptr::open( const QString &path )
spatialite_database_unique_ptr::spatialite_database_unique_ptr()
: std::unique_ptr< sqlite3, std::function<void( sqlite3 * )>> ( nullptr, [this]( sqlite3 * handle )->void
{
deleter( handle );
} )
{
}

int spatialite_database_unique_ptr::open( const QString &path )
{
#if defined(SPATIALITE_HAS_INIT_EX)
void *conn = spatialite_alloc_connection();
mSpatialiteContext = spatialite_alloc_connection();
#else
spatialite_init( 0 );
#endif
Expand All @@ -36,7 +44,7 @@ int spatialite_database_unique_ptr::open( const QString &path )

#if defined(SPATIALITE_HAS_INIT_EX)
if ( result == SQLITE_OK )
spatialite_init_ex( database, conn, 0 );
spatialite_init_ex( database, mSpatialiteContext, 0 );
#endif

return result;
Expand Down Expand Up @@ -77,12 +85,15 @@ sqlite3_statement_unique_ptr spatialite_database_unique_ptr::prepare( const QStr
return s;
}

void QgsSpatialiteCloser::operator()( sqlite3 *database )
void spatialite_database_unique_ptr::deleter( sqlite3 *handle )
{
sqlite3_close( database );

#if defined(SPATIALITE_HAS_INIT_EX)
spatialite_cleanup_ex( database );
spatialite_cleanup_ex( mSpatialiteContext );
#endif

int res = sqlite3_close( handle );
if ( res != SQLITE_OK )
{
QgsDebugMsg( QString( "sqlite3_close() failed: %1" ).arg( res ) );
}
}
32 changes: 15 additions & 17 deletions src/core/qgsspatialiteutils.h
Expand Up @@ -22,22 +22,7 @@

#include "qgis_core.h"
#include "qgssqliteutils.h"

/**
* \ingroup core
*
* Closes a spatialite database.
*
* \since QGIS 3.0
*/
struct CORE_EXPORT QgsSpatialiteCloser
{

/**
* Closes an spatialite \a database.
*/
void operator()( sqlite3 *database );
};
#include <functional>

/**
* \ingroup core
Expand All @@ -47,10 +32,12 @@ struct CORE_EXPORT QgsSpatialiteCloser
*
* \since QGIS 3.0
*/
class CORE_EXPORT spatialite_database_unique_ptr : public std::unique_ptr< sqlite3, QgsSpatialiteCloser>
class CORE_EXPORT spatialite_database_unique_ptr : public std::unique_ptr< sqlite3, std::function<void( sqlite3 * )>>
{
public:

spatialite_database_unique_ptr();

/**
* Opens the database at the specified file \a path.
*
Expand All @@ -76,6 +63,17 @@ class CORE_EXPORT spatialite_database_unique_ptr : public std::unique_ptr< sqlit
*/
sqlite3_statement_unique_ptr prepare( const QString &sql, int &resultCode );

private:

/**
* Will be set as deleter for this pointer in the constructor.
*/
void deleter( sqlite3 *handle );

/**
* Keep track of the spatialite context. Set in open(_v2), unset in deleter.
*/
void *mSpatialiteContext = nullptr;
};

#endif // QGSSPATIALITEUTILS_H

0 comments on commit 2aed700

Please sign in to comment.