-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathDBHostObject.h
More file actions
99 lines (86 loc) · 2.85 KB
/
DBHostObject.h
File metadata and controls
99 lines (86 loc) · 2.85 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once
#include "OPThreadPool.h"
#include "types.hpp"
#include <ReactCommon/CallInvoker.h>
#include <jsi/jsi.h>
#include <set>
#ifdef OP_SQLITE_USE_LIBSQL
#include "libsql/bridge.hpp"
#else
#ifdef __ANDROID__
#include "sqlite3.h"
#else
#include <sqlite3.h>
#endif
#endif
#include <unordered_map>
#include <vector>
namespace opsqlite {
namespace jsi = facebook::jsi;
namespace react = facebook::react;
struct PendingReactiveInvocation {
std::string db_name;
std::string table;
std::string rowid;
};
struct TableRowDiscriminator {
std::string table;
std::vector<int> ids;
};
struct ReactiveQuery {
#ifndef OP_SQLITE_USE_LIBSQL
sqlite3_stmt *stmt;
#endif
std::vector<TableRowDiscriminator> discriminators;
std::shared_ptr<jsi::Value> callback;
};
class JSI_EXPORT DBHostObject : public jsi::HostObject {
public:
// Normal constructor shared between all backends
DBHostObject(jsi::Runtime &rt, std::string &base_path, std::string &db_name,
std::string &path, std::string &crsqlite_path,
std::string &sqlite_vec_path, std::string &encryption_key);
#ifdef OP_SQLITE_USE_LIBSQL
// Constructor for remoteOpen, purely for remote databases
DBHostObject(jsi::Runtime &rt, std::string &url, std::string &auth_token);
// Constructor for a local database with remote sync
DBHostObject(jsi::Runtime &rt, std::string &db_name, std::string &path,
std::string &url, std::string &auth_token, int sync_interval,
bool offline, std::string &encryption_key,
std::string &remote_encryption_key);
#endif
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override;
void set(jsi::Runtime &rt, const jsi::PropNameID &name,
const jsi::Value &value) override;
void on_update(const std::string &table, const std::string &operation,
long long row_id);
void on_commit();
void on_rollback();
void invalidate();
~DBHostObject() override;
private:
std::set<std::shared_ptr<ReactiveQuery>> pending_reactive_queries;
void auto_register_update_hook();
void create_jsi_functions();
void
flush_pending_reactive_queries(const std::shared_ptr<jsi::Value> &resolve);
std::unordered_map<std::string, jsi::Value> function_map;
std::string base_path;
std::shared_ptr<ThreadPool> _thread_pool;
std::string db_name;
std::shared_ptr<jsi::Value> update_hook_callback;
std::shared_ptr<jsi::Value> commit_hook_callback;
std::shared_ptr<jsi::Value> rollback_hook_callback;
jsi::Runtime &rt;
std::vector<std::shared_ptr<ReactiveQuery>> reactive_queries;
std::vector<PendingReactiveInvocation> pending_reactive_invocations;
bool is_update_hook_registered = false;
bool invalidated = false;
#ifdef OP_SQLITE_USE_LIBSQL
DB db;
#else
sqlite3 *db;
#endif
};
} // namespace opsqlite