19#include "moc_qgsstoredquerymanager.cpp"
21#include <QCryptographicHash>
35 if (
query.isEmpty() || name.isEmpty() )
38 bool wasAdded =
false;
39 bool wasUpdated =
false;
42 const QString hash = getQueryHash( name );
48 const bool isExisting = sTreeStoredQueries->items().contains( hash );
49 wasAdded = !isExisting;
50 wasUpdated = isExisting;
51 settingQueryName->setValue( name, hash );
52 settingQueryDefinition->setValue(
query, hash );
59 wasAdded = !isExisting;
60 wasUpdated = isExisting;
69 else if ( wasUpdated )
78 bool wasDeleted =
false;
80 const QString hash = getQueryHash( name );
86 wasDeleted = sTreeStoredQueries->items().contains( hash );
87 sTreeStoredQueries->deleteItem( hash );
95 "DBManager", QStringLiteral(
"savedQueries/%1" ).arg( hash )
112 const QStringList hashes = sTreeStoredQueries->items();
113 names.reserve( hashes.size() );
114 for (
const QString &hash : hashes )
116 names.append( settingQueryName->value( hash ) );
124 names.reserve( hashes.size() );
125 for (
const QString &hash : hashes )
128 QStringLiteral(
"DBManager" ), QStringLiteral(
"savedQueries/%1/name" ).arg( hash )
140 const QString hash = getQueryHash( name );
146 return settingQueryDefinition->value( hash );
159 QList<QgsStoredQueryManager::QueryDetails> res;
161 const QStringList localProfileHashes = sTreeStoredQueries->items();
163 res.reserve( localProfileHashes.size() + projectHashes.size() );
165 for (
const QString &hash : localProfileHashes )
168 details.
name = settingQueryName->value( hash );
169 details.
definition = settingQueryDefinition->value( hash );
171 res.append( details );
174 for (
const QString &hash : projectHashes )
178 QStringLiteral(
"DBManager" ), QStringLiteral(
"savedQueries/%1/name" ).arg( hash )
181 QStringLiteral(
"DBManager" ), QStringLiteral(
"savedQueries/%1/query" ).arg( hash )
184 res.append( details );
188 if ( a.name == b.name )
189 return a.backend == Qgis::QueryStorageBackend::CurrentProject;
191 return QString::localeAwareCompare( a.name, b.name ) < 0;
197QString QgsStoredQueryManager::getQueryHash(
const QString &name )
200 QByteArray nameUtf8 = name.toUtf8();
201 QByteArray hash = QCryptographicHash::hash( nameUtf8, QCryptographicHash::Md5 ).toHex();
202 return QStringLiteral(
"q%1" ).arg( QString::fromUtf8( hash ) );
QueryStorageBackend
Stored query storage backends.
@ CurrentProject
Current QGIS project.
@ LocalProfile
Local user profile.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
Reads a string from the specified scope and key.
bool writeEntry(const QString &scope, const QString &key, bool value)
Write a boolean value to the project file.
QStringList subkeyList(const QString &scope, const QString &key) const
Returns a list of child keys which contain other keys that exist within the the specified scope and k...
bool removeEntry(const QString &scope, const QString &key)
Remove the given key from the specified scope.
Contains details about a stored query.
QString name
Name of the query.
QString definition
Query definition.
Qgis::QueryStorageBackend backend
Storage backend.
QList< QgsStoredQueryManager::QueryDetails > allQueries() const
Returns details of all queries stored in the manager.
QStringList allQueryNames(Qgis::QueryStorageBackend backend=Qgis::QueryStorageBackend::LocalProfile) const
Returns a list of the names of all stored queries for the specified backend.
void queryRemoved(const QString &name, Qgis::QueryStorageBackend backend)
Emitted when a query is removed from the manager.
void queryChanged(const QString &name, Qgis::QueryStorageBackend backend)
Emitted when an existing query is changed in the manager.
void queryAdded(const QString &name, Qgis::QueryStorageBackend backend)
Emitted when a query is added to the manager.
QgsStoredQueryManager(QObject *parent=nullptr)
Constructor for QgsStoredQueryManager, with the specified parent object.
QString query(const QString &name, Qgis::QueryStorageBackend backend=Qgis::QueryStorageBackend::LocalProfile) const
Returns the query definition with matching name, from the specified backend.
void removeQuery(const QString &name, Qgis::QueryStorageBackend backend=Qgis::QueryStorageBackend::LocalProfile)
Removes the stored query with matching name.
void storeQuery(const QString &name, const QString &query, Qgis::QueryStorageBackend backend=Qgis::QueryStorageBackend::LocalProfile)
Saves a query to the manager.
#define BUILTIN_UNREACHABLE