forked from sgomin/ddb_medialib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.hpp
87 lines (62 loc) · 1.7 KB
/
database.hpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef DATABASE_HPP
#define DATABASE_HPP
#include "db_record.hpp"
#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
#include <string>
#include <memory>
#include <stdexcept>
#include <unordered_map>
struct sqlite3;
struct sqlite3_stmt;
class StatementCache
{
public:
explicit StatementCache(sqlite3* pDb);
~StatementCache();
sqlite3_stmt* get(int id, const char* szSQL);
void clear();
void setDb(sqlite3* pDb);
private:
sqlite3* pDb_;
std::unordered_map<int, sqlite3_stmt*> statements_;
};
class DbReader
{
public:
~DbReader();
DbReader(DbReader const&) = delete;
DbReader(DbReader&& other);
DbReader& operator=(DbReader const&) = delete;
FileInfo getFile(RecordID id) const;
FileRecords childrenFiles(RecordID id) const;
FileRecords dirs() const;
protected:
friend class DbOwner;
DbReader(sqlite3* pDb);
void close();
static boost::optional<FileRecord> readNextRecord(sqlite3_stmt* pStmt);
sqlite3* pDb_;
mutable StatementCache statements_;
};
class DbOwner : public DbReader, private boost::noncopyable
{
public:
explicit DbOwner(const std::string& fileName);
RecordID addFile(const FileInfo& record);
void delFile(RecordID id);
void replaceFile(RecordID id, const FileInfo& record);
void beginTransaction();
void commit();
void rollback();
DbReader createReader();
private:
const std::string fileName_;
};
using DbOwnerPtr = std::unique_ptr<DbOwner>;
class DbException : public std::runtime_error
{
public:
explicit DbException(int err);
};
#endif /* DATABASE_HPP */