Conexi?n y desconexi?n de la base de datos en cada operaci?n

esto evita el bloqueo del archivo de BD

Si se detecta una biblioteca que no tiene contenido se ofrece al usuario
la posibilidad de eliminarla (di?logo)
This commit is contained in:
Luis Ángel San Martín 2012-06-29 23:49:30 +02:00
parent 0b1b3df8b2
commit 6cff6162d2
9 changed files with 84 additions and 122 deletions

View File

@ -60,7 +60,7 @@ QSqlDatabase DataBaseManagement::createDatabase(QString name, QString path)
QSqlDatabase DataBaseManagement::createDatabase(QString dest) QSqlDatabase DataBaseManagement::createDatabase(QString dest)
{ {
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",dest);
db.setDatabaseName(dest); db.setDatabaseName(dest);
if (!db.open()) if (!db.open())
qDebug() << db.lastError(); qDebug() << db.lastError();
@ -242,77 +242,9 @@ bool DataBaseManagement::importComicsInfo(QString source, QString dest)
QSqlDatabase sourceDB = loadDatabaseFromFile(source); QSqlDatabase sourceDB = loadDatabaseFromFile(source);
QSqlDatabase destDB = loadDatabaseFromFile(dest); QSqlDatabase destDB = loadDatabaseFromFile(dest);
{
QSqlQuery pragma("PRAGMA synchronous=OFF",destDB); 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); QSqlQuery update(destDB);
update.prepare("UPDATE comic_info SET " update.prepare("UPDATE comic_info SET "
"title = :title," "title = :title,"
@ -484,9 +416,14 @@ bool DataBaseManagement::importComicsInfo(QString source, QString dest)
insert.finish(); insert.finish();
} }
} }
}
destDB.commit(); destDB.commit();
destDB.close();
sourceDB.close();
QSqlDatabase::removeDatabase(source);
QSqlDatabase::removeDatabase(dest);
return b; return b;
} }

View File

@ -140,7 +140,7 @@ QStringList TableModel::getPaths(const QString & _source)
return paths; 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"); //QFile f(QCoreApplication::applicationDirPath()+"/performance.txt");
//f.open(QIODevice::Append); //f.open(QIODevice::Append);
@ -152,7 +152,9 @@ void TableModel::setupModelData(unsigned long long int folderId,QSqlDatabase & d
//QTextStream txtS(&f); //QTextStream txtS(&f);
//txtS << "TABLEMODEL: Tiempo de borrado: " << timer.elapsed() << "ms\r\n"; //txtS << "TABLEMODEL: Tiempo de borrado: " << timer.elapsed() << "ms\r\n";
db.open(); _databasePath = databasePath;
QSqlDatabase db = DataBaseManagement::loadDatabase(databasePath);
{
//crear la consulta //crear la consulta
//timer.restart(); //timer.restart();
QSqlQuery selectQuery(db); //TODO check QSqlQuery selectQuery(db); //TODO check
@ -164,8 +166,9 @@ void TableModel::setupModelData(unsigned long long int folderId,QSqlDatabase & d
setupModelData(selectQuery); setupModelData(selectQuery);
//txtS << "TABLEMODEL: Tiempo de creación del modelo: " << timer.elapsed() << "ms\r\n"; //txtS << "TABLEMODEL: Tiempo de creación del modelo: " << timer.elapsed() << "ms\r\n";
//selectQuery.finish(); //selectQuery.finish();
}
db.close(); db.close();
_database = db; QSqlDatabase::removeDatabase(_databasePath);
endResetModel(); endResetModel();
//f.close(); //f.close();
} }
@ -215,9 +218,12 @@ void TableModel::setupModelData(QSqlQuery &sqlquery)
Comic TableModel::getComic(const QModelIndex & mi) Comic TableModel::getComic(const QModelIndex & mi)
{ {
Comic c; Comic c;
_database.open();
c.load(_data.at(mi.row())->data(0).toLongLong(),_database); QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
_database.close(); c.load(_data.at(mi.row())->data(0).toLongLong(),db);
db.close();
QSqlDatabase::removeDatabase(_databasePath);
return c; return c;
} }
@ -225,7 +231,10 @@ Comic TableModel::_getComic(const QModelIndex & mi)
{ {
Comic c; 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; return c;
} }
@ -245,8 +254,8 @@ QVector<bool> TableModel::getReadList()
QVector<bool> TableModel::setAllComicsRead(bool read) QVector<bool> TableModel::setAllComicsRead(bool read)
{ {
_database.open(); QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
_database.transaction(); db.transaction();
int numComics = _data.count(); int numComics = _data.count();
QVector<bool> readList(numComics); QVector<bool> readList(numComics);
for(int i=0;i<numComics;i++) for(int i=0;i<numComics;i++)
@ -255,12 +264,13 @@ QVector<bool> TableModel::setAllComicsRead(bool read)
readList[i] = read; readList[i] = read;
_data.value(i)->data(5) = QVariant(true); _data.value(i)->data(5) = QVariant(true);
Comic c; 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.read = read;
c.info.update(_database); c.info.update(db);
} }
_database.commit(); db.commit();
_database.close(); db.close();
QSqlDatabase::removeDatabase(_databasePath);
return readList; return readList;
} }
@ -269,14 +279,15 @@ QList<Comic> TableModel::getComics(QList<QModelIndex> list)
{ {
QList<Comic> comics; QList<Comic> comics;
_database.open(); QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
_database.transaction(); db.transaction();
QList<QModelIndex>::const_iterator itr; QList<QModelIndex>::const_iterator itr;
for(itr = list.constBegin(); itr!= list.constEnd();itr++) for(itr = list.constBegin(); itr!= list.constEnd();itr++)
{ {
comics.append(_getComic(*itr)); comics.append(_getComic(*itr));
} }
_database.commit(); db.commit();
_database.close(); db.close();
QSqlDatabase::removeDatabase(_databasePath);
return comics; return comics;
} }

