Skip to content

Commit cf1ace8

Browse files
committed
Switch map layer to RAII sqlite3
1 parent c713878 commit cf1ace8

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

src/core/qgsmaplayer.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include <sqlite3.h>
3030

31+
#include "qgssqliteutils.h"
32+
3133
#include "qgssqliteutils.h"
3234
#include "qgs3drendererregistry.h"
3335
#include "qgsabstract3drenderer.h"
@@ -1342,12 +1344,10 @@ QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag )
13421344
QString qml = myDocument.toString();
13431345

13441346
// read from database
1345-
sqlite3 *myDatabase = nullptr;
1346-
sqlite3_stmt *myPreparedStatement = nullptr;
1347-
const char *myTail = nullptr;
1348-
int myResult;
1347+
sqlite3_database_unique_ptr database;
1348+
sqlite3_statement_unique_ptr statement;
13491349

1350-
myResult = sqlite3_open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ).toUtf8().data(), &myDatabase );
1350+
int myResult = database.open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ) );
13511351
if ( myResult != SQLITE_OK )
13521352
{
13531353
return tr( "User database could not be opened." );
@@ -1357,44 +1357,39 @@ QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag )
13571357
QByteArray param1 = qml.toUtf8();
13581358

13591359
QString mySql = QStringLiteral( "create table if not exists tbl_styles(style varchar primary key,qml varchar)" );
1360-
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1360+
database.prepare( mySql, myResult );
13611361
if ( myResult == SQLITE_OK )
13621362
{
1363-
if ( sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
1363+
if ( sqlite3_step( statement.get() ) != SQLITE_DONE )
13641364
{
1365-
sqlite3_finalize( myPreparedStatement );
1366-
sqlite3_close( myDatabase );
13671365
resultFlag = false;
13681366
return tr( "The style table could not be created." );
13691367
}
13701368
}
13711369

1372-
sqlite3_finalize( myPreparedStatement );
1373-
13741370
mySql = QStringLiteral( "insert into tbl_styles(style,qml) values (?,?)" );
1375-
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1371+
database.prepare( mySql, myResult );
13761372
if ( myResult == SQLITE_OK )
13771373
{
1378-
if ( sqlite3_bind_text( myPreparedStatement, 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1379-
sqlite3_bind_text( myPreparedStatement, 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1380-
sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
1374+
if ( sqlite3_bind_text( statement.get(), 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1375+
sqlite3_bind_text( statement.get(), 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1376+
sqlite3_step( statement.get() ) == SQLITE_DONE )
13811377
{
13821378
resultFlag = true;
13831379
myErrorMessage = tr( "The style %1 was saved to database" ).arg( uri );
13841380
}
13851381
}
13861382

1387-
sqlite3_finalize( myPreparedStatement );
1388-
13891383
if ( !resultFlag )
13901384
{
13911385
QString mySql = QStringLiteral( "update tbl_styles set qml=? where style=?" );
1392-
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1386+
database.prepare( mySql, myResult );
1387+
13931388
if ( myResult == SQLITE_OK )
13941389
{
1395-
if ( sqlite3_bind_text( myPreparedStatement, 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1396-
sqlite3_bind_text( myPreparedStatement, 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1397-
sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
1390+
if ( sqlite3_bind_text( statement.get(), 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1391+
sqlite3_bind_text( statement.get(), 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1392+
sqlite3_step( statement.get() ) == SQLITE_DONE )
13981393
{
13991394
resultFlag = true;
14001395
myErrorMessage = tr( "The style %1 was updated in the database." ).arg( uri );
@@ -1410,11 +1405,7 @@ QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag )
14101405
resultFlag = false;
14111406
myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( uri );
14121407
}
1413-
1414-
sqlite3_finalize( myPreparedStatement );
14151408
}
1416-
1417-
sqlite3_close( myDatabase );
14181409
}
14191410

14201411
return myErrorMessage;

0 commit comments

Comments
 (0)