Skip to content

Commit db6d5fa

Browse files
committed
Database: add mysql
1 parent ad85de8 commit db6d5fa

File tree

10 files changed

+395
-62
lines changed

10 files changed

+395
-62
lines changed

App/Client/Favorite/FavoriteDatabase.cpp

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CFavoriteDatabase::CFavoriteDatabase(QObject *parent)
1818
qDebug(log) << Q_FUNC_INFO;
1919
}
2020

21-
bool CFavoriteDatabase::OnInitializeDatabase()
21+
bool CFavoriteDatabase::OnInitializeSqliteDatabase()
2222
{
2323
QSqlQuery query(GetDatabase());
2424

@@ -69,10 +69,86 @@ bool CFavoriteDatabase::OnInitializeDatabase()
6969
if (!success) {
7070
qWarning(log) << "Failed to create trigger delete_icon_after_favorite." << query.lastError().text();
7171
}
72+
success = CDatabaseTree::OnInitializeSqliteDatabase();
73+
return success;
74+
}
75+
76+
bool CFavoriteDatabase::OnInitializeMySqlDatabase()
77+
{
78+
bool success = false;
79+
QSqlQuery query(GetDatabase());
80+
81+
// Create favorite table
82+
success = query.exec(
83+
"CREATE TABLE IF NOT EXISTS favorite ("
84+
" id INTEGER PRIMARY KEY AUTO_INCREMENT," // the id is the key of tree table
85+
" name TEXT NOT NULL,"
86+
" icon INTEGER DEFAULT 0,"
87+
" file TEXT NOT NULL,"
88+
" description TEXT,"
89+
" UNIQUE KEY idx_favorite_file (file(255))"
90+
")"
91+
);
92+
93+
if (!success) {
94+
qCritical(log) << "Failed to create favorite table:"
95+
<< query.lastError().text()
96+
<< "Sql:" << query.executedQuery();
97+
return false;
98+
}
99+
100+
// Drop trigger if exists
101+
if (!query.exec("DROP TRIGGER IF EXISTS delete_icon_after_favorite")) {
102+
qDebug(log) << "Failed to drop trigger delete_icon_after_favorite:"
103+
<< query.lastError().text()
104+
<< "Sql:" << query.executedQuery();
105+
}
106+
// Create trigger
107+
QString szSql = R"(
108+
CREATE TRIGGER delete_icon_after_favorite
109+
AFTER DELETE ON favorite
110+
FOR EACH ROW
111+
BEGIN
112+
DECLARE favorite_count INT DEFAULT 0;
113+
DECLARE recent_count INT DEFAULT 0;
114+
115+
IF OLD.icon != 0 THEN
116+
-- 统计favorite表中引用该icon的数量
117+
SELECT COUNT(*) INTO favorite_count
118+
FROM favorite
119+
WHERE icon = OLD.icon;
120+
121+
-- 统计recent表中引用该icon的数量
122+
SELECT COUNT(*) INTO recent_count
123+
FROM recent
124+
WHERE icon = OLD.icon;
125+
126+
-- 如果都没有引用,则删除icon
127+
IF favorite_count = 0 AND recent_count = 0 THEN
128+
DELETE FROM icon WHERE id = OLD.icon;
129+
END IF;
130+
END IF;
131+
END
132+
)";
133+
success = query.exec(szSql);
134+
if (!success) {
135+
qWarning(log) << "Failed to create trigger delete_icon_after_favorite."
136+
<< query.lastError().text()
137+
<< "Sql:" << query.executedQuery();
138+
}
139+
140+
return CDatabaseTree::OnInitializeMySqlDatabase();
141+
}
72142

73-
m_IconDB.SetDatabase(GetDatabase());
74-
m_IconDB.OnInitializeDatabase();
75-
return CDatabaseTree::OnInitializeDatabase();
143+
bool CFavoriteDatabase::OnInitializeDatabase()
144+
{
145+
bool bRet = false;
146+
bRet = CDatabaseTree::OnInitializeDatabase();
147+
if(!bRet) return false;
148+
149+
m_IconDB.SetDatabase(GetDatabase(), m_pPara);
150+
bRet = m_IconDB.OnInitializeDatabase();
151+
return bRet;
76152
}
77153