View File

@ -30,7 +30,7 @@ public:
QModelIndex parent(const QModelIndex &index) const; QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(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 //Métodos de conveniencia
QStringList getPaths(const QString & _source); QStringList getPaths(const QString & _source);
@ -47,7 +47,7 @@ private:
Comic _getComic(const QModelIndex & mi); Comic _getComic(const QModelIndex & mi);
QList<TableItem *> _data; QList<TableItem *> _data;
QSqlDatabase _database; QString _databasePath;
signals: signals:
void beforeReset(); void beforeReset();

View File

@ -213,17 +213,16 @@ void TreeModel::setupModelData(QString path)
rootItem->parentItem = 0; rootItem->parentItem = 0;
//cargar la base de datos //cargar la base de datos
if(_database.isOpen()) _databasePath = path;
_database.close(); QSqlDatabase db = DataBaseManagement::loadDatabase(path);
_database = DataBaseManagement::loadDatabase(path);
//crear la consulta //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); setupModelData(selectQuery,rootItem);
} }
//selectQuery.finish(); //selectQuery.finish();
_database.close(); db.close();
QSqlDatabase::removeDatabase(path); QSqlDatabase::removeDatabase(path);
endResetModel(); endResetModel();
@ -274,10 +273,10 @@ void TreeModel::setupFilteredModelData()
rootItem->parentItem = 0; rootItem->parentItem = 0;
//cargar la base de datos //cargar la base de datos
if(_database.isValid()) QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath);
_database.open();
//crear la consulta //crear la consulta
QSqlQuery selectQuery(_database); //TODO check {
QSqlQuery selectQuery(db); //TODO check
if(!includeComics) if(!includeComics)
{ {
selectQuery.prepare("select * from folder where id <> 1 and upper(name) like upper(:filter) order by parentId,name "); 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(); selectQuery.exec();
setupFilteredModelData(selectQuery,rootItem); setupFilteredModelData(selectQuery,rootItem);
}
//selectQuery.finish(); //selectQuery.finish();
_database.close(); db.close();
QSqlDatabase::removeDatabase(_databasePath);
endResetModel(); 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) QString TreeModel::getFolderPath(const QModelIndex &folder)

View File

@ -73,7 +73,7 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const;
void setupModelData(QString path); void setupModelData(QString path);
QSqlDatabase & getDatabase(); QString getDatabase();
//Métodos de conveniencia //Métodos de conveniencia
QString getFolderPath(const QModelIndex &folder); QString getFolderPath(const QModelIndex &folder);
@ -92,7 +92,7 @@ private:
TreeItem *rootBeforeFilter; TreeItem *rootBeforeFilter;
QMap<unsigned long long int, TreeItem *> filteredItems; //relación entre folders QMap<unsigned long long int, TreeItem *> filteredItems; //relación entre folders
QSqlDatabase _database; QString _databasePath;
bool includeComics; bool includeComics;
QString filter; QString filter;

View File

@ -67,6 +67,7 @@ void LibraryCreator::run()
create(QDir(_source)); create(QDir(_source));
_database.commit(); _database.commit();
_database.close(); _database.close();
QSqlDatabase::removeDatabase(_database.connectionName());
} }
else else
{ {
@ -81,6 +82,7 @@ void LibraryCreator::run()
update(QDir(_source)); update(QDir(_source));
_database.commit(); _database.commit();
_database.close(); _database.close();
QSqlDatabase::removeDatabase(_target);
} }
emit(finished()); emit(finished());
} }

View File

