History.cpp 3.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "History.h"
28
#include "Media.h"
29 30

#include "database/SqliteTools.h"
31
#include "database/SqliteQuery.h"
32 33 34 35 36 37 38

namespace medialibrary
{

namespace policy
{
const std::string HistoryTable::Name = "History";
39 40
const std::string HistoryTable::PrimaryKeyColumn = "id_media";
int64_t History::* const HistoryTable::PrimaryKey = &History::m_mediaId;
41 42 43 44 45
}

constexpr unsigned int History::MaxEntries;

History::History( MediaLibraryPtr ml, sqlite::Row& row )
46
    : m_media( Media::load( ml, row ) )
47
{
48 49 50
    // In case we load the media from cache, we won't advance in columns
    row.advanceToColumn( row.nbColumns() - 1 );
    row >> m_date;
51 52
}

53
void History::createTable( sqlite::Connection* dbConnection )
54 55 56
{
    const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::HistoryTable::Name +
            "("
57 58 59 60
                "id_media INTEGER PRIMARY KEY,"
                "insertion_date UNSIGNED INT NOT NULL,"
                "FOREIGN KEY (id_media) REFERENCES " + policy::MediaTable::Name +
                "(id_media) ON DELETE CASCADE"
61
            ")";
62 63 64 65 66 67
    // Don't index the id_media field, we don't want to select history records using the media_id
    sqlite::Tools::executeRequest( dbConnection, req );
}

void History::createTriggers(sqlite::Connection* dbConnection)
{
68 69 70
    const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS limit_nb_records AFTER INSERT ON "
            + policy::HistoryTable::Name +
            " BEGIN "
71 72
            "DELETE FROM " + policy::HistoryTable::Name + " WHERE id_media in "
                "(SELECT id_media FROM " + policy::HistoryTable::Name +
73 74
                " ORDER BY insertion_date DESC LIMIT -1 OFFSET " + std::to_string( MaxEntries ) + ");"
            " END";
75
    sqlite::Tools::executeRequest( dbConnection, triggerReq );
76 77
}

78
bool History::insert( sqlite::Connection* dbConn, int64_t mediaId )
79 80
{
    static const std::string req = "INSERT OR REPLACE INTO " + policy::HistoryTable::Name +
81 82
            "(id_media, insertion_date) VALUES(?, strftime('%s', 'now'))";
    return sqlite::Tools::executeInsert( dbConn, req, mediaId ) != 0;
83 84
}

85
Query<IHistoryEntry> History::fetch( MediaLibraryPtr ml )
86
{
87
    static const std::string req = "FROM " + policy::MediaTable::Name + " f "
88 89
            "INNER JOIN " + policy::HistoryTable::Name + " h ON h.id_media = f.id_media "
            "ORDER BY h.insertion_date DESC";
90
    return make_query<History, IHistoryEntry>( ml, "f.*, h.insertion_date", req );
91 92
}

93
void History::clearStreams( MediaLibraryPtr ml )
94
{
95
    static const std::string req = "DELETE FROM " + policy::HistoryTable::Name;
96
    sqlite::Tools::executeRequest( ml->getConn(), req );
97 98
}

99
MediaPtr History::media() const
100
{
101
    return m_media;
102 103 104 105 106 107 108 109
}

unsigned int History::insertionDate() const
{
    return m_date;
}

}