File indexing completed on 2025-03-09 03:54:59

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam
0004  *
0005  * Date        : 02-02-2012
0006  * Description : Face database interface to train identities.
0007  *
0008  * SPDX-FileCopyrightText: 2012-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0009  * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText:      2019 by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "facedb_p.h"
0017 
0018 namespace Digikam
0019 {
0020 
0021 FaceDb::FaceDb(FaceDbBackend* const db)
0022     : d(new Private)
0023 {
0024     d->db = db;
0025 }
0026 
0027 FaceDb::~FaceDb()
0028 {
0029     delete d;
0030 }
0031 
0032 BdEngineBackend::QueryState FaceDb::setSetting(const QString& keyword, const QString& value)
0033 {
0034     QMap<QString, QVariant> parameters;
0035     parameters.insert(QLatin1String(":keyword"), keyword);
0036     parameters.insert(QLatin1String(":value"), value);
0037 
0038     return d->db->execDBAction(d->db->getDBAction(QLatin1String("ReplaceFaceSetting")), parameters);
0039 }
0040 
0041 QString FaceDb::setting(const QString& keyword) const
0042 {
0043     QMap<QString, QVariant> parameters;
0044     parameters.insert(QLatin1String(":keyword"), keyword);
0045     QList<QVariant> values;
0046 
0047     // TODO Should really check return status here
0048 
0049     BdEngineBackend::QueryState queryStateResult = d->db->execDBAction(d->db->getDBAction(QLatin1String("SelectFaceSetting")), parameters, &values);
0050     qCDebug(DIGIKAM_FACEDB_LOG) << "FaceDB SelectFaceSetting val ret = " << (BdEngineBackend::QueryStateEnum)queryStateResult;
0051 
0052     if (values.isEmpty())
0053     {
0054         return QString();
0055     }
0056     else
0057     {
0058         return values.first().toString();
0059     }
0060 }
0061 
0062 bool FaceDb::integrityCheck()
0063 {
0064     QList<QVariant> values;
0065     d->db->execDBAction(d->db->getDBAction(QString::fromUtf8("checkRecognitionDbIntegrity")), &values);
0066 
0067     switch (d->db->databaseType())
0068     {
0069         case BdEngineBackend::DbType::SQLite:
0070         {
0071             // For SQLite the integrity check returns a single row with one string column "ok" on success and multiple rows on error.
0072 
0073             return(
0074                     (values.size() == 1) &&
0075                     (values.first().toString().toLower().compare(QLatin1String("ok")) == 0)
0076                   );
0077         }
0078 
0079         case BdEngineBackend::DbType::MySQL:
0080         {
0081             // For MySQL, for every checked table, the table name, operation (check), message type (status) and the message text (ok on success)
0082             // are returned. So we check if there are four elements and if yes, whether the fourth element is "ok".
0083 /*
0084             qCDebug(DIGIKAM_DATABASE_LOG) << "MySQL check returned " << values.size() << " rows";
0085 */
0086             if ((values.size() % 4) != 0)
0087             {
0088                 return false;
0089             }
0090 
0091             for (QList<QVariant>::iterator it = values.begin() ; it != values.end() ; )
0092             {
0093                 QString tableName   = (*it).toString();
0094                 ++it;
0095                 QString operation   = (*it).toString();
0096                 ++it;
0097                 QString messageType = (*it).toString();
0098                 ++it;
0099                 QString messageText = (*it).toString();
0100                 ++it;
0101 
0102                 if (messageText.toLower().compare(QLatin1String("ok")) != 0)
0103                 {
0104                     qCDebug(DIGIKAM_DATABASE_LOG) << "Failed integrity check for table " << tableName << ". Reason:" << messageText;
0105                     return false;
0106                 }
0107                 else
0108                 {
0109                     qCDebug(DIGIKAM_DATABASE_LOG) << "Passed integrity check for table " << tableName;
0110                 }
0111             }
0112 
0113             // No error conditions. Db passed the integrity check.
0114 
0115             return true;
0116         }
0117 
0118         default:
0119         {
0120             return false;
0121         }
0122     }
0123 }
0124 
0125 void FaceDb::vacuum()
0126 {
0127     d->db->execDBAction(d->db->getDBAction(QString::fromUtf8("vacuumRecognitionDB")));
0128 }
0129 
0130 } // namespace Digikam