From e1aa32894da3a865c5b0183ea8c631b9e2655ebb Mon Sep 17 00:00:00 2001 From: codestation Date: Fri, 24 Jan 2014 13:08:35 -0430 Subject: [PATCH] Renamed database class to qlistdb in preparation to create generic database class. --- qcma.pro | 12 +++---- src/clientmanager.h | 4 +-- src/cmaclient.h | 2 +- src/cmaevent.cpp | 6 ++-- src/cmaevent.h | 4 +-- src/forms/backupmanagerform.h | 4 +-- src/{database.cpp => qlistdb.cpp} | 56 +++++++++++++++---------------- src/{database.h => qlistdb.h} | 12 +++---- src/sqlitedb.cpp | 34 ++++++++++++------- 9 files changed, 72 insertions(+), 62 deletions(-) rename src/{database.cpp => qlistdb.cpp} (88%) rename src/{database.h => qlistdb.h} (95%) diff --git a/qcma.pro b/qcma.pro index 39291a5..85c3f2a 100644 --- a/qcma.pro +++ b/qcma.pro @@ -18,7 +18,6 @@ TEMPLATE = app SOURCES += src/main.cpp \ src/capability.cpp \ - src/database.cpp \ src/cmaobject.cpp \ src/cmarootobject.cpp \ src/utils.cpp \ @@ -33,18 +32,18 @@ SOURCES += src/main.cpp \ src/filterlineedit.cpp \ src/dds.cpp \ src/sqlitedb.cpp \ + src/httpdownloader.cpp \ + src/qlistdb.cpp \ # forms src/forms/backupitem.cpp \ src/forms/backupmanagerform.cpp \ src/forms/configwidget.cpp \ src/forms/confirmdialog.cpp \ src/forms/pinform.cpp \ - src/forms/progressform.cpp \ - src/httpdownloader.cpp + src/forms/progressform.cpp HEADERS += \ src/capability.h \ - src/database.h \ src/cmaobject.h \ src/cmarootobject.h \ src/utils.h \ @@ -59,14 +58,15 @@ HEADERS += \ src/filterlineedit.h \ src/dds.h \ src/sqlitedb.h \ + src/httpdownloader.h \ + src/qlistdb.h \ # forms src/forms/backupitem.h \ src/forms/backupmanagerform.h \ src/forms/configwidget.h \ src/forms/confirmdialog.h \ src/forms/pinform.h \ - src/forms/progressform.h \ - src/httpdownloader.h + src/forms/progressform.h FORMS += \ src/forms/configwidget.ui \ diff --git a/src/clientmanager.h b/src/clientmanager.h index 9ff8792..54bfede 100644 --- a/src/clientmanager.h +++ b/src/clientmanager.h @@ -20,7 +20,7 @@ #ifndef CLIENTMANAGER_H #define CLIENTMANAGER_H -#include "database.h" +#include "qlistdb.h" #include "forms/pinform.h" #include "forms/progressform.h" @@ -37,7 +37,7 @@ public: void start(); void stop(); - Database db; + QListDB db; private: int thread_count; diff --git a/src/cmaclient.h b/src/cmaclient.h index d2023f8..93bc550 100644 --- a/src/cmaclient.h +++ b/src/cmaclient.h @@ -20,7 +20,7 @@ #ifndef CMACLIENT_H #define CMACLIENT_H -#include "database.h" +#include "qlistdb.h" #include "cmaevent.h" #include "cmaobject.h" #include "cmabroadcast.h" diff --git a/src/cmaevent.cpp b/src/cmaevent.cpp index 8ea2f7b..1a7c8ba 100644 --- a/src/cmaevent.cpp +++ b/src/cmaevent.cpp @@ -29,7 +29,7 @@ #include -Database *CmaEvent::db = NULL; +QListDB *CmaEvent::db = NULL; QFile *CmaEvent::m_file = NULL; metadata_t CmaEvent::g_thumbmeta = {0, 0, 0, NULL, NULL, 0, 0, 0, Thumbnail, {{17, 240, 136, 0, 1, 1.0f, 2}}, NULL}; @@ -412,7 +412,7 @@ void CmaEvent::vitaEventSendObject(vita_event_t *event, int eventId) qDebug("Searching object with OHFI %d", ohfi); - Database::find_data iters; + QListDB::find_data iters; if(!db->find(ohfi, iters)) { qWarning("Failed to find OHFI %d", ohfi); VitaMTP_ReportResult(device, eventId, PTP_RC_VITA_Invalid_OHFI); @@ -816,7 +816,7 @@ void CmaEvent::vitaEventOperateObject(vita_event_t *event, int eventId) //rename the current object root->rename(operateobject.title); - Database::find_data iters; + QListDB::find_data iters; db->find(root->metadata.ohfi, iters); // rename the rest of the list only if has the renamed parent in some part of the chain diff --git a/src/cmaevent.h b/src/cmaevent.h index e2f1d98..3ff4e92 100644 --- a/src/cmaevent.h +++ b/src/cmaevent.h @@ -21,7 +21,7 @@ #define CMAEVENT_H #include "cmaobject.h" -#include "database.h" +#include "qlistdb.h" #include "httpdownloader.h" #include @@ -39,7 +39,7 @@ public: void vitaEventCancelTask(vita_event_t *event, int eventId); // don't make the db reference static - static Database *db; + static QListDB *db; private: uint16_t processAllObjects(CMAObject *parent, uint32_t handle); diff --git a/src/forms/backupmanagerform.h b/src/forms/backupmanagerform.h index e09fef7..eddeb63 100644 --- a/src/forms/backupmanagerform.h +++ b/src/forms/backupmanagerform.h @@ -20,7 +20,7 @@ #ifndef BACKUPMANAGERFORM_H #define BACKUPMANAGERFORM_H -#include "database.h" +#include "qlistdb.h" #include "backupitem.h" #include @@ -37,7 +37,7 @@ public: explicit BackupManagerForm(QWidget *parent = 0); ~BackupManagerForm(); - Database *db; + QListDB *db; private: void setupForm(); diff --git a/src/database.cpp b/src/qlistdb.cpp similarity index 88% rename from src/database.cpp rename to src/qlistdb.cpp index a7a95c2..eff9f9a 100644 --- a/src/database.cpp +++ b/src/qlistdb.cpp @@ -18,7 +18,7 @@ */ #include "utils.h" -#include "database.h" +#include "qlistdb.h" #include "cmaobject.h" #include @@ -28,7 +28,7 @@ #include #include -Database::Database() : +QListDB::QListDB() : mutex(QMutex::Recursive) { QString uuid = QSettings().value("lastAccountId", "ffffffffffffffff").toString(); @@ -43,7 +43,7 @@ Database::Database() : connect(timer, SIGNAL(timeout()), this, SLOT(process())); } -Database::~Database() +QListDB::~QListDB() { destroy(); timer->stop(); @@ -53,13 +53,13 @@ Database::~Database() delete thread; } -void Database::setUUID(const QString uuid) +void QListDB::setUUID(const QString uuid) { CMARootObject::uuid = uuid; QSettings().setValue("lastAccountId", uuid); } -bool Database::reload(bool &prepared) +bool QListDB::reload(bool &prepared) { if(mutex.tryLock()) { if(CMARootObject::uuid != "ffffffffffffffff") { @@ -76,7 +76,7 @@ bool Database::reload(bool &prepared) } } -void Database::process() +void QListDB::process() { destroy(); cancel_operation = false; @@ -90,19 +90,19 @@ void Database::process() mutex.unlock(); } -void Database::cancelOperation() +void QListDB::cancelOperation() { QMutexLocker locker(&cancel); cancel_operation = true; } -bool Database::continueOperation() +bool QListDB::continueOperation() { QMutexLocker locker(&cancel); return !cancel_operation; } -int Database::create() +int QListDB::create() { int total_objects = 0; //QMutexLocker locker(&mutex); @@ -153,7 +153,7 @@ int Database::create() return total_objects; } -CMAObject *Database::getParent(CMAObject *last_dir, const QString ¤t_path) +CMAObject *QListDB::getParent(CMAObject *last_dir, const QString ¤t_path) { while(last_dir && current_path != last_dir->path) { last_dir = last_dir->parent; @@ -162,7 +162,7 @@ CMAObject *Database::getParent(CMAObject *last_dir, const QString ¤t_path) return last_dir; } -int Database::scanRootDirectory(root_list &list, int ohfi_type) +int QListDB::scanRootDirectory(root_list &list, int ohfi_type) { int file_type = -1; int total_objects = 0; @@ -201,7 +201,7 @@ int Database::scanRootDirectory(root_list &list, int ohfi_type) return total_objects; } -int Database::recursiveScanRootDirectory(root_list &list, CMAObject *parent, int ohfi_type) +int QListDB::recursiveScanRootDirectory(root_list &list, CMAObject *parent, int ohfi_type) { int file_type = -1; int total_objects = 0; @@ -236,7 +236,7 @@ int Database::recursiveScanRootDirectory(root_list &list, CMAObject *parent, int return total_objects; } -void Database::destroy() +void QListDB::destroy() { //QMutexLocker locker(&mutex); @@ -249,7 +249,7 @@ void Database::destroy() object_list.clear(); } -bool Database::removeInternal(root_list &list, const CMAObject *obj) +bool QListDB::removeInternal(root_list &list, const CMAObject *obj) { bool found = false; QList::iterator it = list.begin(); @@ -270,7 +270,7 @@ bool Database::removeInternal(root_list &list, const CMAObject *obj) return found; } -bool Database::remove(const CMAObject *obj, int ohfi_root) +bool QListDB::remove(const CMAObject *obj, int ohfi_root) { QMutexLocker locker(&mutex); @@ -286,12 +286,12 @@ bool Database::remove(const CMAObject *obj, int ohfi_root) return false; } -bool Database::lessThanComparator(const CMAObject *a, const CMAObject *b) +bool QListDB::lessThanComparator(const CMAObject *a, const CMAObject *b) { return a->metadata.ohfi < b->metadata.ohfi; } -bool Database::hasFilter(const CMARootObject *object,int ohfi) +bool QListDB::hasFilter(const CMARootObject *object,int ohfi) { for(int i = 0; i < object->num_filters; i++) { if(object->filters[i].ohfi == ohfi) { @@ -301,20 +301,20 @@ bool Database::hasFilter(const CMARootObject *object,int ohfi) return false; } -bool Database::findInternal(const root_list &list, int ohfi, find_data &data) +bool QListDB::findInternal(const root_list &list, int ohfi, find_data &data) { if(hasFilter(static_cast(list.first()), ohfi)) { data.it = list.begin(); } else { CMAObject obj; obj.setOhfi(ohfi); - data.it = qBinaryFind(list.begin(), list.end(), &obj, Database::lessThanComparator); + data.it = qBinaryFind(list.begin(), list.end(), &obj, QListDB::lessThanComparator); } data.end = list.end(); return data.it != data.end; } -bool Database::find(int ohfi, Database::find_data &data) +bool QListDB::find(int ohfi, QListDB::find_data &data) { QMutexLocker locker(&mutex); @@ -326,7 +326,7 @@ bool Database::find(int ohfi, Database::find_data &data) return false; } -void Database::append(int parent_ohfi, CMAObject *object) +void QListDB::append(int parent_ohfi, CMAObject *object) { QMutexLocker locker(&mutex); CMAObject parent; @@ -334,7 +334,7 @@ void Database::append(int parent_ohfi, CMAObject *object) for(map_list::iterator root = object_list.begin(); root != object_list.end(); ++root) { root_list *cat_list = &(*root); - root_list::const_iterator it = qBinaryFind(cat_list->begin(), cat_list->end(), &parent, Database::lessThanComparator); + root_list::const_iterator it = qBinaryFind(cat_list->begin(), cat_list->end(), &parent, QListDB::lessThanComparator); if(it != cat_list->end()) { cat_list->append(object); @@ -343,14 +343,14 @@ void Database::append(int parent_ohfi, CMAObject *object) } } -CMAObject *Database::ohfiToObject(int ohfi) +CMAObject *QListDB::ohfiToObject(int ohfi) { QMutexLocker locker(&mutex); find_data data; return find(ohfi, data) ? *data.it : NULL; } -CMAObject *Database::pathToObjectInternal(const root_list &list, const char *path) +CMAObject *QListDB::pathToObjectInternal(const root_list &list, const char *path) { // skip the first element since is the root element root_list::const_iterator skipped_first = ++list.begin(); @@ -363,7 +363,7 @@ CMAObject *Database::pathToObjectInternal(const root_list &list, const char *pat return NULL; } -CMAObject *Database::pathToObject(const char *path, int ohfiRoot) +CMAObject *QListDB::pathToObject(const char *path, int ohfiRoot) { QMutexLocker locker(&mutex); @@ -381,7 +381,7 @@ CMAObject *Database::pathToObject(const char *path, int ohfiRoot) return NULL; } -int Database::acceptFilteredObject(const CMAObject *parent, const CMAObject *current, int type) +int QListDB::acceptFilteredObject(const CMAObject *parent, const CMAObject *current, int type) { QMutexLocker locker(&mutex); int result = 0; @@ -413,7 +413,7 @@ int Database::acceptFilteredObject(const CMAObject *parent, const CMAObject *cur return result; } -void Database::dumpMetadataList(const metadata_t *p_head) +void QListDB::dumpMetadataList(const metadata_t *p_head) { while(p_head) { qDebug("Metadata: %s with OHFI %d", p_head->name, p_head->ohfi); @@ -421,7 +421,7 @@ void Database::dumpMetadataList(const metadata_t *p_head) } } -int Database::filterObjects(int ohfiParent, metadata_t **p_head, int index, int max_number) +int QListDB::filterObjects(int ohfiParent, metadata_t **p_head, int index, int max_number) { QMutexLocker locker(&mutex); CMARootObject *parent = static_cast(ohfiToObject(ohfiParent)); diff --git a/src/database.h b/src/qlistdb.h similarity index 95% rename from src/database.h rename to src/qlistdb.h index a2effc2..edbb560 100644 --- a/src/database.h +++ b/src/qlistdb.h @@ -17,8 +17,8 @@ * along with this program. If not, see . */ -#ifndef DATABASE_H -#define DATABASE_H +#ifndef QLISTDB_H +#define QLISTDB_H #include "cmarootobject.h" @@ -30,7 +30,7 @@ #include -class Database : public QObject +class QListDB : public QObject { Q_OBJECT public: @@ -39,8 +39,8 @@ public: QList::const_iterator end; } find_data; - explicit Database(); - ~Database(); + explicit QListDB(); + ~QListDB(); bool reload(bool &prepared); void setUUID(const QString uuid); @@ -97,4 +97,4 @@ public slots: void cancelOperation(); }; -#endif // DATABASE_H +#endif // QLISTDB_H diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index d6f1742..6ba19af 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -156,12 +156,14 @@ bool SQLiteDB::initialize() for(unsigned int i = 0; i < sizeof(table_list) / sizeof(const char *); i++) { if(!query.exec(table_list[i])) { + qDebug() << query.lastError(); return false; } } for(unsigned int i = 0; i < sizeof(trigger_list) / sizeof(const char *); i++) { if(!query.exec(trigger_list[i])) { + qDebug() << query.lastError(); return false; } } @@ -275,35 +277,28 @@ int SQLiteDB::recursiveScanRootDirectory(const QString &base_path, int parent, i int SQLiteDB::getPathId(const QString &path) { - if (!db.isOpen()) { - return -1; - } QSqlQuery query(QString("SELECT object_id from sources WHERE path = %1").arg(path)); if(query.next()) { return query.value(0).toInt(); } else { + qDebug() << query.lastError(); return -1; } } QString SQLiteDB::getPathFromId(int ohfi) { - if (!db.isOpen()) { - return QString(); - } QSqlQuery query(QString("SELECT path FROM sources WHERE object_id = %1").arg(ohfi)); if(query.next()) { return query.value(0).toString(); } else { + qDebug() << query.lastError(); return QString(); } } bool SQLiteDB::updateSize(int ohfi, quint64 size) { - if (!db.isOpen()) { - return false; - } QSqlQuery query(QString("UPDATE sources SET size = %1 WHERE object_id == %2").arg(size).arg(ohfi)); return query.exec(); } @@ -311,7 +306,11 @@ bool SQLiteDB::updateSize(int ohfi, quint64 size) bool SQLiteDB::deleteEntry(int ohfi) { QSqlQuery query(QString("DELETE FROM object_node WHERE object_id == %1").arg(ohfi)); - return query.exec(); + bool ret = query.exec(); + if(!ret) { + qDebug() << query.lastError(); + } + return ret; } bool SQLiteDB::deleteEntry(const QString &path) @@ -329,13 +328,19 @@ bool SQLiteDB::updateAdjacencyList(int ohfi, int parent) if(query.exec() && query.next()) { return true; + } else { + qDebug() << query.lastError(); } query.prepare("INSERT INTO adjacent_objects (parent_id, child_id)" "VALUES (:parentid, :child_id)"); query.bindValue(0, parent); query.bindValue(1, ohfi); - return query.exec(); + bool ret = query.exec(); + if(!ret) { + qDebug() << query.lastError(); + } + return ret; } uint SQLiteDB::insertDirectoryEntry(const QString &path, int type, int parent) @@ -376,6 +381,7 @@ uint SQLiteDB::insertObjectEntry(const char *title, int type) query.bindValue(1, title); if(!query.exec() || !query.exec("SELECT last_insert_rowid()") || !query.next()) { + qDebug() << query.lastError(); return 0; } //} @@ -401,7 +407,11 @@ bool SQLiteDB::insertSourceEntry(uint object_id, const QString &path) query.bindValue(2, size); query.bindValue(3, date_created); query.bindValue(4, date_modified); - return query.exec(); + bool ret = query.exec(); + if(!ret) { + qDebug() << query.lastError(); + } + return ret; } uint SQLiteDB::insertMusicEntry(const QString &path, int type, int parent)