Skip to content

Commit

Permalink
Use std::unique_ptr with custom deleter for spatialite and sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Nov 15, 2017
1 parent 340f7ab commit 4df65ac
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 407 deletions.
2 changes: 0 additions & 2 deletions python/core/qgsauxiliarystorage.sip
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@





class QgsAuxiliaryLayer : QgsVectorLayer
{
%Docstring
Expand Down
116 changes: 1 addition & 115 deletions src/app/qgscustomprojectiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgsprojectionselectiondialog.h"
#include "qgscrscache.h"
#include "qgssettings.h"
#include "qgssqliteutils.h"

//qt includes
#include <QFileInfo>
Expand All @@ -42,121 +43,6 @@ extern "C"
#include <proj_api.h>
}

/**
* Closes a sqlite3 database.
*/
struct QgsSqlite3Closer
{

/**
* Closes an sqlite \a database.
*/
void operator()( sqlite3 *database )
{
sqlite3_close( database );
}
};

/**
* Finalizes an sqlite3 statement.
*/
struct QgsSqlite3StatementFinalizer
{

/**
* Finalizes an sqlite3 \a statement.
*/
void operator()( sqlite3_stmt *statement )
{
sqlite3_finalize( statement );
}
};

/**
* Unique pointer for sqlite3 prepared statements, which automatically finalizes
* the statement when the pointer goes out of scope or is reset.
*/
class sqlite3_statement_unique_ptr : public std::unique_ptr< sqlite3_stmt, QgsSqlite3StatementFinalizer>
{
public:

/**
* Steps to the next record in the statement, returning the sqlite3 result code.
*/
int step()
{
return sqlite3_step( get() );
}

/**
* Returns the column value from the current statement row as a string.
*/
QString columnAsText( int column )
{
return QString::fromUtf8( ( char * ) sqlite3_column_text( get(), column ) );
}

};


/**
* Unique pointer for sqlite3 databases, which automatically closes
* the database when the pointer goes out of scope or is reset.
*/
class sqlite3_database_unique_ptr : public std::unique_ptr< sqlite3, QgsSqlite3Closer>
{
public:

/**
* Opens the database at the specified file \a path.
*
* Returns the sqlite error code, or SQLITE_OK if open was successful.
*/
int open( const QString &path )
{
sqlite3 *database = nullptr;
int result = sqlite3_open( path.toUtf8(), &database );
reset( database );
return result;
}

/**
* Opens the database at the specified file \a path.
*
* Returns the sqlite error code, or SQLITE_OK if open was successful.
*/
int open_v2( const QString &path, int flags, const char *zVfs )
{
sqlite3 *database = nullptr;
int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
reset( database );
return result;
}

/**
* Returns the most recent error message encountered by the database.
*/
QString errorMessage() const
{
return QString( sqlite3_errmsg( get() ) );
}

/**
* Prepares a \a sql statement, returning the result. The \a resultCode
* argument will be filled with the sqlite3 result code.
*/
sqlite3_statement_unique_ptr prepare( const QString &sql, int &resultCode )
{
sqlite3_stmt *preparedStatement = nullptr;
const char *tail = nullptr;
resultCode = sqlite3_prepare( get(), sql.toUtf8(), sql.toUtf8().length(), &preparedStatement, &tail );
sqlite3_statement_unique_ptr s;
s.reset( preparedStatement );
return s;
}

};


QgsCustomProjectionDialog::QgsCustomProjectionDialog( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
Expand Down
14 changes: 6 additions & 8 deletions src/app/qgsnewspatialitelayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "qgsproject.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsprojectionselectiondialog.h"
#include "qgsslconnect.h"
#include "qgsspatialiteutils.h"
#include "qgslogger.h"
#include "qgssettings.h"

Expand Down Expand Up @@ -382,8 +382,8 @@ bool QgsNewSpatialiteLayerDialog::apply()
quotedValue( leGeometryColumn->text() ) );
QgsDebugMsg( sqlCreateIndex ); // OK

sqlite3 *db = nullptr;
int rc = QgsSLConnect::sqlite3_open( mDatabaseComboBox->currentText().toUtf8(), &db );
spatialite_database_unique_ptr database;
int rc = database.open( mDatabaseComboBox->currentText() );
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this,
Expand All @@ -393,7 +393,7 @@ bool QgsNewSpatialiteLayerDialog::apply()
else
{
char *errmsg = nullptr;
rc = sqlite3_exec( db, sql.toUtf8(), nullptr, nullptr, &errmsg );
rc = sqlite3_exec( database.get(), sql.toUtf8(), nullptr, nullptr, &errmsg );
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this,
Expand All @@ -404,7 +404,7 @@ bool QgsNewSpatialiteLayerDialog::apply()
else
{
// create the geometry column and the spatial index
rc = sqlite3_exec( db, sqlAddGeom.toUtf8(), nullptr, nullptr, &errmsg );
rc = sqlite3_exec( database.get(), sqlAddGeom.toUtf8(), nullptr, nullptr, &errmsg );
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this,
Expand All @@ -415,7 +415,7 @@ bool QgsNewSpatialiteLayerDialog::apply()
else
{
// create the spatial index
rc = sqlite3_exec( db, sqlCreateIndex.toUtf8(), nullptr, nullptr, &errmsg );
rc = sqlite3_exec( database.get(), sqlCreateIndex.toUtf8(), nullptr, nullptr, &errmsg );
if ( rc != SQLITE_OK )
{
QMessageBox::warning( this,
Expand Down Expand Up @@ -446,8 +446,6 @@ bool QgsNewSpatialiteLayerDialog::apply()
}
}
}

QgsSLConnect::sqlite3_close( db );
}

return false;
Expand Down
6 changes: 5 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,13 @@ SET(QGIS_CORE_SRCS
qgsscalecalculator.cpp
qgsscaleutils.cpp
qgssimplifymethod.cpp
qgsslconnect.cpp
qgssnappingutils.cpp
qgsspatialindex.cpp
qgssqlexpressioncompiler.cpp
qgssqliteexpressioncompiler.cpp
qgssqlstatement.cpp
qgssqliteutils.cpp
qgsspatialiteutils.cpp
qgsstatisticalsummary.cpp
qgsstringstatisticalsummary.cpp
qgsstringutils.cpp
Expand Down Expand Up @@ -924,6 +925,9 @@ SET(QGIS_CORE_HDRS
qgssimplifymethod.h
qgssnappingutils.h
qgsspatialindex.h
qgsspatialiteutils.h
qgssqlstatement.h
qgssqliteutils.h
qgssqlexpressioncompiler.h
qgssqlstatement.h
qgsstatisticalsummary.h
Expand Down
Loading

0 comments on commit 4df65ac

Please sign in to comment.