@ -17,6 +17,8 @@
#include <iterator> #include <iterator>
#include "data_base_management.h"
// //
LibraryWindow::LibraryWindow() LibraryWindow::LibraryWindow()
@ -586,7 +588,6 @@ void LibraryWindow::loadLibrary(const QString & name)
{ {
if(libraries.size()>0) if(libraries.size()>0)
{ {
dm->getDatabase().close();
QString path=libraries.value(name)+"/.yacreaderlibrary"; QString path=libraries.value(name)+"/.yacreaderlibrary";
QDir d; //TODO change this by static methods (utils class?? with delTree for example) QDir d; //TODO change this by static methods (utils class?? with delTree for example)
if(d.exists(path)) if(d.exists(path))
@ -625,6 +626,12 @@ void LibraryWindow::loadLibrary(const QString & name)
foldersView->setModel(NULL); foldersView->setModel(NULL);
comicFlow->clear(); comicFlow->clear();
disableAllActions();//TODO comprobar que se deben deshabilitar 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 else
@ -750,10 +757,11 @@ void LibraryWindow::setCurrentComicReaded()
Comic c = dmCV->getComic(comicView->currentIndex()); Comic c = dmCV->getComic(comicView->currentIndex());
c.info.read = true; c.info.read = true;
QSqlDatabase db = dm->getDatabase(); QSqlDatabase db = DataBaseManagement::loadDatabase(dm->getDatabase());
db.open(); db.open();
c.info.updateRead(db); c.info.updateRead(db);
db.close(); db.close();
QSqlDatabase::removeDatabase(dm->getDatabase());
} }
void LibraryWindow::setComicsReaded() void LibraryWindow::setComicsReaded()
@ -769,10 +777,11 @@ void LibraryWindow::setCurrentComicUnreaded()
Comic c = dmCV->getComic(comicView->currentIndex()); Comic c = dmCV->getComic(comicView->currentIndex());
c.info.read = false; c.info.read = false;
QSqlDatabase db = dm->getDatabase(); QSqlDatabase db = DataBaseManagement::loadDatabase(dm->getDatabase());
db.open(); db.open();
c.info.updateRead(db); c.info.updateRead(db);
db.close(); db.close();
QSqlDatabase::removeDatabase(dm->getDatabase());
} }
@ -881,12 +890,12 @@ void LibraryWindow::deleteLibrary()
void LibraryWindow::deleteCurrentLibrary() void LibraryWindow::deleteCurrentLibrary()
{ {
QSqlDatabase db = dm->getDatabase(); //QSqlDatabase db = dm->getDatabase();
db.commit(); //db.commit();
db.close(); //db.close();
QSqlDatabase::removeDatabase(db.connectionName()); //QSqlDatabase::removeDatabase(db.connectionName());
if(!dm->getDatabase().isOpen()) //if(!dm->getDatabase().isOpen())
{ //{
QString path = libraries.value(selectedLibrary->currentText()); QString path = libraries.value(selectedLibrary->currentText());
libraries.remove(selectedLibrary->currentText()); libraries.remove(selectedLibrary->currentText());
selectedLibrary->removeItem(selectedLibrary->currentIndex()); selectedLibrary->removeItem(selectedLibrary->currentIndex());
@ -903,7 +912,7 @@ void LibraryWindow::deleteCurrentLibrary()
comicFlow->clear(); comicFlow->clear();
} }
saveLibraries(); saveLibraries();
} //}
} }
void LibraryWindow::removeLibrary() void LibraryWindow::removeLibrary()
@ -1046,7 +1055,7 @@ void LibraryWindow::showProperties()
//ThumbnailCreator tc(path,""); //ThumbnailCreator tc(path,"");
//tc.create(); //tc.create();
propertiesDialog->database = dm->getDatabase(); propertiesDialog->databasePath = dm->getDatabase();
propertiesDialog->basePath = currentPath(); propertiesDialog->basePath = currentPath();
propertiesDialog->setComics(comics); propertiesDialog->setComics(comics);

View File

@ -7,6 +7,7 @@
#include <QCheckBox> #include <QCheckBox>
#include <QTabWidget> #include <QTabWidget>
#include "data_base_management.h"
PropertiesDialog::PropertiesDialog(QWidget * parent) PropertiesDialog::PropertiesDialog(QWidget * parent)
:QDialog(parent) :QDialog(parent)
@ -393,16 +394,18 @@ void PropertiesDialog::setComics(QList<Comic> comics)
void PropertiesDialog::updateComics() void PropertiesDialog::updateComics()
{ {
database.open(); QSqlDatabase db = DataBaseManagement::loadDatabase(databasePath);
database.transaction(); db.open();
db.transaction();
QList<Comic>::iterator itr; QList<Comic>::iterator itr;
for(itr = comics.begin();itr!=comics.end();itr++) for(itr = comics.begin();itr!=comics.end();itr++)
{ {
if(itr->info.edited) if(itr->info.edited)
itr->info.update(database); itr->info.update(db);
} }
database.commit(); db.commit();
database.close(); db.close();
QSqlDatabase::removeDatabase(databasePath);
} }
//Deprecated //Deprecated
void PropertiesDialog::setCover(const QPixmap & coverImage) void PropertiesDialog::setCover(const QPixmap & coverImage)

View File

@ -89,7 +89,7 @@
void closeEvent ( QCloseEvent * e ); void closeEvent ( QCloseEvent * e );
public: public:
PropertiesDialog(QWidget * parent = 0); PropertiesDialog(QWidget * parent = 0);
QSqlDatabase database; QString databasePath;
QString basePath; QString basePath;
public slots: public slots: