Folder.cpp 4.66 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 "Folder.h"
24
#include "Media.h"
25

26
#include "database/SqliteTools.h"
27
#include "filesystem/IDirectory.h"
28 29 30 31

namespace policy
{
    const std::string FolderTable::Name = "Folder";
32
    const std::string FolderTable::CacheColumn = "path";
33
    unsigned int Folder::* const FolderTable::PrimaryKey = &Folder::m_id;
34

35
    const FolderCache::KeyType&FolderCache::key( const IFolder* self )
36 37 38 39
    {
        return self->path();
    }

40
    FolderCache::KeyType FolderCache::key(sqlite::Row& row )
41
    {
42
        return row.load<FolderCache::KeyType>( 1 );
43 44
    }

45 46
}

47
Folder::Folder(DBConnection dbConnection, sqlite::Row& row )
48 49
    : m_dbConection( dbConnection )
{
50 51 52 53 54
    row >> m_id
        >> m_path
        >> m_parent
        >> m_lastModificationDate
        >> m_isRemovable;
55 56
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
57
Folder::Folder( const std::string& path, time_t lastModificationDate, bool isRemovable, unsigned int parent )
58 59
    : m_id( 0 )
    , m_path( path )
60
    , m_parent( parent )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
61 62
    , m_lastModificationDate( lastModificationDate )
    , m_isRemovable( isRemovable )
63 64 65 66 67
{
}

bool Folder::createTable(DBConnection connection)
{
68 69 70 71 72
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::FolderTable::Name +
            "("
            "id_folder INTEGER PRIMARY KEY AUTOINCREMENT,"
            "path TEXT UNIQUE ON CONFLICT FAIL,"
            "id_parent UNSIGNED INTEGER,"
73
            "last_modification_date UNSIGNED INTEGER,"
74
            "is_removable INTEGER,"
75 76
            "FOREIGN KEY (id_parent) REFERENCES " + policy::FolderTable::Name +
            "(id_folder) ON DELETE CASCADE"
77
            ")";
78
    return sqlite::Tools::executeRequest( connection, req );
79 80
}

81
std::shared_ptr<Folder> Folder::create( DBConnection connection, const std::string& path, time_t lastModificationDate, bool isRemovable, unsigned int parentId )
82
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
83
    auto self = std::make_shared<Folder>( path, lastModificationDate, isRemovable, parentId );
84
    static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
85
            "(path, id_parent, last_modification_date, is_removable) VALUES(?, ?, ?, ?)";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
86 87
    if ( _Cache::insert( connection, self, req, path, sqlite::ForeignKey( parentId ),
                         lastModificationDate, isRemovable ) == false )
88
        return nullptr;
89 90 91 92 93 94 95 96 97
    self->m_dbConection = connection;
    return self;
}

unsigned int Folder::id() const
{
    return m_id;
}

98
const std::string& Folder::path() const
99 100 101 102
{
    return m_path;
}

103
std::vector<MediaPtr> Folder::files()
104
{
105
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
106
        " WHERE folder_id = ?";
107
    return Media::fetchAll<IMedia>( m_dbConection, req, m_id );
108
}
109

110 111 112 113
std::vector<FolderPtr> Folder::folders()
{
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
            " WHERE id_parent = ?";
114
    return fetchAll<IFolder>( m_dbConection, req, m_id );
115 116
}

117 118 119 120 121
FolderPtr Folder::parent()
{
    //FIXME: use path to be able to fetch from cache?
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
            " WHERE id_folder = ?";
122
    return fetchOne( m_dbConection, req, m_parent );
123
}
124 125 126 127 128

unsigned int Folder::lastModificationDate()
{
    return m_lastModificationDate;
}
129 130 131 132 133 134 135 136 137 138

bool Folder::setLastModificationDate( unsigned int lastModificationDate )
{
    static const std::string req = "UPDATE " + policy::FolderTable::Name +
            " SET last_modification_date = ? WHERE id_folder = ?";
    if ( sqlite::Tools::executeUpdate( m_dbConection, req, lastModificationDate, m_id ) == false )
        return false;
    m_lastModificationDate = lastModificationDate;
    return true;
}
139 140 141 142 143

bool Folder::isRemovable()
{
    return m_isRemovable;
}