Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 49d8060
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
17 changes: 4 additions & 13 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -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();
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgssqliteutils.cpp
Expand Up @@ -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() )
Expand Down
13 changes: 12 additions & 1 deletion src/core/qgssqliteutils.h
Expand Up @@ -21,6 +21,8 @@
#define SIP_NO_FILE

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

#include <memory>
#include <QString>

Expand Down Expand Up @@ -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;
};

/**
Expand Down

0 comments on commit 49d8060

Please sign in to comment.