Skip to content

Commit af79577

Browse files
committed
Fix various qt warnings related to user profile databases
Switch to using QGIS sqlite helper classes instead of Qt database classes
1 parent e91e342 commit af79577

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

src/core/qgsuserprofile.cpp

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515

1616
#include "qgsuserprofile.h"
1717
#include "qgsapplication.h"
18+
#include "qgssqliteutils.h"
1819

1920
#include <QDir>
2021
#include <QTextStream>
2122
#include <QSettings>
22-
#include <QSqlDatabase>
23-
#include <QSqlQuery>
24-
#include <QSqlError>
23+
#include <sqlite3.h>
2524

2625
QgsUserProfile::QgsUserProfile( const QString &folder )
2726
{
@@ -58,64 +57,70 @@ void QgsUserProfile::initSettings() const
5857

5958
const QString QgsUserProfile::alias() const
6059
{
61-
QFile qgisPrivateDbFile( qgisDB() );
60+
const QString dbFile = qgisDB();
61+
QString profileAlias = name();
6262

6363
// Looks for qgis.db
6464
// If it's not there we can just return name.
65-
if ( !qgisPrivateDbFile.exists() )
65+
if ( !QFile::exists( dbFile ) )
6666
{
67-
return name();
67+
return profileAlias;
6868
}
6969

70-
QSqlDatabase db = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), QStringLiteral( "userprofile" ) );
71-
db.setDatabaseName( qgisDB() );
72-
if ( !db.open() )
73-
return name();
70+
sqlite3_database_unique_ptr database;
7471

75-
QSqlQuery query;
76-
query.prepare( QStringLiteral( "SELECT value FROM tbl_config_variables WHERE variable = 'ALIAS'" ) );
77-
QString profileAlias = name();
78-
if ( query.exec() )
72+
//check the db is available
73+
int result = database.open( dbFile );
74+
if ( result != SQLITE_OK )
75+
{
76+
return profileAlias;
77+
}
78+
79+
sqlite3_statement_unique_ptr preparedStatement = database.prepare( QStringLiteral( "SELECT value FROM tbl_config_variables WHERE variable = 'ALIAS'" ), result );
80+
if ( result == SQLITE_OK )
7981
{
80-
if ( query.next() )
82+
if ( preparedStatement.step() == SQLITE_ROW )
8183
{
82-
QString alias = query.value( 0 ).toString();
84+
QString alias = preparedStatement.columnAsText( 0 );
8385
if ( !alias.isEmpty() )
8486
profileAlias = alias;
8587
}
8688
}
87-
db.close();
8889
return profileAlias;
8990
}
9091

9192
QgsError QgsUserProfile::setAlias( const QString &alias )
9293
{
9394
QgsError error;
94-
QFile qgisPrivateDbFile( qgisDB() );
95+
const QString dbFile = qgisDB();
9596

96-
if ( !qgisPrivateDbFile.exists() )
97+
// Looks for qgis.db
98+
// If it's not there we can just return name.
99+
if ( !QFile::exists( dbFile ) )
97100
{
98101
error.append( QObject::tr( "qgis.db doesn't exist in the user's profile folder" ) );
99102
return error;
100103
}
101104

102-
QSqlDatabase db = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), QStringLiteral( "userprofile" ) );
103-
db.setDatabaseName( qgisDB() );
104-
if ( !db.open() )
105+
sqlite3_database_unique_ptr database;
106+
107+
//check the db is available
108+
int result = database.open( dbFile );
109+
if ( result != SQLITE_OK )
105110
{
106111
error.append( QObject::tr( "Unable to open qgis.db for update." ) );
107112
return error;
108113
}
109114

110-
QSqlQuery query;
111-
QString sql = QStringLiteral( "INSERT OR REPLACE INTO tbl_config_variables VALUES ('ALIAS', :alias);" );
112-
query.prepare( sql );
113-
query.bindValue( QStringLiteral( ":alias" ), alias );
114-
if ( !query.exec() )
115+
const QString sql = QStringLiteral( "INSERT OR REPLACE INTO tbl_config_variables VALUES ('ALIAS', %1);" ).arg(
116+
QgsSqliteUtils::quotedString( alias ) );
117+
118+
sqlite3_statement_unique_ptr preparedStatement = database.prepare( sql, result );
119+
if ( result != SQLITE_OK || preparedStatement.step() != SQLITE_DONE )
115120
{
116-
error.append( QObject::tr( "Could not save alias to database: %1" ).arg( query.lastError().text() ) );
121+
error.append( QObject::tr( "Could not save alias to database: %1" ).arg( database.errorMessage() ) );
117122
}
118-
db.close();
123+
119124
return error;
120125
}
121126

0 commit comments

Comments
 (0)