From: APTX Date: Mon, 16 Jul 2012 02:20:19 +0000 (+0200) Subject: Rework getFile* methods. Add fail_count column to request tables X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=78f9ef11b7fa9643e5a5fa391b3c574788120da1;p=localmylist.git Rework getFile* methods. Add fail_count column to request tables --- diff --git a/localmylist/database.cpp b/localmylist/database.cpp index f2517db..323bb1c 100644 --- a/localmylist/database.cpp +++ b/localmylist/database.cpp @@ -514,69 +514,109 @@ File Database::getFile(int fid) { File f; - d->getFileQuery.bindValue(":fid", fid); + QSqlQuery &q = prepare( + "SELECT fid, eid, aid, gid, anidb_update, entry_update, my_update, " + " ed2k, size, length, extension, group_name, group_name_short, crc, " + " release_date, version, censored, type, quality_id, quality, resolution, " + " video_codec, audio_codec, audio_language, subtitle_language, aspect_ratio, " + " my_watched, my_state, my_file_state, my_storage, my_source, my_other " + " FROM file " + " WHERE fid = :fid "); + q.bindValue(":fid", fid); - if (!exec(d->getFileQuery)) + if (!exec(q)) return f; - if (!d->getFileQuery.next()) + if (!q.next()) { - d->getFileQuery.finish(); + q.finish(); return f; } - f.fid = d->getFileQuery.value(0).toInt(); - f.eid = d->getFileQuery.value(1).toInt(); - f.aid = d->getFileQuery.value(2).toInt(); - f.gid = d->getFileQuery.value(3).toInt(); - f.anidbUpdate = d->getFileQuery.value(4).toDateTime(); - f.entryUpdate = d->getFileQuery.value(5).toDateTime(); - f.myUpdate = d->getFileQuery.value(6).toDateTime(); - f.ed2k = d->getFileQuery.value(7).toByteArray(); - f.size = d->getFileQuery.value(8).toLongLong(); - f.length = d->getFileQuery.value(9).toInt(); - f.extension = d->getFileQuery.value(10).toString(); - f.groupName = d->getFileQuery.value(11).toString(); - f.groupNameShort = d->getFileQuery.value(12).toString(); - f.crc = d->getFileQuery.value(13).toString(); - f.releaseDate = d->getFileQuery.value(14).toDateTime(); - f.version = d->getFileQuery.value(15).toInt(); - f.censored = d->getFileQuery.value(16).toBool(); - f.type = d->getFileQuery.value(17).toString(); - f.qualityId = d->getFileQuery.value(18).toInt(); - f.quality = d->getFileQuery.value(19).toString(); - f.resolution = d->getFileQuery.value(20).toString(); - f.videoCodec = d->getFileQuery.value(21).toString(); - f.audioCodec = d->getFileQuery.value(22).toString(); - f.audioLanguage = d->getFileQuery.value(23).toString(); - f.subtitleLanguage = d->getFileQuery.value(24).toString(); - f.aspectRatio = d->getFileQuery.value(25).toString(); - f.myWatched = d->getFileQuery.value(26).toDateTime(); - f.myState = d->getFileQuery.value(27).toInt(); - f.myFileState = d->getFileQuery.value(28).toInt(); - f.myStorage = d->getFileQuery.value(29).toString(); - f.mySource = d->getFileQuery.value(30).toString(); - f.myOther = d->getFileQuery.value(31).toString(); - - d->getFileQuery.finish(); + readFile(q); + q.finish(); return f; } File Database::getFileByPath(const QString &path) { - QSqlQuery q(d->db); - q.prepare("SELECT fid from file_location WHERE path = :path"); + QSqlQuery &q = prepare( + "SELECT f.fid, f.eid, f.aid, f.gid, f.anidb_update, f.entry_update, f.my_update, " + " f.ed2k, f.size, f.length, f.extension, f.group_name, f.group_name_short, f.crc, " + " f.release_date, f.version, f.censored, f.type, f.quality_id, f.quality, f.resolution, " + " f.video_codec, f.audio_codec, f.audio_language, f.subtitle_language, f.aspect_ratio, " + " f.my_watched, f.my_state, f.my_file_state, f.my_storage, f.my_source, f.my_other " + " FROM file f " + " JOIN file_location fl ON (fl.fid = f.fid) " + " WHERE fl.path = :path "); q.bindValue(":path", path); if (!exec(q)) return File(); if (!q.next()) + { + q.finish(); + return File(); + } + + File f = readFile(q); + + q.finish(); + + return f; +} + +File Database::getFileByTitle(const QString &title, int epno) +{ + QSqlQuery &q = prepare( + "SELECT f.fid, f.eid, f.aid, f.gid, f.anidb_update, f.entry_update, f.my_update, " + " f.ed2k, f.size, f.length, f.extension, f.group_name, f.group_name_short, f.crc, " + " f.release_date, f.version, f.censored, f.type, f.quality_id, f.quality, f.resolution, " + " f.video_codec, f.audio_codec, f.audio_language, f.subtitle_language, f.aspect_ratio, " + " f.my_watched, f.my_state, f.my_file_state, f.my_storage, f.my_source, f.my_other " + " FROM file f " + " LEFT JOIN anime_title at ON f.aid = at.aid " + " LEFT JOIN episode e ON f.eid = e.eid " + " WHERE lower(at.title) = :title " + " AND e.epno = :epno " + "UNION " + "SELECT f.fid, f.eid, f.aid, f.gid, f.anidb_update, f.entry_update, f.my_update, " + " f.ed2k, f.size, f.length, f.extension, f.group_name, f.group_name_short, f.crc, " + " f.release_date, f.version, f.censored, f.type, f.quality_id, f.quality, f.resolution, " + " f.video_codec, f.audio_codec, f.audio_language, f.subtitle_language, f.aspect_ratio, " + " f.my_watched, f.my_state, f.my_file_state, f.my_storage, f.my_source, f.my_other " + " FROM file f " + " LEFT JOIN anime a ON f.aid = a.aid " + " LEFT JOIN anime_title at ON f.aid = at.aid " + " LEFT JOIN episode e ON f.eid = e.eid " + " WHERE at.title ILIKE :fuzzyTitle " + " AND e.epno = :epno " + "GROUP BY f.fid, f.eid, f.aid, f.gid, f.anidb_update, f.entry_update, f.my_update, " + " f.ed2k, f.size, f.length, f.extension, f.group_name, f.group_name_short, f.crc, " + " f.release_date, f.version, f.censored, f.type, f.quality_id, f.quality, f.resolution, " + " f.video_codec, f.audio_codec, f.audio_language, f.subtitle_language, f.aspect_ratio, " + " f.my_watched, f.my_state, f.my_file_state, f.my_storage, f.my_source, f.my_other "); + + q.bindValue(":title", title); + q.bindValue(":fuzzyTitle", "%" + title + "%"); + q.bindValue(":epno", epno); + + if (!exec(q)) + return File(); + + if (!q.next()) + { + q.finish(); return File(); + } + + File f = readFile(q); + + q.finish(); - int fid = q.value(0).toInt(); - return getFile(fid); + return f; } bool Database::setAnime(const Anime &anime) @@ -1168,15 +1208,6 @@ void Database::prepareQueries() "FROM episode " "WHERE eid = :eid"); - d->getFileQuery = QSqlQuery(d->db); - d->getFileQuery.prepare("SELECT fid, eid, aid, gid, anidb_update, entry_update, my_update, " - "ed2k, size, length, extension, group_name, group_name_short, crc, " - "release_date, version, censored, type, quality_id, quality, resolution, " - "video_codec, audio_codec, audio_language, subtitle_language, aspect_ratio, " - "my_watched, my_state, my_file_state, my_storage, my_source, my_other " - "FROM file " - "WHERE fid = :fid"); - d->setAnimeQuery = QSqlQuery(d->db); d->setAnimeQuery.prepare("UPDATE anime SET " "anidb_update = :anidbUpdate, entry_update = :entryUpdate, " @@ -1254,7 +1285,7 @@ void Database::prepareQueries() d->removeUnknownFileQuery.prepare("DELETE FROM unknown_file WHERE ed2k = :ed2k AND size = :size"); d->addPendingRequestQuery = QSqlQuery(d->db); - d->addPendingRequestQuery.prepare("INSERT INTO pending_request VALUES(:aid, :eid, :fid, :ed2k, :size, DEFAULT, DEFAULT, DEFAULT)"); + d->addPendingRequestQuery.prepare("INSERT INTO pending_request VALUES(:aid, :eid, :fid, :ed2k, :size, DEFAULT, DEFAULT, DEFAULT, DEFAULT)"); d->getRequestBatchQuery = QSqlQuery(d->db); d->getRequestBatchQuery.prepare("UPDATE pending_request SET start = NOW() " @@ -1270,7 +1301,7 @@ void Database::prepareQueries() d->addPendingMyListUpdateQuery = QSqlQuery(d->db); d->addPendingMyListUpdateQuery.prepare("INSERT INTO pending_mylist_update VALUES(DEFAULT, :fid, :setMyWatched, :myWatched, " ":setMyState, :myState, :setMyFileState, :myFileState, :setMyStorage, :myStorage, " - ":setMySource, :mySource, :setMyOther, :myOther, DEFAULT, DEFAULT, DEFAULT)"); + ":setMySource, :mySource, :setMyOther, :myOther, DEFAULT, DEFAULT, DEFAULT, DEFAULT)"); d->getPendingMyListUpdateBatchQuery = QSqlQuery(d->db); d->getPendingMyListUpdateBatchQuery.prepare("UPDATE pending_mylist_update SET started = NOW() " @@ -1321,6 +1352,45 @@ OpenFileData Database::readOpenFileData(QSqlQuery &q) return data; } +File Database::readFile(QSqlQuery &q) +{ + File f; + f.fid = q.value(0).toInt(); + f.eid = q.value(1).toInt(); + f.aid = q.value(2).toInt(); + f.gid = q.value(3).toInt(); + f.anidbUpdate = q.value(4).toDateTime(); + f.entryUpdate = q.value(5).toDateTime(); + f.myUpdate = q.value(6).toDateTime(); + f.ed2k = q.value(7).toByteArray(); + f.size = q.value(8).toLongLong(); + f.length = q.value(9).toInt(); + f.extension = q.value(10).toString(); + f.groupName = q.value(11).toString(); + f.groupNameShort = q.value(12).toString(); + f.crc = q.value(13).toString(); + f.releaseDate = q.value(14).toDateTime(); + f.version = q.value(15).toInt(); + f.censored = q.value(16).toBool(); + f.type = q.value(17).toString(); + f.qualityId = q.value(18).toInt(); + f.quality = q.value(19).toString(); + f.resolution = q.value(20).toString(); + f.videoCodec = q.value(21).toString(); + f.audioCodec = q.value(22).toString(); + f.audioLanguage = q.value(23).toString(); + f.subtitleLanguage = q.value(24).toString(); + f.aspectRatio = q.value(25).toString(); + f.myWatched = q.value(26).toDateTime(); + f.myState = q.value(27).toInt(); + f.myFileState = q.value(28).toInt(); + f.myStorage = q.value(29).toString(); + f.mySource = q.value(30).toString(); + f.myOther = q.value(31).toString(); + + return f; +} + QSqlQuery &Database::prepare(const char *const sql) { auto it = d->preparedQueries.find(sql); diff --git a/localmylist/database.h b/localmylist/database.h index 9079183..feb67ac 100644 --- a/localmylist/database.h +++ b/localmylist/database.h @@ -252,6 +252,7 @@ public slots: LocalMyList::Episode getEpisode(int eid); LocalMyList::File getFile(int fid); LocalMyList::File getFileByPath(const QString &path); + LocalMyList::File getFileByTitle(const QString &title, int epno = 1); bool setAnime(const LocalMyList::Anime &anime); bool setEpisode(const LocalMyList::Episode &episode); @@ -325,6 +326,7 @@ private: bool checkError(QSqlQuery &query, bool prepared = false); OpenFileData readOpenFileData(QSqlQuery &q); + File readFile(QSqlQuery &q); DatabaseInternal *d; DatabaseConnectionSettings m_connectionSettings; diff --git a/localmylist/share/schema/schema.sql b/localmylist/share/schema/schema.sql index e091f59..a5f0211 100644 --- a/localmylist/share/schema/schema.sql +++ b/localmylist/share/schema/schema.sql @@ -146,6 +146,7 @@ CREATE TABLE pending_mylist_update added timestamp without time zone DEFAULT now(), started timestamp without time zone, finished timestamp without time zone, + fail_count integer NOT NULL DEFAULT 0, CONSTRAINT pending_mylist_update_pk PRIMARY KEY (update_id ) ); @@ -158,6 +159,7 @@ CREATE TABLE pending_request ( priority integer DEFAULT 1 NOT NULL, added timestamp without time zone DEFAULT now(), start timestamp without time zone, + fail_count integer NOT NULL DEFAULT 0, CONSTRAINT pending_request_pk PRIMARY KEY (aid, eid, fid, ed2k, size) ); CREATE INDEX pending_request_added_idx ON pending_request USING btree (added);