Skip to content
Permalink
Browse files

Add sqlite3_database_unique_ptr::exec

A handy shortcut to quickly run commands on a sqlite database.
  • Loading branch information
m-kuhn committed Dec 21, 2018
1 parent e698ba8 commit 49d8060c30c6f7b005bfe3aedb2f9c0c7cce4459
Showing with 31 additions and 14 deletions.
  1. +4 −13 src/core/expression/qgsexpressionfunction.cpp
  2. +15 −0 src/core/qgssqliteutils.cpp
  3. +12 −1 src/core/qgssqliteutils.h
@@ -1421,24 +1421,15 @@ static QVariant fcnSqliteFetchAndIncrement( const QVariantList &values, const Qg
upsertSql += QLatin1String( " VALUES " );
upsertSql += '(' + vals.join( ',' ) + ')';

sqliteStatement = sqliteDb.prepare( upsertSql, result );

QString errorMessage;
result = sqliteDb.exec( upsertSql, errorMessage );
if ( result == SQLITE_OK )
{
result = sqliteStatement.step();
if ( result == SQLITE_DONE )
{
return nextId;
}
else
{
parent->setEvalErrorString( QStringLiteral( "Could not increment value: SQLite error: \"%1\" (%2)." ).arg( sqliteDb.errorMessage(), QString::number( result ) ) );
return QVariant();
}
return nextId;
}
else
{
parent->setEvalErrorString( QStringLiteral( "Could not increment value: SQLite error: \"%1\" (%2)." ).arg( sqliteDb.errorMessage(), QString::number( result ) ) );
parent->setEvalErrorString( QStringLiteral( "Could not increment value: SQLite error: \"%1\" (%2)." ).arg( errorMessage, QString::number( result ) ) );
return QVariant();
}
}
@@ -92,6 +92,21 @@ sqlite3_statement_unique_ptr sqlite3_database_unique_ptr::prepare( const QString
return s;
}

int sqlite3_database_unique_ptr::exec( const QString &sql, QString &errorMessage ) const
{
char *errMsg;

int ret = sqlite3_exec( get(), sql.toUtf8(), nullptr, nullptr, &errMsg );

if ( errMsg )
{
errorMessage = QString::fromUtf8( errMsg );
sqlite3_free( errMsg );
}

return ret;
}

QString QgsSqliteUtils::quotedString( const QString &value )
{
if ( value.isNull() )
@@ -21,6 +21,8 @@
#define SIP_NO_FILE

#include "qgis_core.h"
#include "qgis_sip.h"

#include <memory>
#include <QString>

@@ -135,8 +137,17 @@ class CORE_EXPORT sqlite3_database_unique_ptr : public std::unique_ptr< sqlite3,
* 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 ) const;
sqlite3_statement_unique_ptr prepare( const QString &sql, int &resultCode SIP_OUT ) const;

/**
* Executes the \a sql command in the database. Multiple sql queries can be run within
* one single command.
* Errors are reported to \a errorMessage.
* Returns SQLITE_OK in case of success or an sqlite error code.
*
* \since QGIS 3.6
*/
int exec( const QString &sql, QString &errorMessage SIP_OUT ) const;
};

/**

0 comments on commit 49d8060

Please sign in to comment.
You can’t perform that action at this time.