| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#ifndef DBMANAGER_H |
| 22 |
#define DBMANAGER_H |
| 23 |
|
| 24 |
// ImageTable |
| 25 |
/////////////////////////////////////////////////////// |
| 26 |
//FilePath | FileName | Dir | Time | ChangeTime // |
| 27 |
//TEXT primari key | TEXT | TEXT | TEXT | TEXT // |
| 28 |
/////////////////////////////////////////////////////// |
| 29 |
|
| 30 |
// AlbumTable |
| 31 |
///////////////////////////////////////////////////// |
| 32 |
//AP | AlbumName | FilePath // |
| 33 |
//TEXT primari key | TEXT | TEXT // |
| 34 |
///////////////////////////////////////////////////// |
| 35 |
|
| 36 |
#include <QObject> |
| 37 |
#include <QDateTime> |
| 38 |
#include <QMutex> |
| 39 |
#include <QDebug> |
| 40 |
#include <QSqlDatabase> |
| 41 |
#include "albumgloabl.h" |
| 42 |
//#include "connectionpool.h" |
| 43 |
|
| 44 |
const QString DATETIME_FORMAT_DATABASE = "yyyy.MM.dd hh:mm"; |
| 45 |
|
| 46 |
enum AlbumDBType { |
| 47 |
Favourite, |
| 48 |
Custom |
| 49 |
}; |
| 50 |
|
| 51 |
class QSqlDatabase; |
| 52 |
|
| 53 |
class DBManager : public QObject |
| 54 |
{ |
| 55 |
Q_OBJECT |
| 56 |
public: |
| 57 |
static DBManager *instance(); |
| 58 |
explicit DBManager(QObject *parent = nullptr); |
| 59 |
~DBManager() |
| 60 |
{ |
| 61 |
// ConnectionPool::release(); //释放数据库连接 |
| 62 |
} |
| 63 |
|
| 64 |
// TableImage |
| 65 |
const QStringList getAllPaths() const; |
| 66 |
const DBImgInfoList getAllInfos(int loadCount = 0) const; |
| 67 |
const QStringList getAllTimelines() const; |
| 68 |
const DBImgInfoList getInfosByTimeline(const QString &timeline) const; |
| 69 |
const QStringList getImportTimelines() const; |
| 70 |
const DBImgInfoList getInfosByImportTimeline(const QString &timeline) const; |
| 71 |
// const DBImgInfo getInfoByName(const QString &name) const; |
| 72 |
const DBImgInfo getInfoByPath(const QString &path) const; |
| 73 |
// const DBImgInfo getInfoByPathHash(const QString &pathHash) const; |
| 74 |
int getImgsCount() const; |
| 75 |
// bool isImgExist(const QString &path) const; |
| 76 |
void insertImgInfos(const DBImgInfoList &infos); |
| 77 |
void insertImgInfo(const DBImgInfo &info); |
| 78 |
void removeImgInfos(const QStringList &paths); |
| 79 |
void removeImgInfosNoSignal(const QStringList &paths); |
| 80 |
const DBImgInfoList getInfosForKeyword(const QString &keywords) const; |
| 81 |
const DBImgInfoList getTrashInfosForKeyword(const QString &keywords) const; |
| 82 |
const DBImgInfoList getInfosForKeyword(const QString &album, const QString &keywords) const; |
| 83 |
|
| 84 |
// TableAlbum |
| 85 |
const QMultiMap<QString, QString> getAllPathAlbumNames() const; |
| 86 |
const QStringList getAllAlbumNames(AlbumDBType atype = AlbumDBType::Custom) const; |
| 87 |
const QStringList getPathsByAlbum(const QString &album, AlbumDBType atype = AlbumDBType::Custom) const; |
| 88 |
const DBImgInfoList getInfosByAlbum(const QString &album, AlbumDBType atype = AlbumDBType::Custom) const; |
| 89 |
int getItemsCountByAlbum(const QString &album, const ItemType &type, AlbumDBType atype = AlbumDBType::Custom) const; |
| 90 |
// int getAlbumsCount() const; |
| 91 |
bool isAlbumExistInDB(const QString &album, AlbumDBType atype = AlbumDBType::Custom) const; |
| 92 |
bool isImgExistInAlbum(const QString &album, const QString &path, AlbumDBType atype = AlbumDBType::Custom) const; |
| 93 |
void insertIntoAlbum(const QString &album, const QStringList &paths, AlbumDBType atype = AlbumDBType::Custom); |
| 94 |
void removeAlbum(const QString &album, AlbumDBType atype = AlbumDBType::Custom); |
| 95 |
void removeFromAlbum(const QString &album, const QStringList &paths, AlbumDBType atype = AlbumDBType::Custom); |
| 96 |
void renameAlbum(const QString &oldAlbum, const QString &newAlbum, AlbumDBType atype = AlbumDBType::Custom); |
| 97 |
// void removeFromAlbumNoSignal(const QString &album, const QStringList &paths, AlbumDBType atype = AlbumDBType::Custom); |
| 98 |
void insertIntoAlbumNoSignal(const QString &album, const QStringList &paths, AlbumDBType atype = AlbumDBType::Custom); |
| 99 |
// TabelTrash |
| 100 |
const QStringList getAllTrashPaths() const; |
| 101 |
const DBImgInfoList getAllTrashInfos() const; |
| 102 |
void insertTrashImgInfos(const DBImgInfoList &infos); |
| 103 |
void removeTrashImgInfos(const QStringList &paths); |
| 104 |
void removeTrashImgInfosNoSignal(const QStringList &paths); |
| 105 |
const DBImgInfo getTrashInfoByPath(const QString &path) const; |
| 106 |
const DBImgInfoList getTrashImgInfos(const QString &key, const QString &value) const; |
| 107 |
int getTrashImgsCount() const; |
| 108 |
const QSqlDatabase getDatabase() const; |
| 109 |
private: |
| 110 |
const DBImgInfoList getInfosByNameTimeline(const QString &value) const; |
| 111 |
const DBImgInfoList getImgInfos(const QString &key, const QString &value, const bool &needlock = true) const; |
| 112 |
|
| 113 |
|
| 114 |
void checkDatabase(); |
| 115 |
static DBManager *m_dbManager; |
| 116 |
private: |
| 117 |
//QString m_connectionName; |
| 118 |
mutable QMutex m_mutex; |
| 119 |
|
| 120 |
// mutable QMutex m_mutex1; |
| 121 |
mutable QSqlDatabase m_db; |
| 122 |
}; |
| 123 |
|
| 124 |
#endif // DBMANAGER_H |