From: APTX Date: Mon, 16 Jul 2012 14:28:31 +0000 (+0200) Subject: Add getEpisodes() to get all episodes of an anime. Expose sequence types in scriptable X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=32efc8916cc50e57033d6de86d751682f8aa0c84;p=localmylist.git Add getEpisodes() to get all episodes of an anime. Expose sequence types in scriptable --- diff --git a/localmylist/database.cpp b/localmylist/database.cpp index 323bb1c..96dbae6 100644 --- a/localmylist/database.cpp +++ b/localmylist/database.cpp @@ -136,8 +136,6 @@ struct DatabaseInternal QSqlQuery updateSettingQuery; QSqlQuery getAnimeQuery; - QSqlQuery getEpisodeQuery; - QSqlQuery getFileQuery; QSqlQuery setAnimeQuery; QSqlQuery setEpisodeQuery; @@ -469,43 +467,58 @@ Anime Database::getAnime(int aid) return a; } +QList Database::getEpisodes(int aid) +{ + QList episodes; + + QSqlQuery &q = prepare( + "SELECT e.eid, e.aid, e.anidb_update, e.entry_update, e.my_update, e.epno, " + " e.title_english, e.title_romaji, e.title_kanji, e.length, e.airdate, e.state, " + " e.special, e.recap, e.opening, e.ending, e.rating, e.votes, e.my_vote, e.my_vote_date " + " FROM episode e " + " JOIN anime a ON (a.aid = e.aid)" + " WHERE a.aid = :aid"); + + q.bindValue(":aid", aid); + + if (!exec(q)) + return episodes; + + while (q.next()) + { + episodes << readEpisode(q); + } + + q.finish(); + + return episodes; +} + Episode Database::getEpisode(int eid) { Episode e; - d->getEpisodeQuery.bindValue(":eid", eid); + QSqlQuery &q = prepare( + "SELECT eid, aid, anidb_update, entry_update, my_update, epno, " + " title_english, title_romaji, title_kanji, length, airdate, state, " + " special, recap, opening, ending, rating, votes, my_vote, my_vote_date " + " FROM episode " + " WHERE eid = :eid"); + + q.bindValue(":eid", eid); - if (!exec(d->getEpisodeQuery)) + if (!exec(q)) return e; - if (!d->getEpisodeQuery.next()) + if (!q.next()) { - d->getEpisodeQuery.finish(); + q.finish(); return e; } - e.eid = d->getEpisodeQuery.value(0).toInt(); - e.aid = d->getEpisodeQuery.value(1).toInt(); - e.anidbUpdate = d->getEpisodeQuery.value(2).toDateTime(); - e.entryUpdate = d->getEpisodeQuery.value(3).toDateTime(); - e.myUpdate = d->getEpisodeQuery.value(4).toDateTime(); - e.epno = d->getEpisodeQuery.value(5).toInt(); - e.titleEnglish = d->getEpisodeQuery.value(6).toString(); - e.titleRomaji = d->getEpisodeQuery.value(7).toString(); - e.titleKanji = d->getEpisodeQuery.value(8).toString(); - e.length = d->getEpisodeQuery.value(9).toInt(); - e.airdate = d->getEpisodeQuery.value(10).toDateTime(); - e.state = d->getEpisodeQuery.value(11).toInt(); - e.special = d->getEpisodeQuery.value(12).toBool(); - e.recap = d->getEpisodeQuery.value(13).toBool(); - e.opening = d->getEpisodeQuery.value(14).toBool(); - e.ending = d->getEpisodeQuery.value(15).toBool(); - e.rating = d->getEpisodeQuery.value(16).toDouble(); - e.votes = d->getEpisodeQuery.value(17).toInt(); - e.myVote = d->getEpisodeQuery.value(18).toDouble(); - e.myVoteDate = d->getEpisodeQuery.value(19).toDateTime(); - - d->getEpisodeQuery.finish(); + e = readEpisode(q); + + q.finish(); return e; } @@ -1201,13 +1214,6 @@ void Database::prepareQueries() "FROM anime " "WHERE aid = :aid"); - d->getEpisodeQuery = QSqlQuery(d->db); - d->getEpisodeQuery.prepare("SELECT eid, aid, anidb_update, entry_update, my_update, epno, " - "title_english, title_romaji, title_kanji, length, airdate, state, " - "special, recap, opening, ending, rating, votes, my_vote, my_vote_date " - "FROM episode " - "WHERE eid = :eid"); - d->setAnimeQuery = QSqlQuery(d->db); d->setAnimeQuery.prepare("UPDATE anime SET " "anidb_update = :anidbUpdate, entry_update = :entryUpdate, " @@ -1352,6 +1358,33 @@ OpenFileData Database::readOpenFileData(QSqlQuery &q) return data; } +Episode Database::readEpisode(QSqlQuery &q) +{ + Episode e; + e.eid = q.value(0).toInt(); + e.aid = q.value(1).toInt(); + e.anidbUpdate = q.value(2).toDateTime(); + e.entryUpdate = q.value(3).toDateTime(); + e.myUpdate = q.value(4).toDateTime(); + e.epno = q.value(5).toInt(); + e.titleEnglish = q.value(6).toString(); + e.titleRomaji = q.value(7).toString(); + e.titleKanji = q.value(8).toString(); + e.length = q.value(9).toInt(); + e.airdate = q.value(10).toDateTime(); + e.state = q.value(11).toInt(); + e.special = q.value(12).toBool(); + e.recap = q.value(13).toBool(); + e.opening = q.value(14).toBool(); + e.ending = q.value(15).toBool(); + e.rating = q.value(16).toDouble(); + e.votes = q.value(17).toInt(); + e.myVote = q.value(18).toDouble(); + e.myVoteDate = q.value(19).toDateTime(); + + return e; +} + File Database::readFile(QSqlQuery &q) { File f; diff --git a/localmylist/database.h b/localmylist/database.h index feb67ac..7ce916a 100644 --- a/localmylist/database.h +++ b/localmylist/database.h @@ -249,6 +249,7 @@ public slots: bool setFileLocation(const LocalMyList::FileLocation &fileLocation); LocalMyList::Anime getAnime(int aid); + QList getEpisodes(int aid); LocalMyList::Episode getEpisode(int eid); LocalMyList::File getFile(int fid); LocalMyList::File getFileByPath(const QString &path); @@ -326,6 +327,7 @@ private: bool checkError(QSqlQuery &query, bool prepared = false); OpenFileData readOpenFileData(QSqlQuery &q); + Episode readEpisode(QSqlQuery &q); File readFile(QSqlQuery &q); DatabaseInternal *d; @@ -351,25 +353,47 @@ Q_DECLARE_METATYPE(LocalMyList::Database*) Q_DECLARE_METATYPE(LocalMyList::Anime) Q_DECLARE_METATYPE(LocalMyList::Anime*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::Episode) Q_DECLARE_METATYPE(LocalMyList::Episode*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::File) Q_DECLARE_METATYPE(LocalMyList::File*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::FileLocation) Q_DECLARE_METATYPE(LocalMyList::FileLocation*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::UnknownFile) Q_DECLARE_METATYPE(LocalMyList::UnknownFile*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::FileEpisodeRel) Q_DECLARE_METATYPE(LocalMyList::FileEpisodeRel*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::PendingRequest) Q_DECLARE_METATYPE(LocalMyList::PendingRequest*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::PendingMyListUpdate) Q_DECLARE_METATYPE(LocalMyList::PendingMyListUpdate*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::HostInfo) Q_DECLARE_METATYPE(LocalMyList::HostInfo*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::OpenFileData) Q_DECLARE_METATYPE(LocalMyList::OpenFileData*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(LocalMyList::DatabaseConnectionSettings) Q_DECLARE_METATYPE(LocalMyList::DatabaseConnectionSettings*) +Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) #endif // DATABASE_H diff --git a/localmylist/scriptable.cpp b/localmylist/scriptable.cpp index 48ff43a..199caa7 100644 --- a/localmylist/scriptable.cpp +++ b/localmylist/scriptable.cpp @@ -15,46 +15,68 @@ void registerTypes(QScriptEngine *engine) Scriptable::Anime *AnimePrototype = new Scriptable::Anime(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(AnimePrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(AnimePrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("Anime", engine->newFunction(Scriptable::Anime_ctor)); Scriptable::Episode *EpisodePrototype = new Scriptable::Episode(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(EpisodePrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(EpisodePrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("Episode", engine->newFunction(Scriptable::Episode_ctor)); Scriptable::File *FilePrototype = new Scriptable::File(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(FilePrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(FilePrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("File", engine->newFunction(Scriptable::File_ctor)); Scriptable::FileLocation *FileLocationPrototype = new Scriptable::FileLocation(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(FileLocationPrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(FileLocationPrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("FileLocation", engine->newFunction(Scriptable::FileLocation_ctor)); Scriptable::UnknownFile *UnknownFilePrototype = new Scriptable::UnknownFile(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(UnknownFilePrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(UnknownFilePrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("UnknownFile", engine->newFunction(Scriptable::UnknownFile_ctor)); Scriptable::FileEpisodeRel *FileEpisodeRelPrototype = new Scriptable::FileEpisodeRel(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(FileEpisodeRelPrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(FileEpisodeRelPrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("FileEpisodeRel", engine->newFunction(Scriptable::FileEpisodeRel_ctor)); Scriptable::PendingRequest *PendingRequestPrototype = new Scriptable::PendingRequest(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(PendingRequestPrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(PendingRequestPrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("PendingRequest", engine->newFunction(Scriptable::PendingRequest_ctor)); Scriptable::PendingMyListUpdate *PendingMyListUpdatePrototype = new Scriptable::PendingMyListUpdate(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(PendingMyListUpdatePrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(PendingMyListUpdatePrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("PendingMyListUpdate", engine->newFunction(Scriptable::PendingMyListUpdate_ctor)); Scriptable::HostInfo *HostInfoPrototype = new Scriptable::HostInfo(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(HostInfoPrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(HostInfoPrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("HostInfo", engine->newFunction(Scriptable::HostInfo_ctor)); Scriptable::OpenFileData *OpenFileDataPrototype = new Scriptable::OpenFileData(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(OpenFileDataPrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(OpenFileDataPrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("OpenFileData", engine->newFunction(Scriptable::OpenFileData_ctor)); Scriptable::DatabaseConnectionSettings *DatabaseConnectionSettingsPrototype = new Scriptable::DatabaseConnectionSettings(); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(DatabaseConnectionSettingsPrototype)); engine->setDefaultPrototype(qMetaTypeId(), engine->newQObject(DatabaseConnectionSettingsPrototype)); + qScriptRegisterSequenceMetaType >(engine); + qScriptRegisterSequenceMetaType >(engine); engine->globalObject().setProperty("DatabaseConnectionSettings", engine->newFunction(Scriptable::DatabaseConnectionSettings_ctor)); }