11#include <QtCore/qdatastream.h>
12#include <QtCore/qdatetime.h>
13#include <QtCore/qdir.h>
14#include <QtCore/qfileinfo.h>
15#include <QtCore/qset.h>
16#include <QtCore/qtimer.h>
17#include <QtCore/qversionnumber.h>
18#include <QtSql/qsqldriver.h>
19#include <QtSql/qsqlerror.h>
20#include <QtSql/qsqlquery.h>
27using namespace Qt::StringLiterals;
39 m_inTransaction = m_db.transaction();
54 m_inTransaction =
false;
64 , m_collectionFile(collectionFile)
66 const QFileInfo fi(m_collectionFile);
68 m_collectionFile = fi.absoluteFilePath();
81 emit that->error(tr(
"The collection file \"%1\" is not set up yet.").arg(m_collectionFile));
91 QSqlDatabase::removeDatabase(m_connectionName);
92 m_connectionName.clear();
100 m_connectionName = QHelpGlobal::uniquifyConnectionName(
"QHelpCollectionHandler"_L1,
this);
102 QSqlDatabase db = QSqlDatabase::addDatabase(
"QSQLITE"_L1, m_connectionName);
104 && db.driver()->lastError().type() == QSqlError::ConnectionError) {
105 emit error(tr(
"Cannot load sqlite database driver."));
109 db.setDatabaseName(collectionFile());
111 m_query.reset(
new QSqlQuery(db));
114 QSqlDatabase::removeDatabase(m_connectionName);
115 emit error(tr(
"Cannot open collection file: %1").arg(collectionFile()));
123 m_query->exec(
"PRAGMA synchronous=OFF"_L1);
124 m_query->exec(
"PRAGMA cache_size=3000"_L1);
126 m_query->exec(
"SELECT COUNT(*) FROM sqlite_master WHERE TYPE=\'table\' "
127 "AND Name=\'NamespaceTable\'"_L1);
130 const bool tablesExist = m_query->value(0).toInt() > 0;
132 if (!createTables(m_query.get())) {
134 emit error(tr(
"Cannot create tables in file %1.").arg(collectionFile()));
139 bool indexAndNamespaceFilterTablesMissing =
false;
141 const QStringList newTables = {
145 "FileFilterTable"_L1,
146 "IndexFilterTable"_L1,
147 "ContentsFilterTable"_L1,
148 "FileAttributeSetTable"_L1,
149 "OptimizedFilterTable"_L1,
154 "ComponentMapping"_L1,
155 "ComponentFilter"_L1,
159 QString queryString =
"SELECT COUNT(*) FROM sqlite_master WHERE TYPE=\'table\'"_L1;
160 queryString.append(
" AND (Name=\'"_L1);
161 queryString.append(newTables.join(
"\' OR Name=\'"_L1));
162 queryString.append(
"\')"_L1);
164 m_query->exec(queryString);
166 if (m_query->value(0).toInt() != newTables.size()) {
167 if (!recreateIndexAndNamespaceFilterTables(m_query.get())) {
168 emit error(tr(
"Cannot create index tables in file %1.").arg(collectionFile()));
173 indexAndNamespaceFilterTablesMissing = tablesExist;
177 if (indexAndNamespaceFilterTablesMissing) {
178 for (
const QHelpCollectionHandler::FileInfo &info : docList) {
179 if (!registerIndexAndNamespaceFilterTables(info.namespaceName,
true)) {
180 emit error(tr(
"Cannot register index tables in file %1.").arg(collectionFile()));
187 QList<TimeStamp> timeStamps;
188 m_query->exec(
"SELECT NamespaceId, FolderId, FilePath, Size, TimeStamp FROM TimeStampTable"_L1);
189 while (m_query->next()) {
191 timeStamp.namespaceId = m_query->value(0).toInt();
192 timeStamp.folderId = m_query->value(1).toInt();
193 timeStamp.fileName = m_query->value(2).toString();
194 timeStamp.size = m_query->value(3).toInt();
195 timeStamp.timeStamp = m_query->value(4).toDateTime();
196 timeStamps.append(timeStamp);
199 QList<TimeStamp> toRemove;
200 for (
const TimeStamp &timeStamp : timeStamps) {
201 if (!isTimeStampCorrect(timeStamp))
202 toRemove.append(timeStamp);
208 for (
const TimeStamp &timeStamp : toRemove) {
209 if (!unregisterIndexTable(timeStamp.namespaceId, timeStamp.folderId)) {
210 emit error(tr(
"Cannot unregister index tables in file %1.").arg(collectionFile()));
216 for (
const QHelpCollectionHandler::FileInfo &info : docList) {
217 if (!hasTimeStampInfo(info.namespaceName)
218 && !registerIndexAndNamespaceFilterTables(info.namespaceName)) {
221 unregisterDocumentation(info.namespaceName);
229 const QFileInfo fi(collectionFile());
230 return QDir::isAbsolutePath(fileName)
232 : QFileInfo(fi.absolutePath() + u'/' + fileName).absoluteFilePath();
237 const QFileInfo fi(absoluteDocPath(timeStamp.fileName));
242 if (fi.size() != timeStamp
.size)
245 if (fi.lastModified(QTimeZone::UTC) != timeStamp.timeStamp)
248 m_query->prepare(
"SELECT FilePath FROM NamespaceTable WHERE Id = ?"_L1);
249 m_query->bindValue(0, timeStamp.namespaceId);
250 if (!m_query->exec() || !m_query->next())
253 const QString oldFileName = m_query->value(0).toString();
255 if (oldFileName != timeStamp.fileName)
265 "TimeStampTable.NamespaceId "
269 "WHERE NamespaceTable.Id = TimeStampTable.NamespaceId "
270 "AND NamespaceTable.Name = ? LIMIT 1"_L1);
271 m_query->bindValue(0, nameSpace);
272 if (!m_query->exec())
275 if (!m_query->next())
284 if (m_vacuumScheduled)
287 m_vacuumScheduled =
true;
288 QTimer::singleShot(0,
this, &QHelpCollectionHandler::execVacuum);
296 m_query->exec(
"VACUUM"_L1);
297 m_vacuumScheduled =
false;
305 const QFileInfo fi(fileName);
307 emit error(tr(
"The collection file \"%1\" already exists.").arg(fileName));
311 if (!fi.absoluteDir().exists() && !QDir().mkpath(fi.absolutePath())) {
312 emit error(tr(
"Cannot create directory: %1").arg(fi.absolutePath()));
316 const QString &colFile = fi.absoluteFilePath();
317 const QString &connectionName =
318 QHelpGlobal::uniquifyConnectionName(
"QHelpCollectionHandlerCopy"_L1,
this);
319 QSqlDatabase db = QSqlDatabase::addDatabase(
"QSQLITE"_L1, connectionName);
320 db.setDatabaseName(colFile);
322 emit error(tr(
"Cannot open collection file: %1").arg(colFile));
326 QSqlQuery copyQuery(db);
327 copyQuery.exec(
"PRAGMA synchronous=OFF"_L1);
328 copyQuery.exec(
"PRAGMA cache_size=3000"_L1);
330 if (!createTables(©Query) || !recreateIndexAndNamespaceFilterTables(©Query)) {
331 emit error(tr(
"Cannot copy collection file: %1").arg(colFile));
335 const QString &oldBaseDir = QFileInfo(collectionFile()).absolutePath();
336 const QFileInfo newColFi(colFile);
337 m_query->exec(
"SELECT Name, FilePath FROM NamespaceTable"_L1);
338 while (m_query->next()) {
339 copyQuery.prepare(
"INSERT INTO NamespaceTable VALUES(NULL, ?, ?)"_L1);
340 copyQuery.bindValue(0, m_query->value(0).toString());
341 QString oldFilePath = m_query->value(1).toString();
342 if (!QDir::isAbsolutePath(oldFilePath))
343 oldFilePath = oldBaseDir + u'/' + oldFilePath;
344 copyQuery.bindValue(1, newColFi.absoluteDir().relativeFilePath(oldFilePath));
348 m_query->exec(
"SELECT NamespaceId, Name FROM FolderTable"_L1);
349 while (m_query->next()) {
350 copyQuery.prepare(
"INSERT INTO FolderTable VALUES(NULL, ?, ?)"_L1);
351 copyQuery.bindValue(0, m_query->value(0).toString());
352 copyQuery.bindValue(1, m_query->value(1).toString());
356 m_query->exec(
"SELECT Name FROM FilterAttributeTable"_L1);
357 while (m_query->next()) {
358 copyQuery.prepare(
"INSERT INTO FilterAttributeTable VALUES(NULL, ?)"_L1);
359 copyQuery.bindValue(0, m_query->value(0).toString());
363 m_query->exec(
"SELECT Name FROM FilterNameTable"_L1);
364 while (m_query->next()) {
365 copyQuery.prepare(
"INSERT INTO FilterNameTable VALUES(NULL, ?)"_L1);
366 copyQuery.bindValue(0, m_query->value(0).toString());
370 m_query->exec(
"SELECT NameId, FilterAttributeId FROM FilterTable"_L1);
371 while (m_query->next()) {
372 copyQuery.prepare(
"INSERT INTO FilterTable VALUES(?, ?)"_L1);
373 copyQuery.bindValue(0, m_query->value(0).toInt());
374 copyQuery.bindValue(1, m_query->value(1).toInt());
378 m_query->exec(
"SELECT Key, Value FROM SettingsTable"_L1);
379 while (m_query->next()) {
380 if (m_query->value(0).toString() ==
"FTS5IndexedNamespaces"_L1)
382 copyQuery.prepare(
"INSERT INTO SettingsTable VALUES(?, ?)"_L1);
383 copyQuery.bindValue(0, m_query->value(0).toString());
384 copyQuery.bindValue(1, m_query->value(1));
389 QSqlDatabase::removeDatabase(connectionName);
395 const QStringList tables = {
396 "CREATE TABLE NamespaceTable ("
397 "Id INTEGER PRIMARY KEY, "
399 "FilePath TEXT )"_L1,
400 "CREATE TABLE FolderTable ("
401 "Id INTEGER PRIMARY KEY, "
402 "NamespaceId INTEGER, "
404 "CREATE TABLE FilterAttributeTable ("
405 "Id INTEGER PRIMARY KEY, "
407 "CREATE TABLE FilterNameTable ("
408 "Id INTEGER PRIMARY KEY, "
410 "CREATE TABLE FilterTable ("
412 "FilterAttributeId INTEGER )"_L1,
413 "CREATE TABLE SettingsTable ("
414 "Key TEXT PRIMARY KEY, "
418 for (
const QString &q : tables) {
427 const QStringList tables = {
428 "DROP TABLE IF EXISTS FileNameTable"_L1,
429 "DROP TABLE IF EXISTS IndexTable"_L1,
430 "DROP TABLE IF EXISTS ContentsTable"_L1,
431 "DROP TABLE IF EXISTS FileFilterTable"_L1,
432 "DROP TABLE IF EXISTS IndexFilterTable"_L1,
433 "DROP TABLE IF EXISTS ContentsFilterTable"_L1,
434 "DROP TABLE IF EXISTS FileAttributeSetTable"_L1,
435 "DROP TABLE IF EXISTS OptimizedFilterTable"_L1,
436 "DROP TABLE IF EXISTS TimeStampTable"_L1,
437 "DROP TABLE IF EXISTS VersionTable"_L1,
438 "DROP TABLE IF EXISTS Filter"_L1,
439 "DROP TABLE IF EXISTS ComponentTable"_L1,
440 "DROP TABLE IF EXISTS ComponentMapping"_L1,
441 "DROP TABLE IF EXISTS ComponentFilter"_L1,
442 "DROP TABLE IF EXISTS VersionFilter"_L1,
443 "CREATE TABLE FileNameTable ("
446 "FileId INTEGER PRIMARY KEY, "
448 "CREATE TABLE IndexTable ("
449 "Id INTEGER PRIMARY KEY, "
452 "NamespaceId INTEGER, "
455 "CREATE TABLE ContentsTable ("
456 "Id INTEGER PRIMARY KEY, "
457 "NamespaceId INTEGER, "
459 "CREATE TABLE FileFilterTable ("
460 "FilterAttributeId INTEGER, "
461 "FileId INTEGER)"_L1,
462 "CREATE TABLE IndexFilterTable ("
463 "FilterAttributeId INTEGER, "
464 "IndexId INTEGER)"_L1,
465 "CREATE TABLE ContentsFilterTable ("
466 "FilterAttributeId INTEGER, "
467 "ContentsId INTEGER )"_L1,
468 "CREATE TABLE FileAttributeSetTable ("
469 "NamespaceId INTEGER, "
470 "FilterAttributeSetId INTEGER, "
471 "FilterAttributeId INTEGER)"_L1,
472 "CREATE TABLE OptimizedFilterTable ("
473 "NamespaceId INTEGER, "
474 "FilterAttributeId INTEGER)"_L1,
475 "CREATE TABLE TimeStampTable ("
476 "NamespaceId INTEGER, "
480 "TimeStamp TEXT)"_L1,
481 "CREATE TABLE VersionTable ("
482 "NamespaceId INTEGER, "
484 "CREATE TABLE Filter ("
485 "FilterId INTEGER PRIMARY KEY, "
487 "CREATE TABLE ComponentTable ("
488 "ComponentId INTEGER PRIMARY KEY, "
490 "CREATE TABLE ComponentMapping ("
491 "ComponentId INTEGER, "
492 "NamespaceId INTEGER)"_L1,
493 "CREATE TABLE ComponentFilter ("
494 "ComponentName TEXT, "
495 "FilterId INTEGER)"_L1,
496 "CREATE TABLE VersionFilter ("
498 "FilterId INTEGER)"_L1
501 for (
const QString &q : tables) {
512 m_query->exec(
"SELECT Name FROM FilterNameTable"_L1);
513 while (m_query->next())
514 list.append(m_query->value(0).toString());
523 m_query->exec(
"SELECT Name FROM Filter ORDER BY Name"_L1);
524 while (m_query->next())
525 list.append(m_query->value(0).toString());
534 m_query->exec(
"SELECT DISTINCT Name FROM ComponentTable ORDER BY Name"_L1);
535 while (m_query->next())
536 list.append(m_query->value(0).toString());
543 QList<QVersionNumber> list;
545 m_query->exec(
"SELECT DISTINCT Version FROM VersionTable ORDER BY Version"_L1);
546 while (m_query->next())
547 list.append(QVersionNumber::fromString(m_query->value(0).toString()));
554 QMap<QString, QString> result;
558 "NamespaceTable.Name, "
559 "ComponentTable.Name "
560 "FROM NamespaceTable, "
563 "WHERE NamespaceTable.Id = ComponentMapping.NamespaceId "
564 "AND ComponentMapping.ComponentId = ComponentTable.ComponentId"_L1);
565 while (m_query->next())
566 result.insert(m_query->value(0).toString(), m_query->value(1).toString());
573 QMap<QString, QVersionNumber> result;
577 "NamespaceTable.Name, "
578 "VersionTable.Version "
579 "FROM NamespaceTable, "
581 "WHERE NamespaceTable.Id = VersionTable.NamespaceId"_L1);
582 while (m_query->next()) {
583 result.insert(m_query->value(0).toString(),
584 QVersionNumber::fromString(m_query->value(1).toString()));
592 QStringList components;
593 QList<QVersionNumber> versions;
596 "SELECT ComponentFilter.ComponentName "
597 "FROM ComponentFilter, Filter "
598 "WHERE ComponentFilter.FilterId = Filter.FilterId "
599 "AND Filter.Name = ? "
600 "ORDER BY ComponentFilter.ComponentName"_L1);
601 m_query->bindValue(0, filterName);
603 while (m_query->next())
604 components.append(m_query->value(0).toString());
607 "SELECT VersionFilter.Version "
608 "FROM VersionFilter, Filter "
609 "WHERE VersionFilter.FilterId = Filter.FilterId "
610 "AND Filter.Name = ? "
611 "ORDER BY VersionFilter.Version"_L1);
612 m_query->bindValue(0, filterName);
614 while (m_query->next())
615 versions.append(QVersionNumber::fromString(m_query->value(0).toString()));
618 QHelpFilterData data;
619 data.setComponents(components);
620 data.setVersions(versions);
625 const QHelpFilterData &filterData)
627 if (!removeFilter(filterName))
630 m_query->prepare(
"INSERT INTO Filter VALUES (NULL, ?)"_L1);
631 m_query->bindValue(0, filterName);
632 if (!m_query->exec())
635 const int filterId = m_query->lastInsertId().toInt();
637 QVariantList componentList;
638 QVariantList versionList;
639 QVariantList filterIdList;
641 const auto &components = filterData.components();
642 for (
const QString &component : components) {
643 componentList.append(component);
644 filterIdList.append(filterId);
647 m_query->prepare(
"INSERT INTO ComponentFilter VALUES (?, ?)"_L1);
648 m_query->addBindValue(componentList);
649 m_query->addBindValue(filterIdList);
650 if (!m_query->execBatch())
653 filterIdList.clear();
654 const auto &versions = filterData.versions();
655 for (
const QVersionNumber &version : versions) {
656 versionList.append(version.isNull() ? QString() : version.toString());
657 filterIdList.append(filterId);
660 m_query->prepare(
"INSERT INTO VersionFilter VALUES (?, ?)"_L1);
661 m_query->addBindValue(versionList);
662 m_query->addBindValue(filterIdList);
663 if (!m_query->execBatch())
671 m_query->prepare(
"SELECT FilterId FROM Filter WHERE Name = ?"_L1);
672 m_query->bindValue(0, filterName);
673 if (!m_query->exec())
676 if (!m_query->next())
679 const int filterId = m_query->value(0).toInt();
681 m_query->prepare(
"DELETE FROM Filter WHERE Filter.Name = ?"_L1);
682 m_query->bindValue(0, filterName);
683 if (!m_query->exec())
686 m_query->prepare(
"DELETE FROM ComponentFilter WHERE ComponentFilter.FilterId = ?"_L1);
687 m_query->bindValue(0, filterId);
688 if (!m_query->exec())
691 m_query->prepare(
"DELETE FROM VersionFilter WHERE VersionFilter.FilterId = ?"_L1);
692 m_query->bindValue(0, filterId);
693 if (!m_query->exec())
701 if (!isDBOpened() || filterName.isEmpty())
704 int filterNameId = -1;
705 m_query->prepare(
"SELECT Id FROM FilterNameTable WHERE Name=?"_L1);
706 m_query->bindValue(0, filterName);
709 filterNameId = m_query->value(0).toInt();
711 if (filterNameId < 0) {
712 emit error(tr(
"Unknown filter \"%1\".").arg(filterName));
716 m_query->prepare(
"DELETE FROM FilterTable WHERE NameId=?"_L1);
717 m_query->bindValue(0, filterNameId);
720 m_query->prepare(
"DELETE FROM FilterNameTable WHERE Id=?"_L1);
721 m_query->bindValue(0, filterNameId);
728 const QStringList &attributes)
730 if (!isDBOpened() || filterName.isEmpty())
734 m_query->prepare(
"SELECT Id FROM FilterNameTable WHERE Name=?"_L1);
735 m_query->bindValue(0, filterName);
738 nameId = m_query->value(0).toInt();
740 m_query->exec(
"SELECT Id, Name FROM FilterAttributeTable"_L1);
741 QStringList idsToInsert = attributes;
742 QMap<QString,
int> attributeMap;
743 while (m_query->next()) {
745 const QString attributeName = m_query->value(1).toString();
746 attributeMap.insert(attributeName, m_query->value(0).toInt());
747 idsToInsert.removeAll(attributeName);
750 for (
const QString &id : std::as_const(idsToInsert)) {
751 m_query->prepare(
"INSERT INTO FilterAttributeTable VALUES(NULL, ?)"_L1);
752 m_query->bindValue(0, id);
754 attributeMap.insert(id, m_query->lastInsertId().toInt());
758 m_query->prepare(
"INSERT INTO FilterNameTable VALUES(NULL, ?)"_L1);
759 m_query->bindValue(0, filterName);
761 nameId = m_query->lastInsertId().toInt();
765 emit error(tr(
"Cannot register filter %1.").arg(filterName));
769 m_query->prepare(
"DELETE FROM FilterTable WHERE NameId=?"_L1);
770 m_query->bindValue(0, nameId);
773 for (
const QString &att : attributes) {
774 m_query->prepare(
"INSERT INTO FilterTable VALUES(?, ?)"_L1);
775 m_query->bindValue(0, nameId);
776 m_query->bindValue(1, attributeMap[att]);
777 if (!m_query->exec())
784 const QString &namespaceName)
const
793 "NamespaceTable.Name, "
794 "NamespaceTable.FilePath, "
799 "WHERE NamespaceTable.Id = FolderTable.NamespaceId "
800 "AND NamespaceTable.Name = ? LIMIT 1"_L1);
801 m_query->bindValue(0, namespaceName);
802 if (!m_query->exec() || !m_query->next())
805 fileInfo.namespaceName = m_query->value(0).toString();
806 fileInfo.fileName = m_query->value(1).toString();
807 fileInfo.folderName = m_query->value(2).toString();
822 "NamespaceTable.Name, "
823 "NamespaceTable.FilePath, "
828 "WHERE NamespaceTable.Id = FolderTable.NamespaceId"_L1);
830 while (m_query->next()) {
832 fileInfo.namespaceName = m_query->value(0).toString();
833 fileInfo.fileName = m_query->value(1).toString();
834 fileInfo.folderName = m_query->value(2).toString();
835 list.append(fileInfo);
846 QHelpDBReader reader(fileName, QHelpGlobal::uniquifyConnectionName(
847 "QHelpCollectionHandler"_L1,
this),
nullptr);
849 emit error(tr(
"Cannot open documentation file %1.").arg(fileName));
853 const QString &ns = reader.namespaceName();
855 emit error(tr(
"Invalid documentation file \"%1\".").arg(fileName));
859 const int nsId = registerNamespace(ns, fileName);
863 const int vfId = registerVirtualFolder(reader.virtualFolder(), nsId);
867 registerVersion(reader.version(), nsId);
868 registerFilterAttributes(reader.filterAttributeSets(), nsId);
869 const auto &customFilters = reader.customFilters();
870 for (
const QString &filterName : customFilters)
871 addCustomFilter(filterName, reader.filterAttributes(filterName));
873 if (!registerIndexTable(reader.indexTable(), nsId, vfId, registeredDocumentation(ns).fileName))
884 m_query->prepare(
"SELECT Id FROM NamespaceTable WHERE Name = ?"_L1);
885 m_query->bindValue(0, namespaceName);
888 if (!m_query->next()) {
889 emit error(tr(
"The namespace %1 was not registered.").arg(namespaceName));
893 const int nsId = m_query->value(0).toInt();
895 m_query->prepare(
"DELETE FROM NamespaceTable WHERE Id = ?"_L1);
896 m_query->bindValue(0, nsId);
897 if (!m_query->exec())
900 m_query->prepare(
"SELECT Id FROM FolderTable WHERE NamespaceId = ?"_L1);
901 m_query->bindValue(0, nsId);
904 if (!m_query->next()) {
905 emit error(tr(
"The namespace %1 was not registered.").arg(namespaceName));
909 const int vfId = m_query->value(0).toInt();
911 m_query->prepare(
"DELETE FROM NamespaceTable WHERE Id = ?"_L1);
912 m_query->bindValue(0, nsId);
913 if (!m_query->exec())
916 m_query->prepare(
"DELETE FROM FolderTable WHERE NamespaceId = ?"_L1);
917 m_query->bindValue(0, nsId);
918 if (!m_query->exec())
921 if (!unregisterIndexTable(nsId, vfId))
931 QHelpCollectionHandler::FileInfo fileInfo;
933 if (!url.isValid() || url.toString().count(u'/') < 4
934 || url.scheme() !=
"qthelp"_L1) {
938 fileInfo.namespaceName = url.authority();
939 fileInfo.fileName = url.path();
940 if (fileInfo.fileName.startsWith(u'/'))
941 fileInfo.fileName = fileInfo.fileName.mid(1);
942 fileInfo.folderName = fileInfo.fileName.mid(0, fileInfo.fileName.indexOf(u'/', 1));
943 fileInfo.fileName.remove(0, fileInfo.folderName.size() + 1);
953 const FileInfo fileInfo = extractFileInfo(url);
954 if (fileInfo.namespaceName.isEmpty())
958 "SELECT COUNT (DISTINCT NamespaceTable.Id) "
963 "WHERE FolderTable.Name = ? "
964 "AND FileNameTable.Name = ? "
965 "AND FileNameTable.FolderId = FolderTable.Id "
966 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1);
967 m_query->bindValue(0, fileInfo.folderName);
968 m_query->bindValue(1, fileInfo.fileName);
969 if (!m_query->exec() || !m_query->next())
972 const int count = m_query->value(0).toInt();
980 if (filterName.isEmpty())
983 return " AND EXISTS(SELECT * FROM Filter WHERE Filter.Name = ?) "
989 "WHERE ComponentFilter.FilterId = Filter.FilterId "
990 "AND Filter.Name = ?) "
991 "OR NamespaceTable.Id IN ("
1000 "WHERE ComponentMapping.NamespaceId = NamespaceTable.Id "
1001 "AND ComponentTable.ComponentId = ComponentMapping.ComponentId "
1002 "AND ((ComponentTable.Name = ComponentFilter.ComponentName) "
1003 "OR (ComponentTable.Name IS NULL AND ComponentFilter.ComponentName IS NULL)) "
1004 "AND ComponentFilter.FilterId = Filter.FilterId "
1005 "AND Filter.Name = ?))"
1011 "WHERE VersionFilter.FilterId = Filter.FilterId "
1012 "AND Filter.Name = ?) "
1013 "OR NamespaceTable.Id IN ("
1015 "NamespaceTable.Id "
1021 "WHERE VersionFilter.FilterId = Filter.FilterId "
1022 "AND ((VersionFilter.Version = VersionTable.Version) "
1023 "OR (VersionFilter.Version IS NULL AND VersionTable.Version IS NULL)) "
1024 "AND VersionTable.NamespaceId = NamespaceTable.Id "
1025 "AND Filter.Name = ?))"
1031 if (filterName.isEmpty())
1034 query->bindValue(bindStart, filterName);
1035 query->bindValue(bindStart + 1, filterName);
1036 query->bindValue(bindStart + 2, filterName);
1037 query->bindValue(bindStart + 3, filterName);
1038 query->bindValue(bindStart + 4, filterName);
1042 const QString &idTableName,
1043 const QString &idColumnName,
1044 const QString &filterTableName,
1045 const QString &filterColumnName)
1047 if (!attributesCount)
1050 QString filterQuery =
" AND (%1.%2 IN ("_L1.arg(idTableName, idColumnName);
1052 const QString filterQueryTemplate =
1054 "FROM %1, FilterAttributeTable "
1055 "WHERE %1.FilterAttributeId = FilterAttributeTable.Id "
1056 "AND FilterAttributeTable.Name = ?"_L1.arg(filterTableName, filterColumnName);
1058 for (
int i = 0; i < attributesCount; ++i) {
1060 filterQuery.append(
" INTERSECT "_L1);
1061 filterQuery.append(filterQueryTemplate);
1064 filterQuery.append(
") OR NamespaceTable.Id IN ("_L1);
1066 const QString optimizedFilterQueryTemplate =
1067 "SELECT OptimizedFilterTable.NamespaceId "
1068 "FROM OptimizedFilterTable, FilterAttributeTable "
1069 "WHERE OptimizedFilterTable.FilterAttributeId = FilterAttributeTable.Id "
1070 "AND FilterAttributeTable.Name = ?"_L1;
1072 for (
int i = 0; i < attributesCount; ++i) {
1074 filterQuery.append(
" INTERSECT "_L1);
1075 filterQuery.append(optimizedFilterQueryTemplate);
1078 filterQuery.append(
"))"_L1);
1083static void bindFilterQuery(QSqlQuery *query,
int startingBindPos,
const QStringList &filterAttributes)
1085 for (
int i = 0; i < 2; ++i) {
1086 for (
int j = 0; j < filterAttributes.size(); j++) {
1087 query->bindValue(i * filterAttributes.size() + j + startingBindPos,
1088 filterAttributes.at(j));
1094 const QStringList &filterAttributes)
const
1099 const FileInfo fileInfo = extractFileInfo(url);
1100 if (fileInfo.namespaceName.isEmpty())
1103 const QString filterlessQuery =
1105 "NamespaceTable.Name "
1110 "WHERE FolderTable.Name = ? "
1111 "AND FileNameTable.Name = ? "
1112 "AND FileNameTable.FolderId = FolderTable.Id "
1113 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1;
1115 const QString filterQuery = filterlessQuery
1116 + prepareFilterQuery(filterAttributes.size(),
"FileNameTable"_L1,
"FileId"_L1,
1117 "FileFilterTable"_L1,
"FileId"_L1);
1119 m_query->prepare(filterQuery);
1120 m_query->bindValue(0, fileInfo.folderName);
1121 m_query->bindValue(1, fileInfo.fileName);
1122 bindFilterQuery(m_query.get(), 2, filterAttributes);
1124 if (!m_query->exec())
1127 QStringList namespaceList;
1128 while (m_query->next())
1129 namespaceList.append(m_query->value(0).toString());
1131 if (namespaceList.isEmpty())
1134 if (namespaceList.contains(fileInfo.namespaceName))
1135 return fileInfo.namespaceName;
1137 const QString originalVersion = namespaceVersion(fileInfo.namespaceName);
1139 for (
const QString &ns : std::as_const(namespaceList)) {
1140 const QString nsVersion = namespaceVersion(ns);
1141 if (originalVersion == nsVersion)
1146 return namespaceList.first();
1150 const QString &filterName)
const
1155 const FileInfo fileInfo = extractFileInfo(url);
1156 if (fileInfo.namespaceName.isEmpty())
1159 const QString filterlessQuery =
1161 "NamespaceTable.Name "
1166 "WHERE FolderTable.Name = ? "
1167 "AND FileNameTable.Name = ? "
1168 "AND FileNameTable.FolderId = FolderTable.Id "
1169 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1;
1171 const QString filterQuery = filterlessQuery
1172 + prepareFilterQuery(filterName);
1174 m_query->prepare(filterQuery);
1175 m_query->bindValue(0, fileInfo.folderName);
1176 m_query->bindValue(1, fileInfo.fileName);
1177 bindFilterQuery(m_query.get(), 2, filterName);
1179 if (!m_query->exec())
1182 QStringList namespaceList;
1183 while (m_query->next())
1184 namespaceList.append(m_query->value(0).toString());
1186 if (namespaceList.isEmpty())
1189 if (namespaceList.contains(fileInfo.namespaceName))
1190 return fileInfo.namespaceName;
1192 const QString originalVersion = namespaceVersion(fileInfo.namespaceName);
1194 for (
const QString &ns : std::as_const(namespaceList)) {
1195 const QString nsVersion = namespaceVersion(ns);
1196 if (originalVersion == nsVersion)
1201 return namespaceList.first();
1205 const QStringList &filterAttributes,
1206 const QString &extensionFilter)
const
1211 const QString extensionQuery = extensionFilter.isEmpty()
1212 ? QString() :
" AND FileNameTable.Name LIKE ?"_L1;
1213 const QString filterlessQuery =
1215 "FolderTable.Name, "
1216 "FileNameTable.Name "
1221 "WHERE FileNameTable.FolderId = FolderTable.Id "
1222 "AND FolderTable.NamespaceId = NamespaceTable.Id "
1223 "AND NamespaceTable.Name = ?"_L1 + extensionQuery;
1225 const QString filterQuery = filterlessQuery
1226 + prepareFilterQuery(filterAttributes.size(),
"FileNameTable"_L1,
"FileId"_L1,
1227 "FileFilterTable"_L1,
"FileId"_L1);
1229 m_query->prepare(filterQuery);
1230 m_query->bindValue(0, namespaceName);
1232 if (!extensionFilter.isEmpty()) {
1233 m_query->bindValue(bindCount,
"%.%1"_L1.arg(extensionFilter));
1236 bindFilterQuery(m_query.get(), bindCount, filterAttributes);
1238 if (!m_query->exec())
1241 QStringList fileNames;
1242 while (m_query->next())
1243 fileNames.append(m_query->value(0).toString() + u'/' + m_query->value(1).toString());
1248 const QString &filterName,
1249 const QString &extensionFilter)
const
1254 const QString extensionQuery = extensionFilter.isEmpty()
1255 ? QString() :
" AND FileNameTable.Name LIKE ?"_L1;
1256 const QString filterlessQuery =
1258 "FolderTable.Name, "
1259 "FileNameTable.Name "
1264 "WHERE FileNameTable.FolderId = FolderTable.Id "
1265 "AND FolderTable.NamespaceId = NamespaceTable.Id "
1266 "AND NamespaceTable.Name = ?"_L1 + extensionQuery;
1268 const QString filterQuery = filterlessQuery
1269 + prepareFilterQuery(filterName);
1271 m_query->prepare(filterQuery);
1272 m_query->bindValue(0, namespaceName);
1274 if (!extensionFilter.isEmpty()) {
1275 m_query->bindValue(bindCount,
"%.%1"_L1.arg(extensionFilter));
1279 bindFilterQuery(m_query.get(), bindCount, filterName);
1281 if (!m_query->exec())
1284 QStringList fileNames;
1285 while (m_query->next())
1286 fileNames.append(m_query->value(0).toString() + u'/' + m_query->value(1).toString());
1295 const QString namespaceName = namespaceForFile(url, filterAttributes);
1296 if (namespaceName.isEmpty())
1300 result.setAuthority(namespaceName);
1309 const QString namespaceName = namespaceForFile(url, filterName);
1310 if (namespaceName.isEmpty())
1314 result.setAuthority(namespaceName);
1323 const QString namespaceName = namespaceForFile(url, QString());
1324 if (namespaceName.isEmpty())
1327 const FileInfo fileInfo = extractFileInfo(url);
1329 const FileInfo docInfo = registeredDocumentation(namespaceName);
1330 const QString absFileName = absoluteDocPath(docInfo.fileName);
1332 QHelpDBReader reader(absFileName, QHelpGlobal::uniquifyConnectionName(
1333 docInfo.fileName,
const_cast<QHelpCollectionHandler *>(
this)),
nullptr);
1337 return reader.fileData(fileInfo.folderName, fileInfo.fileName);
1342 QStringList indices;
1347 const QString filterlessQuery =
1355 "WHERE IndexTable.FileId = FileNameTable.FileId "
1356 "AND FileNameTable.FolderId = FolderTable.Id "
1357 "AND IndexTable.NamespaceId = NamespaceTable.Id"_L1;
1359 const QString filterQuery = filterlessQuery
1360 + prepareFilterQuery(filterAttributes.size(),
"IndexTable"_L1,
"Id"_L1,
1361 "IndexFilterTable"_L1,
"IndexId"_L1)
1362 +
" ORDER BY LOWER(IndexTable.Name), IndexTable.Name"_L1;
1365 m_query->prepare(filterQuery);
1366 bindFilterQuery(m_query.get(), 0, filterAttributes);
1370 while (m_query->next())
1371 indices.append(m_query->value(0).toString());
1378 QStringList indices;
1383 const QString filterlessQuery =
1391 "WHERE IndexTable.FileId = FileNameTable.FileId "
1392 "AND FileNameTable.FolderId = FolderTable.Id "
1393 "AND IndexTable.NamespaceId = NamespaceTable.Id"_L1;
1395 const QString filterQuery = filterlessQuery
1396 + prepareFilterQuery(filterName)
1397 +
" ORDER BY LOWER(IndexTable.Name), IndexTable.Name"_L1;
1399 m_query->prepare(filterQuery);
1400 bindFilterQuery(m_query.get(), 0, filterName);
1404 while (m_query->next())
1405 indices.append(m_query->value(0).toString());
1412 if (!contents.size())
1419 QDataStream s(contents);
1428 const QStringList &filterAttributes)
const
1433 const QString filterlessQuery =
1435 "NamespaceTable.Name, "
1436 "FolderTable.Name, "
1437 "ContentsTable.Data, "
1438 "VersionTable.Version "
1444 "WHERE ContentsTable.NamespaceId = NamespaceTable.Id "
1445 "AND NamespaceTable.Id = FolderTable.NamespaceId "
1446 "AND ContentsTable.NamespaceId = NamespaceTable.Id "
1447 "AND VersionTable.NamespaceId = NamespaceTable.Id"_L1;
1449 const QString filterQuery = filterlessQuery
1450 + prepareFilterQuery(filterAttributes.size(),
"ContentsTable"_L1,
"Id"_L1,
1451 "ContentsFilterTable"_L1,
"ContentsId"_L1);
1453 m_query->prepare(filterQuery);
1454 bindFilterQuery(m_query.get(), 0, filterAttributes);
1458 QMap<QString, QMap<QVersionNumber, ContentsData>> contentsMap;
1460 while (m_query->next()) {
1461 const QString namespaceName = m_query->value(0).toString();
1462 const QByteArray contents = m_query->value(2).toByteArray();
1463 const QString versionString = m_query->value(3).toString();
1465 const QString title = getTitle(contents);
1466 const QVersionNumber version = QVersionNumber::fromString(versionString);
1468 ContentsData &contentsData = contentsMap[title][version];
1469 contentsData.namespaceName = namespaceName;
1470 contentsData.folderName = m_query->value(1).toString();
1471 contentsData.contentsList.append(contents);
1474 QList<QHelpCollectionHandler::ContentsData> result;
1475 for (
const auto &versionContents : std::as_const(contentsMap)) {
1477 const auto itBegin = versionContents.constBegin();
1478 auto it = versionContents.constEnd();
1479 while (it != itBegin) {
1481 result.append(it.value());
1492 const QString filterlessQuery =
1494 "NamespaceTable.Name, "
1495 "FolderTable.Name, "
1496 "ContentsTable.Data, "
1497 "VersionTable.Version "
1503 "WHERE ContentsTable.NamespaceId = NamespaceTable.Id "
1504 "AND NamespaceTable.Id = FolderTable.NamespaceId "
1505 "AND ContentsTable.NamespaceId = NamespaceTable.Id "
1506 "AND VersionTable.NamespaceId = NamespaceTable.Id"_L1;
1508 const QString filterQuery = filterlessQuery + prepareFilterQuery(filterName);
1510 m_query->prepare(filterQuery);
1511 bindFilterQuery(m_query.get(), 0, filterName);
1515 QMap<QString, QMap<QVersionNumber, ContentsData>> contentsMap;
1517 while (m_query->next()) {
1518 const QString namespaceName = m_query->value(0).toString();
1519 const QByteArray contents = m_query->value(2).toByteArray();
1520 const QString versionString = m_query->value(3).toString();
1522 const QString title = getTitle(contents);
1523 const QVersionNumber version = QVersionNumber::fromString(versionString);
1525 ContentsData &contentsData = contentsMap[title][version];
1526 contentsData.namespaceName = namespaceName;
1527 contentsData.folderName = m_query->value(1).toString();
1528 contentsData.contentsList.append(contents);
1531 QList<QHelpCollectionHandler::ContentsData> result;
1532 for (
const auto &versionContents : std::as_const(contentsMap)) {
1534 const auto itBegin = versionContents.constBegin();
1535 auto it = versionContents.constEnd();
1536 while (it != itBegin) {
1538 result.append(it.value());
1549 m_query->prepare(
"DELETE FROM SettingsTable WHERE Key=?"_L1);
1550 m_query->bindValue(0, key);
1551 return m_query->exec();
1555 const QVariant &defaultValue)
const
1558 return defaultValue;
1560 m_query->prepare(
"SELECT COUNT(Key) FROM SettingsTable WHERE Key=?"_L1);
1561 m_query->bindValue(0, key);
1562 if (!m_query->exec() || !m_query->next() || !m_query->value(0).toInt()) {
1564 return defaultValue;
1568 m_query->prepare(
"SELECT Value FROM SettingsTable WHERE Key=?"_L1);
1569 m_query->bindValue(0, key);
1570 if (m_query->exec() && m_query->next()) {
1571 const QVariant &value = m_query->value(0);
1575 return defaultValue;
1579 const QVariant &value)
1584 m_query->prepare(
"SELECT Value FROM SettingsTable WHERE Key=?"_L1);
1585 m_query->bindValue(0, key);
1587 if (m_query->next()) {
1588 m_query->prepare(
"UPDATE SettingsTable SET Value=? where Key=?"_L1);
1589 m_query->bindValue(0, value);
1590 m_query->bindValue(1, key);
1592 m_query->prepare(
"INSERT INTO SettingsTable VALUES(?, ?)"_L1);
1593 m_query->bindValue(0, key);
1594 m_query->bindValue(1, value);
1596 return m_query->exec();
1605 m_query->exec(
"SELECT Name FROM FilterAttributeTable"_L1);
1607 while (m_query->next())
1608 atts.insert(m_query->value(0).toString());
1610 for (
const QStringList &attributeSet : attributeSets) {
1611 for (
const QString &attribute : attributeSet) {
1612 if (!atts.contains(attribute)) {
1613 m_query->prepare(
"INSERT INTO FilterAttributeTable VALUES(NULL, ?)"_L1);
1614 m_query->bindValue(0, attribute);
1619 return registerFileAttributeSets(attributeSets, nsId);
1628 if (attributeSets.isEmpty())
1632 QVariantList attributeSetIds;
1633 QVariantList filterAttributeIds;
1635 if (!m_query->exec(
"SELECT MAX(FilterAttributeSetId) FROM FileAttributeSetTable"_L1)
1636 || !m_query->next()) {
1640 int attributeSetId = m_query->value(0).toInt();
1642 for (
const QStringList &attributeSet : attributeSets) {
1645 for (
const QString &attribute : attributeSet) {
1646 m_query->prepare(
"SELECT Id FROM FilterAttributeTable WHERE Name=?"_L1);
1647 m_query->bindValue(0, attribute);
1649 if (!m_query->exec() || !m_query->next())
1653 attributeSetIds.append(attributeSetId);
1654 filterAttributeIds.append(m_query->value(0).toInt());
1658 m_query->prepare(
"INSERT INTO FileAttributeSetTable "
1659 "(NamespaceId, FilterAttributeSetId, FilterAttributeId) "
1660 "VALUES(?, ?, ?)"_L1);
1661 m_query->addBindValue(nsIds);
1662 m_query->addBindValue(attributeSetIds);
1663 m_query->addBindValue(filterAttributeIds);
1664 return m_query->execBatch();
1671 m_query->exec(
"SELECT Name FROM FilterAttributeTable"_L1);
1672 while (m_query->next())
1673 list.append(m_query->value(0).toString());
1684 "FilterAttributeTable.Name "
1686 "FilterAttributeTable, "
1689 "WHERE FilterAttributeTable.Id = FilterTable.FilterAttributeId "
1690 "AND FilterTable.NameId = FilterNameTable.Id "
1691 "AND FilterNameTable.Name=?"_L1);
1692 m_query->bindValue(0, filterName);
1694 while (m_query->next())
1695 list.append(m_query->value(0).toString());
1707 "FileAttributeSetTable.FilterAttributeSetId, "
1708 "FilterAttributeTable.Name "
1710 "FileAttributeSetTable, "
1711 "FilterAttributeTable, "
1713 "WHERE FileAttributeSetTable.FilterAttributeId = FilterAttributeTable.Id "
1714 "AND FileAttributeSetTable.NamespaceId = NamespaceTable.Id "
1715 "AND NamespaceTable.Name = ? "
1716 "ORDER BY FileAttributeSetTable.FilterAttributeSetId"_L1);
1717 m_query->bindValue(0, namespaceName);
1720 QList<QStringList> result;
1721 while (m_query->next()) {
1722 const int id = m_query->value(0).toInt();
1724 result.append(QStringList());
1727 result.last().append(m_query->value(1).toString());
1730 if (result.isEmpty())
1731 result.append(QStringList());
1742 "VersionTable.Version "
1746 "WHERE NamespaceTable.Name = ? "
1747 "AND NamespaceTable.Id = VersionTable.NamespaceId"_L1);
1748 m_query->bindValue(0, namespaceName);
1749 if (!m_query->exec() || !m_query->next())
1752 const QString ret = m_query->value(0).toString();
1759 const int errorValue = -1;
1763 m_query->prepare(
"SELECT COUNT(Id) FROM NamespaceTable WHERE Name=?"_L1);
1764 m_query->bindValue(0, nspace);
1766 while (m_query->next()) {
1767 if (m_query->value(0).toInt() > 0) {
1768 emit error(tr(
"Namespace %1 already exists.").arg(nspace));
1773 QFileInfo fi(m_collectionFile);
1774 m_query->prepare(
"INSERT INTO NamespaceTable VALUES(NULL, ?, ?)"_L1);
1775 m_query->bindValue(0, nspace);
1776 m_query->bindValue(1, fi.absoluteDir().relativeFilePath(fileName));
1777 int namespaceId = errorValue;
1778 if (m_query->exec()) {
1779 namespaceId = m_query->lastInsertId().toInt();
1782 if (namespaceId < 1) {
1783 emit error(tr(
"Cannot register namespace \"%1\".").arg(nspace));
1794 m_query->prepare(
"INSERT INTO FolderTable VALUES(NULL, ?, ?)"_L1);
1795 m_query->bindValue(0, namespaceId);
1796 m_query->bindValue(1, folderName);
1799 if (m_query->exec()) {
1800 virtualId = m_query->lastInsertId().toInt();
1803 if (virtualId < 1) {
1804 emit error(tr(
"Cannot register virtual folder '%1'.").arg(folderName));
1807 if (registerComponent(folderName, namespaceId) < 0)
1814 m_query->prepare(
"SELECT ComponentId FROM ComponentTable WHERE Name = ?"_L1);
1815 m_query->bindValue(0, componentName);
1816 if (!m_query->exec())
1819 if (!m_query->next()) {
1820 m_query->prepare(
"INSERT INTO ComponentTable VALUES(NULL, ?)"_L1);
1821 m_query->bindValue(0, componentName);
1822 if (!m_query->exec())
1825 m_query->prepare(
"SELECT ComponentId FROM ComponentTable WHERE Name = ?"_L1);
1826 m_query->bindValue(0, componentName);
1827 if (!m_query->exec() || !m_query->next())
1831 const int componentId = m_query->value(0).toInt();
1833 m_query->prepare(
"INSERT INTO ComponentMapping VALUES(?, ?)"_L1);
1834 m_query->bindValue(0, componentId);
1835 m_query->bindValue(1, namespaceId);
1836 if (!m_query->exec())
1847 m_query->prepare(
"INSERT INTO VersionTable (NamespaceId, Version) VALUES(?, ?)"_L1);
1848 m_query->addBindValue(namespaceId);
1849 m_query->addBindValue(version);
1850 return m_query->exec();
1854 const QString &nameSpace,
bool createDefaultVersionFilter)
1859 m_query->prepare(
"SELECT Id, FilePath FROM NamespaceTable WHERE Name=?"_L1);
1860 m_query->bindValue(0, nameSpace);
1862 if (!m_query->next())
1865 const int nsId = m_query->value(0).toInt();
1866 const QString fileName = m_query->value(1).toString();
1868 m_query->prepare(
"SELECT Id, Name FROM FolderTable WHERE NamespaceId=?"_L1);
1869 m_query->bindValue(0, nsId);
1871 if (!m_query->next())
1874 const int vfId = m_query->value(0).toInt();
1875 const QString vfName = m_query->value(1).toString();
1877 const QString absFileName = absoluteDocPath(fileName);
1878 QHelpDBReader reader(absFileName, QHelpGlobal::uniquifyConnectionName(
1879 fileName,
this),
this);
1883 registerComponent(vfName, nsId);
1884 registerVersion(reader.version(), nsId);
1885 if (!registerFileAttributeSets(reader.filterAttributeSets(), nsId))
1888 if (!registerIndexTable(reader.indexTable(), nsId, vfId, fileName))
1891 if (createDefaultVersionFilter)
1892 createVersionFilter(reader.version());
1898 if (version.isEmpty())
1901 const QVersionNumber versionNumber = QVersionNumber::fromString(version);
1902 if (versionNumber.isNull())
1905 const QString filterName = tr(
"Version %1").arg(version);
1906 if (filters().contains(filterName))
1909 QHelpFilterData filterData;
1910 filterData.setVersions({versionNumber});
1911 setFilterData(filterName, filterData);
1915 int nsId,
int vfId,
const QString &fileName)
1919 QMap<QString, QVariantList> filterAttributeToNewFileId;
1921 QVariantList fileFolderIds;
1922 QVariantList fileNames;
1923 QVariantList fileTitles;
1924 const int fileSize = indexTable.fileItems.size();
1925 fileFolderIds.reserve(fileSize);
1926 fileNames.reserve(fileSize);
1927 fileTitles.reserve(fileSize);
1929 if (!m_query->exec(
"SELECT MAX(FileId) FROM FileNameTable"_L1) || !m_query->next())
1932 const int maxFileId = m_query->value(0).toInt();
1935 for (
const QHelpDBReader::FileItem &item : indexTable.fileItems) {
1936 fileFolderIds.append(vfId);
1937 fileNames.append(item.name);
1938 fileTitles.append(item.title);
1940 for (
const QString &filterAttribute : item.filterAttributes)
1941 filterAttributeToNewFileId[filterAttribute].append(maxFileId + newFileId + 1);
1945 m_query->prepare(
"INSERT INTO FileNameTable VALUES(?, ?, NULL, ?)"_L1);
1946 m_query->addBindValue(fileFolderIds);
1947 m_query->addBindValue(fileNames);
1948 m_query->addBindValue(fileTitles);
1949 if (!m_query->execBatch())
1952 for (
auto it = filterAttributeToNewFileId.cbegin(),
1953 end = filterAttributeToNewFileId.cend(); it != end; ++it) {
1954 const QString filterAttribute = it.key();
1955 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
1956 m_query->bindValue(0, filterAttribute);
1957 if (!m_query->exec() || !m_query->next())
1960 const int attributeId = m_query->value(0).toInt();
1962 QVariantList attributeIds;
1963 for (
int i = 0; i < it.value().size(); i++)
1964 attributeIds.append(attributeId);
1966 m_query->prepare(
"INSERT INTO FileFilterTable VALUES(?, ?)"_L1);
1967 m_query->addBindValue(attributeIds);
1968 m_query->addBindValue(it.value());
1969 if (!m_query->execBatch())
1973 QMap<QString, QVariantList> filterAttributeToNewIndexId;
1975 if (!m_query->exec(
"SELECT MAX(Id) FROM IndexTable"_L1) || !m_query->next())
1978 const int maxIndexId = m_query->value(0).toInt();
1981 QVariantList indexNames;
1982 QVariantList indexIdentifiers;
1983 QVariantList indexNamespaceIds;
1984 QVariantList indexFileIds;
1985 QVariantList indexAnchors;
1986 const int indexSize = indexTable.indexItems.size();
1987 indexNames.reserve(indexSize);
1988 indexIdentifiers.reserve(indexSize);
1989 indexNamespaceIds.reserve(indexSize);
1990 indexFileIds.reserve(indexSize);
1991 indexAnchors.reserve(indexSize);
1993 for (
const QHelpDBReader::IndexItem &item : indexTable.indexItems) {
1994 indexNames.append(item.name);
1995 indexIdentifiers.append(item.identifier);
1996 indexNamespaceIds.append(nsId);
1997 indexFileIds.append(maxFileId + item.fileId + 1);
1998 indexAnchors.append(item.anchor);
2000 for (
const QString &filterAttribute : item.filterAttributes)
2001 filterAttributeToNewIndexId[filterAttribute].append(maxIndexId + newIndexId + 1);
2005 m_query->prepare(
"INSERT INTO IndexTable VALUES(NULL, ?, ?, ?, ?, ?)"_L1);
2006 m_query->addBindValue(indexNames);
2007 m_query->addBindValue(indexIdentifiers);
2008 m_query->addBindValue(indexNamespaceIds);
2009 m_query->addBindValue(indexFileIds);
2010 m_query->addBindValue(indexAnchors);
2011 if (!m_query->execBatch())
2014 for (
auto it = filterAttributeToNewIndexId.cbegin(),
2015 end = filterAttributeToNewIndexId.cend(); it != end; ++it) {
2016 const QString filterAttribute = it.key();
2017 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2018 m_query->bindValue(0, filterAttribute);
2019 if (!m_query->exec() || !m_query->next())
2022 const int attributeId = m_query->value(0).toInt();
2024 QVariantList attributeIds;
2025 for (
int i = 0; i < it.value().size(); i++)
2026 attributeIds.append(attributeId);
2028 m_query->prepare(
"INSERT INTO IndexFilterTable VALUES(?, ?)"_L1);
2029 m_query->addBindValue(attributeIds);
2030 m_query->addBindValue(it.value());
2031 if (!m_query->execBatch())
2035 QMap<QString, QVariantList> filterAttributeToNewContentsId;
2037 QVariantList contentsNsIds;
2038 QVariantList contentsData;
2039 const int contentsSize = indexTable.contentsItems.size();
2040 contentsNsIds.reserve(contentsSize);
2041 contentsData.reserve(contentsSize);
2043 if (!m_query->exec(
"SELECT MAX(Id) FROM ContentsTable"_L1) || !m_query->next())
2046 const int maxContentsId = m_query->value(0).toInt();
2048 int newContentsId = 0;
2049 for (
const QHelpDBReader::ContentsItem &item : indexTable.contentsItems) {
2050 contentsNsIds.append(nsId);
2051 contentsData.append(item.data);
2053 for (
const QString &filterAttribute : item.filterAttributes) {
2054 filterAttributeToNewContentsId[filterAttribute]
2055 .append(maxContentsId + newContentsId + 1);
2060 m_query->prepare(
"INSERT INTO ContentsTable VALUES(NULL, ?, ?)"_L1);
2061 m_query->addBindValue(contentsNsIds);
2062 m_query->addBindValue(contentsData);
2063 if (!m_query->execBatch())
2066 for (
auto it = filterAttributeToNewContentsId.cbegin(),
2067 end = filterAttributeToNewContentsId.cend(); it != end; ++it) {
2068 const QString filterAttribute = it.key();
2069 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2070 m_query->bindValue(0, filterAttribute);
2071 if (!m_query->exec() || !m_query->next())
2074 const int attributeId = m_query->value(0).toInt();
2076 QVariantList attributeIds;
2077 for (
int i = 0; i < it.value().size(); i++)
2078 attributeIds.append(attributeId);
2080 m_query->prepare(
"INSERT INTO ContentsFilterTable VALUES(?, ?)"_L1);
2081 m_query->addBindValue(attributeIds);
2082 m_query->addBindValue(it.value());
2083 if (!m_query->execBatch())
2087 QVariantList filterNsIds;
2088 QVariantList filterAttributeIds;
2089 for (
const QString &filterAttribute : indexTable.usedFilterAttributes) {
2090 filterNsIds.append(nsId);
2092 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2093 m_query->bindValue(0, filterAttribute);
2094 if (!m_query->exec() || !m_query->next())
2097 filterAttributeIds.append(m_query->value(0).toInt());
2100 m_query->prepare(
"INSERT INTO OptimizedFilterTable "
2101 "(NamespaceId, FilterAttributeId) VALUES(?, ?)"_L1);
2102 m_query->addBindValue(filterNsIds);
2103 m_query->addBindValue(filterAttributeIds);
2104 if (!m_query->execBatch())
2107 m_query->prepare(
"INSERT INTO TimeStampTable "
2108 "(NamespaceId, FolderId, FilePath, Size, TimeStamp) "
2109 "VALUES(?, ?, ?, ?, ?)"_L1);
2110 m_query->addBindValue(nsId);
2111 m_query->addBindValue(vfId);
2112 m_query->addBindValue(fileName);
2113 const QFileInfo fi(absoluteDocPath(fileName));
2114 m_query->addBindValue(fi.size());
2115 QDateTime lastModified = fi.lastModified(QTimeZone::UTC);
2116 if (qEnvironmentVariableIsSet(
"SOURCE_DATE_EPOCH")) {
2117 const QString sourceDateEpochStr = qEnvironmentVariable(
"SOURCE_DATE_EPOCH");
2119 const qlonglong sourceDateEpoch = sourceDateEpochStr.toLongLong(&ok);
2120 if (ok && sourceDateEpoch < lastModified.toSecsSinceEpoch())
2121 lastModified.setSecsSinceEpoch(sourceDateEpoch);
2123 m_query->addBindValue(lastModified);
2124 if (!m_query->exec())
2133 m_query->prepare(
"DELETE FROM IndexFilterTable WHERE IndexId IN "
2134 "(SELECT Id FROM IndexTable WHERE NamespaceId = ?)"_L1);
2135 m_query->bindValue(0, nsId);
2136 if (!m_query->exec())
2139 m_query->prepare(
"DELETE FROM IndexTable WHERE NamespaceId = ?"_L1);
2140 m_query->bindValue(0, nsId);
2141 if (!m_query->exec())
2144 m_query->prepare(
"DELETE FROM FileFilterTable WHERE FileId IN "
2145 "(SELECT FileId FROM FileNameTable WHERE FolderId = ?)"_L1);
2146 m_query->bindValue(0, vfId);
2147 if (!m_query->exec())
2150 m_query->prepare(
"DELETE FROM FileNameTable WHERE FolderId = ?"_L1);
2151 m_query->bindValue(0, vfId);
2152 if (!m_query->exec())
2155 m_query->prepare(
"DELETE FROM ContentsFilterTable WHERE ContentsId IN "
2156 "(SELECT Id FROM ContentsTable WHERE NamespaceId = ?)"_L1);
2157 m_query->bindValue(0, nsId);
2158 if (!m_query->exec())
2161 m_query->prepare(
"DELETE FROM ContentsTable WHERE NamespaceId = ?"_L1);
2162 m_query->bindValue(0, nsId);
2163 if (!m_query->exec())
2166 m_query->prepare(
"DELETE FROM FileAttributeSetTable WHERE NamespaceId = ?"_L1);
2167 m_query->bindValue(0, nsId);
2168 if (!m_query->exec())
2171 m_query->prepare(
"DELETE FROM OptimizedFilterTable WHERE NamespaceId = ?"_L1);
2172 m_query->bindValue(0, nsId);
2173 if (!m_query->exec())
2176 m_query->prepare(
"DELETE FROM TimeStampTable WHERE NamespaceId = ?"_L1);
2177 m_query->bindValue(0, nsId);
2178 if (!m_query->exec())
2181 m_query->prepare(
"DELETE FROM VersionTable WHERE NamespaceId = ?"_L1);
2182 m_query->bindValue(0, nsId);
2183 if (!m_query->exec())
2186 m_query->prepare(
"SELECT ComponentId FROM ComponentMapping WHERE NamespaceId = ?"_L1);
2187 m_query->bindValue(0, nsId);
2188 if (!m_query->exec())
2191 if (!m_query->next())
2194 const int componentId = m_query->value(0).toInt();
2196 m_query->prepare(
"DELETE FROM ComponentMapping WHERE NamespaceId = ?"_L1);
2197 m_query->bindValue(0, nsId);
2198 if (!m_query->exec())
2201 m_query->prepare(
"SELECT ComponentId FROM ComponentMapping WHERE ComponentId = ?"_L1);
2202 m_query->bindValue(0, componentId);
2203 if (!m_query->exec())
2206 if (!m_query->next()) {
2207 m_query->prepare(
"DELETE FROM ComponentTable WHERE ComponentId = ?"_L1);
2208 m_query->bindValue(0, componentId);
2209 if (!m_query->exec())
2217 const QString &relFileName,
const QString &anchor)
2220 url.setScheme(
"qthelp"_L1);
2221 url.setAuthority(ns);
2222 url.setPath(u'/' + folder + u'/' + relFileName);
2223 url.setFragment(anchor);
2228 const QString &id,
const QStringList &filterAttributes)
const
2230 return documentsForField(
"Identifier"_L1, id, filterAttributes);
2234 const QString &keyword,
const QStringList &filterAttributes)
const
2236 return documentsForField(
"Name"_L1, keyword, filterAttributes);
2240 const QString &fieldValue,
const QStringList &filterAttributes)
const
2245 const QString filterlessQuery =
2247 "FileNameTable.Title, "
2248 "NamespaceTable.Name, "
2249 "FolderTable.Name, "
2250 "FileNameTable.Name, "
2251 "IndexTable.Anchor "
2257 "WHERE IndexTable.FileId = FileNameTable.FileId "
2258 "AND FileNameTable.FolderId = FolderTable.Id "
2259 "AND IndexTable.NamespaceId = NamespaceTable.Id "
2260 "AND IndexTable.%1 = ?"_L1.arg(fieldName);
2262 const QString filterQuery = filterlessQuery
2263 + prepareFilterQuery(filterAttributes.size(),
"IndexTable"_L1,
"Id"_L1,
2264 "IndexFilterTable"_L1,
"IndexId"_L1);
2266 m_query->prepare(filterQuery);
2267 m_query->bindValue(0, fieldValue);
2268 bindFilterQuery(m_query.get(), 1, filterAttributes);
2272 QList<QHelpLink> docList;
2273 while (m_query->next()) {
2274 QString title = m_query->value(0).toString();
2275 if (title.isEmpty())
2276 title = fieldValue +
" : "_L1 + m_query->value(3).toString();
2278 const QUrl url = buildQUrl(m_query->value(1).toString(),
2279 m_query->value(2).toString(),
2280 m_query->value(3).toString(),
2281 m_query->value(4).toString());
2282 docList.append(QHelpLink {url, title});
2288 const QString &id,
const QString &filterName)
const
2290 return documentsForField(
"Identifier"_L1, id, filterName);
2294 const QString &keyword,
const QString &filterName)
const
2296 return documentsForField(
"Name"_L1, keyword, filterName);
2300 const QString &fieldValue,
const QString &filterName)
const
2302 QMultiMap<QString, QUrl> linkMap;
2303 const auto documents = documentsForField(fieldName, fieldValue, filterName);
2304 for (
const auto &document : documents)
2305 linkMap.insert(document.title, document.url);
2318 return lhs.title == rhs.title && lhs.url == rhs.url && lhs.version == rhs.version;
2325 return !e.version.isNull();
2330 return lhs.version < rhs.version;
2335 return { e.url, e.title };
2339 const QString &fieldValue,
const QString &filterName)
const
2344 const QString filterlessQuery =
2346 "FileNameTable.Title, "
2347 "NamespaceTable.Name, "
2348 "FolderTable.Name, "
2349 "FileNameTable.Name, "
2350 "IndexTable.Anchor "
2356 "WHERE IndexTable.FileId = FileNameTable.FileId "
2357 "AND FileNameTable.FolderId = FolderTable.Id "
2358 "AND IndexTable.NamespaceId = NamespaceTable.Id "
2359 "AND IndexTable.%1 = ?"_L1.arg(fieldName);
2361 const QString filterQuery = filterlessQuery
2362 + prepareFilterQuery(filterName)
2363 +
" ORDER BY LOWER(FileNameTable.Title), FileNameTable.Title"_L1;
2365 m_query->prepare(filterQuery);
2366 m_query->bindValue(0, fieldValue);
2367 bindFilterQuery(m_query.get(), 1, filterName);
2371 QList<IndexEntry> entries;
2372 while (m_query->next()) {
2373 QString title = m_query->value(0).toString();
2374 if (title.isEmpty())
2375 title = fieldValue +
" : "_L1 + m_query->value(3).toString();
2376 const QString nameSpace = m_query->value(1).toString();
2377 const QUrl url = buildQUrl(nameSpace,
2378 m_query->value(2).toString(),
2379 m_query->value(3).toString(),
2380 m_query->value(4).toString());
2381 IndexEntry newEntry{ title, url, QHelpDBReader::versionHeuristic(nameSpace) };
2382 if (!entries.contains(newEntry))
2383 entries.emplaceBack(newEntry);
2387 if (
std::all_of(entries.cbegin(), entries.cend(),
hasVersion))
2390 QList<QHelpLink> docList;
2391 docList.reserve(entries.size());
2392 std::transform(entries.cbegin(), entries.cend(),
std::back_inserter(docList), toHelpLink);
2398 QStringList namespaceList;
2401 return namespaceList;
2403 const QString filterlessQuery =
2405 "NamespaceTable.Name "
2410 const QString filterQuery = filterlessQuery
2411 + prepareFilterQuery(filterName);
2413 m_query->prepare(filterQuery);
2414 bindFilterQuery(m_query.get(), 0, filterName);
2418 while (m_query->next())
2419 namespaceList.append(m_query->value(0).toString());
2420 return namespaceList;
QStringList filters() const
QUrl findFile(const QUrl &url, const QStringList &filterAttributes) const
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< FileInfo > FileInfoList
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)
static bool hasVersion(const IndexEntry &e)
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 bool versionLessThan(const IndexEntry &lhs, const IndexEntry &rhs)
static void bindFilterQuery(QSqlQuery *query, int startingBindPos, const QStringList &filterAttributes)
static QString prepareFilterQuery(const QString &filterName)
static QHelpLink toHelpLink(const IndexEntry &e)
friend bool comparesEqual(const IndexEntry &lhs, const IndexEntry &rhs) noexcept