SqliteConnection.h 3.26 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 24 25
#ifndef SQLITECONNECTION_H
#define SQLITECONNECTION_H

26
#include <functional>
27 28
#include <memory>
#include <sqlite3.h>
29
#include "compat/ConditionVariable.h"
30
#include <unordered_map>
31
#include <string>
32

33
#include "utils/SWMRLock.h"
34
#include "compat/Mutex.h"
35
#include "compat/Thread.h"
36

37 38 39
namespace medialibrary
{

40 41 42 43
namespace sqlite
{
    class Transaction;
}
44

45 46 47
class SqliteConnection
{
public:
48 49
    using ReadContext = std::unique_lock<utils::ReadLocker>;
    using WriteContext = std::unique_lock<utils::WriteLocker>;
50
    using Handle = sqlite3*;
51 52 53 54 55 56
    enum class HookReason
    {
        Insert,
        Delete,
        Update
    };
57 58 59 60 61 62 63 64 65 66 67 68
    struct WeakDbContext
    {
        WeakDbContext( SqliteConnection* conn );
        ~WeakDbContext();
        WeakDbContext( const WeakDbContext& ) = delete;
        WeakDbContext( WeakDbContext&& ) = delete;
        WeakDbContext& operator=( const WeakDbContext& ) = delete;
        WeakDbContext& operator=( WeakDbContext&& ) = delete;
    private:
        SqliteConnection* m_conn;
    };

69
    using UpdateHookCb = std::function<void(HookReason, int64_t)>;
70

71
    explicit SqliteConnection( const std::string& dbPath );
72
    ~SqliteConnection();
73 74
    // Returns the current thread's connection
    // This will initiate a connection if required
75
    Handle getConn();
76
    std::unique_ptr<sqlite::Transaction> newTransaction();
77 78
    ReadContext acquireReadContext();
    WriteContext acquireWriteContext();
79 80
    void setForeignKeyEnabled( bool value );
    void setRecursiveTriggers( bool value );
81

82 83 84
    void registerUpdateHook( const std::string& table, UpdateHookCb cb );

private:
85
    void setPragmaEnabled( Handle conn, const std::string& pragmaName, bool value );
86 87 88
    static void updateHook( void* data, int reason, const char* database,
                            const char* table, sqlite_int64 rowId );

89 90 91
private:
    using ConnPtr = std::unique_ptr<sqlite3, int(*)(sqlite3*)>;
    const std::string m_dbPath;
92
    compat::Mutex m_connMutex;
93
    std::unordered_map<compat::Thread::id, ConnPtr> m_conns;
94 95 96
    utils::SWMRLock m_contextLock;
    utils::ReadLocker m_readLock;
    utils::WriteLocker m_writeLock;
97
    std::unordered_map<std::string, UpdateHookCb> m_hooks;
98 99
};

100 101
}

102
#endif // SQLITECONNECTION_H