added ordering field to all the 'lists' tables

This commit is contained in:
Luis Ángel San Martín 2015-01-27 11:53:55 +01:00
parent eb0fa7e0fb
commit 27d096162d
3 changed files with 81 additions and 53 deletions

View File

@ -320,10 +320,11 @@ void ComicModel::setupLabelModelData(unsigned long long parentLabel, const QStri
selectQuery.prepare("SELECT ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read,ci.isBis,ci.currentPage,ci.rating,ci.hasBeenOpened " selectQuery.prepare("SELECT ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read,ci.isBis,ci.currentPage,ci.rating,ci.hasBeenOpened "
"FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) " "FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) "
"INNER JOIN comic_label cl ON (c.id == cl.comic_id) " "INNER JOIN comic_label cl ON (c.id == cl.comic_id) "
"WHERE cl.label_id = :parentLabelId"); "WHERE cl.label_id = :parentLabelId "
"ORDER BY cl.ordering");
selectQuery.bindValue(":parentLabelId", parentLabel); selectQuery.bindValue(":parentLabelId", parentLabel);
selectQuery.exec(); selectQuery.exec();
setupModelData(selectQuery); setupModelDataForList(selectQuery);
} }
db.close(); db.close();
QSqlDatabase::removeDatabase(_databasePath); QSqlDatabase::removeDatabase(_databasePath);
@ -361,7 +362,8 @@ void ComicModel::setupReadingListModelData(unsigned long long parentReadingList,
selectQuery.prepare("SELECT ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read,ci.isBis,ci.currentPage,ci.rating,ci.hasBeenOpened " selectQuery.prepare("SELECT ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read,ci.isBis,ci.currentPage,ci.rating,ci.hasBeenOpened "
"FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) " "FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) "
"INNER JOIN comic_reading_list crl ON (c.id == crl.comic_id) " "INNER JOIN comic_reading_list crl ON (c.id == crl.comic_id) "
"WHERE crl.reading_list_id = :parentReadingList"); "WHERE crl.reading_list_id = :parentReadingList "
"ORDER BY crl.ordering");
selectQuery.bindValue(":parentReadingList", id); selectQuery.bindValue(":parentReadingList", id);
selectQuery.exec(); selectQuery.exec();
@ -393,7 +395,8 @@ void ComicModel::setupFavoritesModelData(const QString &databasePath)
selectQuery.prepare("SELECT ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read,ci.isBis,ci.currentPage,ci.rating,ci.hasBeenOpened " selectQuery.prepare("SELECT ci.number,ci.title,c.fileName,ci.numPages,c.id,c.parentId,c.path,ci.hash,ci.read,ci.isBis,ci.currentPage,ci.rating,ci.hasBeenOpened "
"FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) " "FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) "
"INNER JOIN comic_default_reading_list cdrl ON (c.id == cdrl.comic_id) " "INNER JOIN comic_default_reading_list cdrl ON (c.id == cdrl.comic_id) "
"WHERE cdrl.default_reading_list_id = :parentDefaultListId"); "WHERE cdrl.default_reading_list_id = :parentDefaultListId "
"ORDER BY cdrl.ordering");
selectQuery.bindValue(":parentDefaultListId", 1); selectQuery.bindValue(":parentDefaultListId", 1);
selectQuery.exec(); selectQuery.exec();
setupModelData(selectQuery); setupModelData(selectQuery);
@ -588,7 +591,21 @@ void ComicModel::setupModelData(QSqlQuery &sqlquery)
} }
} }
} }
}
//comics are sorted by "ordering", the sorting is done in the sql query
void ComicModel::setupModelDataForList(QSqlQuery &sqlquery)
{
while (sqlquery.next())
{
QList<QVariant> data;
QSqlRecord record = sqlquery.record();
for(int i=0;i<record.count();i++)
data << record.value(i);
_data.append(new ComicItem(data));
}
} }
ComicDB ComicModel::getComic(const QModelIndex & mi) ComicDB ComicModel::getComic(const QModelIndex & mi)

View File

