-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database.h
56 lines (51 loc) · 1.3 KB
/
Database.h
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
#pragma once
#include "Logger.h"
#include <string>
#include <vector>
#include "lib/sqlite3.h"
class Database {
public:
struct Contact {
int id;
std::string pubkey;
std::wstring name;
std::string host;
};
Database(Logger& logger)
: log(logger)
{}
~Database();
bool OpenOrCreate(const std::wstring& path);
void GetKeys(std::string* pub, std::string* priv);
std::vector<Contact> GetContacts();
void AddContact(const std::string& pubkey, const std::wstring& name);
void UpdateContactName(std::string pubkey, const std::wstring& name);
private:
class Stmt {
public:
Stmt(sqlite3_stmt* stmt)
: stmt(stmt)
{}
Stmt(const Stmt&) = delete;
Stmt(Stmt&& other)
: stmt(other.stmt)
{
other.stmt = nullptr;
}
~Stmt() {
sqlite3_finalize(stmt);
}
sqlite3_stmt* get() {
return stmt;
}
private:
sqlite3_stmt* stmt = nullptr;
};
sqlite3* db = nullptr;
Logger& log;
Stmt createStatement(const char* sql);
void queryExec(const char* sql);
int queryInt(const char* sql);
void initSchema();
void upgradeDb(int oldver);
};