export comics info listo

cambio de acceso a las columnas de las querys (enteros por literales)
This commit is contained in:
Luis Ángel San Martín 2012-06-20 23:19:37 +02:00
parent 5f89443060
commit cd2b9f798a
10 changed files with 131 additions and 30 deletions

View File

@ -45,7 +45,7 @@ QList<LibraryItem *> 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();

View File

@ -17,8 +17,9 @@ public:
qulonglong id;
bool read;
bool edited;
QString hash;
QString name;
QString title;
bool existOnDb;
};

View File

@ -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()
{
}

View File

@ -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

View File

@ -33,7 +33,7 @@ QList<LibraryItem *> Folder::getFoldersFromParent(qulonglong parentId, QSqlDatab
for(int i=0;i<record.count();i++)
data << record.value(i);
//TODO sort by sort indicator and name
currentItem = new Folder(record.value(0).toLongLong(),record.value(1).toLongLong(),record.value(2).toString(),record.value(2).toString());
currentItem = new Folder(record.value("id").toLongLong(),record.value("parentId").toLongLong(),record.value("name").toString(),record.value("path").toString());
int lessThan = 0;
if(list.isEmpty())
list.append(currentItem);

View File

@ -169,7 +169,7 @@ void TableModel::setupModelData(unsigned long long int folderId,QSqlDatabase & d
//f.close();
}
QString TableModel::getComicPath(QModelIndex & mi)
QString TableModel::getComicPath(QModelIndex mi)
{
if(mi.isValid())
return _data.at(mi.row())->data(3).toString();

View File

@ -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<bool> getReadList();
QVector<bool> setAllComicsRead(bool read);

View File

@ -235,13 +235,15 @@ void TreeModel::setupModelData(QSqlQuery &sqlquery, TreeItem *parent)
while (sqlquery.next()) {
QList<QVariant> 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<QVariant> 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))

View File

@ -1150,6 +1150,7 @@ void LibraryWindow::hideComicFlow(bool hide)
void LibraryWindow::showExportComicsInfo()
{
exportComicsInfoDialog->source = currentPath() + "/.yacreaderlibrary/library.ydb";
exportComicsInfoDialog->show();
}

View File

@ -237,7 +237,7 @@ void PropertiesDialog::setComics(QList<Comic> comics)
else
{
Comic comic = comics.at(0);
title->setText(comic.info.name);
title->setText(comic.info.title);
}
}