78154
int CFavoriteDatabase::AddFavorite(const QString &szFile,

App/Client/Favorite/FavoriteDatabase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class CFavoriteDatabase : public CDatabaseTree
5454

5555
private:
5656
bool OnInitializeDatabase() override;
57+
bool OnInitializeSqliteDatabase() override;
58+
bool OnInitializeMySqlDatabase() override;
5759
CDatabaseIcon m_IconDB;
5860

5961
// CDatabaseTree interface

App/Client/Recent/RecentDatabase.cpp

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ CRecentDatabase::~CRecentDatabase()
2828
}
2929

3030
bool CRecentDatabase::OnInitializeDatabase()
31+
{
32+
bool bRet = false;
33+
bRet = CDatabase::OnInitializeDatabase();
34+
if(!bRet) return false;
35+
36+
// Create icon table
37+
m_IconDB.SetDatabase(GetDatabase(), m_pPara);
38+
bRet = m_IconDB.OnInitializeDatabase();
39+
return bRet;
40+
}
41+
42+
bool CRecentDatabase::OnInitializeSqliteDatabase()
3143
{
3244
QSqlQuery query(GetDatabase());
3345

@@ -54,11 +66,15 @@ bool CRecentDatabase::OnInitializeDatabase()
5466
// Create index
5567
success = query.exec("CREATE INDEX IF NOT EXISTS idx_recent_file ON recent(file)");
5668
if(!success) {
57-
qWarning(log) << "Failed to drop index idx_recent_file:" << query.lastError().text();
69+
qWarning(log) << "Failed to drop index idx_recent_file:"
70+
<< query.lastError().text()
71+
<< "Sql:" << query.executedQuery();
5872
}
5973

6074
if (!query.exec("DROP TRIGGER IF EXISTS delete_icon_after_recent")) {
61-
qDebug(log) << "Failed to drop trigger delete_icon_after_recent:" << query.lastError().text();
75+
qDebug(log) << "Failed to drop trigger delete_icon_after_recent:"
76+
<< query.lastError().text()
77+
<< "Sql:" << query.executedQuery();
6278
}
6379

6480
QString szSql = R"(
@@ -79,12 +95,82 @@ bool CRecentDatabase::OnInitializeDatabase()
7995
)";
8096
success = query.exec(szSql);
8197
if (!success) {
82-
qWarning(log) << "Failed to create trigger delete_icon_after_recent." << query.lastError().text();
98+
qWarning(log) << "Failed to create trigger delete_icon_after_recent."
99+
<< query.lastError().text()
100+
<< "Sql:" << query.executedQuery();
101+
}
102+
103+
return true;
104+
}
105+
106+
bool CRecentDatabase::OnInitializeMySqlDatabase()
107+
{
108+
bool success = false;
109+
QSqlQuery query(GetDatabase());
110+
111+
// Create recent table
112+
success = query.exec(
113+
"CREATE TABLE IF NOT EXISTS recent ("
114+
" id INTEGER PRIMARY KEY AUTO_INCREMENT,"
115+
" operate_id TEXT NOT NULL,"
116+
" icon INTEGER DEFAULT 0,"
117+
" name TEXT NOT NULL,"
118+
" protocol TEXT,"
119+
" operate_type TEXT,"
120+
" file TEXT NOT NULL,"
121+
" time DATETIME DEFAULT CURRENT_TIMESTAMP,"
122+
" description TEXT,"
123+
" UNIQUE KEY uk_recent_file (file(255))"
124+
")"
125+
);
126+
127+
if (!success) {
128+
qCritical(log) << "Failed to create recent table:"
129+
<< query.lastError().text()
130+
<< "Sql:" << query.executedQuery();
131+
return false;
132+
}
133+
134+
// Create trigger
135+
if (!query.exec("DROP TRIGGER IF EXISTS delete_icon_after_recent")) {
136+
qDebug(log) << "Failed to drop trigger delete_icon_after_recent:"
137+
<< query.lastError().text()
138+
<< "Sql:" << query.executedQuery();
139+
}
140+
141+
QString szSql = R"(
142+
CREATE TRIGGER delete_icon_after_recent
143+
AFTER DELETE ON recent
144+
FOR EACH ROW
145+
BEGIN
146+
DECLARE favorite_count INT DEFAULT 0;
147+
DECLARE recent_count INT DEFAULT 0;
148+
149+
IF OLD.icon != 0 THEN
150+
-- 统计favorite表中引用该icon的数量
151+
SELECT COUNT(*) INTO favorite_count
152+
FROM favorite
153+
WHERE icon = OLD.icon;
154+
155+
-- 统计recent表中引用该icon的数量
156+
SELECT COUNT(*) INTO recent_count
157+
FROM recent
158+
WHERE icon = OLD.icon;
159+
160+
-- 如果都没有引用,则删除icon
161+
IF favorite_count = 0 AND recent_count = 0 THEN
162+
DELETE FROM icon WHERE id = OLD.icon;
163+
END IF;
164+
END IF;
165+
END
166+
)";
167+
success = query.exec(szSql);
168+
if (!success) {
169+
qWarning(log) << "Failed to create trigger delete_icon_after_recent."
170+
<< query.lastError().text()
171+
<< "Sql:" << query.executedQuery();
83172
}
84173

