10#include <QtCore/qdatastream.h>
11#include <QtCore/qdatetime.h>
12#include <QtCore/qdir.h>
13#include <QtCore/qfileinfo.h>
14#include <QtCore/qset.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 const auto &components = filterData.components();
638 for (
const QString &component : components) {
639 componentList.append(component);
640 filterIdList.append(filterId);
643 m_query->prepare(
"INSERT INTO ComponentFilter VALUES (?, ?)"_L1);
644 m_query->addBindValue(componentList);
645 m_query->addBindValue(filterIdList);
646 if (!m_query->execBatch())
649 filterIdList.clear();
650 const auto &versions = filterData.versions();
651 for (
const QVersionNumber &version : versions) {
652 versionList.append(version.isNull() ? QString() : version.toString());
653 filterIdList.append(filterId);
656 m_query->prepare(
"INSERT INTO VersionFilter VALUES (?, ?)"_L1);
657 m_query->addBindValue(versionList);
658 m_query->addBindValue(filterIdList);
659 if (!m_query->execBatch())
667 m_query->prepare(
"SELECT FilterId FROM Filter WHERE Name = ?"_L1);
668 m_query->bindValue(0, filterName);
669 if (!m_query->exec())
672 if (!m_query->next())
675 const int filterId = m_query->value(0).toInt();
677 m_query->prepare(
"DELETE FROM Filter WHERE Filter.Name = ?"_L1);
678 m_query->bindValue(0, filterName);
679 if (!m_query->exec())
682 m_query->prepare(
"DELETE FROM ComponentFilter WHERE ComponentFilter.FilterId = ?"_L1);
683 m_query->bindValue(0, filterId);
684 if (!m_query->exec())
687 m_query->prepare(
"DELETE FROM VersionFilter WHERE VersionFilter.FilterId = ?"_L1);
688 m_query->bindValue(0, filterId);
689 if (!m_query->exec())
697 if (!isDBOpened() || filterName.isEmpty())
700 int filterNameId = -1;
701 m_query->prepare(
"SELECT Id FROM FilterNameTable WHERE Name=?"_L1);
702 m_query->bindValue(0, filterName);
705 filterNameId = m_query->value(0).toInt();
707 if (filterNameId < 0) {
708 emit error(tr(
"Unknown filter \"%1\".").arg(filterName));
712 m_query->prepare(
"DELETE FROM FilterTable WHERE NameId=?"_L1);
713 m_query->bindValue(0, filterNameId);
716 m_query->prepare(
"DELETE FROM FilterNameTable WHERE Id=?"_L1);
717 m_query->bindValue(0, filterNameId);
724 const QStringList &attributes)
726 if (!isDBOpened() || filterName.isEmpty())
730 m_query->prepare(
"SELECT Id FROM FilterNameTable WHERE Name=?"_L1);
731 m_query->bindValue(0, filterName);
734 nameId = m_query->value(0).toInt();
736 m_query->exec(
"SELECT Id, Name FROM FilterAttributeTable"_L1);
737 QStringList idsToInsert = attributes;
738 QMap<QString,
int> attributeMap;
739 while (m_query->next()) {
741 const QString attributeName = m_query->value(1).toString();
742 attributeMap.insert(attributeName, m_query->value(0).toInt());
743 idsToInsert.removeAll(attributeName);
746 for (
const QString &id : std::as_const(idsToInsert)) {
747 m_query->prepare(
"INSERT INTO FilterAttributeTable VALUES(NULL, ?)"_L1);
748 m_query->bindValue(0, id);
750 attributeMap.insert(id, m_query->lastInsertId().toInt());
754 m_query->prepare(
"INSERT INTO FilterNameTable VALUES(NULL, ?)"_L1);
755 m_query->bindValue(0, filterName);
757 nameId = m_query->lastInsertId().toInt();
761 emit error(tr(
"Cannot register filter %1.").arg(filterName));
765 m_query->prepare(
"DELETE FROM FilterTable WHERE NameId=?"_L1);
766 m_query->bindValue(0, nameId);
769 for (
const QString &att : attributes) {
770 m_query->prepare(
"INSERT INTO FilterTable VALUES(?, ?)"_L1);
771 m_query->bindValue(0, nameId);
772 m_query->bindValue(1, attributeMap[att]);
773 if (!m_query->exec())
780 const QString &namespaceName)
const
789 "NamespaceTable.Name, "
790 "NamespaceTable.FilePath, "
795 "WHERE NamespaceTable.Id = FolderTable.NamespaceId "
796 "AND NamespaceTable.Name = ? LIMIT 1"_L1);
797 m_query->bindValue(0, namespaceName);
798 if (!m_query->exec() || !m_query->next())
801 fileInfo.namespaceName = m_query->value(0).toString();
802 fileInfo.fileName = m_query->value(1).toString();
803 fileInfo.folderName = m_query->value(2).toString();
818 "NamespaceTable.Name, "
819 "NamespaceTable.FilePath, "
824 "WHERE NamespaceTable.Id = FolderTable.NamespaceId"_L1);
826 while (m_query->next()) {
828 fileInfo.namespaceName = m_query->value(0).toString();
829 fileInfo.fileName = m_query->value(1).toString();
830 fileInfo.folderName = m_query->value(2).toString();
831 list.append(fileInfo);
842 QHelpDBReader reader(fileName, QHelpGlobal::uniquifyConnectionName(
843 "QHelpCollectionHandler"_L1,
this),
nullptr);
845 emit error(tr(
"Cannot open documentation file %1.").arg(fileName));
849 const QString &ns = reader.namespaceName();
851 emit error(tr(
"Invalid documentation file \"%1\".").arg(fileName));
855 const int nsId = registerNamespace(ns, fileName);
859 const int vfId = registerVirtualFolder(reader.virtualFolder(), nsId);
863 registerVersion(reader.version(), nsId);
864 registerFilterAttributes(reader.filterAttributeSets(), nsId);
865 const auto &customFilters = reader.customFilters();
866 for (
const QString &filterName : customFilters)
867 addCustomFilter(filterName, reader.filterAttributes(filterName));
869 if (!registerIndexTable(reader.indexTable(), nsId, vfId, registeredDocumentation(ns).fileName))
880 m_query->prepare(
"SELECT Id FROM NamespaceTable WHERE Name = ?"_L1);
881 m_query->bindValue(0, namespaceName);
884 if (!m_query->next()) {
885 emit error(tr(
"The namespace %1 was not registered.").arg(namespaceName));
889 const int nsId = m_query->value(0).toInt();
891 m_query->prepare(
"DELETE FROM NamespaceTable WHERE Id = ?"_L1);
892 m_query->bindValue(0, nsId);
893 if (!m_query->exec())
896 m_query->prepare(
"SELECT Id FROM FolderTable WHERE NamespaceId = ?"_L1);
897 m_query->bindValue(0, nsId);
900 if (!m_query->next()) {
901 emit error(tr(
"The namespace %1 was not registered.").arg(namespaceName));
905 const int vfId = m_query->value(0).toInt();
907 m_query->prepare(
"DELETE FROM NamespaceTable WHERE Id = ?"_L1);
908 m_query->bindValue(0, nsId);
909 if (!m_query->exec())
912 m_query->prepare(
"DELETE FROM FolderTable WHERE NamespaceId = ?"_L1);
913 m_query->bindValue(0, nsId);
914 if (!m_query->exec())
917 if (!unregisterIndexTable(nsId, vfId))
927 QHelpCollectionHandler::FileInfo fileInfo;
929 if (!url.isValid() || url.toString().count(u'/') < 4
930 || url.scheme() !=
"qthelp"_L1) {
934 fileInfo.namespaceName = url.authority();
935 fileInfo.fileName = url.path();
936 if (fileInfo.fileName.startsWith(u'/'))
937 fileInfo.fileName = fileInfo.fileName.mid(1);
938 fileInfo.folderName = fileInfo.fileName.mid(0, fileInfo.fileName.indexOf(u'/', 1));
939 fileInfo.fileName.remove(0, fileInfo.folderName.size() + 1);
949 const FileInfo fileInfo = extractFileInfo(url);
950 if (fileInfo.namespaceName.isEmpty())
954 "SELECT COUNT (DISTINCT NamespaceTable.Id) "
959 "WHERE FolderTable.Name = ? "
960 "AND FileNameTable.Name = ? "
961 "AND FileNameTable.FolderId = FolderTable.Id "
962 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1);
963 m_query->bindValue(0, fileInfo.folderName);
964 m_query->bindValue(1, fileInfo.fileName);
965 if (!m_query->exec() || !m_query->next())
968 const int count = m_query->value(0).toInt();
976 if (filterName.isEmpty())
979 return " AND EXISTS(SELECT * FROM Filter WHERE Filter.Name = ?) "
985 "WHERE ComponentFilter.FilterId = Filter.FilterId "
986 "AND Filter.Name = ?) "
987 "OR NamespaceTable.Id IN ("
996 "WHERE ComponentMapping.NamespaceId = NamespaceTable.Id "
997 "AND ComponentTable.ComponentId = ComponentMapping.ComponentId "
998 "AND ((ComponentTable.Name = ComponentFilter.ComponentName) "
999 "OR (ComponentTable.Name IS NULL AND ComponentFilter.ComponentName IS NULL)) "
1000 "AND ComponentFilter.FilterId = Filter.FilterId "
1001 "AND Filter.Name = ?))"
1007 "WHERE VersionFilter.FilterId = Filter.FilterId "
1008 "AND Filter.Name = ?) "
1009 "OR NamespaceTable.Id IN ("
1011 "NamespaceTable.Id "
1017 "WHERE VersionFilter.FilterId = Filter.FilterId "
1018 "AND ((VersionFilter.Version = VersionTable.Version) "
1019 "OR (VersionFilter.Version IS NULL AND VersionTable.Version IS NULL)) "
1020 "AND VersionTable.NamespaceId = NamespaceTable.Id "
1021 "AND Filter.Name = ?))"
1027 if (filterName.isEmpty())
1030 query->bindValue(bindStart, filterName);
1031 query->bindValue(bindStart + 1, filterName);
1032 query->bindValue(bindStart + 2, filterName);
1033 query->bindValue(bindStart + 3, filterName);
1034 query->bindValue(bindStart + 4, filterName);
1038 const QString &idTableName,
1039 const QString &idColumnName,
1040 const QString &filterTableName,
1041 const QString &filterColumnName)
1043 if (!attributesCount)
1046 QString filterQuery =
" AND (%1.%2 IN ("_L1.arg(idTableName, idColumnName);
1048 const QString filterQueryTemplate =
1050 "FROM %1, FilterAttributeTable "
1051 "WHERE %1.FilterAttributeId = FilterAttributeTable.Id "
1052 "AND FilterAttributeTable.Name = ?"_L1.arg(filterTableName, filterColumnName);
1054 for (
int i = 0; i < attributesCount; ++i) {
1056 filterQuery.append(
" INTERSECT "_L1);
1057 filterQuery.append(filterQueryTemplate);
1060 filterQuery.append(
") OR NamespaceTable.Id IN ("_L1);
1062 const QString optimizedFilterQueryTemplate =
1063 "SELECT OptimizedFilterTable.NamespaceId "
1064 "FROM OptimizedFilterTable, FilterAttributeTable "
1065 "WHERE OptimizedFilterTable.FilterAttributeId = FilterAttributeTable.Id "
1066 "AND FilterAttributeTable.Name = ?"_L1;
1068 for (
int i = 0; i < attributesCount; ++i) {
1070 filterQuery.append(
" INTERSECT "_L1);
1071 filterQuery.append(optimizedFilterQueryTemplate);
1074 filterQuery.append(
"))"_L1);
1079static void bindFilterQuery(QSqlQuery *query,
int startingBindPos,
const QStringList &filterAttributes)
1081 for (
int i = 0; i < 2; ++i) {
1082 for (
int j = 0; j < filterAttributes.size(); j++) {
1083 query->bindValue(i * filterAttributes.size() + j + startingBindPos,
1084 filterAttributes.at(j));
1090 const QStringList &filterAttributes)
const
1095 const FileInfo fileInfo = extractFileInfo(url);
1096 if (fileInfo.namespaceName.isEmpty())
1099 const QString filterlessQuery =
1101 "NamespaceTable.Name "
1106 "WHERE FolderTable.Name = ? "
1107 "AND FileNameTable.Name = ? "
1108 "AND FileNameTable.FolderId = FolderTable.Id "
1109 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1;
1111 const QString filterQuery = filterlessQuery
1112 + prepareFilterQuery(filterAttributes.size(),
"FileNameTable"_L1,
"FileId"_L1,
1113 "FileFilterTable"_L1,
"FileId"_L1);
1115 m_query->prepare(filterQuery);
1116 m_query->bindValue(0, fileInfo.folderName);
1117 m_query->bindValue(1, fileInfo.fileName);
1118 bindFilterQuery(m_query.get(), 2, filterAttributes);
1120 if (!m_query->exec())
1123 QStringList namespaceList;
1124 while (m_query->next())
1125 namespaceList.append(m_query->value(0).toString());
1127 if (namespaceList.isEmpty())
1130 if (namespaceList.contains(fileInfo.namespaceName))
1131 return fileInfo.namespaceName;
1133 const QString originalVersion = namespaceVersion(fileInfo.namespaceName);
1135 for (
const QString &ns : std::as_const(namespaceList)) {
1136 const QString nsVersion = namespaceVersion(ns);
1137 if (originalVersion == nsVersion)
1142 return namespaceList.first();
1146 const QString &filterName)
const
1151 const FileInfo fileInfo = extractFileInfo(url);
1152 if (fileInfo.namespaceName.isEmpty())
1155 const QString filterlessQuery =
1157 "NamespaceTable.Name "
1162 "WHERE FolderTable.Name = ? "
1163 "AND FileNameTable.Name = ? "
1164 "AND FileNameTable.FolderId = FolderTable.Id "
1165 "AND FolderTable.NamespaceId = NamespaceTable.Id"_L1;
1167 const QString filterQuery = filterlessQuery
1168 + prepareFilterQuery(filterName);
1170 m_query->prepare(filterQuery);
1171 m_query->bindValue(0, fileInfo.folderName);
1172 m_query->bindValue(1, fileInfo.fileName);
1173 bindFilterQuery(m_query.get(), 2, filterName);
1175 if (!m_query->exec())
1178 QStringList namespaceList;
1179 while (m_query->next())
1180 namespaceList.append(m_query->value(0).toString());
1182 if (namespaceList.isEmpty())
1185 if (namespaceList.contains(fileInfo.namespaceName))
1186 return fileInfo.namespaceName;
1188 const QString originalVersion = namespaceVersion(fileInfo.namespaceName);
1190 for (
const QString &ns : std::as_const(namespaceList)) {
1191 const QString nsVersion = namespaceVersion(ns);
1192 if (originalVersion == nsVersion)
1197 return namespaceList.first();
1201 const QStringList &filterAttributes,
1202 const QString &extensionFilter)
const
1207 const QString extensionQuery = extensionFilter.isEmpty()
1208 ? QString() :
" AND FileNameTable.Name LIKE ?"_L1;
1209 const QString filterlessQuery =
1211 "FolderTable.Name, "
1212 "FileNameTable.Name "
1217 "WHERE FileNameTable.FolderId = FolderTable.Id "
1218 "AND FolderTable.NamespaceId = NamespaceTable.Id "
1219 "AND NamespaceTable.Name = ?"_L1 + extensionQuery;
1221 const QString filterQuery = filterlessQuery
1222 + prepareFilterQuery(filterAttributes.size(),
"FileNameTable"_L1,
"FileId"_L1,
1223 "FileFilterTable"_L1,
"FileId"_L1);
1225 m_query->prepare(filterQuery);
1226 m_query->bindValue(0, namespaceName);
1228 if (!extensionFilter.isEmpty()) {
1229 m_query->bindValue(bindCount,
"%.%1"_L1.arg(extensionFilter));
1232 bindFilterQuery(m_query.get(), bindCount, filterAttributes);
1234 if (!m_query->exec())
1237 QStringList fileNames;
1238 while (m_query->next())
1239 fileNames.append(m_query->value(0).toString() + u'/' + m_query->value(1).toString());
1244 const QString &filterName,
1245 const QString &extensionFilter)
const
1250 const QString extensionQuery = extensionFilter.isEmpty()
1251 ? QString() :
" AND FileNameTable.Name LIKE ?"_L1;
1252 const QString filterlessQuery =
1254 "FolderTable.Name, "
1255 "FileNameTable.Name "
1260 "WHERE FileNameTable.FolderId = FolderTable.Id "
1261 "AND FolderTable.NamespaceId = NamespaceTable.Id "
1262 "AND NamespaceTable.Name = ?"_L1 + extensionQuery;
1264 const QString filterQuery = filterlessQuery
1265 + prepareFilterQuery(filterName);
1267 m_query->prepare(filterQuery);
1268 m_query->bindValue(0, namespaceName);
1270 if (!extensionFilter.isEmpty()) {
1271 m_query->bindValue(bindCount,
"%.%1"_L1.arg(extensionFilter));
1275 bindFilterQuery(m_query.get(), bindCount, filterName);
1277 if (!m_query->exec())
1280 QStringList fileNames;
1281 while (m_query->next())
1282 fileNames.append(m_query->value(0).toString() + u'/' + m_query->value(1).toString());
1291 const QString namespaceName = namespaceForFile(url, filterAttributes);
1292 if (namespaceName.isEmpty())
1296 result.setAuthority(namespaceName);
1305 const QString namespaceName = namespaceForFile(url, filterName);
1306 if (namespaceName.isEmpty())
1310 result.setAuthority(namespaceName);
1319 const QString namespaceName = namespaceForFile(url, QString());
1320 if (namespaceName.isEmpty())
1323 const FileInfo fileInfo = extractFileInfo(url);
1325 const FileInfo docInfo = registeredDocumentation(namespaceName);
1326 const QString absFileName = absoluteDocPath(docInfo.fileName);
1328 QHelpDBReader reader(absFileName, QHelpGlobal::uniquifyConnectionName(
1329 docInfo.fileName,
const_cast<QHelpCollectionHandler *>(
this)),
nullptr);
1333 return reader.fileData(fileInfo.folderName, fileInfo.fileName);
1338 QStringList indices;
1343 const QString filterlessQuery =
1351 "WHERE IndexTable.FileId = FileNameTable.FileId "
1352 "AND FileNameTable.FolderId = FolderTable.Id "
1353 "AND IndexTable.NamespaceId = NamespaceTable.Id"_L1;
1355 const QString filterQuery = filterlessQuery
1356 + prepareFilterQuery(filterAttributes.size(),
"IndexTable"_L1,
"Id"_L1,
1357 "IndexFilterTable"_L1,
"IndexId"_L1)
1358 +
" ORDER BY LOWER(IndexTable.Name), IndexTable.Name"_L1;
1361 m_query->prepare(filterQuery);
1362 bindFilterQuery(m_query.get(), 0, filterAttributes);
1366 while (m_query->next())
1367 indices.append(m_query->value(0).toString());
1374 QStringList indices;
1379 const QString filterlessQuery =
1387 "WHERE IndexTable.FileId = FileNameTable.FileId "
1388 "AND FileNameTable.FolderId = FolderTable.Id "
1389 "AND IndexTable.NamespaceId = NamespaceTable.Id"_L1;
1391 const QString filterQuery = filterlessQuery
1392 + prepareFilterQuery(filterName)
1393 +
" ORDER BY LOWER(IndexTable.Name), IndexTable.Name"_L1;
1395 m_query->prepare(filterQuery);
1396 bindFilterQuery(m_query.get(), 0, filterName);
1400 while (m_query->next())
1401 indices.append(m_query->value(0).toString());
1408 if (!contents.size())
1415 QDataStream s(contents);
1424 const QStringList &filterAttributes)
const
1429 const QString filterlessQuery =
1431 "NamespaceTable.Name, "
1432 "FolderTable.Name, "
1433 "ContentsTable.Data, "
1434 "VersionTable.Version "
1440 "WHERE ContentsTable.NamespaceId = NamespaceTable.Id "
1441 "AND NamespaceTable.Id = FolderTable.NamespaceId "
1442 "AND ContentsTable.NamespaceId = NamespaceTable.Id "
1443 "AND VersionTable.NamespaceId = NamespaceTable.Id"_L1;
1445 const QString filterQuery = filterlessQuery
1446 + prepareFilterQuery(filterAttributes.size(),
"ContentsTable"_L1,
"Id"_L1,
1447 "ContentsFilterTable"_L1,
"ContentsId"_L1);
1449 m_query->prepare(filterQuery);
1450 bindFilterQuery(m_query.get(), 0, filterAttributes);
1454 QMap<QString, QMap<QVersionNumber, ContentsData>> contentsMap;
1456 while (m_query->next()) {
1457 const QString namespaceName = m_query->value(0).toString();
1458 const QByteArray contents = m_query->value(2).toByteArray();
1459 const QString versionString = m_query->value(3).toString();
1461 const QString title = getTitle(contents);
1462 const QVersionNumber version = QVersionNumber::fromString(versionString);
1464 ContentsData &contentsData = contentsMap[title][version];
1465 contentsData.namespaceName = namespaceName;
1466 contentsData.folderName = m_query->value(1).toString();
1467 contentsData.contentsList.append(contents);
1470 QList<QHelpCollectionHandler::ContentsData> result;
1471 for (
const auto &versionContents : std::as_const(contentsMap)) {
1473 const auto itBegin = versionContents.constBegin();
1474 auto it = versionContents.constEnd();
1475 while (it != itBegin) {
1477 result.append(it.value());
1488 const QString filterlessQuery =
1490 "NamespaceTable.Name, "
1491 "FolderTable.Name, "
1492 "ContentsTable.Data, "
1493 "VersionTable.Version "
1499 "WHERE ContentsTable.NamespaceId = NamespaceTable.Id "
1500 "AND NamespaceTable.Id = FolderTable.NamespaceId "
1501 "AND ContentsTable.NamespaceId = NamespaceTable.Id "
1502 "AND VersionTable.NamespaceId = NamespaceTable.Id"_L1;
1504 const QString filterQuery = filterlessQuery + prepareFilterQuery(filterName);
1506 m_query->prepare(filterQuery);
1507 bindFilterQuery(m_query.get(), 0, filterName);
1511 QMap<QString, QMap<QVersionNumber, ContentsData>> contentsMap;
1513 while (m_query->next()) {
1514 const QString namespaceName = m_query->value(0).toString();
1515 const QByteArray contents = m_query->value(2).toByteArray();
1516 const QString versionString = m_query->value(3).toString();
1518 const QString title = getTitle(contents);
1519 const QVersionNumber version = QVersionNumber::fromString(versionString);
1521 ContentsData &contentsData = contentsMap[title][version];
1522 contentsData.namespaceName = namespaceName;
1523 contentsData.folderName = m_query->value(1).toString();
1524 contentsData.contentsList.append(contents);
1527 QList<QHelpCollectionHandler::ContentsData> result;
1528 for (
const auto &versionContents : std::as_const(contentsMap)) {
1530 const auto itBegin = versionContents.constBegin();
1531 auto it = versionContents.constEnd();
1532 while (it != itBegin) {
1534 result.append(it.value());
1545 m_query->prepare(
"DELETE FROM SettingsTable WHERE Key=?"_L1);
1546 m_query->bindValue(0, key);
1547 return m_query->exec();
1551 const QVariant &defaultValue)
const
1554 return defaultValue;
1556 m_query->prepare(
"SELECT COUNT(Key) FROM SettingsTable WHERE Key=?"_L1);
1557 m_query->bindValue(0, key);
1558 if (!m_query->exec() || !m_query->next() || !m_query->value(0).toInt()) {
1560 return defaultValue;
1564 m_query->prepare(
"SELECT Value FROM SettingsTable WHERE Key=?"_L1);
1565 m_query->bindValue(0, key);
1566 if (m_query->exec() && m_query->next()) {
1567 const QVariant &value = m_query->value(0);
1571 return defaultValue;
1575 const QVariant &value)
1580 m_query->prepare(
"SELECT Value FROM SettingsTable WHERE Key=?"_L1);
1581 m_query->bindValue(0, key);
1583 if (m_query->next()) {
1584 m_query->prepare(
"UPDATE SettingsTable SET Value=? where Key=?"_L1);
1585 m_query->bindValue(0, value);
1586 m_query->bindValue(1, key);
1588 m_query->prepare(
"INSERT INTO SettingsTable VALUES(?, ?)"_L1);
1589 m_query->bindValue(0, key);
1590 m_query->bindValue(1, value);
1592 return m_query->exec();
1601 m_query->exec(
"SELECT Name FROM FilterAttributeTable"_L1);
1603 while (m_query->next())
1604 atts.insert(m_query->value(0).toString());
1606 for (
const QStringList &attributeSet : attributeSets) {
1607 for (
const QString &attribute : attributeSet) {
1608 if (!atts.contains(attribute)) {
1609 m_query->prepare(
"INSERT INTO FilterAttributeTable VALUES(NULL, ?)"_L1);
1610 m_query->bindValue(0, attribute);
1615 return registerFileAttributeSets(attributeSets, nsId);
1624 if (attributeSets.isEmpty())
1628 QVariantList attributeSetIds;
1629 QVariantList filterAttributeIds;
1631 if (!m_query->exec(
"SELECT MAX(FilterAttributeSetId) FROM FileAttributeSetTable"_L1)
1632 || !m_query->next()) {
1636 int attributeSetId = m_query->value(0).toInt();
1638 for (
const QStringList &attributeSet : attributeSets) {
1641 for (
const QString &attribute : attributeSet) {
1642 m_query->prepare(
"SELECT Id FROM FilterAttributeTable WHERE Name=?"_L1);
1643 m_query->bindValue(0, attribute);
1645 if (!m_query->exec() || !m_query->next())
1649 attributeSetIds.append(attributeSetId);
1650 filterAttributeIds.append(m_query->value(0).toInt());
1654 m_query->prepare(
"INSERT INTO FileAttributeSetTable "
1655 "(NamespaceId, FilterAttributeSetId, FilterAttributeId) "
1656 "VALUES(?, ?, ?)"_L1);
1657 m_query->addBindValue(nsIds);
1658 m_query->addBindValue(attributeSetIds);
1659 m_query->addBindValue(filterAttributeIds);
1660 return m_query->execBatch();
1667 m_query->exec(
"SELECT Name FROM FilterAttributeTable"_L1);
1668 while (m_query->next())
1669 list.append(m_query->value(0).toString());
1680 "FilterAttributeTable.Name "
1682 "FilterAttributeTable, "
1685 "WHERE FilterAttributeTable.Id = FilterTable.FilterAttributeId "
1686 "AND FilterTable.NameId = FilterNameTable.Id "
1687 "AND FilterNameTable.Name=?"_L1);
1688 m_query->bindValue(0, filterName);
1690 while (m_query->next())
1691 list.append(m_query->value(0).toString());
1703 "FileAttributeSetTable.FilterAttributeSetId, "
1704 "FilterAttributeTable.Name "
1706 "FileAttributeSetTable, "
1707 "FilterAttributeTable, "
1709 "WHERE FileAttributeSetTable.FilterAttributeId = FilterAttributeTable.Id "
1710 "AND FileAttributeSetTable.NamespaceId = NamespaceTable.Id "
1711 "AND NamespaceTable.Name = ? "
1712 "ORDER BY FileAttributeSetTable.FilterAttributeSetId"_L1);
1713 m_query->bindValue(0, namespaceName);
1716 QList<QStringList> result;
1717 while (m_query->next()) {
1718 const int id = m_query->value(0).toInt();
1720 result.append(QStringList());
1723 result.last().append(m_query->value(1).toString());
1726 if (result.isEmpty())
1727 result.append(QStringList());
1738 "VersionTable.Version "
1742 "WHERE NamespaceTable.Name = ? "
1743 "AND NamespaceTable.Id = VersionTable.NamespaceId"_L1);
1744 m_query->bindValue(0, namespaceName);
1745 if (!m_query->exec() || !m_query->next())
1748 const QString ret = m_query->value(0).toString();
1755 const int errorValue = -1;
1759 m_query->prepare(
"SELECT COUNT(Id) FROM NamespaceTable WHERE Name=?"_L1);
1760 m_query->bindValue(0, nspace);
1762 while (m_query->next()) {
1763 if (m_query->value(0).toInt() > 0) {
1764 emit error(tr(
"Namespace %1 already exists.").arg(nspace));
1769 QFileInfo fi(m_collectionFile);
1770 m_query->prepare(
"INSERT INTO NamespaceTable VALUES(NULL, ?, ?)"_L1);
1771 m_query->bindValue(0, nspace);
1772 m_query->bindValue(1, fi.absoluteDir().relativeFilePath(fileName));
1773 int namespaceId = errorValue;
1774 if (m_query->exec()) {
1775 namespaceId = m_query->lastInsertId().toInt();
1778 if (namespaceId < 1) {
1779 emit error(tr(
"Cannot register namespace \"%1\".").arg(nspace));
1790 m_query->prepare(
"INSERT INTO FolderTable VALUES(NULL, ?, ?)"_L1);
1791 m_query->bindValue(0, namespaceId);
1792 m_query->bindValue(1, folderName);
1795 if (m_query->exec()) {
1796 virtualId = m_query->lastInsertId().toInt();
1799 if (virtualId < 1) {
1800 emit error(tr(
"Cannot register virtual folder '%1'.").arg(folderName));
1803 if (registerComponent(folderName, namespaceId) < 0)
1810 m_query->prepare(
"SELECT ComponentId FROM ComponentTable WHERE Name = ?"_L1);
1811 m_query->bindValue(0, componentName);
1812 if (!m_query->exec())
1815 if (!m_query->next()) {
1816 m_query->prepare(
"INSERT INTO ComponentTable VALUES(NULL, ?)"_L1);
1817 m_query->bindValue(0, componentName);
1818 if (!m_query->exec())
1821 m_query->prepare(
"SELECT ComponentId FROM ComponentTable WHERE Name = ?"_L1);
1822 m_query->bindValue(0, componentName);
1823 if (!m_query->exec() || !m_query->next())
1827 const int componentId = m_query->value(0).toInt();
1829 m_query->prepare(
"INSERT INTO ComponentMapping VALUES(?, ?)"_L1);
1830 m_query->bindValue(0, componentId);
1831 m_query->bindValue(1, namespaceId);
1832 if (!m_query->exec())
1843 m_query->prepare(
"INSERT INTO VersionTable (NamespaceId, Version) VALUES(?, ?)"_L1);
1844 m_query->addBindValue(namespaceId);
1845 m_query->addBindValue(version);
1846 return m_query->exec();
1850 const QString &nameSpace,
bool createDefaultVersionFilter)
1855 m_query->prepare(
"SELECT Id, FilePath FROM NamespaceTable WHERE Name=?"_L1);
1856 m_query->bindValue(0, nameSpace);
1858 if (!m_query->next())
1861 const int nsId = m_query->value(0).toInt();
1862 const QString fileName = m_query->value(1).toString();
1864 m_query->prepare(
"SELECT Id, Name FROM FolderTable WHERE NamespaceId=?"_L1);
1865 m_query->bindValue(0, nsId);
1867 if (!m_query->next())
1870 const int vfId = m_query->value(0).toInt();
1871 const QString vfName = m_query->value(1).toString();
1873 const QString absFileName = absoluteDocPath(fileName);
1874 QHelpDBReader reader(absFileName, QHelpGlobal::uniquifyConnectionName(
1875 fileName,
this),
this);
1879 registerComponent(vfName, nsId);
1880 registerVersion(reader.version(), nsId);
1881 if (!registerFileAttributeSets(reader.filterAttributeSets(), nsId))
1884 if (!registerIndexTable(reader.indexTable(), nsId, vfId, fileName))
1887 if (createDefaultVersionFilter)
1888 createVersionFilter(reader.version());
1894 if (version.isEmpty())
1897 const QVersionNumber versionNumber = QVersionNumber::fromString(version);
1898 if (versionNumber.isNull())
1901 const QString filterName = tr(
"Version %1").arg(version);
1902 if (filters().contains(filterName))
1905 QHelpFilterData filterData;
1906 filterData.setVersions({versionNumber});
1907 setFilterData(filterName, filterData);
1911 int nsId,
int vfId,
const QString &fileName)
1915 QMap<QString, QVariantList> filterAttributeToNewFileId;
1917 QVariantList fileFolderIds;
1918 QVariantList fileNames;
1919 QVariantList fileTitles;
1920 const int fileSize = indexTable.fileItems.size();
1921 fileFolderIds.reserve(fileSize);
1922 fileNames.reserve(fileSize);
1923 fileTitles.reserve(fileSize);
1925 if (!m_query->exec(
"SELECT MAX(FileId) FROM FileNameTable"_L1) || !m_query->next())
1928 const int maxFileId = m_query->value(0).toInt();
1931 for (
const QHelpDBReader::FileItem &item : indexTable.fileItems) {
1932 fileFolderIds.append(vfId);
1933 fileNames.append(item.name);
1934 fileTitles.append(item.title);
1936 for (
const QString &filterAttribute : item.filterAttributes)
1937 filterAttributeToNewFileId[filterAttribute].append(maxFileId + newFileId + 1);
1941 m_query->prepare(
"INSERT INTO FileNameTable VALUES(?, ?, NULL, ?)"_L1);
1942 m_query->addBindValue(fileFolderIds);
1943 m_query->addBindValue(fileNames);
1944 m_query->addBindValue(fileTitles);
1945 if (!m_query->execBatch())
1948 for (
auto it = filterAttributeToNewFileId.cbegin(),
1949 end = filterAttributeToNewFileId.cend(); it != end; ++it) {
1950 const QString filterAttribute = it.key();
1951 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
1952 m_query->bindValue(0, filterAttribute);
1953 if (!m_query->exec() || !m_query->next())
1956 const int attributeId = m_query->value(0).toInt();
1958 QVariantList attributeIds;
1959 for (
int i = 0; i < it.value().size(); i++)
1960 attributeIds.append(attributeId);
1962 m_query->prepare(
"INSERT INTO FileFilterTable VALUES(?, ?)"_L1);
1963 m_query->addBindValue(attributeIds);
1964 m_query->addBindValue(it.value());
1965 if (!m_query->execBatch())
1969 QMap<QString, QVariantList> filterAttributeToNewIndexId;
1971 if (!m_query->exec(
"SELECT MAX(Id) FROM IndexTable"_L1) || !m_query->next())
1974 const int maxIndexId = m_query->value(0).toInt();
1977 QVariantList indexNames;
1978 QVariantList indexIdentifiers;
1979 QVariantList indexNamespaceIds;
1980 QVariantList indexFileIds;
1981 QVariantList indexAnchors;
1982 const int indexSize = indexTable.indexItems.size();
1983 indexNames.reserve(indexSize);
1984 indexIdentifiers.reserve(indexSize);
1985 indexNamespaceIds.reserve(indexSize);
1986 indexFileIds.reserve(indexSize);
1987 indexAnchors.reserve(indexSize);
1989 for (
const QHelpDBReader::IndexItem &item : indexTable.indexItems) {
1990 indexNames.append(item.name);
1991 indexIdentifiers.append(item.identifier);
1992 indexNamespaceIds.append(nsId);
1993 indexFileIds.append(maxFileId + item.fileId + 1);
1994 indexAnchors.append(item.anchor);
1996 for (
const QString &filterAttribute : item.filterAttributes)
1997 filterAttributeToNewIndexId[filterAttribute].append(maxIndexId + newIndexId + 1);
2001 m_query->prepare(
"INSERT INTO IndexTable VALUES(NULL, ?, ?, ?, ?, ?)"_L1);
2002 m_query->addBindValue(indexNames);
2003 m_query->addBindValue(indexIdentifiers);
2004 m_query->addBindValue(indexNamespaceIds);
2005 m_query->addBindValue(indexFileIds);
2006 m_query->addBindValue(indexAnchors);
2007 if (!m_query->execBatch())
2010 for (
auto it = filterAttributeToNewIndexId.cbegin(),
2011 end = filterAttributeToNewIndexId.cend(); it != end; ++it) {
2012 const QString filterAttribute = it.key();
2013 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2014 m_query->bindValue(0, filterAttribute);
2015 if (!m_query->exec() || !m_query->next())
2018 const int attributeId = m_query->value(0).toInt();
2020 QVariantList attributeIds;
2021 for (
int i = 0; i < it.value().size(); i++)
2022 attributeIds.append(attributeId);
2024 m_query->prepare(
"INSERT INTO IndexFilterTable VALUES(?, ?)"_L1);
2025 m_query->addBindValue(attributeIds);
2026 m_query->addBindValue(it.value());
2027 if (!m_query->execBatch())
2031 QMap<QString, QVariantList> filterAttributeToNewContentsId;
2033 QVariantList contentsNsIds;
2034 QVariantList contentsData;
2035 const int contentsSize = indexTable.contentsItems.size();
2036 contentsNsIds.reserve(contentsSize);
2037 contentsData.reserve(contentsSize);
2039 if (!m_query->exec(
"SELECT MAX(Id) FROM ContentsTable"_L1) || !m_query->next())
2042 const int maxContentsId = m_query->value(0).toInt();
2044 int newContentsId = 0;
2045 for (
const QHelpDBReader::ContentsItem &item : indexTable.contentsItems) {
2046 contentsNsIds.append(nsId);
2047 contentsData.append(item.data);
2049 for (
const QString &filterAttribute : item.filterAttributes) {
2050 filterAttributeToNewContentsId[filterAttribute]
2051 .append(maxContentsId + newContentsId + 1);
2056 m_query->prepare(
"INSERT INTO ContentsTable VALUES(NULL, ?, ?)"_L1);
2057 m_query->addBindValue(contentsNsIds);
2058 m_query->addBindValue(contentsData);
2059 if (!m_query->execBatch())
2062 for (
auto it = filterAttributeToNewContentsId.cbegin(),
2063 end = filterAttributeToNewContentsId.cend(); it != end; ++it) {
2064 const QString filterAttribute = it.key();
2065 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2066 m_query->bindValue(0, filterAttribute);
2067 if (!m_query->exec() || !m_query->next())
2070 const int attributeId = m_query->value(0).toInt();
2072 QVariantList attributeIds;
2073 for (
int i = 0; i < it.value().size(); i++)
2074 attributeIds.append(attributeId);
2076 m_query->prepare(
"INSERT INTO ContentsFilterTable VALUES(?, ?)"_L1);
2077 m_query->addBindValue(attributeIds);
2078 m_query->addBindValue(it.value());
2079 if (!m_query->execBatch())
2083 QVariantList filterNsIds;
2084 QVariantList filterAttributeIds;
2085 for (
const QString &filterAttribute : indexTable.usedFilterAttributes) {
2086 filterNsIds.append(nsId);
2088 m_query->prepare(
"SELECT Id From FilterAttributeTable WHERE Name = ?"_L1);
2089 m_query->bindValue(0, filterAttribute);
2090 if (!m_query->exec() || !m_query->next())
2093 filterAttributeIds.append(m_query->value(0).toInt());
2096 m_query->prepare(
"INSERT INTO OptimizedFilterTable "
2097 "(NamespaceId, FilterAttributeId) VALUES(?, ?)"_L1);
2098 m_query->addBindValue(filterNsIds);
2099 m_query->addBindValue(filterAttributeIds);
2100 if (!m_query->execBatch())
2103 m_query->prepare(
"INSERT INTO TimeStampTable "
2104 "(NamespaceId, FolderId, FilePath, Size, TimeStamp) "
2105 "VALUES(?, ?, ?, ?, ?)"_L1);
2106 m_query->addBindValue(nsId);
2107 m_query->addBindValue(vfId);
2108 m_query->addBindValue(fileName);
2109 const QFileInfo fi(absoluteDocPath(fileName));
2110 m_query->addBindValue(fi.size());
2111 QDateTime lastModified = fi.lastModified(QTimeZone::UTC);
2112 if (qEnvironmentVariableIsSet(
"SOURCE_DATE_EPOCH")) {
2113 const QString sourceDateEpochStr = qEnvironmentVariable(
"SOURCE_DATE_EPOCH");
2115 const qlonglong sourceDateEpoch = sourceDateEpochStr.toLongLong(&ok);
2116 if (ok && sourceDateEpoch < lastModified.toSecsSinceEpoch())
2117 lastModified.setSecsSinceEpoch(sourceDateEpoch);
2119 m_query->addBindValue(lastModified);
2120 if (!m_query->exec())
2129 m_query->prepare(
"DELETE FROM IndexFilterTable WHERE IndexId IN "
2130 "(SELECT Id FROM IndexTable WHERE NamespaceId = ?)"_L1);
2131 m_query->bindValue(0, nsId);
2132 if (!m_query->exec())
2135 m_query->prepare(
"DELETE FROM IndexTable WHERE NamespaceId = ?"_L1);
2136 m_query->bindValue(0, nsId);
2137 if (!m_query->exec())
2140 m_query->prepare(
"DELETE FROM FileFilterTable WHERE FileId IN "
2141 "(SELECT FileId FROM FileNameTable WHERE FolderId = ?)"_L1);
2142 m_query->bindValue(0, vfId);
2143 if (!m_query->exec())
2146 m_query->prepare(
"DELETE FROM FileNameTable WHERE FolderId = ?"_L1);
2147 m_query->bindValue(0, vfId);
2148 if (!m_query->exec())
2151 m_query->prepare(
"DELETE FROM ContentsFilterTable WHERE ContentsId IN "
2152 "(SELECT Id FROM ContentsTable WHERE NamespaceId = ?)"_L1);
2153 m_query->bindValue(0, nsId);
2154 if (!m_query->exec())
2157 m_query->prepare(
"DELETE FROM ContentsTable WHERE NamespaceId = ?"_L1);
2158 m_query->bindValue(0, nsId);
2159 if (!m_query->exec())
2162 m_query->prepare(
"DELETE FROM FileAttributeSetTable WHERE NamespaceId = ?"_L1);
2163 m_query->bindValue(0, nsId);
2164 if (!m_query->exec())
2167 m_query->prepare(
"DELETE FROM OptimizedFilterTable WHERE NamespaceId = ?"_L1);
2168 m_query->bindValue(0, nsId);
2169 if (!m_query->exec())
2172 m_query->prepare(
"DELETE FROM TimeStampTable WHERE NamespaceId = ?"_L1);
2173 m_query->bindValue(0, nsId);
2174 if (!m_query->exec())
2177 m_query->prepare(
"DELETE FROM VersionTable WHERE NamespaceId = ?"_L1);
2178 m_query->bindValue(0, nsId);
2179 if (!m_query->exec())
2182 m_query->prepare(
"SELECT ComponentId FROM ComponentMapping WHERE NamespaceId = ?"_L1);
2183 m_query->bindValue(0, nsId);
2184 if (!m_query->exec())
2187 if (!m_query->next())
2190 const int componentId = m_query->value(0).toInt();
2192 m_query->prepare(
"DELETE FROM ComponentMapping WHERE NamespaceId = ?"_L1);
2193 m_query->bindValue(0, nsId);
2194 if (!m_query->exec())
2197 m_query->prepare(
"SELECT ComponentId FROM ComponentMapping WHERE ComponentId = ?"_L1);
2198 m_query->bindValue(0, componentId);
2199 if (!m_query->exec())
2202 if (!m_query->next()) {
2203 m_query->prepare(
"DELETE FROM ComponentTable WHERE ComponentId = ?"_L1);
2204 m_query->bindValue(0, componentId);
2205 if (!m_query->exec())
2213 const QString &relFileName,
const QString &anchor)
2216 url.setScheme(
"qthelp"_L1);
2217 url.setAuthority(ns);
2218 url.setPath(u'/' + folder + u'/' + relFileName);
2219 url.setFragment(anchor);
2224 const QString &id,
const QStringList &filterAttributes)
const
2226 return documentsForField(
"Identifier"_L1, id, filterAttributes);
2230 const QString &keyword,
const QStringList &filterAttributes)
const
2232 return documentsForField(
"Name"_L1, keyword, filterAttributes);
2236 const QString &fieldValue,
const QStringList &filterAttributes)
const
2241 const QString filterlessQuery =
2243 "FileNameTable.Title, "
2244 "NamespaceTable.Name, "
2245 "FolderTable.Name, "
2246 "FileNameTable.Name, "
2247 "IndexTable.Anchor "
2253 "WHERE IndexTable.FileId = FileNameTable.FileId "
2254 "AND FileNameTable.FolderId = FolderTable.Id "
2255 "AND IndexTable.NamespaceId = NamespaceTable.Id "
2256 "AND IndexTable.%1 = ?"_L1.arg(fieldName);
2258 const QString filterQuery = filterlessQuery
2259 + prepareFilterQuery(filterAttributes.size(),
"IndexTable"_L1,
"Id"_L1,
2260 "IndexFilterTable"_L1,
"IndexId"_L1);
2262 m_query->prepare(filterQuery);
2263 m_query->bindValue(0, fieldValue);
2264 bindFilterQuery(m_query.get(), 1, filterAttributes);
2268 QList<QHelpLink> docList;
2269 while (m_query->next()) {
2270 QString title = m_query->value(0).toString();
2271 if (title.isEmpty())
2272 title = fieldValue +
" : "_L1 + m_query->value(3).toString();
2274 const QUrl url = buildQUrl(m_query->value(1).toString(),
2275 m_query->value(2).toString(),
2276 m_query->value(3).toString(),
2277 m_query->value(4).toString());
2278 docList.append(QHelpLink {url, title});
2284 const QString &id,
const QString &filterName)
const
2286 return documentsForField(
"Identifier"_L1, id, filterName);
2290 const QString &keyword,
const QString &filterName)
const
2292 return documentsForField(
"Name"_L1, keyword, filterName);
2296 const QString &fieldValue,
const QString &filterName)
const
2298 QMultiMap<QString, QUrl> linkMap;
2299 const auto documents = documentsForField(fieldName, fieldValue, filterName);
2300 for (
const auto &document : documents)
2301 linkMap.insert(document.title, document.url);
2306 const QString &fieldValue,
const QString &filterName)
const
2311 const QString filterlessQuery =
2313 "FileNameTable.Title, "
2314 "NamespaceTable.Name, "
2315 "FolderTable.Name, "
2316 "FileNameTable.Name, "
2317 "IndexTable.Anchor "
2323 "WHERE IndexTable.FileId = FileNameTable.FileId "
2324 "AND FileNameTable.FolderId = FolderTable.Id "
2325 "AND IndexTable.NamespaceId = NamespaceTable.Id "
2326 "AND IndexTable.%1 = ?"_L1.arg(fieldName);
2328 const QString filterQuery = filterlessQuery
2329 + prepareFilterQuery(filterName)
2330 +
" ORDER BY LOWER(FileNameTable.Title), FileNameTable.Title"_L1;
2332 m_query->prepare(filterQuery);
2333 m_query->bindValue(0, fieldValue);
2334 bindFilterQuery(m_query.get(), 1, filterName);
2338 QList<QHelpLink> docList;
2339 while (m_query->next()) {
2340 QString title = m_query->value(0).toString();
2341 if (title.isEmpty())
2342 title = fieldValue +
" : "_L1 + m_query->value(3).toString();
2344 const QUrl url = buildQUrl(m_query->value(1).toString(),
2345 m_query->value(2).toString(),
2346 m_query->value(3).toString(),
2347 m_query->value(4).toString());
2348 docList.append(QHelpLink {url, title});
2355 QStringList namespaceList;
2358 return namespaceList;
2360 const QString filterlessQuery =
2362 "NamespaceTable.Name "
2367 const QString filterQuery = filterlessQuery
2368 + prepareFilterQuery(filterName);
2370 m_query->prepare(filterQuery);
2371 bindFilterQuery(m_query.get(), 0, filterName);
2375 while (m_query->next())
2376 namespaceList.append(m_query->value(0).toString());
2377 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)
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)