10#include <QtCore/qdatastream.h>
11#include <QtCore/qdatetime.h>
12#include <QtCore/qdir.h>
13#include <QtCore/qfileinfo.h>
14#include <QtCore/qmap.h>
15#include <QtCore/qtimer.h>
16#include <QtCore/qversionnumber.h>
17#include <QtSql/qsqldriver.h>
18#include <QtSql/qsqlerror.h>
19#include <QtSql/qsqlquery.h>
23using namespace Qt::StringLiterals;
35 m_inTransaction = m_db.transaction();
50 m_inTransaction =
false;
60 , m_collectionFile(collectionFile)
62 const QFileInfo fi(m_collectionFile);
64 m_collectionFile = fi.absoluteFilePath();
77 emit that->error(tr(
"The collection file \"%1\" is not set up yet.").arg(m_collectionFile));
87 QSqlDatabase::removeDatabase(m_connectionName);
88 m_connectionName.clear();
96 m_connectionName = QHelpGlobal::uniquifyConnectionName(
"QHelpCollectionHandler"_L1,
this);
98 QSqlDatabase db = QSqlDatabase::addDatabase(
"QSQLITE"_L1, m_connectionName);
100 && db.driver()->lastError().type() == QSqlError::ConnectionError) {
101 emit error(tr(
"Cannot load sqlite database driver."));
105 db.setDatabaseName(collectionFile());
107 m_query.reset(
new QSqlQuery(db));
110 QSqlDatabase::removeDatabase(m_connectionName);
111 emit error(tr(
"Cannot open collection file: %1").arg(collectionFile()));
119 m_query->exec(
"PRAGMA synchronous=OFF"_L1);
120 m_query->exec(
"PRAGMA cache_size=3000"_L1);
122 m_query->exec(
"SELECT COUNT(*) FROM sqlite_master WHERE TYPE=\'table\' "
123 "AND Name=\'NamespaceTable\'"_L1);
126 const bool tablesExist = m_query->value(0).toInt() > 0;
128 if (!createTables(m_query.get())) {
130 emit error(tr(
"Cannot create tables in file %1.").arg(collectionFile()));
135 bool indexAndNamespaceFilterTablesMissing =
false;
137 const QStringList newTables = {
141 "FileFilterTable"_L1,
142 "IndexFilterTable"_L1,
143 "ContentsFilterTable"_L1,
144 "FileAttributeSetTable"_L1,
145 "OptimizedFilterTable"_L1,
150 "ComponentMapping"_L1,
151 "ComponentFilter"_L1,
155 QString queryString =
"SELECT COUNT(*) FROM sqlite_master WHERE TYPE=\'table\'"_L1;
156 queryString.append(
" AND (Name=\'"_L1);
157 queryString.append(newTables.join(
"\' OR Name=\'"_L1));
158 queryString.append(
"\')"_L1);
160 m_query->exec(queryString);
162 if (m_query->value(0).toInt() != newTables.size()) {
163 if (!recreateIndexAndNamespaceFilterTables(m_query.get())) {
164 emit error(tr(
"Cannot create index tables in file %1.").arg(collectionFile()));
169 indexAndNamespaceFilterTablesMissing = tablesExist;
173 if (indexAndNamespaceFilterTablesMissing) {
174 for (
const QHelpCollectionHandler::FileInfo &info : docList) {
175 if (!registerIndexAndNamespaceFilterTables(info.namespaceName,
true)) {
176 emit error(tr(
"Cannot register index tables in file %1.").arg(collectionFile()));
183 QList<TimeStamp> timeStamps;
184 m_query->exec(
"SELECT NamespaceId, FolderId, FilePath, Size, TimeStamp FROM TimeStampTable"_L1);
185 while (m_query->next()) {
187 timeStamp.namespaceId = m_query->value(0).toInt();
188 timeStamp.folderId = m_query->value(1).toInt();
189 timeStamp.fileName = m_query->value(2).toString();
190 timeStamp.size = m_query->value(3).toInt();
191 timeStamp.timeStamp = m_query->value(4).toDateTime();
192 timeStamps.append(timeStamp);
195 QList<TimeStamp> toRemove;
196 for (
const TimeStamp &timeStamp : timeStamps) {
197 if (!isTimeStampCorrect(timeStamp))
198 toRemove.append(timeStamp);
204 for (
const TimeStamp &timeStamp : toRemove) {
205 if (!unregisterIndexTable(timeStamp.namespaceId, timeStamp.folderId)) {
206 emit error(tr(
"Cannot unregister index tables in file %1.").arg(collectionFile()));
212 for (
const QHelpCollectionHandler::FileInfo &info : docList) {
213 if (!hasTimeStampInfo(info.namespaceName)
214 && !registerIndexAndNamespaceFilterTables(info.namespaceName)) {
217 unregisterDocumentation(info.namespaceName);
225 const QFileInfo fi(collectionFile());
226 return QDir::isAbsolutePath(fileName)
228 : QFileInfo(fi.absolutePath() + u'/' + fileName).absoluteFilePath();
233 const QFileInfo fi(absoluteDocPath(timeStamp.fileName));
238 if (fi.size() != timeStamp
.size)
241 if (fi.lastModified(QTimeZone::UTC) != timeStamp.timeStamp)
244 m_query->prepare(
"SELECT FilePath FROM NamespaceTable WHERE Id = ?"_L1);
245 m_query->bindValue(0, timeStamp.namespaceId);
246 if (!m_query->exec() || !m_query->next())
249 const QString oldFileName = m_query->value(0).toString();
251 if (oldFileName != timeStamp.fileName)
261 "TimeStampTable.NamespaceId "
265 "WHERE NamespaceTable.Id = TimeStampTable.NamespaceId "
266 "AND NamespaceTable.Name = ? LIMIT 1"_L1);
267 m_query->bindValue(0, nameSpace);
268 if (!m_query->exec())
271 if (!m_query->next())
280 if (m_vacuumScheduled)
283 m_vacuumScheduled =
true;
284 QTimer::singleShot(0,
this, &QHelpCollectionHandler::execVacuum);
292 m_query->exec(
"VACUUM"_L1);
293 m_vacuumScheduled =
false;
301 const QFileInfo fi(fileName);
303 emit error(tr(
"The collection file \"%1\" already exists.").arg(fileName));
307 if (!fi.absoluteDir().exists() && !QDir().mkpath(fi.absolutePath())) {
308 emit error(tr(
"Cannot create directory: %1").arg(fi.absolutePath()));
312 const QString &colFile = fi.absoluteFilePath();
313 const QString &connectionName =
314 QHelpGlobal::uniquifyConnectionName(
"QHelpCollectionHandlerCopy"_L1,
this);
315 QSqlDatabase db = QSqlDatabase::addDatabase(
"QSQLITE"_L1, connectionName);
316 db.setDatabaseName(colFile);
318 emit error(tr(
"Cannot open collection file: %1").arg(colFile));
322 QSqlQuery copyQuery(db);
323 copyQuery.exec(
"PRAGMA synchronous=OFF"_L1);
324 copyQuery.exec(
"PRAGMA cache_size=3000"_L1);
326 if (!createTables(©Query) || !recreateIndexAndNamespaceFilterTables(©Query)) {
327 emit error(tr(
"Cannot copy collection file: %1").arg(colFile));
331 const QString &oldBaseDir = QFileInfo(collectionFile()).absolutePath();
332 const QFileInfo newColFi(colFile);
333 m_query->exec(
"SELECT Name, FilePath FROM NamespaceTable"_L1);
334 while (m_query->next()) {
335 copyQuery.prepare(
"INSERT INTO NamespaceTable VALUES(NULL, ?, ?)"_L1);
336 copyQuery.bindValue(0, m_query->value(0).toString());
337 QString oldFilePath = m_query->value(1).toString();
338 if (!QDir::isAbsolutePath(oldFilePath))
339 oldFilePath = oldBaseDir + u'/' + oldFilePath;
340 copyQuery.bindValue(1, newColFi.absoluteDir().relativeFilePath(oldFilePath));
344 m_query->exec(
"SELECT NamespaceId, Name FROM FolderTable"_L1);
345 while (m_query->next()) {
346 copyQuery.prepare(
"INSERT INTO FolderTable VALUES(NULL, ?, ?)"_L1);
347 copyQuery.bindValue(0, m_query->value(0).toString());
348 copyQuery.bindValue(1, m_query->value(1).toString());
352 m_query->exec(
"SELECT Name FROM FilterAttributeTable"_L1);
353 while (m_query->next()) {
354 copyQuery.prepare(
"INSERT INTO FilterAttributeTable VALUES(NULL, ?)"_L1);
355 copyQuery.bindValue(0, m_query->value(0).toString());
359 m_query->exec(
"SELECT Name FROM FilterNameTable"_L1);
360 while (m_query->next()) {
361 copyQuery.prepare(
"INSERT INTO FilterNameTable VALUES(NULL, ?)"_L1);
362 copyQuery.bindValue(0, m_query->value(0).toString());
366 m_query->exec(
"SELECT NameId, FilterAttributeId FROM FilterTable"_L1);
367 while (m_query->next()) {
368 copyQuery.prepare(
"INSERT INTO FilterTable VALUES(?, ?)"_L1);
369 copyQuery.bindValue(0, m_query->value(0).toInt());
370 copyQuery.bindValue(1, m_query->value(1).toInt());
374 m_query->exec(
"SELECT Key, Value FROM SettingsTable"_L1);
375 while (m_query->next()) {
376 if (m_query->value(0).toString() ==
"FTS5IndexedNamespaces"_L1)
378 copyQuery.prepare(
"INSERT INTO SettingsTable VALUES(?, ?)"_L1);
379 copyQuery.bindValue(0, m_query->value(0).toString());
380 copyQuery.bindValue(1, m_query->value(1));
385 QSqlDatabase::removeDatabase(connectionName);
391 const QStringList tables = {
392 "CREATE TABLE NamespaceTable ("
393 "Id INTEGER PRIMARY KEY, "
395 "FilePath TEXT )"_L1,
396 "CREATE TABLE FolderTable ("
397 "Id INTEGER PRIMARY KEY, "
398 "NamespaceId INTEGER, "
400 "CREATE TABLE FilterAttributeTable ("
401 "Id INTEGER PRIMARY KEY, "
403 "CREATE TABLE FilterNameTable ("
404 "Id INTEGER PRIMARY KEY, "
406 "CREATE TABLE FilterTable ("
408 "FilterAttributeId INTEGER )"_L1,
409 "CREATE TABLE SettingsTable ("
410 "Key TEXT PRIMARY KEY, "
414 for (
const QString &q : tables) {
423 const QStringList tables = {
424 "DROP TABLE IF EXISTS FileNameTable"_L1,
425 "DROP TABLE IF EXISTS IndexTable"_L1,
426 "DROP TABLE IF EXISTS ContentsTable"_L1,
427 "DROP TABLE IF EXISTS FileFilterTable"_L1,
428 "DROP TABLE IF EXISTS IndexFilterTable"_L1,
429 "DROP TABLE IF EXISTS ContentsFilterTable"_L1,
430 "DROP TABLE IF EXISTS FileAttributeSetTable"_L1,
431 "DROP TABLE IF EXISTS OptimizedFilterTable"_L1,
432 "DROP TABLE IF EXISTS TimeStampTable"_L1,
433 "DROP TABLE IF EXISTS VersionTable"_L1,
434 "DROP TABLE IF EXISTS Filter"_L1,
435 "DROP TABLE IF EXISTS ComponentTable"_L1,
436 "DROP TABLE IF EXISTS ComponentMapping"_L1,
437 "DROP TABLE IF EXISTS ComponentFilter"_L1,
438 "DROP TABLE IF EXISTS VersionFilter"_L1,
439 "CREATE TABLE FileNameTable ("
442 "FileId INTEGER PRIMARY KEY, "
444 "CREATE TABLE IndexTable ("
445 "Id INTEGER PRIMARY KEY, "
448 "NamespaceId INTEGER, "
451 "CREATE TABLE ContentsTable ("
452 "Id INTEGER PRIMARY KEY, "
453 "NamespaceId INTEGER, "
455 "CREATE TABLE FileFilterTable ("
456 "FilterAttributeId INTEGER, "
457 "FileId INTEGER)"_L1,
458 "CREATE TABLE IndexFilterTable ("
459 "FilterAttributeId INTEGER, "
460 "IndexId INTEGER)"_L1,
461 "CREATE TABLE ContentsFilterTable ("
462 "FilterAttributeId INTEGER, "
463 "ContentsId INTEGER )"_L1,
464 "CREATE TABLE FileAttributeSetTable ("
465 "NamespaceId INTEGER, "
466 "FilterAttributeSetId INTEGER, "
467 "FilterAttributeId INTEGER)"_L1,
468 "CREATE TABLE OptimizedFilterTable ("
469 "NamespaceId INTEGER, "
470 "FilterAttributeId INTEGER)"_L1,
471 "CREATE TABLE TimeStampTable ("
472 "NamespaceId INTEGER, "
476 "TimeStamp TEXT)"_L1,
477 "CREATE TABLE VersionTable ("
478 "NamespaceId INTEGER, "
480 "CREATE TABLE Filter ("
481 "FilterId INTEGER PRIMARY KEY, "
483 "CREATE TABLE ComponentTable ("
484 "ComponentId INTEGER PRIMARY KEY, "
486 "CREATE TABLE ComponentMapping ("
487 "ComponentId INTEGER, "
488 "NamespaceId INTEGER)"_L1,
489 "CREATE TABLE ComponentFilter ("
490 "ComponentName TEXT, "
491 "FilterId INTEGER)"_L1,
492 "CREATE TABLE VersionFilter ("
494 "FilterId INTEGER)"_L1
497 for (
const QString &q : tables) {
508 m_query->exec(
"SELECT Name FROM FilterNameTable"_L1);
509 while (m_query->next())
510 list.append(m_query->value(0).toString());
519 m_query->exec(
"SELECT Name FROM Filter ORDER BY Name"_L1);
520 while (m_query->next())
521 list.append(m_query->value(0).toString());
530 m_query->exec(
"SELECT DISTINCT Name FROM ComponentTable ORDER BY Name"_L1);
531 while (m_query->next())
532 list.append(m_query->value(0).toString());
539 QList<QVersionNumber> list;
541 m_query->exec(
"SELECT DISTINCT Version FROM VersionTable ORDER BY Version"_L1);
542 while (m_query->next())
543 list.append(QVersionNumber::fromString(m_query->value(0).toString()));
550 QMap<QString, QString> result;
554 "NamespaceTable.Name, "
555 "ComponentTable.Name "
556 "FROM NamespaceTable, "
559 "WHERE NamespaceTable.Id = ComponentMapping.NamespaceId "
560 "AND ComponentMapping.ComponentId = ComponentTable.ComponentId"_L1);
561 while (m_query->next())
562 result.insert(m_query->value(0).toString(), m_query->value(1).toString());
569 QMap<QString, QVersionNumber> result;
573 "NamespaceTable.Name, "
574 "VersionTable.Version "
575 "FROM NamespaceTable, "
577 "WHERE NamespaceTable.Id = VersionTable.NamespaceId"_L1);
578 while (m_query->next()) {
579 result.insert(m_query->value(0).toString(),
580 QVersionNumber::fromString(m_query->value(1).toString()));
588 QStringList components;
589 QList<QVersionNumber> versions;
592 "SELECT ComponentFilter.ComponentName "
593 "FROM ComponentFilter, Filter "
594 "WHERE ComponentFilter.FilterId = Filter.FilterId "
595 "AND Filter.Name = ? "
596 "ORDER BY ComponentFilter.ComponentName"_L1);
597 m_query->bindValue(0, filterName);
599 while (m_query->next())
600 components.append(m_query->value(0).toString());
603 "SELECT VersionFilter.Version "
604 "FROM VersionFilter, Filter "
605 "WHERE VersionFilter.FilterId = Filter.FilterId "
606 "AND Filter.Name = ? "
607 "ORDER BY VersionFilter.Version"_L1);
608 m_query->bindValue(0, filterName);
610 while (m_query->next())
611 versions.append(QVersionNumber::fromString(m_query->value(0).toString()));
614 QHelpFilterData data;
615 data.setComponents(components);
616 data.setVersions(versions);
621 const QHelpFilterData &filterData)
623 if (!removeFilter(filterName))
626 m_query->prepare(
"INSERT INTO Filter VALUES (NULL, ?)"_L1);
627 m_query->bindValue(0, filterName);
628 if (!m_query->exec())
631 const int filterId = m_query->lastInsertId().toInt();
633 QVariantList componentList;
634 QVariantList versionList;
635 QVariantList filterIdList;
637 for (
const QString &component : filterData.components()) {
638 componentList.append(component);
639 filterIdList.append(filterId);
642 m_query->prepare(
"INSERT INTO ComponentFilter VALUES (?, ?)"_L1);
643 m_query->addBindValue(componentList);
644 m_query->addBindValue(filterIdList);
645 if (!m_query->execBatch())
648 filterIdList.clear();
649 for (
const QVersionNumber &version : filterData.versions()) {
650 versionList.append(version.isNull() ? QString() : version.toString());
651 filterIdList.append(filterId);
654 m_query->prepare(
"INSERT INTO VersionFilter VALUES (?, ?)"_L1);
655 m_query->addBindValue(versionList);
656 m_query->addBindValue(filterIdList);
657 if (!m_query->execBatch())
665 m_query->prepare(
"SELECT FilterId FROM Filter WHERE Name = ?"_L1);
666 m_query->bindValue(0, filterName);
667 if (!m_query->exec())
670 if (!m_query->next())
673 const int filterId = m_query->value(0).toInt();
675 m_query->prepare(
"DELETE FROM Filter WHERE Filter.Name = ?"_L1);
676 m_query->bindValue(0, filterName);
677 if (!m_query->exec())
680 m_query->prepare(
"DELETE FROM ComponentFilter WHERE ComponentFilter.FilterId = ?"_L1);
681 m_query->bindValue(0, filterId);
682 if (!m_query->exec())
685 m_query->prepare(
"DELETE FROM VersionFilter WHERE VersionFilter.FilterId = ?"_L1);
686 m_query->bindValue(0, filterId);
687 if (!m_query->exec())
695 if (!isDBOpened() || filterName.isEmpty())
698 int filterNameId = -1;
699 m_query->prepare(
"SELECT Id FROM FilterNameTable WHERE Name=?"_L1);
700 m_query->bindValue(0, filterName);
703 filterNameId = m_query->value(0).toInt();
705 if (filterNameId < 0) {
706 emit error(tr(
"Unknown filter \"%1\".").arg(filterName));
710 m_query->prepare(
"DELETE FROM FilterTable WHERE NameId=?"_L1);
711 m_query->bindValue(0, filterNameId);
714 m_query->prepare(
"DELETE FROM FilterNameTable WHERE Id=?"_L1);
715 m_query->bindValue(0, filterNameId);
722 const QStringList &attributes)
724 if (!isDBOpened() || filterName.isEmpty())
728 m_query->prepare(
"SELECT Id FROM FilterNameTable WHERE Name=?"_L1);
729 m_query->bindValue(0, filterName);
732 nameId = m_query->value(0).toInt();
734 m_query->exec(
"SELECT Id, Name FROM FilterAttributeTable"_L1);
735 QStringList idsToInsert = attributes;
736 QMap<QString,
int> attributeMap;
737 while (m_query->next()) {
739 const QString attributeName = m_query->value(1).toString();
740 attributeMap.insert(attributeName, m_query->value(0).toInt());
741 idsToInsert.removeAll(attributeName);
744 for (
const QString &id : std::as_const(idsToInsert)) {
745 m_query->prepare(
"INSERT INTO FilterAttributeTable VALUES(NULL, ?)"_L1);
746 m_query->bindValue(0, id);
748 attributeMap.insert(id, m_query->lastInsertId().toInt());
752 m_query->prepare(
"INSERT INTO FilterNameTable VALUES(NULL, ?)"_L1);
753 m_query->bindValue(0, filterName);
755 nameId = m_query->lastInsertId().toInt();
759 emit error(tr(
"Cannot register filter %1.").arg(filterName));
763 m_query->prepare(
"DELETE FROM FilterTable WHERE NameId=?"_L1);
764 m_query->bindValue(0, nameId);
767 for (
const QString &att : attributes) {
768 m_query->prepare(
"INSERT INTO FilterTable VALUES(?, ?)"_L1);
769 m_query->bindValue(0, nameId);
770 m_query->bindValue(1, attributeMap[att]);
771 if (!m_query->exec())
778 const QString &namespaceName)
const
787 "NamespaceTable.Name, "
788 "NamespaceTable.FilePath, "
793 "WHERE NamespaceTable.Id = FolderTable.NamespaceId "
794 "AND NamespaceTable.Name = ? LIMIT 1"_L1);
795 m_query->bindValue(0, namespaceName);
796 if (!m_query->exec() || !m_query->next())
799 fileInfo.namespaceName = m_query->value(0).toString();
800 fileInfo.fileName = m_query->value(1).toString();
801 fileInfo.folderName = m_query->value(2).toString();
816 "NamespaceTable.Name, "
817 "NamespaceTable.FilePath, "
822 "WHERE NamespaceTable.Id = FolderTable.NamespaceId"_L1);
824 while (m_query->next()) {
826 fileInfo.namespaceName = m_query->value(0).toString();
827 fileInfo.fileName = m_query->value(1).toString();
828 fileInfo.folderName = m_query->value(2).toString();
829 list.append(fileInfo);
840 QHelpDBReader reader(fileName, QHelpGlobal::uniquifyConnectionName(
841 "QHelpCollectionHandler"_L1,
this),
nullptr);
843 emit error(tr(
"Cannot open documentation file %1.").arg(fileName));
847 const QString &ns = reader.namespaceName();
849 emit error(tr(
"Invalid documentation file \"%1\".").arg(fileName));
853 const int nsId = registerNamespace(ns, fileName);
857 const int vfId = registerVirtualFolder(reader.virtualFolder(), nsId);
861 registerVersion(reader.version(), nsId);
862 registerFilterAttributes(reader.filterAttributeSets(), nsId);
863 for (
const QString &filterName : reader.customFilters())
864 addCustomFilter(filterName, reader.filterAttributes(filterName));
866 if (!registerIndexTable(reader.indexTable(), nsId, vfId, registeredDocumentation(ns).fileName))
877 m_query->prepare(
"SELECT Id FROM NamespaceTable WHERE Name = ?"_L1);
878 m_query->bindValue(0, namespaceName);
881 if (!m_query->next()) {
882 emit error(tr(
"The namespace %1 was not registered.").arg(namespaceName));
886 const int nsId = m_query->value(0).toInt();
888 m_query->prepare(
"DELETE FROM NamespaceTable WHERE Id = ?"_L1);
889 m_query->bindValue(0, nsId);
890 if (!m_query->exec())
893 m_query->prepare(
"SELECT Id FROM FolderTable WHERE NamespaceId = ?"_L1);
894 m_query->bindValue(0, nsId);
897 if (!m_query->next()) {
898 emit error(tr(
"The namespace %1 was not registered.").arg(namespaceName));
902 const int vfId = m_query->value(0).toInt();
904 m_query->prepare(
"DELETE FROM NamespaceTable WHERE Id = ?"_L1);
905 m_query->bindValue(0, nsId);
906 if (!m_query->exec())
909 m_query->prepare(
"DELETE FROM FolderTable WHERE NamespaceId = ?"_L1);
910 m_query->bindValue(0, nsId);
911 if (!m_query->exec())
914 if (!unregisterIndexTable(nsId, vfId))
924 QHelpCollectionHandler::FileInfo fileInfo;
926 if (!url.isValid() || url.toString().count(u'/') < 4
927 || url.scheme() !=
"qthelp"_L1) {
931 fileInfo.namespaceName = url.authority();
932 fileInfo.fileName = url.path();
933 if (fileInfo.fileName.startsWith(u'/'))
934 fileInfo.fileName = fileInfo.fileName.mid(1);
935 fileInfo.folderName = fileInfo.fileName.mid(0, fileInfo.fileName.indexOf(u'/', 1));
936 fileInfo.fileName.remove(0, fileInfo.folderName.size() + 1);
946 const FileInfo fileInfo = extractFileInfo(url);
947 if (fileInfo.namespaceName.isEmpty())
951 "SELECT COUNT (DISTINCT NamespaceTable.Id) "
956 "WHERE FolderTable.Name = ? "
957 "AND FileNameTable.Name = ? "
958 "AND FileNameTable.FolderId = FolderTable.Id "
959 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1);
960 m_query->bindValue(0, fileInfo.folderName);
961 m_query->bindValue(1, fileInfo.fileName);
962 if (!m_query->exec() || !m_query->next())
965 const int count = m_query->value(0).toInt();
973 if (filterName.isEmpty())
976 return " AND EXISTS(SELECT * FROM Filter WHERE Filter.Name = ?) "
982 "WHERE ComponentFilter.FilterId = Filter.FilterId "
983 "AND Filter.Name = ?) "
984 "OR NamespaceTable.Id IN ("
993 "WHERE ComponentMapping.NamespaceId = NamespaceTable.Id "
994 "AND ComponentTable.ComponentId = ComponentMapping.ComponentId "
995 "AND ((ComponentTable.Name = ComponentFilter.ComponentName) "
996 "OR (ComponentTable.Name IS NULL AND ComponentFilter.ComponentName IS NULL)) "
997 "AND ComponentFilter.FilterId = Filter.FilterId "
998 "AND Filter.Name = ?))"
1004 "WHERE VersionFilter.FilterId = Filter.FilterId "
1005 "AND Filter.Name = ?) "
1006 "OR NamespaceTable.Id IN ("
1008 "NamespaceTable.Id "
1014 "WHERE VersionFilter.FilterId = Filter.FilterId "
1015 "AND ((VersionFilter.Version = VersionTable.Version) "
1016 "OR (VersionFilter.Version IS NULL AND VersionTable.Version IS NULL)) "
1017 "AND VersionTable.NamespaceId = NamespaceTable.Id "
1018 "AND Filter.Name = ?))"
1024 if (filterName.isEmpty())
1027 query->bindValue(bindStart, filterName);
1028 query->bindValue(bindStart + 1, filterName);
1029 query->bindValue(bindStart + 2, filterName);
1030 query->bindValue(bindStart + 3, filterName);
1031 query->bindValue(bindStart + 4, filterName);
1035 const QString &idTableName,
1036 const QString &idColumnName,
1037 const QString &filterTableName,
1038 const QString &filterColumnName)
1040 if (!attributesCount)
1043 QString filterQuery =
" AND (%1.%2 IN ("_L1.arg(idTableName, idColumnName);
1045 const QString filterQueryTemplate =
1047 "FROM %1, FilterAttributeTable "
1048 "WHERE %1.FilterAttributeId = FilterAttributeTable.Id "
1049 "AND FilterAttributeTable.Name = ?"_L1.arg(filterTableName, filterColumnName);
1051 for (
int i = 0; i < attributesCount; ++i) {
1053 filterQuery.append(
" INTERSECT "_L1);
1054 filterQuery.append(filterQueryTemplate);
1057 filterQuery.append(
") OR NamespaceTable.Id IN ("_L1);
1059 const QString optimizedFilterQueryTemplate =
1060 "SELECT OptimizedFilterTable.NamespaceId "
1061 "FROM OptimizedFilterTable, FilterAttributeTable "
1062 "WHERE OptimizedFilterTable.FilterAttributeId = FilterAttributeTable.Id "
1063 "AND FilterAttributeTable.Name = ?"_L1;
1065 for (
int i = 0; i < attributesCount; ++i) {
1067 filterQuery.append(
" INTERSECT "_L1);
1068 filterQuery.append(optimizedFilterQueryTemplate);
1071 filterQuery.append(
"))"_L1);
1076static void bindFilterQuery(QSqlQuery *query,
int startingBindPos,
const QStringList &filterAttributes)
1078 for (
int i = 0; i < 2; ++i) {
1079 for (
int j = 0; j < filterAttributes.size(); j++) {
1080 query->bindValue(i * filterAttributes.size() + j + startingBindPos,
1081 filterAttributes.at(j));
1087 const QStringList &filterAttributes)
const
1092 const FileInfo fileInfo = extractFileInfo(url);
1093 if (fileInfo.namespaceName.isEmpty())
1096 const QString filterlessQuery =
1098 "NamespaceTable.Name "
1103 "WHERE FolderTable.Name = ? "
1104 "AND FileNameTable.Name = ? "
1105 "AND FileNameTable.FolderId = FolderTable.Id "
1106 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1;
1108 const QString filterQuery = filterlessQuery
1109 + prepareFilterQuery(filterAttributes.size(),
"FileNameTable"_L1,
"FileId"_L1,
1110 "FileFilterTable"_L1,
"FileId"_L1);
1112 m_query->prepare(filterQuery);
1113 m_query->bindValue(0, fileInfo.folderName);
1114 m_query->bindValue(1, fileInfo.fileName);
1115 bindFilterQuery(m_query.get(), 2, filterAttributes);
1117 if (!m_query->exec())
1120 QStringList namespaceList;
1121 while (m_query->next())
1122 namespaceList.append(m_query->value(0).toString());
1124 if (namespaceList.isEmpty())
1127 if (namespaceList.contains(fileInfo.namespaceName))
1128 return fileInfo.namespaceName;
1130 const QString originalVersion = namespaceVersion(fileInfo.namespaceName);
1132 for (
const QString &ns : namespaceList) {
1133 const QString nsVersion = namespaceVersion(ns);
1134 if (originalVersion == nsVersion)
1139 return namespaceList.first();
1143 const QString &filterName)
const
1148 const FileInfo fileInfo = extractFileInfo(url);
1149 if (fileInfo.namespaceName.isEmpty())
1152 const QString filterlessQuery =
1154 "NamespaceTable.Name "
1159 "WHERE FolderTable.Name = ? "
1160 "AND FileNameTable.Name = ? "
1161 "AND FileNameTable.FolderId = FolderTable.Id "
1162 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1;
1164 const QString filterQuery = filterlessQuery
1165 + prepareFilterQuery(filterName);
1167 m_query->prepare(filterQuery);
1168 m_query->bindValue(0, fileInfo.folderName);
1169 m_query->bindValue(1, fileInfo.fileName);
1170 bindFilterQuery(m_query.get(), 2, filterName);
1172 if (!m_query->exec())
1175 QStringList namespaceList;
1176 while (m_query->next())
1177 namespaceList.append(m_query->value(0).toString());
1179 if (namespaceList.isEmpty())
1182 if (namespaceList.contains(fileInfo.namespaceName))
1183 return fileInfo.namespaceName;
1185 const QString originalVersion = namespaceVersion(fileInfo.namespaceName);
1187 for (
const QString &ns : namespaceList) {
1188 const QString nsVersion = namespaceVersion(ns);
1189 if (originalVersion == nsVersion)
1194 return namespaceList.first();
1198 const QStringList &filterAttributes,
1199 const QString &extensionFilter)
const
1204 const QString extensionQuery = extensionFilter.isEmpty()
1205 ? QString() :
" AND FileNameTable.Name LIKE ?"_L1;
1206 const QString filterlessQuery =
1208 "FolderTable.Name, "
1209 "FileNameTable.Name "
1214 "WHERE FileNameTable.FolderId = FolderTable.Id "
1215 "AND FolderTable.NamespaceId = NamespaceTable.Id "
1216 "AND NamespaceTable.Name = ?"_L1 + extensionQuery;
1218 const QString filterQuery = filterlessQuery
1219 + prepareFilterQuery(filterAttributes.size(),
"FileNameTable"_L1,
"FileId"_L1,
1220 "FileFilterTable"_L1,
"FileId"_L1);
1222 m_query->prepare(filterQuery);
1223 m_query->bindValue(0, namespaceName);
1225 if (!extensionFilter.isEmpty()) {
1226 m_query->bindValue(bindCount,
"%.%1"_L1.arg(extensionFilter));
1229 bindFilterQuery(m_query.get(), bindCount, filterAttributes);
1231 if (!m_query->exec())
1234 QStringList fileNames;
1235 while (m_query->next())
1236 fileNames.append(m_query->value(0).toString() + u'/' + m_query->value(1).toString());
1241 const QString &filterName,
1242 const QString &extensionFilter)
const
1247 const QString extensionQuery = extensionFilter.isEmpty()
1248 ? QString() :
" AND FileNameTable.Name LIKE ?"_L1;
1249 const QString filterlessQuery =
1251 "FolderTable.Name, "
1252 "FileNameTable.Name "
1257 "WHERE FileNameTable.FolderId = FolderTable.Id "
1258 "AND FolderTable.NamespaceId = NamespaceTable.Id "
1259 "AND NamespaceTable.Name = ?"_L1 + extensionQuery;
1261 const QString filterQuery = filterlessQuery
1262 + prepareFilterQuery(filterName);
1264 m_query->prepare(filterQuery);
1265 m_query->bindValue(0, namespaceName);
1267 if (!extensionFilter.isEmpty()) {
1268 m_query->bindValue(bindCount,
"%.%1"_L1.arg(extensionFilter));
1272 bindFilterQuery(m_query.get(), bindCount, filterName);
1274 if (!m_query->exec())
1277 QStringList fileNames;
1278 while (m_query->next())
1279 fileNames.append(m_query->value(0).toString() + u'/' + m_query->value(1).toString());
1288 const QString namespaceName = namespaceForFile(url, filterAttributes);
1289 if (namespaceName.isEmpty())
1293 result.setAuthority(namespaceName);
1302 const QString namespaceName = namespaceForFile(url, filterName);
1303 if (namespaceName.isEmpty())
1307 result.setAuthority(namespaceName);
1316 const QString namespaceName = namespaceForFile(url, QString());
1317 if (namespaceName.isEmpty())
1320 const FileInfo fileInfo = extractFileInfo(url);
1322 const FileInfo docInfo = registeredDocumentation(namespaceName);
1323 const QString absFileName = absoluteDocPath(docInfo.fileName);
1325 QHelpDBReader reader(absFileName, QHelpGlobal::uniquifyConnectionName(
1326 docInfo.fileName,
const_cast<QHelpCollectionHandler *>(
this)),
nullptr);
1330 return reader.fileData(fileInfo.folderName, fileInfo.fileName);
1335 QStringList indices;
1340 const QString filterlessQuery =
1348 "WHERE IndexTable.FileId = FileNameTable.FileId "
1349 "AND FileNameTable.FolderId = FolderTable.Id "
1350 "AND IndexTable.NamespaceId = NamespaceTable.Id"_L1;
1352 const QString filterQuery = filterlessQuery
1353 + prepareFilterQuery(filterAttributes.size(),
"IndexTable"_L1,
"Id"_L1,
1354 "IndexFilterTable"_L1,
"IndexId"_L1)
1355 +
" ORDER BY LOWER(IndexTable.Name), IndexTable.Name"_L1;
1358 m_query->prepare(filterQuery);
1359 bindFilterQuery(m_query.get(), 0, filterAttributes);
1363 while (m_query->next())
1364 indices.append(m_query->value(0).toString());
1371 QStringList indices;
1376 const QString filterlessQuery =
1384 "WHERE IndexTable.FileId = FileNameTable.FileId "
1385 "AND FileNameTable.FolderId = FolderTable.Id "
1386 "AND IndexTable.NamespaceId = NamespaceTable.Id"_L1;
1388 const QString filterQuery = filterlessQuery
1389 + prepareFilterQuery(filterName)
1390 +
" ORDER BY LOWER(IndexTable.Name), IndexTable.Name"_L1;
1392 m_query->prepare(filterQuery);
1393 bindFilterQuery(m_query.get(), 0, filterName);
1397 while (m_query->next())
1398 indices.append(m_query->value(0).toString());
1405 if (!contents.size())
1412 QDataStream s(contents);
1421 const QStringList &filterAttributes)
const
1426 const QString filterlessQuery =
1428 "NamespaceTable.Name, "
1429 "FolderTable.Name, "
1430 "ContentsTable.Data, "
1431 "VersionTable.Version "
1437 "WHERE ContentsTable.NamespaceId = NamespaceTable.Id "
1438 "AND NamespaceTable.Id = FolderTable.NamespaceId "
1439 "AND ContentsTable.NamespaceId = NamespaceTable.Id "
1440 "AND VersionTable.NamespaceId = NamespaceTable.Id"_L1;
1442 const QString filterQuery = filterlessQuery
1443 + prepareFilterQuery(filterAttributes.size(),
"ContentsTable"_L1,
"Id"_L1,
1444 "ContentsFilterTable"_L1,
"ContentsId"_L1);
1446 m_query->prepare(filterQuery);
1447 bindFilterQuery(m_query.get(), 0, filterAttributes);
1451 QMap<QString, QMap<QVersionNumber, ContentsData>> contentsMap;
1453 while (m_query->next()) {
1454 const QString namespaceName = m_query->value(0).toString();
1455 const QByteArray contents = m_query->value(2).toByteArray();
1456 const QString versionString = m_query->value(3).toString();
1458 const QString title = getTitle(contents);
1459 const QVersionNumber version = QVersionNumber::fromString(versionString);
1461 ContentsData &contentsData = contentsMap[title][version];
1462 contentsData.namespaceName = namespaceName;
1463 contentsData.folderName = m_query->value(1).toString();
1464 contentsData.contentsList.append(contents);
1467 QList<QHelpCollectionHandler::ContentsData> result;
1468 for (
const auto &versionContents : std::as_const(contentsMap)) {
1470 const auto itBegin = versionContents.constBegin();
1471 auto it = versionContents.constEnd();
1472 while (it != itBegin) {
1474 result.append(it.value());
1485 const QString filterlessQuery =
1487 "NamespaceTable.Name, "
1488 "FolderTable.Name, "
1489 "ContentsTable.Data, "
1490 "VersionTable.Version "
1496 "WHERE ContentsTable.NamespaceId = NamespaceTable.Id "
1497 "AND NamespaceTable.Id = FolderTable.NamespaceId "
1498 "AND ContentsTable.NamespaceId = NamespaceTable.Id "
1499 "AND VersionTable.NamespaceId = NamespaceTable.Id"_L1;
1501 const QString filterQuery = filterlessQuery + prepareFilterQuery(filterName);
1503 m_query->prepare(filterQuery);
1504 bindFilterQuery(m_query.get(), 0, filterName);
1508 QMap<QString, QMap<QVersionNumber, ContentsData>> contentsMap;
1510 while (m_query->next()) {
1511 const QString namespaceName = m_query->value(0).toString();
1512 const QByteArray contents = m_query->value(2).toByteArray();
1513 const QString versionString = m_query->value(3).toString();
1515 const QString title = getTitle(contents);
1516 const QVersionNumber version = QVersionNumber::fromString(versionString);
1518 ContentsData &contentsData = contentsMap[title][version];
1519 contentsData.namespaceName = namespaceName;
1520 contentsData.folderName = m_query->value(1).toString();
1521 contentsData.contentsList.append(contents);
1524 QList<QHelpCollectionHandler::ContentsData> result;
1525 for (
const auto &versionContents : std::as_const(contentsMap)) {
1527 const auto itBegin = versionContents.constBegin();
1528 auto it = versionContents.constEnd();
1529 while (it != itBegin) {
1531 result.append(it.value());
1542 m_query->prepare(
"DELETE FROM SettingsTable WHERE Key=?"_L1);
1543 m_query->bindValue(0, key);
1544 return m_query->exec();
1548 const QVariant &defaultValue)
const
1551 return defaultValue;
1553 m_query->prepare(
"SELECT COUNT(Key) FROM SettingsTable WHERE Key=?"_L1);
1554 m_query->bindValue(0, key);
1555 if (!m_query->exec() || !m_query->next() || !m_query->value(0).toInt()) {
1557 return defaultValue;
1561 m_query->prepare(
"SELECT Value FROM SettingsTable WHERE Key=?"_L1);
1562 m_query->bindValue(0, key);
1563 if (m_query->exec() && m_query->next()) {
1564 const QVariant &value = m_query->value(0);
1568 return defaultValue;
1572 const QVariant &value)
1577 m_query->prepare(
"SELECT Value FROM SettingsTable WHERE Key=?"_L1);
1578 m_query->bindValue(0, key);
1580 if (m_query->next()) {
1581 m_query->prepare(
"UPDATE SettingsTable SET Value=? where Key=?"_L1);
1582 m_query->bindValue(0, value);
1583 m_query->bindValue(1, key);
1585 m_query->prepare(
"INSERT INTO SettingsTable VALUES(?, ?)"_L1);
1586 m_query->bindValue(0, key);
1587 m_query->bindValue(1, value);
1589 return m_query->exec();
1598 m_query->exec(
"SELECT Name FROM FilterAttributeTable"_L1);
1600 while (m_query->next())
1601 atts.insert(m_query->value(0).toString());
1603 for (
const QStringList &attributeSet : attributeSets) {
1604 for (
const QString &attribute : attributeSet) {
1605 if (!atts.contains(attribute)) {
1606 m_query->prepare(
"INSERT INTO FilterAttributeTable VALUES(NULL, ?)"_L1);
1607 m_query->bindValue(0, attribute);
1612 return registerFileAttributeSets(attributeSets, nsId);
1621 if (attributeSets.isEmpty())
1625 QVariantList attributeSetIds;
1626 QVariantList filterAttributeIds;
1628 if (!m_query->exec(
"SELECT MAX(FilterAttributeSetId) FROM FileAttributeSetTable"_L1)
1629 || !m_query->next()) {
1633 int attributeSetId = m_query->value(0).toInt();
1635 for (
const QStringList &attributeSet : attributeSets) {
1638 for (
const QString &attribute : attributeSet) {
1639 m_query->prepare(
"SELECT Id FROM FilterAttributeTable WHERE Name=?"_L1);
1640 m_query->bindValue(0, attribute);
1642 if (!m_query->exec() || !m_query->next())
1646 attributeSetIds.append(attributeSetId);
1647 filterAttributeIds.append(m_query->value(0).toInt());
1651 m_query->prepare(
"INSERT INTO FileAttributeSetTable "
1652 "(NamespaceId, FilterAttributeSetId, FilterAttributeId) "
1653 "VALUES(?, ?, ?)"_L1);
1654 m_query->addBindValue(nsIds);
1655 m_query->addBindValue(attributeSetIds);
1656 m_query->addBindValue(filterAttributeIds);
1657 return m_query->execBatch();
1664 m_query->exec(
"SELECT Name FROM FilterAttributeTable"_L1);
1665 while (m_query->next())
1666 list.append(m_query->value(0).toString());
1677 "FilterAttributeTable.Name "
1679 "FilterAttributeTable, "
1682 "WHERE FilterAttributeTable.Id = FilterTable.FilterAttributeId "
1683 "AND FilterTable.NameId = FilterNameTable.Id "
1684 "AND FilterNameTable.Name=?"_L1);
1685 m_query->bindValue(0, filterName);
1687 while (m_query->next())
1688 list.append(m_query->value(0).toString());
1700 "FileAttributeSetTable.FilterAttributeSetId, "
1701 "FilterAttributeTable.Name "
1703 "FileAttributeSetTable, "
1704 "FilterAttributeTable, "
1706 "WHERE FileAttributeSetTable.FilterAttributeId = FilterAttributeTable.Id "
1707 "AND FileAttributeSetTable.NamespaceId = NamespaceTable.Id "
1708 "AND NamespaceTable.Name = ? "
1709 "ORDER BY FileAttributeSetTable.FilterAttributeSetId"_L1);
1710 m_query->bindValue(0, namespaceName);
1713 QList<QStringList> result;
1714 while (m_query->next()) {
1715 const int id = m_query->value(0).toInt();
1717 result.append(QStringList());
1720 result.last().append(m_query->value(1).toString());
1723 if (result.isEmpty())
1724 result.append(QStringList());
1735 "VersionTable.Version "
1739 "WHERE NamespaceTable.Name = ? "
1740 "AND NamespaceTable.Id = VersionTable.NamespaceId"_L1);
1741 m_query->bindValue(0, namespaceName);
1742 if (!m_query->exec() || !m_query->next())
1745 const QString ret = m_query->value(0).toString();
1752 const int errorValue = -1;
1756 m_query->prepare(
"SELECT COUNT(Id) FROM NamespaceTable WHERE Name=?"_L1);
1757 m_query->bindValue(0, nspace);
1759 while (m_query->next()) {
1760 if (m_query->value(0).toInt() > 0) {
1761 emit error(tr(
"Namespace %1 already exists.").arg(nspace));
1766 QFileInfo fi(m_collectionFile);
1767 m_query->prepare(
"INSERT INTO NamespaceTable VALUES(NULL, ?, ?)"_L1);
1768 m_query->bindValue(0, nspace);
1769 m_query->bindValue(1, fi.absoluteDir().relativeFilePath(fileName));
1770 int namespaceId = errorValue;
1771 if (m_query->exec()) {
1772 namespaceId = m_query->lastInsertId().toInt();
1775 if (namespaceId < 1) {
1776 emit error(tr(
"Cannot register namespace \"%1\".").arg(nspace));
1787 m_query->prepare(
"INSERT INTO FolderTable VALUES(NULL, ?, ?)"_L1);
1788 m_query->bindValue(0, namespaceId);
1789 m_query->bindValue(1, folderName);
1792 if (m_query->exec()) {
1793 virtualId = m_query->lastInsertId().toInt();
1796 if (virtualId < 1) {
1797 emit error(tr(
"Cannot register virtual folder '%1'.").arg(folderName));
1800 if (registerComponent(folderName, namespaceId) < 0)
1807 m_query->prepare(
"SELECT ComponentId FROM ComponentTable WHERE Name = ?"_L1);
1808 m_query->bindValue(0, componentName);
1809 if (!m_query->exec())
1812 if (!m_query->next()) {
1813 m_query->prepare(
"INSERT INTO ComponentTable VALUES(NULL, ?)"_L1);
1814 m_query->bindValue(0, componentName);
1815 if (!m_query->exec())
1818 m_query->prepare(
"SELECT ComponentId FROM ComponentTable WHERE Name = ?"_L1);
1819 m_query->bindValue(0, componentName);
1820 if (!m_query->exec() || !m_query->next())
1824 const int componentId = m_query->value(0).toInt();
1826 m_query->prepare(
"INSERT INTO ComponentMapping VALUES(?, ?)"_L1);
1827 m_query->bindValue(0, componentId);
1828 m_query->bindValue(1, namespaceId);
1829 if (!m_query->exec())
1840 m_query->prepare(
"INSERT INTO VersionTable (NamespaceId, Version) VALUES(?, ?)"_L1);
1841 m_query->addBindValue(namespaceId);
1842 m_query->addBindValue(version);
1843 return m_query->exec();
1847 const QString &nameSpace,
bool createDefaultVersionFilter)
1852 m_query->prepare(
"SELECT Id, FilePath FROM NamespaceTable WHERE Name=?"_L1);
1853 m_query->bindValue(0, nameSpace);
1855 if (!m_query->next())
1858 const int nsId = m_query->value(0).toInt();
1859 const QString fileName = m_query->value(1).toString();
1861 m_query->prepare(
"SELECT Id, Name FROM FolderTable WHERE NamespaceId=?"_L1);
1862 m_query->bindValue(0, nsId);
1864 if (!m_query->next())
1867 const int vfId = m_query->value(0).toInt();
1868 const QString vfName = m_query->value(1).toString();
1870 const QString absFileName = absoluteDocPath(fileName);
1871 QHelpDBReader reader(absFileName, QHelpGlobal::uniquifyConnectionName(
1872 fileName,
this),
this);
1876 registerComponent(vfName, nsId);
1877 registerVersion(reader.version(), nsId);
1878 if (!registerFileAttributeSets(reader.filterAttributeSets(), nsId))
1881 if (!registerIndexTable(reader.indexTable(), nsId, vfId, fileName))
1884 if (createDefaultVersionFilter)
1885 createVersionFilter(reader.version());
1891 if (version.isEmpty())
1894 const QVersionNumber versionNumber = QVersionNumber::fromString(version);
1895 if (versionNumber.isNull())
1898 const QString filterName = tr(
"Version %1").arg(version);
1899 if (filters().contains(filterName))
1902 QHelpFilterData filterData;
1903 filterData.setVersions({versionNumber});
1904 setFilterData(filterName, filterData);
1908 int nsId,
int vfId,
const QString &fileName)
1912 QMap<QString, QVariantList> filterAttributeToNewFileId;
1914 QVariantList fileFolderIds;
1915 QVariantList fileNames;
1916 QVariantList fileTitles;
1917 const int fileSize = indexTable.fileItems.size();
1918 fileFolderIds.reserve(fileSize);
1919 fileNames.reserve(fileSize);
1920 fileTitles.reserve(fileSize);
1922 if (!m_query->exec(
"SELECT MAX(FileId) FROM FileNameTable"_L1) || !m_query->next())
1925 const int maxFileId = m_query->value(0).toInt();
1928 for (
const QHelpDBReader::FileItem &item : indexTable.fileItems) {
1929 fileFolderIds.append(vfId);
1930 fileNames.append(item.name);
1931 fileTitles.append(item.title);
1933 for (
const QString &filterAttribute : item.filterAttributes)
1934 filterAttributeToNewFileId[filterAttribute].append(maxFileId + newFileId + 1);
1938 m_query->prepare(
"INSERT INTO FileNameTable VALUES(?, ?, NULL, ?)"_L1);
1939 m_query->addBindValue(fileFolderIds);
1940 m_query->addBindValue(fileNames);
1941 m_query->addBindValue(fileTitles);
1942 if (!m_query->execBatch())
1945 for (
auto it = filterAttributeToNewFileId.cbegin(),
1946 end = filterAttributeToNewFileId.cend(); it != end; ++it) {
1947 const QString filterAttribute = it.key();
1948 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
1949 m_query->bindValue(0, filterAttribute);
1950 if (!m_query->exec() || !m_query->next())
1953 const int attributeId = m_query->value(0).toInt();
1955 QVariantList attributeIds;
1956 for (
int i = 0; i < it.value().size(); i++)
1957 attributeIds.append(attributeId);
1959 m_query->prepare(
"INSERT INTO FileFilterTable VALUES(?, ?)"_L1);
1960 m_query->addBindValue(attributeIds);
1961 m_query->addBindValue(it.value());
1962 if (!m_query->execBatch())
1966 QMap<QString, QVariantList> filterAttributeToNewIndexId;
1968 if (!m_query->exec(
"SELECT MAX(Id) FROM IndexTable"_L1) || !m_query->next())
1971 const int maxIndexId = m_query->value(0).toInt();
1974 QVariantList indexNames;
1975 QVariantList indexIdentifiers;
1976 QVariantList indexNamespaceIds;
1977 QVariantList indexFileIds;
1978 QVariantList indexAnchors;
1979 const int indexSize = indexTable.indexItems.size();
1980 indexNames.reserve(indexSize);
1981 indexIdentifiers.reserve(indexSize);
1982 indexNamespaceIds.reserve(indexSize);
1983 indexFileIds.reserve(indexSize);
1984 indexAnchors.reserve(indexSize);
1986 for (
const QHelpDBReader::IndexItem &item : indexTable.indexItems) {
1987 indexNames.append(item.name);
1988 indexIdentifiers.append(item.identifier);
1989 indexNamespaceIds.append(nsId);
1990 indexFileIds.append(maxFileId + item.fileId + 1);
1991 indexAnchors.append(item.anchor);
1993 for (
const QString &filterAttribute : item.filterAttributes)
1994 filterAttributeToNewIndexId[filterAttribute].append(maxIndexId + newIndexId + 1);
1998 m_query->prepare(
"INSERT INTO IndexTable VALUES(NULL, ?, ?, ?, ?, ?)"_L1);
1999 m_query->addBindValue(indexNames);
2000 m_query->addBindValue(indexIdentifiers);
2001 m_query->addBindValue(indexNamespaceIds);
2002 m_query->addBindValue(indexFileIds);
2003 m_query->addBindValue(indexAnchors);
2004 if (!m_query->execBatch())
2007 for (
auto it = filterAttributeToNewIndexId.cbegin(),
2008 end = filterAttributeToNewIndexId.cend(); it != end; ++it) {
2009 const QString filterAttribute = it.key();
2010 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2011 m_query->bindValue(0, filterAttribute);
2012 if (!m_query->exec() || !m_query->next())
2015 const int attributeId = m_query->value(0).toInt();
2017 QVariantList attributeIds;
2018 for (
int i = 0; i < it.value().size(); i++)
2019 attributeIds.append(attributeId);
2021 m_query->prepare(
"INSERT INTO IndexFilterTable VALUES(?, ?)"_L1);
2022 m_query->addBindValue(attributeIds);
2023 m_query->addBindValue(it.value());
2024 if (!m_query->execBatch())
2028 QMap<QString, QVariantList> filterAttributeToNewContentsId;
2030 QVariantList contentsNsIds;
2031 QVariantList contentsData;
2032 const int contentsSize = indexTable.contentsItems.size();
2033 contentsNsIds.reserve(contentsSize);
2034 contentsData.reserve(contentsSize);
2036 if (!m_query->exec(
"SELECT MAX(Id) FROM ContentsTable"_L1) || !m_query->next())
2039 const int maxContentsId = m_query->value(0).toInt();
2041 int newContentsId = 0;
2042 for (
const QHelpDBReader::ContentsItem &item : indexTable.contentsItems) {
2043 contentsNsIds.append(nsId);
2044 contentsData.append(item.data);
2046 for (
const QString &filterAttribute : item.filterAttributes) {
2047 filterAttributeToNewContentsId[filterAttribute]
2048 .append(maxContentsId + newContentsId + 1);
2053 m_query->prepare(
"INSERT INTO ContentsTable VALUES(NULL, ?, ?)"_L1);
2054 m_query->addBindValue(contentsNsIds);
2055 m_query->addBindValue(contentsData);
2056 if (!m_query->execBatch())
2059 for (
auto it = filterAttributeToNewContentsId.cbegin(),
2060 end = filterAttributeToNewContentsId.cend(); it != end; ++it) {
2061 const QString filterAttribute = it.key();
2062 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2063 m_query->bindValue(0, filterAttribute);
2064 if (!m_query->exec() || !m_query->next())
2067 const int attributeId = m_query->value(0).toInt();
2069 QVariantList attributeIds;
2070 for (
int i = 0; i < it.value().size(); i++)
2071 attributeIds.append(attributeId);
2073 m_query->prepare(
"INSERT INTO ContentsFilterTable VALUES(?, ?)"_L1);
2074 m_query->addBindValue(attributeIds);
2075 m_query->addBindValue(it.value());
2076 if (!m_query->execBatch())
2080 QVariantList filterNsIds;
2081 QVariantList filterAttributeIds;
2082 for (
const QString &filterAttribute : indexTable.usedFilterAttributes) {
2083 filterNsIds.append(nsId);
2085 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2086 m_query->bindValue(0, filterAttribute);
2087 if (!m_query->exec() || !m_query->next())
2090 filterAttributeIds.append(m_query->value(0).toInt());
2093 m_query->prepare(
"INSERT INTO OptimizedFilterTable "
2094 "(NamespaceId, FilterAttributeId) VALUES(?, ?)"_L1);
2095 m_query->addBindValue(filterNsIds);
2096 m_query->addBindValue(filterAttributeIds);
2097 if (!m_query->execBatch())
2100 m_query->prepare(
"INSERT INTO TimeStampTable "
2101 "(NamespaceId, FolderId, FilePath, Size, TimeStamp) "
2102 "VALUES(?, ?, ?, ?, ?)"_L1);
2103 m_query->addBindValue(nsId);
2104 m_query->addBindValue(vfId);
2105 m_query->addBindValue(fileName);
2106 const QFileInfo fi(absoluteDocPath(fileName));
2107 m_query->addBindValue(fi.size());
2108 QDateTime lastModified = fi.lastModified(QTimeZone::UTC);
2109 if (qEnvironmentVariableIsSet(
"SOURCE_DATE_EPOCH")) {
2110 const QString sourceDateEpochStr = qEnvironmentVariable(
"SOURCE_DATE_EPOCH");
2112 const qlonglong sourceDateEpoch = sourceDateEpochStr.toLongLong(&ok);
2113 if (ok && sourceDateEpoch < lastModified.toSecsSinceEpoch())
2114 lastModified.setSecsSinceEpoch(sourceDateEpoch);
2116 m_query->addBindValue(lastModified);
2117 if (!m_query->exec())
2126 m_query->prepare(
"DELETE FROM IndexFilterTable WHERE IndexId IN "
2127 "(SELECT Id FROM IndexTable WHERE NamespaceId = ?)"_L1);
2128 m_query->bindValue(0, nsId);
2129 if (!m_query->exec())
2132 m_query->prepare(
"DELETE FROM IndexTable WHERE NamespaceId = ?"_L1);
2133 m_query->bindValue(0, nsId);
2134 if (!m_query->exec())
2137 m_query->prepare(
"DELETE FROM FileFilterTable WHERE FileId IN "
2138 "(SELECT FileId FROM FileNameTable WHERE FolderId = ?)"_L1);
2139 m_query->bindValue(0, vfId);
2140 if (!m_query->exec())
2143 m_query->prepare(
"DELETE FROM FileNameTable WHERE FolderId = ?"_L1);
2144 m_query->bindValue(0, vfId);
2145 if (!m_query->exec())
2148 m_query->prepare(
"DELETE FROM ContentsFilterTable WHERE ContentsId IN "
2149 "(SELECT Id FROM ContentsTable WHERE NamespaceId = ?)"_L1);
2150 m_query->bindValue(0, nsId);
2151 if (!m_query->exec())
2154 m_query->prepare(
"DELETE FROM ContentsTable WHERE NamespaceId = ?"_L1);
2155 m_query->bindValue(0, nsId);
2156 if (!m_query->exec())
2159 m_query->prepare(
"DELETE FROM FileAttributeSetTable WHERE NamespaceId = ?"_L1);
2160 m_query->bindValue(0, nsId);
2161 if (!m_query->exec())
2164 m_query->prepare(
"DELETE FROM OptimizedFilterTable WHERE NamespaceId = ?"_L1);
2165 m_query->bindValue(0, nsId);
2166 if (!m_query->exec())
2169 m_query->prepare(
"DELETE FROM TimeStampTable WHERE NamespaceId = ?"_L1);
2170 m_query->bindValue(0, nsId);
2171 if (!m_query->exec())
2174 m_query->prepare(
"DELETE FROM VersionTable WHERE NamespaceId = ?"_L1);
2175 m_query->bindValue(0, nsId);
2176 if (!m_query->exec())
2179 m_query->prepare(
"SELECT ComponentId FROM ComponentMapping WHERE NamespaceId = ?"_L1);
2180 m_query->bindValue(0, nsId);
2181 if (!m_query->exec())
2184 if (!m_query->next())
2187 const int componentId = m_query->value(0).toInt();
2189 m_query->prepare(
"DELETE FROM ComponentMapping WHERE NamespaceId = ?"_L1);
2190 m_query->bindValue(0, nsId);
2191 if (!m_query->exec())
2194 m_query->prepare(
"SELECT ComponentId FROM ComponentMapping WHERE ComponentId = ?"_L1);
2195 m_query->bindValue(0, componentId);
2196 if (!m_query->exec())
2199 if (!m_query->next()) {
2200 m_query->prepare(
"DELETE FROM ComponentTable WHERE ComponentId = ?"_L1);
2201 m_query->bindValue(0, componentId);
2202 if (!m_query->exec())
2210 const QString &relFileName,
const QString &anchor)
2213 url.setScheme(
"qthelp"_L1);
2214 url.setAuthority(ns);
2215 url.setPath(u'/' + folder + u'/' + relFileName);
2216 url.setFragment(anchor);
2221 const QString &id,
const QStringList &filterAttributes)
const
2223 return documentsForField(
"Identifier"_L1, id, filterAttributes);
2227 const QString &keyword,
const QStringList &filterAttributes)
const
2229 return documentsForField(
"Name"_L1, keyword, filterAttributes);
2233 const QString &fieldValue,
const QStringList &filterAttributes)
const
2238 const QString filterlessQuery =
2240 "FileNameTable.Title, "
2241 "NamespaceTable.Name, "
2242 "FolderTable.Name, "
2243 "FileNameTable.Name, "
2244 "IndexTable.Anchor "
2250 "WHERE IndexTable.FileId = FileNameTable.FileId "
2251 "AND FileNameTable.FolderId = FolderTable.Id "
2252 "AND IndexTable.NamespaceId = NamespaceTable.Id "
2253 "AND IndexTable.%1 = ?"_L1.arg(fieldName);
2255 const QString filterQuery = filterlessQuery
2256 + prepareFilterQuery(filterAttributes.size(),
"IndexTable"_L1,
"Id"_L1,
2257 "IndexFilterTable"_L1,
"IndexId"_L1);
2259 m_query->prepare(filterQuery);
2260 m_query->bindValue(0, fieldValue);
2261 bindFilterQuery(m_query.get(), 1, filterAttributes);
2265 QList<QHelpLink> docList;
2266 while (m_query->next()) {
2267 QString title = m_query->value(0).toString();
2268 if (title.isEmpty())
2269 title = fieldValue +
" : "_L1 + m_query->value(3).toString();
2271 const QUrl url = buildQUrl(m_query->value(1).toString(),
2272 m_query->value(2).toString(),
2273 m_query->value(3).toString(),
2274 m_query->value(4).toString());
2275 docList.append(QHelpLink {url, title});
2281 const QString &id,
const QString &filterName)
const
2283 return documentsForField(
"Identifier"_L1, id, filterName);
2287 const QString &keyword,
const QString &filterName)
const
2289 return documentsForField(
"Name"_L1, keyword, filterName);
2293 const QString &fieldValue,
const QString &filterName)
const
2295 QMultiMap<QString, QUrl> linkMap;
2296 const auto documents = documentsForField(fieldName, fieldValue, filterName);
2297 for (
const auto &document : documents)
2298 linkMap.insert(document.title, document.url);
2303 const QString &fieldValue,
const QString &filterName)
const
2308 const QString filterlessQuery =
2310 "FileNameTable.Title, "
2311 "NamespaceTable.Name, "
2312 "FolderTable.Name, "
2313 "FileNameTable.Name, "
2314 "IndexTable.Anchor "
2320 "WHERE IndexTable.FileId = FileNameTable.FileId "
2321 "AND FileNameTable.FolderId = FolderTable.Id "
2322 "AND IndexTable.NamespaceId = NamespaceTable.Id "
2323 "AND IndexTable.%1 = ?"_L1.arg(fieldName);
2325 const QString filterQuery = filterlessQuery
2326 + prepareFilterQuery(filterName)
2327 +
" ORDER BY LOWER(FileNameTable.Title), FileNameTable.Title"_L1;
2329 m_query->prepare(filterQuery);
2330 m_query->bindValue(0, fieldValue);
2331 bindFilterQuery(m_query.get(), 1, filterName);
2335 QList<QHelpLink> docList;
2336 while (m_query->next()) {
2337 QString title = m_query->value(0).toString();
2338 if (title.isEmpty())
2339 title = fieldValue +
" : "_L1 + m_query->value(3).toString();
2341 const QUrl url = buildQUrl(m_query->value(1).toString(),
2342 m_query->value(2).toString(),
2343 m_query->value(3).toString(),
2344 m_query->value(4).toString());
2345 docList.append(QHelpLink {url, title});
2352 QStringList namespaceList;
2355 return namespaceList;
2357 const QString filterlessQuery =
2359 "NamespaceTable.Name "
2364 const QString filterQuery = filterlessQuery
2365 + prepareFilterQuery(filterName);
2367 m_query->prepare(filterQuery);
2368 bindFilterQuery(m_query.get(), 0, filterName);
2372 while (m_query->next())
2373 namespaceList.append(m_query->value(0).toString());
2374 return namespaceList;
QStringList filters() const
QUrl findFile(const QUrl &url, const QStringList &filterAttributes) const
QList< FileInfo > FileInfoList
QByteArray fileData(const QUrl &url) const
QStringList customFilters() const
FileInfoList registeredDocumentations() const
QList< ContentsData > contentsForFilter(const QStringList &filterAttributes) const
~QHelpCollectionHandler()
QMap< QString, QVersionNumber > namespaceToVersion() const
QList< QVersionNumber > availableVersions() const
QString namespaceForFile(const QUrl &url, const QStringList &filterAttributes) const
QStringList filterAttributes() const
QMap< QString, QString > namespaceToComponent() const
QStringList availableComponents() const
QStringList indicesForFilter(const QStringList &filterAttributes) const
bool fileExists(const QUrl &url) const
bool openCollectionFile()
Transaction(const QString &connectionName)
Combined button and popup list for selecting options.
static QString getTitle(const QByteArray &contents)
static QHelpCollectionHandler::FileInfo extractFileInfo(const QUrl &url)
static QString prepareFilterQuery(int attributesCount, const QString &idTableName, const QString &idColumnName, const QString &filterTableName, const QString &filterColumnName)
static void bindFilterQuery(QSqlQuery *query, int bindStart, const QString &filterName)
static void bindFilterQuery(QSqlQuery *query, int startingBindPos, const QStringList &filterAttributes)
static QString prepareFilterQuery(const QString &filterName)