From cd2b9f798aad0946029bd29af081896731e95563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Wed, 20 Jun 2012 23:19:37 +0200 Subject: [PATCH] export comics info listo cambio de acceso a las columnas de las querys (enteros por literales) --- YACReaderLibrary/db/comic.cpp | 22 ++--- YACReaderLibrary/db/comic.h | 3 +- YACReaderLibrary/db/data_base_management.cpp | 84 ++++++++++++++++++-- YACReaderLibrary/db/data_base_management.h | 25 +++++- YACReaderLibrary/db/folder.cpp | 2 +- YACReaderLibrary/db/tablemodel.cpp | 2 +- YACReaderLibrary/db/tablemodel.h | 2 +- YACReaderLibrary/db/treemodel.cpp | 18 +++-- YACReaderLibrary/library_window.cpp | 1 + YACReaderLibrary/properties_dialog.cpp | 2 +- 10 files changed, 131 insertions(+), 30 deletions(-) diff --git a/YACReaderLibrary/db/comic.cpp b/YACReaderLibrary/db/comic.cpp index 1556512e..82a36f0a 100644 --- a/YACReaderLibrary/db/comic.cpp +++ b/YACReaderLibrary/db/comic.cpp @@ -45,7 +45,7 @@ QList Comic::getComicsFromParent(qulonglong parentId, QSqlDatabas data << record.value(i); //TODO sort by sort indicator and name currentItem = new Comic(); - currentItem->id = record.value(0).toLongLong(); + currentItem->id = record.value("id").toLongLong(); currentItem->parentId = record.value(1).toLongLong(); currentItem->name = record.value(2).toString(); currentItem->path = record.value(3).toString(); @@ -88,10 +88,10 @@ bool Comic::load(qulonglong id, QSqlDatabase & db) if(selectQuery.next()) { QSqlRecord record = selectQuery.record(); - id = record.value(0).toLongLong(); - parentId = record.value(1).toLongLong(); - name = record.value(2).toString(); - info.load(record.value(4).toString(),db); + id = record.value("id").toLongLong(); + parentId = record.value("parentId").toLongLong(); + name = record.value("name").toString(); + info.load(record.value("hash").toString(),db); return true; } return false; @@ -101,6 +101,7 @@ bool Comic::load(qulonglong id, QSqlDatabase & db) qulonglong Comic::insert(QSqlDatabase & db) { //TODO comprobar si ya hay comic info con ese hash + //TODO cambiar por info.insert(db) if(!info.existOnDb) { @@ -166,9 +167,10 @@ bool ComicInfo::load(QString hash, QSqlDatabase & db) QSqlRecord record = findComicInfo.record(); this->hash = hash; - this->id = record.value(0).toLongLong(); - this->name = record.value(2).toString(); - this->read = record.value(3).toBool(); + this->id = record.value("id").toLongLong(); + this->title = record.value("title").toString(); + this->read = record.value("read").toBool(); + this->edited = record.value("edited").toBool(); existOnDb = true; return true; } @@ -189,8 +191,8 @@ void ComicInfo::update(QSqlDatabase & db) { //db.open(); QSqlQuery findComicInfo(db); - findComicInfo.prepare("UPDATE comic_info SET name = :name, read = :read WHERE id = :id "); - findComicInfo.bindValue(":name", name); + findComicInfo.prepare("UPDATE comic_info SET title = :title, read = :read WHERE id = :id "); + findComicInfo.bindValue(":title", title); findComicInfo.bindValue(":read", read?1:0); findComicInfo.bindValue(":id", id); findComicInfo.exec(); diff --git a/YACReaderLibrary/db/comic.h b/YACReaderLibrary/db/comic.h index 41096a30..b6276922 100644 --- a/YACReaderLibrary/db/comic.h +++ b/YACReaderLibrary/db/comic.h @@ -17,8 +17,9 @@ public: qulonglong id; bool read; + bool edited; QString hash; - QString name; + QString title; bool existOnDb; }; diff --git a/YACReaderLibrary/db/data_base_management.cpp b/YACReaderLibrary/db/data_base_management.cpp index 38136ae1..6e31ac51 100644 --- a/YACReaderLibrary/db/data_base_management.cpp +++ b/YACReaderLibrary/db/data_base_management.cpp @@ -18,9 +18,14 @@ TreeModel * DataBaseManagement::newTreeModel(QString path) } QSqlDatabase DataBaseManagement::createDatabase(QString name, QString path) +{ + return createDatabase(QDir::cleanPath(path) + "/" + name + ".ydb"); +} + +QSqlDatabase DataBaseManagement::createDatabase(QString dest) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); - db.setDatabaseName(QDir::cleanPath(path) + "/" + name + ".ydb"); + db.setDatabaseName(dest); if (!db.open()) qDebug() << db.lastError(); else { @@ -63,7 +68,7 @@ bool DataBaseManagement::createTables(QSqlDatabase & database) //COMIC INFO (representa la información de un cómic, cada cómic tendrá un idéntificador único formado por un hash sha1'de los primeros 512kb' + su tamaño en bytes) QSqlQuery queryComicInfo(database); - queryComicInfo.prepare("CREATE TABLE comic_info (id INTEGER PRIMARY KEY, hash TEXT NOT NULL, name TEXT, read BOOLEAN)"); + queryComicInfo.prepare("CREATE TABLE comic_info (id INTEGER PRIMARY KEY, hash TEXT NOT NULL, edited BOOLEAN DEFAULT 0, title TEXT, read BOOLEAN)"); success = success && queryComicInfo.exec(); //COMIC (representa un cómic en disco, contiene el nombre de fichero) @@ -85,13 +90,80 @@ bool DataBaseManagement::createTables(QSqlDatabase & database) void DataBaseManagement::exportComicsInfo(QString source, QString dest) { - QSqlDatabase source = loadDatabase(source); - QSqlDatabase dest = loadDatabase(dest); + QSqlDatabase sourceDB = loadDatabase(source); + QSqlDatabase destDB = QSqlDatabase::addDatabase("QSQLITE"); + destDB.setDatabaseName(dest); + destDB.open(); + sourceDB.open(); + + QSqlQuery attach(sourceDB); + attach.prepare("ATTACH DATABASE '"+QDir().toNativeSeparators(dest) +"' AS dest;"); + //attach.bindValue(":dest",QDir().toNativeSeparators(dest)); + attach.exec(); + + QSqlQuery attach2(sourceDB); + attach2.prepare("ATTACH DATABASE '"+QDir().toNativeSeparators(source) +"' AS source;"); + attach2.exec(); + + //sourceDB.close(); + QSqlQuery queryDBInfo(sourceDB); + queryDBInfo.prepare("CREATE TABLE dest.db_info (version TEXT NOT NULL)"); + queryDBInfo.exec(); + + /*QSqlQuery queryComicsInfo(sourceDB); + queryComicsInfo.prepare("CREATE TABLE dest.comic_info (id INTEGER PRIMARY KEY, hash TEXT NOT NULL, edited BOOLEAN DEFAULT FALSE, title TEXT, read BOOLEAN)"); + queryComicsInfo.exec();*/ + + QSqlQuery query("INSERT INTO dest.db_info (version) " + "VALUES ('5.0.0')",sourceDB); + + QSqlQuery exportData(sourceDB); + exportData.prepare("create table dest.comic_info as select * from source.comic_info where source.comic_info.edited = 1"); + exportData.exec(); + + QString error = exportData.lastError().databaseText(); + QString error2 = exportData.lastError().text(); + + sourceDB.close(); + destDB.close(); } void DataBaseManagement::importComicsInfo(QString source, QString dest) { - QSqlDatabase source = loadDatabase(source); - QSqlDatabase dest = loadDatabase(dest); + QSqlDatabase sourceDB = loadDatabase(source); + QSqlDatabase destDB = loadDatabase(dest); + +} + +//COMICS_INFO_EXPORTER +ComicsInfoExporter::ComicsInfoExporter() +:QThread() +{ +} + +void ComicsInfoExporter::exportComicsInfo(QSqlDatabase & source, QSqlDatabase & dest) +{ + +} + +void ComicsInfoExporter::run() +{ + +} + + +//COMICS_INFO_IMPORTER +ComicsInfoImporter::ComicsInfoImporter() +:QThread() +{ +} + +void ComicsInfoImporter::importComicsInfo(QSqlDatabase & source, QSqlDatabase & dest) +{ + +} + +void ComicsInfoImporter::run() +{ } \ No newline at end of file diff --git a/YACReaderLibrary/db/data_base_management.h b/YACReaderLibrary/db/data_base_management.h index 8450a8d3..ac5ef11d 100644 --- a/YACReaderLibrary/db/data_base_management.h +++ b/YACReaderLibrary/db/data_base_management.h @@ -7,6 +7,26 @@ #include "treemodel.h" +class ComicsInfoExporter : public QThread +{ + Q_OBJECT +public: + ComicsInfoExporter(); + void exportComicsInfo(QSqlDatabase & source, QSqlDatabase & dest); +private: + void run(); +}; + +class ComicsInfoImporter : public QThread +{ + Q_OBJECT +public: + ComicsInfoImporter(); + void importComicsInfo(QSqlDatabase & source, QSqlDatabase & dest); +private: + void run(); +}; + class DataBaseManagement : public QObject { Q_OBJECT @@ -17,12 +37,13 @@ public: TreeModel * newTreeModel(QString path); //crea una base de datos y todas sus tablas static QSqlDatabase createDatabase(QString name, QString path); + static QSqlDatabase createDatabase(QString dest); //carga una base de datos desde la ruta path static QSqlDatabase loadDatabase(QString path); static bool createTables(QSqlDatabase & database); - void exportComicsInfo(QString source, QString dest); - void importComicsInfo(QString source, QString dest); + static void exportComicsInfo(QString source, QString dest); + static void importComicsInfo(QString source, QString dest); }; #endif \ No newline at end of file diff --git a/YACReaderLibrary/db/folder.cpp b/YACReaderLibrary/db/folder.cpp index 045ebed5..a69bcbe8 100644 --- a/YACReaderLibrary/db/folder.cpp +++ b/YACReaderLibrary/db/folder.cpp @@ -33,7 +33,7 @@ QList Folder::getFoldersFromParent(qulonglong parentId, QSqlDatab for(int i=0;idata(3).toString(); diff --git a/YACReaderLibrary/db/tablemodel.h b/YACReaderLibrary/db/tablemodel.h index 4b9fc8f4..be6a39eb 100644 --- a/YACReaderLibrary/db/tablemodel.h +++ b/YACReaderLibrary/db/tablemodel.h @@ -34,7 +34,7 @@ public: //Métodos de conveniencia QStringList getPaths(const QString & _source); - QString getComicPath(QModelIndex & mi); + QString getComicPath(QModelIndex mi); Comic getComic(const QModelIndex & mi); //--> para la edición QVector getReadList(); QVector setAllComicsRead(bool read); diff --git a/YACReaderLibrary/db/treemodel.cpp b/YACReaderLibrary/db/treemodel.cpp index 1bc10c3f..fdde6829 100644 --- a/YACReaderLibrary/db/treemodel.cpp +++ b/YACReaderLibrary/db/treemodel.cpp @@ -235,13 +235,15 @@ void TreeModel::setupModelData(QSqlQuery &sqlquery, TreeItem *parent) while (sqlquery.next()) { QList data; - data << sqlquery.value(2).toString(); - data << sqlquery.value(3).toString(); + QSqlRecord record = sqlquery.record(); + + data << record.value("name").toString(); + data << record.value("path").toString(); TreeItem * item = new TreeItem(data); - item->id = sqlquery.value(0).toLongLong(); + item->id = record.value("id").toLongLong(); //la inserción de hijos se hace de forma ordenada - items.value(sqlquery.value(1).toLongLong())->appendChild(item); + items.value(record.value("parentId").toLongLong())->appendChild(item); //se añade el item al map, de forma que se pueda encontrar como padre en siguientes iteraciones items.insert(item->id,item); } @@ -300,13 +302,15 @@ void TreeModel::setupFilteredModelData(QSqlQuery &sqlquery, TreeItem *parent) while (sqlquery.next()) { //se procesan todos los folders que cumplen con el filtro //datos de la base de datos QList data; - data << sqlquery.value(2).toString(); - data << sqlquery.value(3).toString(); + QSqlRecord record = sqlquery.record(); + + data << record.value("name").toString(); + data << record.value("path").toString(); TreeItem * item = new TreeItem(data); item->id = sqlquery.value(0).toLongLong(); //id del padre - quint64 parentId = sqlquery.value(1).toLongLong(); + quint64 parentId = record.value("parentId").toLongLong(); //se añade el item al map, de forma que se pueda encontrar como padre en siguientes iteraciones if(!filteredItems.contains(item->id)) diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index 36528783..3658e223 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -1150,6 +1150,7 @@ void LibraryWindow::hideComicFlow(bool hide) void LibraryWindow::showExportComicsInfo() { + exportComicsInfoDialog->source = currentPath() + "/.yacreaderlibrary/library.ydb"; exportComicsInfoDialog->show(); } diff --git a/YACReaderLibrary/properties_dialog.cpp b/YACReaderLibrary/properties_dialog.cpp index 4951f012..8856a347 100644 --- a/YACReaderLibrary/properties_dialog.cpp +++ b/YACReaderLibrary/properties_dialog.cpp @@ -237,7 +237,7 @@ void PropertiesDialog::setComics(QList comics) else { Comic comic = comics.at(0); - title->setText(comic.info.name); + title->setText(comic.info.title); } }