85-
// Create icon table
86-
m_IconDB.SetDatabase(GetDatabase());
87-
m_IconDB.OnInitializeDatabase();
88174
return true;
89175
}
90176

App/Client/Recent/RecentDatabase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class CRecentDatabase : public CDatabase
3535

3636
private:
3737
bool OnInitializeDatabase() override;
38+
virtual bool OnInitializeSqliteDatabase() override;
39+
virtual bool OnInitializeMySqlDatabase() override;
3840
virtual bool ExportToJson(QJsonObject &obj) override;
3941
virtual bool ImportFromJson(const QJsonObject &obj) override;
4042

App/Client/Resource/Database/mysql.sql

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
-- 创建表
2-
CREATE TABLE favorite_folders (
3-
id INTEGER PRIMARY KEY AUTO_INCREMENT,
4-
name TEXT NOT NULL,
5-
parent_id INTEGER DEFAULT 0,
6-
sort_order INTEGER DEFAULT 0,
7-
created_time DATETIME DEFAULT CURRENT_TIMESTAMP
2+
CREATE TABLE `favorite_folders` (
3+
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
4+
`name` TEXT NOT NULL,
5+
`parent_id` INTEGER DEFAULT 0,
6+
`sort_order` INTEGER DEFAULT 0,
7+
`created_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
8+
-- 直接在表定义中添加索引
9+
INDEX `idx_parent` (`parent_id`),
10+
INDEX `idx_parent_sort` (`parent_id`, `sort_order`)
811
);
912

10-
-- 创建索引
11-
CREATE INDEX idx_favorite_folders_parent ON favorite_folders(parent_id);
12-
1313
-- 创建表
1414
CREATE TABLE favorite_tree (
1515
id INTEGER PRIMARY KEY AUTO_INCREMENT,
@@ -31,12 +31,11 @@ CREATE TABLE icon (
3131
name TEXT,
3232
hash TEXT,
3333
data LONGBLOB,
34-
UNIQUE KEY uk_icon_name (name(255))
35-
);
3634

37-
-- 创建索引
38-
CREATE INDEX idx_icon_hash ON icon(hash(255));
39-
CREATE INDEX idx_icon_name ON icon(name(255));
35+
-- 创建索引
36+
UNIQUE KEY idx_icon_name (name(255)),
37+
UNIQUE KEY idx_icon_name (hash(255))
38+
);
4039

4140
-- 创建表
4241
CREATE TABLE recent (
@@ -49,24 +48,22 @@ CREATE TABLE recent (
4948
file TEXT NOT NULL,
5049
time DATETIME DEFAULT CURRENT_TIMESTAMP,
5150
description TEXT,
52-
UNIQUE KEY uk_recent_file (file(255))
53-
);
5451

55-
-- 创建索引
56-
CREATE INDEX idx_recent_file ON recent(file(255));
52+
-- 创建索引
53+
UNIQUE KEY idx_recent_file (file(255))
54+
);
5755

5856
-- 创建表
59-
CREATE TABLE favorite (
60-
id INTEGER PRIMARY KEY AUTO_INCREMENT,
61-
name TEXT NOT NULL,
62-
icon INTEGER DEFAULT 0,
63-
file TEXT NOT NULL,
64-
description TEXT,
65-
UNIQUE KEY uk_favorite_file (file(255))
66-
);
57+
CREATE TABLE `favorite` (
58+
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
59+
`name` TEXT NOT NULL,
60+
`icon` INTEGER DEFAULT 0,
61+
`file` TEXT NOT NULL,
62+
`description` TEXT,
6763

68-
-- 创建索引
69-
CREATE INDEX idx_favorite_file ON favorite(file(255));
64+
-- 创建索引
65+
UNIQUE KEY `idx_favorite_file` (`file`(255))
66+
);
7067

7168
-- 首先设置分隔符
7269
DELIMITER $$

0 commit comments

Comments
 (0)