@@ -81,31 +81,41 @@ void CDatabase::CloseDatabase()
8181 QSqlDatabase::removeDatabase (m_szConnectName);
8282}
8383
84- CDatabaseIcon::CDatabaseIcon (QObject *parent) : CDatabase(parent)
84+ CDatabaseIcon::CDatabaseIcon (QObject *parent)
85+ : CDatabase(parent)
8586{
8687 m_szConnectName = " icon_connect" ;
88+ m_szTableName = " icon" ;
89+ }
90+
91+ CDatabaseIcon::CDatabaseIcon (const QString &szPrefix, QObject *parent)
92+ : CDatabaseIcon(parent)
93+ {
94+ if (!szPrefix.isEmpty ())
95+ m_szTableName = szPrefix + " _" + m_szTableName;
8796}
8897
8998bool CDatabaseIcon::OnInitializeDatabase ()
9099{
91100 QSqlQuery query (GetDatabase ());
92101
93102 // Create icon table
94- bool success = query. exec (
95- " CREATE TABLE IF NOT EXISTS icon ("
103+ QString szSql =
104+ " CREATE TABLE IF NOT EXISTS " + m_szTableName + " ("
96105 " id INTEGER PRIMARY KEY AUTOINCREMENT,"
97106 " name TEXT UNIQUE," // Icon name. see QIcon::name()
98107 " hash TEXT," // Icon hash value
99108 " data BLOB," // Icon binary data
100109 " visit_time DATETIME DEFAULT CURRENT_TIMESTAMP"
101110 " )"
102- );
111+ ;
112+ bool success = query.exec (szSql);
103113 if (!success) {
104- qCritical (log) << " Failed to create icon table:" << query.lastError ().text ();
114+ qCritical (log) << " Failed to create icon table:" << m_szTableName << query.lastError ().text ();
105115 return false ;
106116 }
107- query.exec (" CREATE INDEX IF NOT EXISTS idx_icon_name ON icon (name)" );
108- query.exec (" CREATE INDEX IF NOT EXISTS idx_icon_hash ON icon (hash)" );
117+ query.exec (" CREATE INDEX IF NOT EXISTS idx_ " + m_szTableName + " _name ON " + m_szTableName + " (name)" );
118+ query.exec (" CREATE INDEX IF NOT EXISTS idx_ " + m_szTableName + " _hash ON " + m_szTableName + " (hash)" );
109119 return true ;
110120}
111121
@@ -114,6 +124,7 @@ int CDatabaseIcon::GetIcon(const QIcon &icon)
114124 bool bRet = false ;
115125 if (icon.isNull ()) return 0 ;
116126
127+ QString szSql;
117128 QSqlQuery query (GetDatabase ());
118129 QString szName = icon.name ();
119130 if (szName.isEmpty ()) {
@@ -122,8 +133,11 @@ int CDatabaseIcon::GetIcon(const QIcon &icon)
122133 QString szHash = RabbitCommon::CIconUtils::hashIconData (data);
123134 if (data.isEmpty () || szHash.isEmpty ())
124135 return 0 ;
125- query.prepare (" SELECT id, data FROM icon WHERE hash=:hash" );
136+ szSql = " SELECT id, data FROM " + m_szTableName + " WHERE hash=:hash" ;
137+ query.prepare (szSql);
126138 query.bindValue (" :hash" , szHash);
139+ // qDebug(log) << "prepare:" << query.executedQuery();
140+ // qDebug(log) << "Bound values:" << query.boundValues();
127141 bRet = query.exec ();
128142 if (!bRet) {
129143 qCritical (log) << " Failed to select icon hash:"
@@ -137,10 +151,9 @@ int CDatabaseIcon::GetIcon(const QIcon &icon)
137151 }
138152 }
139153
140- query.prepare (
141- " INSERT INTO icon (hash, data) "
142- " VALUES (:hash, :data)"
143- );
154+ szSql = " INSERT INTO " + m_szTableName + " (hash, data) "
155+ " VALUES (:hash, :data)" ;
156+ query.prepare (szSql);
144157 query.bindValue (" :hash" , szHash);
145158 query.bindValue (" :data" , data);
146159 bRet = query.exec ();
@@ -153,7 +166,8 @@ int CDatabaseIcon::GetIcon(const QIcon &icon)
153166 }
154167
155168 // Check name
156- query.prepare (" SELECT id FROM icon WHERE name=:name" );
169+ szSql = " SELECT id FROM " + m_szTableName + " WHERE name=:name" ;
170+ query.prepare (szSql);
157171 query.bindValue (" :name" , szName);
158172 bRet = query.exec ();
159173 if (!bRet) {
@@ -166,7 +180,8 @@ int CDatabaseIcon::GetIcon(const QIcon &icon)
166180 }
167181
168182 // Insert icon
169- query.prepare (" INSERT INTO icon (name) VALUES (:name)" );
183+ szSql = " INSERT INTO " + m_szTableName + " (name) VALUES (:name)" ;
184+ query.prepare (szSql);
170185 query.bindValue (" :name" , szName);
171186 bRet = query.exec ();
172187 if (!bRet) {
@@ -182,8 +197,8 @@ QIcon CDatabaseIcon::GetIcon(int id)
182197 QIcon icon;
183198 QSqlQuery query (GetDatabase ());
184199 query.prepare (
185- " SELECT name, data FROM icon "
186- " WHERE id=:id "
200+ " SELECT name, data FROM " + m_szTableName +
201+ " WHERE id=:id "
187202 );
188203 query.bindValue (" :id" , id);
189204 bool bRet = query.exec ();
@@ -203,187 +218,3 @@ QIcon CDatabaseIcon::GetIcon(int id)
203218 }
204219 return icon;
205220}
206-
207- CDatabaseFolder::CDatabaseFolder (QObject *parent) : CDatabase(parent)
208- {
209- m_szConnectName = " folder_connect" ;
210- }
211-
212- bool CDatabaseFolder::OnInitializeDatabase ()
213- {
214- QSqlQuery query (GetDatabase ());
215-
216- // 启用外键约束
217- query.exec (" PRAGMA foreign_keys = ON" );
218-
219- // 创建文件夹表
220- bool success = query.exec (
221- " CREATE TABLE IF NOT EXISTS folders ("
222- " id INTEGER PRIMARY KEY AUTOINCREMENT,"
223- " name TEXT NOT NULL,"
224- " parent_id INTEGER DEFAULT 0,"
225- " sort_order INTEGER DEFAULT 0,"
226- " created_time DATETIME DEFAULT CURRENT_TIMESTAMP"
227- " )"
228- );
229-
230- if (!success) {
231- qCritical (log) << " Failed to create folders table:" << query.lastError ().text ();
232- return false ;
233- }
234-
235- // 创建索引
236- query.exec (" CREATE INDEX IF NOT EXISTS idx_folders_parent ON folders(parent_id)" );
237-
238- return true ;
239- }
240-
241-
242- bool CDatabaseFolder::AddFolder (const QString &name, int parentId)
243- {
244- QSqlQuery query (GetDatabase ());
245-
246- // 获取最大排序值
247- query.prepare (" SELECT MAX(sort_order) FROM folders WHERE parent_id = :parent_id" );
248- query.bindValue (" :parent_id" , parentId);
249- query.exec ();
250-
251- int maxOrder = 0 ;
252- if (query.next ()) {
253- maxOrder = query.value (0 ).toInt () + 1 ;
254- }
255-
256- query.prepare (
257- " INSERT INTO folders (name, parent_id, sort_order) "
258- " VALUES (:name, :parent_id, :sort_order)"
259- );
260- query.bindValue (" :name" , name);
261- query.bindValue (" :parent_id" , parentId);
262- query.bindValue (" :sort_order" , maxOrder);
263-
264- bool success = query.exec ();
265-
266- if (success)
267- emit sigChanged ();
268- else
269- qCritical (log) << " Failed to add folders:" << query.lastError ().text ();
270-
271- return success;
272- }
273-
274- bool CDatabaseFolder::RenameFolder (int folderId, const QString &newName)
275- {
276- QSqlQuery query (GetDatabase ());
277- query.prepare (" UPDATE folders SET name = :name WHERE id = :id" );
278- query.bindValue (" :id" , folderId);
279- query.bindValue (" :name" , newName);
280-
281- bool success = query.exec ();
282-
283- if (success)
284- emit sigChanged ();
285- else
286- qCritical (log) << " Failed to rename folders:" << query.lastError ().text ();
287-
288- return success;
289- }
290-
291- bool CDatabaseFolder::DeleteFolder (int folderId)
292- {
293- // 删除子目录
294- auto folders = GetSubFolders (folderId);
295- foreach (auto f, folders) {
296- DeleteFolder (f.id );
297- }
298-
299- // 删除其下面的所有条目
300- OnDeleteItems ();
301-
302- // 删除文件夹
303- QSqlQuery query (GetDatabase ());
304- query.prepare (" DELETE FROM folders WHERE id = :id" );
305- query.bindValue (" :id" , folderId);
306-
307- bool success = query.exec ();
308-
309- if (success)
310- emit sigChanged ();
311- else
312- qCritical (log) << " Failed to delete folders:" << query.lastError ().text ();
313-
314- return success;
315- }
316-
317- bool CDatabaseFolder::MoveFolder (int folderId, int newParentId)
318- {
319- QSqlQuery query (GetDatabase ());
320- query.prepare (" UPDATE folders SET parent_id = :parent_id WHERE id = :id" );
321- query.bindValue (" :id" , folderId);
322- query.bindValue (" :parent_id" , newParentId);
323-
324- bool success = query.exec ();
325-
326- if (success)
327- emit sigChanged ();
328- else
329- qCritical (log) << " Failed to move folders:" << query.lastError ().text ();
330-
331- return success;
332- }
333-
334-
335- QList<CDatabaseFolder::FolderItem> CDatabaseFolder::GetAllFolders ()
336- {
337- QList<CDatabaseFolder::FolderItem> folders;
338-
339- QSqlQuery query (GetDatabase ());
340- query.prepare (
341- " SELECT id, name, parent_id, sort_order, created_time "
342- " FROM folders "
343- " ORDER BY parent_id, sort_order, name"
344- );
345-
346- if (query.exec ()) {
347- while (query.next ()) {
348- CDatabaseFolder::FolderItem folder;
349- folder.id = query.value (0 ).toInt ();
350- folder.szName = query.value (1 ).toString ();
351- folder.parentId = query.value (2 ).toInt ();
352- folder.sortOrder = query.value (3 ).toInt ();
353- folder.createTime = query.value (4 ).toDateTime ();
354-
355- folders.append (folder);
356- }
357- }
358-
359- return folders;
360- }
361-
362- QList<CDatabaseFolder::FolderItem> CDatabaseFolder::GetSubFolders (int parentId)
363- {
364- QList<CDatabaseFolder::FolderItem> folders;
365-
366- QSqlQuery query (GetDatabase ());
367- query.prepare (
368- " SELECT id, name, parent_id, sort_order, created_time "
369- " FROM folders "
370- " WHERE parent_id = :parent_id "
371- " ORDER BY sort_order, name"
372- );
373- query.bindValue (" :parent_id" , parentId);
374-
375- if (query.exec ()) {
376- while (query.next ()) {
377- CDatabaseFolder::FolderItem folder;
378- folder.id = query.value (0 ).toInt ();
379- folder.szName = query.value (1 ).toString ();
380- folder.parentId = query.value (2 ).toInt ();
381- folder.sortOrder = query.value (3 ).toInt ();
382- folder.createTime = query.value (4 ).toDateTime ();
383-
384- folders.append (folder);
385- }
386- }
387-
388- return folders;
389- }
0 commit comments