]> Some of my projects - localmylist.git/commitdiff
Add getEpisodes() to get all episodes of an anime. Expose sequence types in scriptable
authorAPTX <marek321@gmail.com>
Mon, 16 Jul 2012 14:28:31 +0000 (16:28 +0200)
committerAPTX <marek321@gmail.com>
Mon, 16 Jul 2012 14:28:31 +0000 (16:28 +0200)
localmylist/database.cpp
localmylist/database.h
localmylist/scriptable.cpp

index 323bb1cb8293fdf09b7eb6411896d4f91854d809..96dbae61a847f211d3451ea88e7afa049d3a0c52 100644 (file)
@@ -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<Episode> Database::getEpisodes(int aid)
+{
+       QList<Episode> 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;
index feb67ac2bfa313804aa48a27359c77f05b4291dd..7ce916a6172a51b203dff8202705ed1560b1b818 100644 (file)
@@ -249,6 +249,7 @@ public slots:
        bool setFileLocation(const LocalMyList::FileLocation &fileLocation);
 
        LocalMyList::Anime getAnime(int aid);
+       QList<LocalMyList::Episode> 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<LocalMyList::Anime>)
+Q_DECLARE_METATYPE(QList<LocalMyList::Anime*>)
 Q_DECLARE_METATYPE(LocalMyList::Episode)
 Q_DECLARE_METATYPE(LocalMyList::Episode*)
+Q_DECLARE_METATYPE(QList<LocalMyList::Episode>)
+Q_DECLARE_METATYPE(QList<LocalMyList::Episode*>)
 Q_DECLARE_METATYPE(LocalMyList::File)
 Q_DECLARE_METATYPE(LocalMyList::File*)
+Q_DECLARE_METATYPE(QList<LocalMyList::File>)
+Q_DECLARE_METATYPE(QList<LocalMyList::File*>)
 Q_DECLARE_METATYPE(LocalMyList::FileLocation)
 Q_DECLARE_METATYPE(LocalMyList::FileLocation*)
+Q_DECLARE_METATYPE(QList<LocalMyList::FileLocation>)
+Q_DECLARE_METATYPE(QList<LocalMyList::FileLocation*>)
 Q_DECLARE_METATYPE(LocalMyList::UnknownFile)
 Q_DECLARE_METATYPE(LocalMyList::UnknownFile*)
+Q_DECLARE_METATYPE(QList<LocalMyList::UnknownFile>)
+Q_DECLARE_METATYPE(QList<LocalMyList::UnknownFile*>)
 Q_DECLARE_METATYPE(LocalMyList::FileEpisodeRel)
 Q_DECLARE_METATYPE(LocalMyList::FileEpisodeRel*)
+Q_DECLARE_METATYPE(QList<LocalMyList::FileEpisodeRel>)
+Q_DECLARE_METATYPE(QList<LocalMyList::FileEpisodeRel*>)
 Q_DECLARE_METATYPE(LocalMyList::PendingRequest)
 Q_DECLARE_METATYPE(LocalMyList::PendingRequest*)
+Q_DECLARE_METATYPE(QList<LocalMyList::PendingRequest>)
+Q_DECLARE_METATYPE(QList<LocalMyList::PendingRequest*>)
 Q_DECLARE_METATYPE(LocalMyList::PendingMyListUpdate)
 Q_DECLARE_METATYPE(LocalMyList::PendingMyListUpdate*)
+Q_DECLARE_METATYPE(QList<LocalMyList::PendingMyListUpdate>)
+Q_DECLARE_METATYPE(QList<LocalMyList::PendingMyListUpdate*>)
 Q_DECLARE_METATYPE(LocalMyList::HostInfo)
 Q_DECLARE_METATYPE(LocalMyList::HostInfo*)
+Q_DECLARE_METATYPE(QList<LocalMyList::HostInfo>)
+Q_DECLARE_METATYPE(QList<LocalMyList::HostInfo*>)
 Q_DECLARE_METATYPE(LocalMyList::OpenFileData)
 Q_DECLARE_METATYPE(LocalMyList::OpenFileData*)
+Q_DECLARE_METATYPE(QList<LocalMyList::OpenFileData>)
+Q_DECLARE_METATYPE(QList<LocalMyList::OpenFileData*>)
 Q_DECLARE_METATYPE(LocalMyList::DatabaseConnectionSettings)
 Q_DECLARE_METATYPE(LocalMyList::DatabaseConnectionSettings*)
+Q_DECLARE_METATYPE(QList<LocalMyList::DatabaseConnectionSettings>)
+Q_DECLARE_METATYPE(QList<LocalMyList::DatabaseConnectionSettings*>)
 
 #endif // DATABASE_H
index 48ff43ad483dab5f59f76860d3aeda520af31e8b..199caa714a528974c945cf0891b1d0a32e43d2bf 100644 (file)
@@ -15,46 +15,68 @@ void registerTypes(QScriptEngine *engine)
        Scriptable::Anime *AnimePrototype = new Scriptable::Anime();
        engine->setDefaultPrototype(qMetaTypeId<Anime>(), engine->newQObject(AnimePrototype));
        engine->setDefaultPrototype(qMetaTypeId<Anime*>(), engine->newQObject(AnimePrototype));
+       qScriptRegisterSequenceMetaType<QList<Anime> >(engine);
+       qScriptRegisterSequenceMetaType<QList<Anime*> >(engine);
        engine->globalObject().setProperty("Anime", engine->newFunction(Scriptable::Anime_ctor));
        Scriptable::Episode *EpisodePrototype = new Scriptable::Episode();
        engine->setDefaultPrototype(qMetaTypeId<Episode>(), engine->newQObject(EpisodePrototype));
        engine->setDefaultPrototype(qMetaTypeId<Episode*>(), engine->newQObject(EpisodePrototype));
+       qScriptRegisterSequenceMetaType<QList<Episode> >(engine);
+       qScriptRegisterSequenceMetaType<QList<Episode*> >(engine);
        engine->globalObject().setProperty("Episode", engine->newFunction(Scriptable::Episode_ctor));
        Scriptable::File *FilePrototype = new Scriptable::File();
        engine->setDefaultPrototype(qMetaTypeId<File>(), engine->newQObject(FilePrototype));
        engine->setDefaultPrototype(qMetaTypeId<File*>(), engine->newQObject(FilePrototype));
+       qScriptRegisterSequenceMetaType<QList<File> >(engine);
+       qScriptRegisterSequenceMetaType<QList<File*> >(engine);
        engine->globalObject().setProperty("File", engine->newFunction(Scriptable::File_ctor));
        Scriptable::FileLocation *FileLocationPrototype = new Scriptable::FileLocation();
        engine->setDefaultPrototype(qMetaTypeId<FileLocation>(), engine->newQObject(FileLocationPrototype));
        engine->setDefaultPrototype(qMetaTypeId<FileLocation*>(), engine->newQObject(FileLocationPrototype));
+       qScriptRegisterSequenceMetaType<QList<FileLocation> >(engine);
+       qScriptRegisterSequenceMetaType<QList<FileLocation*> >(engine);
        engine->globalObject().setProperty("FileLocation", engine->newFunction(Scriptable::FileLocation_ctor));
        Scriptable::UnknownFile *UnknownFilePrototype = new Scriptable::UnknownFile();
        engine->setDefaultPrototype(qMetaTypeId<UnknownFile>(), engine->newQObject(UnknownFilePrototype));
        engine->setDefaultPrototype(qMetaTypeId<UnknownFile*>(), engine->newQObject(UnknownFilePrototype));
+       qScriptRegisterSequenceMetaType<QList<UnknownFile> >(engine);
+       qScriptRegisterSequenceMetaType<QList<UnknownFile*> >(engine);
        engine->globalObject().setProperty("UnknownFile", engine->newFunction(Scriptable::UnknownFile_ctor));
        Scriptable::FileEpisodeRel *FileEpisodeRelPrototype = new Scriptable::FileEpisodeRel();
        engine->setDefaultPrototype(qMetaTypeId<FileEpisodeRel>(), engine->newQObject(FileEpisodeRelPrototype));
        engine->setDefaultPrototype(qMetaTypeId<FileEpisodeRel*>(), engine->newQObject(FileEpisodeRelPrototype));