@ -127,6 +127,7 @@ protected:
private: private:
void setupModelData( QSqlQuery &sqlquery); void setupModelData( QSqlQuery &sqlquery);
void setupModelDataForList(QSqlQuery &sqlquery);
ComicDB _getComic(const QModelIndex & mi); ComicDB _getComic(const QModelIndex & mi);
QList<ComicItem *> _data; QList<ComicItem *> _data;

View File

@ -220,7 +220,7 @@ bool DataBaseManagement::createTables(QSqlDatabase & database)
return success; return success;
} }
#include "QsLog.h"
bool DataBaseManagement::createV8Tables(QSqlDatabase &database) bool DataBaseManagement::createV8Tables(QSqlDatabase &database)
{ {
bool success = true; bool success = true;
@ -228,73 +228,83 @@ bool DataBaseManagement::createV8Tables(QSqlDatabase &database)
//8.0> tables //8.0> tables
//LABEL //LABEL
QSqlQuery queryLabel(database); QSqlQuery queryLabel(database);
queryLabel.prepare("CREATE TABLE label (id INTEGER PRIMARY KEY, " success = success && queryLabel.exec("CREATE TABLE label (id INTEGER PRIMARY KEY, "
"name TEXT NOT NULL, " "name TEXT NOT NULL, "
"color TEXT NOT NULL, " "color TEXT NOT NULL, "
"ordering INTEGER NOT NULL)"); //order depends on the color "ordering INTEGER NOT NULL); "); //order depends on the color
success = success && queryLabel.exec();
QSqlQuery queryIndexLabel(database);
success = success && queryIndexLabel.exec("CREATE INDEX label_ordering_index ON label (ordering)");
//COMIC LABEL //COMIC LABEL
QSqlQuery queryComicLabel(database); QSqlQuery queryComicLabel(database);
queryComicLabel.prepare("CREATE TABLE comic_label (" success = success && queryComicLabel.exec("CREATE TABLE comic_label ("
"comic_id INTEGER, " "comic_id INTEGER, "
"label_id INTEGER, " "label_id INTEGER, "
//"order INTEGER, " //TODO order???? "ordering INTEGER, " //TODO order????
"FOREIGN KEY(label_id) REFERENCES label(id) ON DELETE CASCADE, " "FOREIGN KEY(label_id) REFERENCES label(id) ON DELETE CASCADE, "
"FOREIGN KEY(comic_id) REFERENCES comic(id) ON DELETE CASCADE, " "FOREIGN KEY(comic_id) REFERENCES comic(id) ON DELETE CASCADE, "
"PRIMARY KEY(label_id, comic_id))"); "PRIMARY KEY(label_id, comic_id))");
success = success && queryComicLabel.exec();
QSqlQuery queryIndexComicLabel(database);
success = success && queryIndexComicLabel.exec("CREATE INDEX comic_label_ordering_index ON label (ordering)");
//READING LIST //READING LIST
QSqlQuery queryReadingList(database); QSqlQuery queryReadingList(database);
queryReadingList.prepare("CREATE TABLE reading_list (" success = success && queryReadingList.exec("CREATE TABLE reading_list ("
"id INTEGER PRIMARY KEY, " "id INTEGER PRIMARY KEY, "
"parentId INTEGER, " "parentId INTEGER, "
"ordering INTEGER DEFAULT 0, " //only use it if the parentId is NULL "ordering INTEGER DEFAULT 0, " //only use it if the parentId is NULL
"name TEXT NOT NULL, " "name TEXT NOT NULL, "
"finished BOOLEAN DEFAULT 0, " "finished BOOLEAN DEFAULT 0, "
"completed BOOLEAN DEFAULT 1, " "completed BOOLEAN DEFAULT 1, "
"FOREIGN KEY(parentId) REFERENCES reading_list(id) ON DELETE CASCADE)"); "FOREIGN KEY(parentId) REFERENCES reading_list(id) ON DELETE CASCADE)");
success = success && queryReadingList.exec();
QSqlQuery queryIndexReadingList(database);
success = success && queryIndexReadingList.exec("CREATE INDEX reading_list_ordering_index ON label (ordering)");
//COMIC READING LIST //COMIC READING LIST
QSqlQuery queryComicReadingList(database); QSqlQuery queryComicReadingList(database);
queryComicReadingList.prepare("CREATE TABLE comic_reading_list (" success = success && queryComicReadingList.exec("CREATE TABLE comic_reading_list ("
"reading_list_id INTEGER, " "reading_list_id INTEGER, "
"comic_id INTEGER, " "comic_id INTEGER, "
"ordering INTEGER, " "ordering INTEGER, "
"FOREIGN KEY(reading_list_id) REFERENCES reading_list(id) ON DELETE CASCADE, " "FOREIGN KEY(reading_list_id) REFERENCES reading_list(id) ON DELETE CASCADE, "
"FOREIGN KEY(comic_id) REFERENCES comic(id) ON DELETE CASCADE, " "FOREIGN KEY(comic_id) REFERENCES comic(id) ON DELETE CASCADE, "
"PRIMARY KEY(reading_list_id, comic_id))"); "PRIMARY KEY(reading_list_id, comic_id))");
success = success && queryComicReadingList.exec();
QSqlQuery queryIndexComicReadingList(database);
success = success && queryIndexComicReadingList.exec("CREATE INDEX comic_reading_list_ordering_index ON label (ordering)");
//DEFAULT READING LISTS //DEFAULT READING LISTS
QSqlQuery queryDefaultReadingList(database); QSqlQuery queryDefaultReadingList(database);
queryDefaultReadingList.prepare("CREATE TABLE default_reading_list (" success = success && queryDefaultReadingList.exec("CREATE TABLE default_reading_list ("
"id INTEGER PRIMARY KEY, " "id INTEGER PRIMARY KEY, "
"name TEXT NOT NULL" "name TEXT NOT NULL"
//TODO icon???? //TODO icon????
")"); ")");
success = success && queryDefaultReadingList.exec();
//COMIC DEFAULT READING LISTS //COMIC DEFAULT READING LISTS
QSqlQuery queryComicDefaultReadingList(database); QSqlQuery queryComicDefaultReadingList(database);
queryComicDefaultReadingList.prepare("CREATE TABLE comic_default_reading_list (" success = success && queryComicDefaultReadingList.exec("CREATE TABLE comic_default_reading_list ("
"comic_id INTEGER, " "comic_id INTEGER, "
"default_reading_list_id INTEGER, " "default_reading_list_id INTEGER, "
//"order INTEGER, " //order???? "ordering INTEGER, " //order????
"FOREIGN KEY(default_reading_list_id) REFERENCES default_reading_list(id) ON DELETE CASCADE, " "FOREIGN KEY(default_reading_list_id) REFERENCES default_reading_list(id) ON DELETE CASCADE, "
"FOREIGN KEY(comic_id) REFERENCES comic(id) ON DELETE CASCADE," "FOREIGN KEY(comic_id) REFERENCES comic(id) ON DELETE CASCADE,"
"PRIMARY KEY(default_reading_list_id, comic_id))"); "PRIMARY KEY(default_reading_list_id, comic_id))");
success = success && queryComicDefaultReadingList.exec();
QSqlQuery queryIndexComicDefaultReadingList(database);
success = success && queryIndexComicDefaultReadingList.exec("CREATE INDEX comic_default_reading_list_ordering_index ON label (ordering)");
//INSERT DEFAULT READING LISTS //INSERT DEFAULT READING LISTS
QSqlQuery queryInsertDefaultReadingList(database); QSqlQuery queryInsertDefaultReadingList(database);
queryInsertDefaultReadingList.prepare("INSERT INTO default_reading_list (name) VALUES (:name)"); //if(!queryInsertDefaultReadingList.prepare())
//1 Favorites //1 Favorites
queryInsertDefaultReadingList.bindValue(":name", "Favorites"); //queryInsertDefaultReadingList.bindValue(":name", "Favorites");
success = success && queryInsertDefaultReadingList.exec(); success = success && queryInsertDefaultReadingList.exec("INSERT INTO default_reading_list (name) VALUES (\"Favorites\")");
QLOG_ERROR() << success;
//Reading doesn't need its onw list //Reading doesn't need its onw list