|
15 | 15 |
|
16 | 16 | #include "qgsuserprofile.h"
|
17 | 17 | #include "qgsapplication.h"
|
| 18 | +#include "qgssqliteutils.h" |
18 | 19 |
|
19 | 20 | #include <QDir>
|
20 | 21 | #include <QTextStream>
|
21 | 22 | #include <QSettings>
|
22 |
| -#include <QSqlDatabase> |
23 |
| -#include <QSqlQuery> |
24 |
| -#include <QSqlError> |
| 23 | +#include <sqlite3.h> |
25 | 24 |
|
26 | 25 | QgsUserProfile::QgsUserProfile( const QString &folder )
|
27 | 26 | {
|
@@ -58,64 +57,70 @@ void QgsUserProfile::initSettings() const
|
58 | 57 |
|
59 | 58 | const QString QgsUserProfile::alias() const
|
60 | 59 | {
|
61 |
| - QFile qgisPrivateDbFile( qgisDB() ); |
| 60 | + const QString dbFile = qgisDB(); |
| 61 | + QString profileAlias = name(); |
62 | 62 |
|
63 | 63 | // Looks for qgis.db
|
64 | 64 | // If it's not there we can just return name.
|
65 |
| - if ( !qgisPrivateDbFile.exists() ) |
| 65 | + if ( !QFile::exists( dbFile ) ) |
66 | 66 | {
|
67 |
| - return name(); |
| 67 | + return profileAlias; |
68 | 68 | }
|
69 | 69 |
|
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; |
74 | 71 |
|
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 ) |
79 | 81 | {
|
80 |
| - if ( query.next() ) |
| 82 | + if ( preparedStatement.step() == SQLITE_ROW ) |
81 | 83 | {
|
82 |
| - QString alias = query.value( 0 ).toString(); |
| 84 | + QString alias = preparedStatement.columnAsText( 0 ); |
83 | 85 | if ( !alias.isEmpty() )
|
84 | 86 | profileAlias = alias;
|
85 | 87 | }
|
86 | 88 | }
|
87 |
| - db.close(); |
88 | 89 | return profileAlias;
|
89 | 90 | }
|
90 | 91 |
|
91 | 92 | QgsError QgsUserProfile::setAlias( const QString &alias )
|
92 | 93 | {
|
93 | 94 | QgsError error;
|
94 |
| - QFile qgisPrivateDbFile( qgisDB() ); |
| 95 | + const QString dbFile = qgisDB(); |
95 | 96 |
|
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 ) ) |
97 | 100 | {
|
98 | 101 | error.append( QObject::tr( "qgis.db doesn't exist in the user's profile folder" ) );
|
99 | 102 | return error;
|
100 | 103 | }
|
101 | 104 |
|
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 ) |
105 | 110 | {
|
106 | 111 | error.append( QObject::tr( "Unable to open qgis.db for update." ) );
|
107 | 112 | return error;
|
108 | 113 | }
|
109 | 114 |
|
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 ) |
115 | 120 | {
|
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() ) ); |
117 | 122 | }
|
118 |
| - db.close(); |
| 123 | + |
119 | 124 | return error;
|
120 | 125 | }
|
121 | 126 |
|
|
0 commit comments