Media.cpp 17.4 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
/*****************************************************************************
 * 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.
 *****************************************************************************/

23
#include <algorithm>
24
#include <cassert>
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
25 26
#include <cstdlib>
#include <cstring>
27
#include <ctime>
28

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
29
#include "Album.h"
30
#include "AlbumTrack.h"
31
#include "Artist.h"
32
#include "AudioTrack.h"
33
#include "Media.h"
34
#include "File.h"
35
#include "Folder.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36
#include "Label.h"
37
#include "logging/Logger.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
38
#include "Movie.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
39
#include "ShowEpisode.h"
40
#include "database/SqliteTools.h"
41
#include "VideoTrack.h"
42
#include "filesystem/IFile.h"
43 44
#include "filesystem/IDirectory.h"
#include "filesystem/IDevice.h"
45
#include "utils/Filename.h"
46

47
const std::string policy::MediaTable::Name = "Media";
48
const std::string policy::MediaTable::PrimaryKeyColumn = "id_media";
49
int64_t Media::* const policy::MediaTable::PrimaryKey = &Media::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
50

51 52
Media::Media( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
53
    , m_changed( false )
54
{
55 56
    row >> m_id
        >> m_type
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
57
        >> m_subType
58 59
        >> m_duration
        >> m_playCount
60
        >> m_lastPlayedDate
61 62
        >> m_progress
        >> m_rating
63
        >> m_insertionDate
64
        >> m_releaseDate
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
65
        >> m_thumbnail
66
        >> m_title
67
        >> m_filename
68 69
        >> m_isFavorite
        >> m_isPresent;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
70 71
}

72 73 74
Media::Media( MediaLibraryPtr ml, const std::string& title, Type type )
    : m_ml( ml )
    , m_id( 0 )
75
    , m_type( type )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
76
    , m_subType( SubType::Unknown )
77
    , m_duration( -1 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
78
    , m_playCount( 0 )
79
    , m_lastPlayedDate( 0 )
80 81
    , m_progress( .0f )
    , m_rating( -1 )
82
    , m_insertionDate( time( nullptr ) )
83
    , m_releaseDate( 0 )
84
    , m_title( title )
85 86
    // When creating a Media, meta aren't parsed, and therefor, is the filename
    , m_filename( title )
87
    , m_isFavorite( false )
88
    , m_isPresent( true )
89
    , m_changed( false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
90
{
91 92
}

93
std::shared_ptr<Media> Media::create( MediaLibraryPtr ml, Type type, const fs::IFile& file )
94
{
95
    auto self = std::make_shared<Media>( ml, file.name(), type );
96
    static const std::string req = "INSERT INTO " + policy::MediaTable::Name +
97
            "(type, insertion_date, title, filename) VALUES(?, ?, ?, ?)";
98

99
    if ( insert( ml, self, req, type, self->m_insertionDate, self->m_title, self->m_filename ) == false )
100
        return nullptr;
101
    return self;
102 103
}

104
medialibrary::AlbumTrackPtr Media::albumTrack() const
105
{
106 107
    if ( m_subType != SubType::AlbumTrack )
        return nullptr;
108 109
    auto lock = m_albumTrack.lock();

110
    if ( m_albumTrack.isCached() == false )
111
        m_albumTrack = AlbumTrack::fromMedia( m_ml, m_id );
112
    return m_albumTrack.get();
113 114
}

115
void Media::setAlbumTrack( medialibrary::AlbumTrackPtr albumTrack )
116
{
117
    auto lock = m_albumTrack.lock();
118
    m_albumTrack = albumTrack;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
119 120
    m_subType = SubType::AlbumTrack;
    m_changed = true;
121 122
}

123
int64_t Media::duration() const
124 125 126 127
{
    return m_duration;
}

128
void Media::setDuration( int64_t duration )
129
{
130 131
    if ( m_duration == duration )
        return;
132
    m_duration = duration;
133
    m_changed = true;
134 135
}

136
medialibrary::ShowEpisodePtr Media::showEpisode() const
137
{
138 139
    if ( m_subType != SubType::ShowEpisode )
        return nullptr;
140

141 142
    auto lock = m_showEpisode.lock();
    if ( m_showEpisode.isCached() == false )
143
        m_showEpisode = ShowEpisode::fromMedia( m_ml, m_id );
144
    return m_showEpisode.get();
145 146
}

147
void Media::setShowEpisode( medialibrary::ShowEpisodePtr episode )
148
{
149 150
    auto lock = m_showEpisode.lock();
    m_showEpisode = episode;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
151 152
    m_subType = SubType::ShowEpisode;
    m_changed = true;
153 154
}

155
std::vector<medialibrary::LabelPtr> Media::labels()
156
{
157
    static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
158
            "INNER JOIN LabelFileRelation lfr ON lfr.label_id = l.id_label "
159
            "WHERE lfr.media_id = ?";
160
    return Label::fetchAll<medialibrary::ILabel>( m_ml, req, m_id );
161 162
}

163
int Media::playCount() const
164
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
165
    return m_playCount;
166 167
}

168
bool Media::increasePlayCount()
169
{
170 171 172 173 174
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
            "play_count = ?, last_played_date = ? WHERE id_media = ?";
    auto lastPlayedDate = time( nullptr );
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_playCount + 1, lastPlayedDate, m_id ) == false )
        return false;
175
    m_playCount++;
176 177
    m_lastPlayedDate = lastPlayedDate;
    return true;
178 179
}

180 181 182 183 184
float Media::progress() const
{
    return m_progress;
}

185
bool Media::setProgress( float progress )
186
{
187
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET progress = ? WHERE id_media = ?";
188
    if ( progress == m_progress || progress < 0 || progress > 1.0 )
189 190 191
        return true;
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, progress, m_id ) == false )
        return false;
192
    m_progress = progress;
193
    return true;
194 195 196 197 198 199 200
}

int Media::rating() const
{
    return m_rating;
}

201
bool Media::setRating( int rating )
202
{
203
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET rating = ? WHERE id_media = ?";
204
    if ( m_rating == rating )
205 206 207
        return true;
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, rating, m_id ) == false )
        return false;
208
    m_rating = rating;
209
    return true;
210 211
}

212 213 214 215 216
bool Media::isFavorite() const
{
    return m_isFavorite;
}

217
bool Media::setFavorite( bool favorite )
218
{
219
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET is_favorite = ? WHERE id_media = ?";
220
    if ( m_isFavorite == favorite )
221 222 223
        return true;
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, favorite, m_id ) == false )
        return false;
224
    m_isFavorite = favorite;
225
    return true;
226 227
}

228
const std::vector<medialibrary::FilePtr>& Media::files() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
229
{
230 231 232 233 234
    auto lock = m_files.lock();
    if ( m_files.isCached() == false )
    {
        static const std::string req = "SELECT * FROM " + policy::FileTable::Name
                + " WHERE media_id = ?";
235
        m_files = File::fetchAll<medialibrary::IFile>( m_ml, req, m_id );
236 237
    }
    return m_files;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
238 239
}

240
medialibrary::MoviePtr Media::movie() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
241
{
242 243 244
    if ( m_subType != SubType::Movie )
        return nullptr;

245 246
    auto lock = m_movie.lock();

247
    if ( m_movie.isCached() == false )
248
        m_movie = Movie::fromMedia( m_ml, m_id );
249
    return m_movie.get();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
250 251
}

252
void Media::setMovie(medialibrary::MoviePtr movie)
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
253
{
254
    auto lock = m_movie.lock();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
255
    m_movie = movie;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
256 257
    m_subType = SubType::Movie;
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
258 259
}

260
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
261
{
262
    return VideoTrack::create( m_ml, codec, width, height, fps, m_id ) != nullptr;
263 264
}

265
std::vector<medialibrary::VideoTrackPtr> Media::videoTracks()
266
{
267 268
    static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
            " WHERE media_id = ?";
269
    return VideoTrack::fetchAll<medialibrary::IVideoTrack>( m_ml, req, m_id );
270 271
}

272
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
273 274
                          unsigned int sampleRate, unsigned int nbChannels,
                          const std::string& language, const std::string& desc )
275
{
276
    return AudioTrack::create( m_ml, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
277 278
}

279
std::vector<medialibrary::AudioTrackPtr> Media::audioTracks()
280
{
281 282
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
            " WHERE media_id = ?";
283
    return AudioTrack::fetchAll<medialibrary::IAudioTrack>( m_ml, req, m_id );
284 285
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
286
const std::string &Media::thumbnail()
287
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
288
    return m_thumbnail;
289 290
}

291 292 293 294 295
unsigned int Media::insertionDate() const
{
    return m_insertionDate;
}

296 297 298 299 300 301 302 303 304 305 306 307 308
unsigned int Media::releaseDate() const
{
    return m_releaseDate;
}

void Media::setReleaseDate( unsigned int date )
{
    if ( m_releaseDate == date )
        return;
    m_releaseDate = date;
    m_changed = true;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
309
void Media::setThumbnail(const std::string& thumbnail )
310
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
311
    if ( m_thumbnail == thumbnail )
312
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
313
    m_thumbnail = thumbnail;
314 315 316 317 318 319
    m_changed = true;
}

bool Media::save()
{
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
320
            "type = ?, subtype = ?, duration = ?, progress = ?, release_date = ?,"
321
            "thumbnail = ?, title = ? WHERE id_media = ?";
322 323
    if ( m_changed == false )
        return true;
324
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_type, m_subType, m_duration,
325
                                       m_progress, m_releaseDate, m_thumbnail, m_title, m_id ) == false )
326 327 328 329
    {
        return false;
    }
    m_changed = false;
330 331 332
    return true;
}

333
std::shared_ptr<File> Media::addFile( const fs::IFile& fileFs, Folder& parentFolder, fs::IDirectory& parentFolderFs, medialibrary::IFile::Type type )
334
{
335
    auto file = File::create( m_ml, m_id, type, fileFs, parentFolder.id(), parentFolderFs.device()->isRemovable() );
336 337 338 339 340 341
    if ( file == nullptr )
        return nullptr;
    auto lock = m_files.lock();
    if ( m_files.isCached() )
        m_files.get().push_back( file );
    return file;
342 343
}

344
void Media::removeFile( File& file )
345
{
346 347 348
    file.destroy();
    auto lock = m_files.lock();
    if ( m_files.isCached() == false )
349
        return;
350
    m_files.get().erase( std::remove_if( begin( m_files.get() ), end( m_files.get() ), [&file]( const medialibrary::FilePtr& f ) {
351 352
        return f->id() == file.id();
    }));
353 354
}

355
std::vector<medialibrary::MediaPtr> Media::listAll( MediaLibraryPtr ml, IMedia::Type type, medialibrary::SortingCriteria sort, bool desc )
356
{
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    std::string req;
    if ( sort == medialibrary::SortingCriteria::LastModificationDate )
    {
        req = "SELECT m.* FROM " + policy::MediaTable::Name + " m INNER JOIN "
                + policy::FileTable::Name + " f ON m.id_media = f.media_id"
                " WHERE m.type = ?"
                " AND ( f.type = ? OR f.type = ? )"
                " ORDER BY f.last_modification_date";
        if ( desc == true )
            req += " DESC";
        return fetchAll<IMedia>( ml, req, type, File::Type::Entire, File::Type::Main );
    }
    req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ? AND is_present = 1 ORDER BY ";
    switch ( sort )
    {
    case medialibrary::SortingCriteria::Duration:
        req += "duration";
        break;
    case medialibrary::SortingCriteria::InsertionDate:
        req += "insertion_date";
        break;
    case medialibrary::SortingCriteria::ReleaseDate:
        req += "release_date";
        break;
    default:
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
382
        req += "title";
383 384 385 386 387 388
        break;
    }
    if ( desc == true )
        req += " DESC";

    return fetchAll<IMedia>( ml, req, type );
389 390
}

391
int64_t Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
392 393 394 395
{
    return m_id;
}

396
medialibrary::IMedia::Type Media::type()
397 398 399 400
{
    return m_type;
}

401
medialibrary::IMedia::SubType Media::subType() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
402 403 404 405
{
    return m_subType;
}

406
void Media::setType( Type type )
407
{
408 409
    if ( m_type != type )
        return;
410
    m_type = type;
411
    m_changed = true;
412 413
}

414
const std::string &Media::title() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
415
{
416
    return m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
417 418
}

419
void Media::setTitle( const std::string &title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
420
{
421 422
    if ( m_title == title )
        return;
423
    m_title = title;
424
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
425 426
}

427
bool Media::createTable( DBConnection connection )
428
{
429 430
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MediaTable::Name + "("
            "id_media INTEGER PRIMARY KEY AUTOINCREMENT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
431
            "type INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
432
            "subtype INTEGER,"
433
            "duration INTEGER DEFAULT -1,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
434
            "play_count UNSIGNED INTEGER,"
435
            "last_played_date UNSIGNED INTEGER,"
436 437
            "progress REAL,"
            "rating INTEGER DEFAULT -1,"
438
            "insertion_date UNSIGNED INTEGER,"
439
            "release_date UNSIGNED INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
440
            "thumbnail TEXT,"
441
            "title TEXT,"
442
            "filename TEXT,"
443
            "is_favorite BOOLEAN NOT NULL DEFAULT 0,"
444
            "is_present BOOLEAN NOT NULL DEFAULT 1"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
445
            ")";
446 447
    static const std::string indexReq = "CREATE INDEX IF NOT EXISTS index_last_played_date ON "
            + policy::MediaTable::Name + "(last_played_date DESC)";
448 449
    static const std::string vtableReq = "CREATE VIRTUAL TABLE IF NOT EXISTS "
                + policy::MediaTable::Name + "Fts USING FTS3("
450 451
                "title,"
                "labels"
452 453
            ")";
    return sqlite::Tools::executeRequest( connection, req ) &&
454
            sqlite::Tools::executeRequest( connection, indexReq ) &&
455
            sqlite::Tools::executeRequest( connection, vtableReq );
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
}

bool Media::createTriggers( DBConnection connection )
{
    static const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS has_files_present AFTER UPDATE OF "
            "is_present ON " + policy::FileTable::Name +
            " BEGIN "
            " UPDATE " + policy::MediaTable::Name + " SET is_present="
                "(SELECT COUNT(id_file) FROM " + policy::FileTable::Name + " WHERE media_id=new.media_id AND is_present=1) "
                "WHERE id_media=new.media_id;"
            " END;";
    static const std::string triggerReq2 = "CREATE TRIGGER IF NOT EXISTS cascade_file_deletion AFTER DELETE ON "
            + policy::FileTable::Name +
            " BEGIN "
            " DELETE FROM " + policy::MediaTable::Name + " WHERE "
                "(SELECT COUNT(id_file) FROM " + policy::FileTable::Name + " WHERE media_id=old.media_id) = 0"
                " AND id_media=old.media_id;"
            " END;";
474 475 476 477

    static const std::string vtableInsertTrigger = "CREATE TRIGGER IF NOT EXISTS insert_media_fts"
            " AFTER INSERT ON " + policy::MediaTable::Name +
            " BEGIN"
478
            " INSERT INTO " + policy::MediaTable::Name + "Fts(rowid,title,labels) VALUES(new.id_media, new.title, '');"
479 480
            " END";
    static const std::string vtableDeleteTrigger = "CREATE TRIGGER IF NOT EXISTS delete_media_fts"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
481
            " BEFORE DELETE ON " + policy::MediaTable::Name +
482 483 484 485 486 487 488 489
            " BEGIN"
            " DELETE FROM " + policy::MediaTable::Name + "Fts WHERE rowid = old.id_media;"
            " END";
    static const std::string vtableUpdateTitleTrigger2 = "CREATE TRIGGER IF NOT EXISTS update_media_title_fts"
              " AFTER UPDATE OF title ON " + policy::MediaTable::Name +
              " BEGIN"
              " UPDATE " + policy::MediaTable::Name + "Fts SET title = new.title WHERE rowid = new.id_media;"
              " END";
490
    return sqlite::Tools::executeRequest( connection, triggerReq ) &&
491 492 493 494
            sqlite::Tools::executeRequest( connection, triggerReq2 ) &&
            sqlite::Tools::executeRequest( connection, vtableInsertTrigger ) &&
            sqlite::Tools::executeRequest( connection, vtableDeleteTrigger ) &&
            sqlite::Tools::executeRequest( connection, vtableUpdateTitleTrigger2 );
495
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
496

497
bool Media::addLabel( medialibrary::LabelPtr label )
498
{
499
    if ( m_id == 0 || label->id() == 0 )
500
    {
501
        LOG_ERROR( "Both file & label need to be inserted in database before being linked together" );
502 503 504
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
505
    if ( sqlite::Tools::executeInsert( m_ml->getConn(), req, label->id(), m_id ) == 0 )
506 507 508
        return false;
    const std::string reqFts = "UPDATE " + policy::MediaTable::Name + "Fts "
        "SET labels = labels || ' ' || ? WHERE rowid = ?";
509
    return sqlite::Tools::executeUpdate( m_ml->getConn(), reqFts, label->name(), m_id );
510 511
}

512
bool Media::removeLabel( medialibrary::LabelPtr label )
513
{
514
    if ( m_id == 0 || label->id() == 0 )
515
    {
516
        LOG_ERROR( "Can't unlink a label/file not inserted in database" );
517 518
        return false;
    }
519
    const char* req = "DELETE FROM LabelFileRelation WHERE label_id = ? AND media_id = ?";
520
    if ( sqlite::Tools::executeDelete( m_ml->getConn(), req, label->id(), m_id ) == false )
521 522 523
        return false;
    const std::string reqFts = "UPDATE " + policy::MediaTable::Name + "Fts "
            "SET labels = TRIM(REPLACE(labels, ?, '')) WHERE rowid = ?";
524
    return sqlite::Tools::executeUpdate( m_ml->getConn(), reqFts, label->name(), m_id );
525
}
526 527


528
std::vector<medialibrary::MediaPtr> Media::search( MediaLibraryPtr ml, const std::string& title )
529 530 531
{
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE"
            " id_media IN (SELECT rowid FROM " + policy::MediaTable::Name + "Fts"
532 533
            " WHERE " + policy::MediaTable::Name + "Fts MATCH ?)"
            "AND is_present = 1";
534
    return Media::fetchAll<IMedia>( ml, req, title + "*" );
535
}
536

537
std::vector<medialibrary::MediaPtr> Media::fetchHistory( MediaLibraryPtr ml )
538 539 540
{
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE last_played_date IS NOT NULL"
            " ORDER BY last_played_date DESC LIMIT 100";
541
    return fetchAll<IMedia>( ml, req );
542
}