+       qScriptRegisterSequenceMetaType<QList<FileEpisodeRel> >(engine);
+       qScriptRegisterSequenceMetaType<QList<FileEpisodeRel*> >(engine);
        engine->globalObject().setProperty("FileEpisodeRel", engine->newFunction(Scriptable::FileEpisodeRel_ctor));
        Scriptable::PendingRequest *PendingRequestPrototype = new Scriptable::PendingRequest();
        engine->setDefaultPrototype(qMetaTypeId<PendingRequest>(), engine->newQObject(PendingRequestPrototype));
        engine->setDefaultPrototype(qMetaTypeId<PendingRequest*>(), engine->newQObject(PendingRequestPrototype));
+       qScriptRegisterSequenceMetaType<QList<PendingRequest> >(engine);
+       qScriptRegisterSequenceMetaType<QList<PendingRequest*> >(engine);
        engine->globalObject().setProperty("PendingRequest", engine->newFunction(Scriptable::PendingRequest_ctor));
        Scriptable::PendingMyListUpdate *PendingMyListUpdatePrototype = new Scriptable::PendingMyListUpdate();
        engine->setDefaultPrototype(qMetaTypeId<PendingMyListUpdate>(), engine->newQObject(PendingMyListUpdatePrototype));
        engine->setDefaultPrototype(qMetaTypeId<PendingMyListUpdate*>(), engine->newQObject(PendingMyListUpdatePrototype));
+       qScriptRegisterSequenceMetaType<QList<PendingMyListUpdate> >(engine);
+       qScriptRegisterSequenceMetaType<QList<PendingMyListUpdate*> >(engine);
        engine->globalObject().setProperty("PendingMyListUpdate", engine->newFunction(Scriptable::PendingMyListUpdate_ctor));
        Scriptable::HostInfo *HostInfoPrototype = new Scriptable::HostInfo();
        engine->setDefaultPrototype(qMetaTypeId<HostInfo>(), engine->newQObject(HostInfoPrototype));
        engine->setDefaultPrototype(qMetaTypeId<HostInfo*>(), engine->newQObject(HostInfoPrototype));
+       qScriptRegisterSequenceMetaType<QList<HostInfo> >(engine);
+       qScriptRegisterSequenceMetaType<QList<HostInfo*> >(engine);
        engine->globalObject().setProperty("HostInfo", engine->newFunction(Scriptable::HostInfo_ctor));
        Scriptable::OpenFileData *OpenFileDataPrototype = new Scriptable::OpenFileData();
        engine->setDefaultPrototype(qMetaTypeId<OpenFileData>(), engine->newQObject(OpenFileDataPrototype));
        engine->setDefaultPrototype(qMetaTypeId<OpenFileData*>(), engine->newQObject(OpenFileDataPrototype));
+       qScriptRegisterSequenceMetaType<QList<OpenFileData> >(engine);
+       qScriptRegisterSequenceMetaType<QList<OpenFileData*> >(engine);
        engine->globalObject().setProperty("OpenFileData", engine->newFunction(Scriptable::OpenFileData_ctor));
        Scriptable::DatabaseConnectionSettings *DatabaseConnectionSettingsPrototype = new Scriptable::DatabaseConnectionSettings();
        engine->setDefaultPrototype(qMetaTypeId<DatabaseConnectionSettings>(), engine->newQObject(DatabaseConnectionSettingsPrototype));
        engine->setDefaultPrototype(qMetaTypeId<DatabaseConnectionSettings*>(), engine->newQObject(DatabaseConnectionSettingsPrototype));
+       qScriptRegisterSequenceMetaType<QList<DatabaseConnectionSettings> >(engine);
+       qScriptRegisterSequenceMetaType<QList<DatabaseConnectionSettings*> >(engine);
        engine->globalObject().setProperty("DatabaseConnectionSettings", engine->newFunction(Scriptable::DatabaseConnectionSettings_ctor));
 }