diff --git a/YACReaderLibrary/db/data_base_management.cpp b/YACReaderLibrary/db/data_base_management.cpp index f390cab5..7fd019f0 100644 --- a/YACReaderLibrary/db/data_base_management.cpp +++ b/YACReaderLibrary/db/data_base_management.cpp @@ -60,7 +60,7 @@ QSqlDatabase DataBaseManagement::createDatabase(QString name, QString path) QSqlDatabase DataBaseManagement::createDatabase(QString dest) { - QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",dest); db.setDatabaseName(dest); if (!db.open()) qDebug() << db.lastError(); @@ -242,77 +242,9 @@ bool DataBaseManagement::importComicsInfo(QString source, QString dest) QSqlDatabase sourceDB = loadDatabaseFromFile(source); QSqlDatabase destDB = loadDatabaseFromFile(dest); + { QSqlQuery pragma("PRAGMA synchronous=OFF",destDB); - - //QSqlQuery attach(destDB); - //attach.prepare("ATTACH DATABASE '"+QDir().toNativeSeparators(dest) +"' AS dest;"); - //attach.exec(); - - //error = attach.lastError().databaseText(); - //driver = attach.lastError().driverText(); - - //QMessageBox::critical(NULL,tr("db error"),error); - //QMessageBox::critical(NULL,tr("db error"),driver); - - //QSqlQuery attach2(destDB); - //attach2.prepare("ATTACH DATABASE '"+QDir().toNativeSeparators(source) +"' AS source;"); - //attach2.exec(); - // - //error = attach2.lastError().databaseText(); - //driver = attach2.lastError().driverText(); - - //QMessageBox::critical(NULL,tr("db error"),error); - //QMessageBox::critical(NULL,tr("db error"),driver); - //TODO check versions... - //QSqlQuery update(destDB); - //update.prepare("UPDATE dest.comic_info" - // "SET" - //"title = coalesce(title , (select source.comic_info.title from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"coverPage = coalesce(coverPage , (select source.comic_info.coverPage from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"numPages = coalesce(numPages , (select source.comic_info.numPages from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"number = coalesce(number , (select source.comic_info.number from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"isBis = coalesce(isBis , (select source.comic_info.isBis from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"count = coalesce(count , (select source.comic_info.count from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"volume = coalesce(volume , (select source.comic_info.volume from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"storyArc = coalesce(storyArc , (select source.comic_info.storyArc from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"arcNumber = coalesce(arcNumber , (select source.comic_info.arcNumber from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"arcCount = coalesce(arcCount , (select source.comic_info.arcCount from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"genere = coalesce(genere , (select source.comic_info.genere from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"writer = coalesce(writer , (select source.comic_info.writer from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"penciller = coalesce(penciller , (select source.comic_info.penciller from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"inker = coalesce(inker , (select source.comic_info.inker from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"colorist = coalesce(colorist , (select source.comic_info.colorist from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"letterer = coalesce(letterer , (select source.comic_info.letterer from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"coverArtist = coalesce(coverArtist , (select source.comic_info.coverArtist from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"date = coalesce(date , (select source.comic_info.date from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"publisher = coalesce(publisher , (select source.comic_info.publisher from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"format = coalesce(format , (select source.comic_info.format from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"color = coalesce(color , (select source.comic_info.color from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"ageRating = coalesce(ageRating , (select source.comic_info.ageRating from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"synopsis = coalesce(synopsis , (select source.comic_info.synopsis from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"characters = coalesce(characters , (select source.comic_info.characters from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"notes = coalesce(notes , (select source.comic_info.notes from source.comic_info where dest.comic_info.hash = source.comic_info.hash))," - //"edited = 1" - // ); - //b = b || update.exec(); - - //error = update.lastError().databaseText(); - //driver = update.lastError().driverText(); - - //QMessageBox::critical(NULL,tr("db error"),error); - //QMessageBox::critical(NULL,tr("db error"),driver); - - //QSqlQuery import(destDB); - //import.prepare("insert or ignore into dest.comic_info (" +fields + ",edited,read) select " + fields + ",1 as edited, 0 as read from source.comic_info;"); - ////import.prepare("insert into dest.comic_info (" +fields + ",edited,read) select " + fields + ",1 as edited, 0 as read from source.comic_info where sourc.comic_info.hash not in (select dest.comic_info.hash from dest.comic_info);"); - //b = b || import.exec(); - //error = import.lastError().databaseText(); - //driver = import.lastError().driverText(); - - //QMessageBox::critical(NULL,tr("db error"),error); - //QMessageBox::critical(NULL,tr("db error"),driver); - QSqlQuery update(destDB); update.prepare("UPDATE comic_info SET " "title = :title," @@ -484,9 +416,14 @@ bool DataBaseManagement::importComicsInfo(QString source, QString dest) insert.finish(); } } - - destDB.commit(); + } + destDB.commit(); + destDB.close(); + sourceDB.close(); + + QSqlDatabase::removeDatabase(source); + QSqlDatabase::removeDatabase(dest); return b; } diff --git a/YACReaderLibrary/db/tablemodel.cpp b/YACReaderLibrary/db/tablemodel.cpp index 32130719..9b642fb4 100644 --- a/YACReaderLibrary/db/tablemodel.cpp +++ b/YACReaderLibrary/db/tablemodel.cpp @@ -140,7 +140,7 @@ QStringList TableModel::getPaths(const QString & _source) return paths; } -void TableModel::setupModelData(unsigned long long int folderId,QSqlDatabase & db) +void TableModel::setupModelData(unsigned long long int folderId,const QString & databasePath) { //QFile f(QCoreApplication::applicationDirPath()+"/performance.txt"); //f.open(QIODevice::Append); @@ -152,7 +152,9 @@ void TableModel::setupModelData(unsigned long long int folderId,QSqlDatabase & d //QTextStream txtS(&f); //txtS << "TABLEMODEL: Tiempo de borrado: " << timer.elapsed() << "ms\r\n"; - db.open(); + _databasePath = databasePath; + QSqlDatabase db = DataBaseManagement::loadDatabase(databasePath); + { //crear la consulta //timer.restart(); QSqlQuery selectQuery(db); //TODO check @@ -164,8 +166,9 @@ void TableModel::setupModelData(unsigned long long int folderId,QSqlDatabase & d setupModelData(selectQuery); //txtS << "TABLEMODEL: Tiempo de creación del modelo: " << timer.elapsed() << "ms\r\n"; //selectQuery.finish(); + } db.close(); - _database = db; + QSqlDatabase::removeDatabase(_databasePath); endResetModel(); //f.close(); } @@ -215,9 +218,12 @@ void TableModel::setupModelData(QSqlQuery &sqlquery) Comic TableModel::getComic(const QModelIndex & mi) { Comic c; - _database.open(); - c.load(_data.at(mi.row())->data(0).toLongLong(),_database); - _database.close(); + + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + c.load(_data.at(mi.row())->data(0).toLongLong(),db); + db.close(); + QSqlDatabase::removeDatabase(_databasePath); + return c; } @@ -225,7 +231,10 @@ Comic TableModel::_getComic(const QModelIndex & mi) { Comic c; - c.load(_data.at(mi.row())->data(0).toLongLong(),_database); + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + c.load(_data.at(mi.row())->data(0).toLongLong(),db); + db.close(); + QSqlDatabase::removeDatabase(_databasePath); return c; } @@ -245,8 +254,8 @@ QVector TableModel::getReadList() QVector TableModel::setAllComicsRead(bool read) { - _database.open(); - _database.transaction(); + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + db.transaction(); int numComics = _data.count(); QVector readList(numComics); for(int i=0;i TableModel::setAllComicsRead(bool read) readList[i] = read; _data.value(i)->data(5) = QVariant(true); Comic c; - c.load(_data.value(i)->data(0).toLongLong(),_database); + c.load(_data.value(i)->data(0).toLongLong(),db); c.info.read = read; - c.info.update(_database); + c.info.update(db); } - _database.commit(); - _database.close(); + db.commit(); + db.close(); + QSqlDatabase::removeDatabase(_databasePath); return readList; } @@ -269,14 +279,15 @@ QList TableModel::getComics(QList list) { QList comics; - _database.open(); - _database.transaction(); + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + db.transaction(); QList::const_iterator itr; for(itr = list.constBegin(); itr!= list.constEnd();itr++) { comics.append(_getComic(*itr)); } - _database.commit(); - _database.close(); + db.commit(); + db.close(); + QSqlDatabase::removeDatabase(_databasePath); return comics; } \ No newline at end of file diff --git a/YACReaderLibrary/db/tablemodel.h b/YACReaderLibrary/db/tablemodel.h index be6a39eb..6bd2eae4 100644 --- a/YACReaderLibrary/db/tablemodel.h +++ b/YACReaderLibrary/db/tablemodel.h @@ -30,7 +30,7 @@ public: QModelIndex parent(const QModelIndex &index) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; - void setupModelData(unsigned long long int parentFolder,QSqlDatabase & db); + void setupModelData(unsigned long long int parentFolder,const QString & databasePath); //Métodos de conveniencia QStringList getPaths(const QString & _source); @@ -47,7 +47,7 @@ private: Comic _getComic(const QModelIndex & mi); QList _data; - QSqlDatabase _database; + QString _databasePath; signals: void beforeReset(); diff --git a/YACReaderLibrary/db/treemodel.cpp b/YACReaderLibrary/db/treemodel.cpp index 97b3de38..9dd182e5 100644 --- a/YACReaderLibrary/db/treemodel.cpp +++ b/YACReaderLibrary/db/treemodel.cpp @@ -213,17 +213,16 @@ void TreeModel::setupModelData(QString path) rootItem->parentItem = 0; //cargar la base de datos - if(_database.isOpen()) - _database.close(); - _database = DataBaseManagement::loadDatabase(path); + _databasePath = path; + QSqlDatabase db = DataBaseManagement::loadDatabase(path); //crear la consulta { - QSqlQuery selectQuery("select * from folder where id <> 1 order by parentId,name",_database); + QSqlQuery selectQuery("select * from folder where id <> 1 order by parentId,name",db); setupModelData(selectQuery,rootItem); } //selectQuery.finish(); - _database.close(); + db.close(); QSqlDatabase::removeDatabase(path); endResetModel(); @@ -274,10 +273,10 @@ void TreeModel::setupFilteredModelData() rootItem->parentItem = 0; //cargar la base de datos - if(_database.isValid()) - _database.open(); + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); //crear la consulta - QSqlQuery selectQuery(_database); //TODO check + { + QSqlQuery selectQuery(db); //TODO check if(!includeComics) { selectQuery.prepare("select * from folder where id <> 1 and upper(name) like upper(:filter) order by parentId,name "); @@ -292,9 +291,10 @@ void TreeModel::setupFilteredModelData() selectQuery.exec(); setupFilteredModelData(selectQuery,rootItem); - + } //selectQuery.finish(); - _database.close(); + db.close(); + QSqlDatabase::removeDatabase(_databasePath); endResetModel(); } @@ -374,9 +374,9 @@ void TreeModel::setupFilteredModelData(QSqlQuery &sqlquery, TreeItem *parent) -QSqlDatabase & TreeModel::getDatabase() +QString TreeModel::getDatabase() { - return _database; + return _databasePath; } QString TreeModel::getFolderPath(const QModelIndex &folder) diff --git a/YACReaderLibrary/db/treemodel.h b/YACReaderLibrary/db/treemodel.h index 736f4c66..eb61b13d 100644 --- a/YACReaderLibrary/db/treemodel.h +++ b/YACReaderLibrary/db/treemodel.h @@ -73,7 +73,7 @@ public: int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; void setupModelData(QString path); - QSqlDatabase & getDatabase(); + QString getDatabase(); //Métodos de conveniencia QString getFolderPath(const QModelIndex &folder); @@ -92,7 +92,7 @@ private: TreeItem *rootBeforeFilter; QMap filteredItems; //relación entre folders - QSqlDatabase _database; + QString _databasePath; bool includeComics; QString filter; diff --git a/YACReaderLibrary/library_creator.cpp b/YACReaderLibrary/library_creator.cpp index af69773f..14621501 100644 --- a/YACReaderLibrary/library_creator.cpp +++ b/YACReaderLibrary/library_creator.cpp @@ -67,6 +67,7 @@ void LibraryCreator::run() create(QDir(_source)); _database.commit(); _database.close(); + QSqlDatabase::removeDatabase(_database.connectionName()); } else { @@ -81,6 +82,7 @@ void LibraryCreator::run() update(QDir(_source)); _database.commit(); _database.close(); + QSqlDatabase::removeDatabase(_target); } emit(finished()); } diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index 7afd5f83..d472a2ee 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -17,6 +17,8 @@ #include +#include "data_base_management.h" + // LibraryWindow::LibraryWindow() @@ -586,7 +588,6 @@ void LibraryWindow::loadLibrary(const QString & name) { if(libraries.size()>0) { - dm->getDatabase().close(); QString path=libraries.value(name)+"/.yacreaderlibrary"; QDir d; //TODO change this by static methods (utils class?? with delTree for example) if(d.exists(path)) @@ -625,6 +626,12 @@ void LibraryWindow::loadLibrary(const QString & name) foldersView->setModel(NULL); comicFlow->clear(); disableAllActions();//TODO comprobar que se deben deshabilitar + + QString currentLibrary = selectedLibrary->currentText(); + if(QMessageBox::question(this,tr("Library not available"),tr("Library ")+currentLibrary+tr(" is no longer available. Do you want to remove it?"),QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes) + { + deleteCurrentLibrary(); + } } } else @@ -750,10 +757,11 @@ void LibraryWindow::setCurrentComicReaded() Comic c = dmCV->getComic(comicView->currentIndex()); c.info.read = true; - QSqlDatabase db = dm->getDatabase(); + QSqlDatabase db = DataBaseManagement::loadDatabase(dm->getDatabase()); db.open(); c.info.updateRead(db); db.close(); + QSqlDatabase::removeDatabase(dm->getDatabase()); } void LibraryWindow::setComicsReaded() @@ -769,10 +777,11 @@ void LibraryWindow::setCurrentComicUnreaded() Comic c = dmCV->getComic(comicView->currentIndex()); c.info.read = false; - QSqlDatabase db = dm->getDatabase(); + QSqlDatabase db = DataBaseManagement::loadDatabase(dm->getDatabase()); db.open(); c.info.updateRead(db); db.close(); + QSqlDatabase::removeDatabase(dm->getDatabase()); } @@ -881,12 +890,12 @@ void LibraryWindow::deleteLibrary() void LibraryWindow::deleteCurrentLibrary() { - QSqlDatabase db = dm->getDatabase(); - db.commit(); - db.close(); - QSqlDatabase::removeDatabase(db.connectionName()); - if(!dm->getDatabase().isOpen()) - { + //QSqlDatabase db = dm->getDatabase(); + //db.commit(); + //db.close(); + //QSqlDatabase::removeDatabase(db.connectionName()); + //if(!dm->getDatabase().isOpen()) + //{ QString path = libraries.value(selectedLibrary->currentText()); libraries.remove(selectedLibrary->currentText()); selectedLibrary->removeItem(selectedLibrary->currentIndex()); @@ -903,7 +912,7 @@ void LibraryWindow::deleteCurrentLibrary() comicFlow->clear(); } saveLibraries(); - } + //} } void LibraryWindow::removeLibrary() @@ -1046,7 +1055,7 @@ void LibraryWindow::showProperties() //ThumbnailCreator tc(path,""); //tc.create(); - propertiesDialog->database = dm->getDatabase(); + propertiesDialog->databasePath = dm->getDatabase(); propertiesDialog->basePath = currentPath(); propertiesDialog->setComics(comics); diff --git a/YACReaderLibrary/properties_dialog.cpp b/YACReaderLibrary/properties_dialog.cpp index ee831c89..05dfbb6b 100644 --- a/YACReaderLibrary/properties_dialog.cpp +++ b/YACReaderLibrary/properties_dialog.cpp @@ -7,6 +7,7 @@ #include #include +#include "data_base_management.h" PropertiesDialog::PropertiesDialog(QWidget * parent) :QDialog(parent) @@ -393,16 +394,18 @@ void PropertiesDialog::setComics(QList comics) void PropertiesDialog::updateComics() { - database.open(); - database.transaction(); + QSqlDatabase db = DataBaseManagement::loadDatabase(databasePath); + db.open(); + db.transaction(); QList::iterator itr; for(itr = comics.begin();itr!=comics.end();itr++) { if(itr->info.edited) - itr->info.update(database); + itr->info.update(db); } - database.commit(); - database.close(); + db.commit(); + db.close(); + QSqlDatabase::removeDatabase(databasePath); } //Deprecated void PropertiesDialog::setCover(const QPixmap & coverImage) diff --git a/YACReaderLibrary/properties_dialog.h b/YACReaderLibrary/properties_dialog.h index 966e6f14..5d670e0e 100644 --- a/YACReaderLibrary/properties_dialog.h +++ b/YACReaderLibrary/properties_dialog.h @@ -89,7 +89,7 @@ void closeEvent ( QCloseEvent * e ); public: PropertiesDialog(QWidget * parent = 0); - QSqlDatabase database; + QString databasePath; QString basePath; public slots: