Folder.cpp 3.77 KB
Newer Older
1 2 3
#include "Folder.h"
#include "File.h"

4
#include "database/SqliteTools.h"
5
#include "filesystem/IDirectory.h"
6 7 8 9

namespace policy
{
    const std::string FolderTable::Name = "Folder";
10
    const std::string FolderTable::CacheColumn = "path";
11
    unsigned int Folder::* const FolderTable::PrimaryKey = &Folder::m_id;
12 13 14 15 16 17 18 19

    const FolderCache::KeyType&FolderCache::key(const std::shared_ptr<Folder>& self)
    {
        return self->path();
    }

    FolderCache::KeyType FolderCache::key( sqlite3_stmt* stmt )
    {
20
        return sqlite::Traits<FolderCache::KeyType>::Load( stmt, 1 );
21 22
    }

23 24 25 26 27
}

Folder::Folder( DBConnection dbConnection, sqlite3_stmt* stmt )
    : m_dbConection( dbConnection )
{
28 29 30
    m_id = sqlite::Traits<unsigned int>::Load( stmt, 0 );
    m_path = sqlite::Traits<std::string>::Load( stmt, 1 );
    m_parent = sqlite::Traits<unsigned int>::Load( stmt, 2 );
31
    m_lastModificationDate = sqlite::Traits<unsigned int>::Load( stmt, 3 );
32
    m_isRemovable = sqlite::Traits<bool>::Load( stmt, 4 );
33 34
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
35
Folder::Folder( const std::string& path, time_t lastModificationDate, bool isRemovable, unsigned int parent )
36 37
    : m_id( 0 )
    , m_path( path )
38
    , m_parent( parent )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
39 40
    , m_lastModificationDate( lastModificationDate )
    , m_isRemovable( isRemovable )
41 42 43 44 45
{
}

bool Folder::createTable(DBConnection connection)
{
46 47 48 49 50
    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,"
51
            "last_modification_date UNSIGNED INTEGER,"
52
            "is_removable INTEGER,"
53 54
            "FOREIGN KEY (id_parent) REFERENCES " + policy::FolderTable::Name +
            "(id_folder) ON DELETE CASCADE"
55
            ")";
56
    return sqlite::Tools::executeRequest( connection, req );
57 58
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59
FolderPtr Folder::create( DBConnection connection, const std::string& path, time_t lastModificationDate, bool isRemovable, unsigned int parentId )
60
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
61
    auto self = std::make_shared<Folder>( path, lastModificationDate, isRemovable, parentId );
62
    static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
63
            "(path, id_parent, last_modification_date, is_removable) VALUES(?, ?, ?, ?)";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
64 65
    if ( _Cache::insert( connection, self, req, path, sqlite::ForeignKey( parentId ),
                         lastModificationDate, isRemovable ) == false )
66
        return nullptr;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    self->m_dbConection = connection;
    return self;
}

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

const std::string& Folder::path()
{
    return m_path;
}

std::vector<FilePtr> Folder::files()
{
83 84
    static const std::string req = "SELECT * FROM " + policy::FileTable::Name +
        " WHERE folder_id = ?";
85
    return File::fetchAll( m_dbConection, req, m_id );
86
}
87

88 89 90 91
std::vector<FolderPtr> Folder::folders()
{
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
            " WHERE id_parent = ?";
92
    return fetchAll( m_dbConection, req, m_id );
93 94
}

95 96 97 98 99
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 = ?";
100
    return fetchOne( m_dbConection, req, m_parent );
101
}
102 103 104 105 106

unsigned int Folder::lastModificationDate()
{
    return m_lastModificationDate;
}
107 108 109 110 111 112 113 114 115 116

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;
}
117 118 119 120 121

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