-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsqliteBridge.cpp
195 lines (161 loc) · 4.92 KB
/
sqliteBridge.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* sequel.cpp
*
* Created by Oscar Franco on 2021/03/07
* Copyright (c) 2021 Oscar Franco
*
* This code is licensed under the MIT license
*/
#include "sqliteBridge.h"
#include "ConnectionPool.h"
#include "fileUtils.h"
#include "logs.h"
#include "sqlite3.h"
#include <ctime>
#include <iostream>
#include <map>
#include <sstream>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
using namespace facebook;
std::map<std::string, ConnectionPool *> dbMap =
std::map<std::string, ConnectionPool *>();
SQLiteOPResult generateNotOpenResult(std::string const &dbName) {
return SQLiteOPResult{
.type = SQLiteError,
.errorMessage = dbName + " is not open",
};
}
/**
* Opens SQL database with default settings
*/
SQLiteOPResult
sqliteOpenDb(string const dbName, string const docPath,
void (*contextAvailableCallback)(std::string, ConnectionLockId),
void (*updateTableCallback)(void *, int, const char *,
const char *, sqlite3_int64),
void (*onTransactionFinalizedCallback)(
const TransactionCallbackPayload *event),
uint32_t numReadConnections) {
if (dbMap.count(dbName) == 1) {
return SQLiteOPResult{
.type = SQLiteError,
.errorMessage = dbName + " is already open",
};
}
dbMap[dbName] = new ConnectionPool(dbName, docPath, numReadConnections);
dbMap[dbName]->setOnContextAvailable(contextAvailableCallback);
dbMap[dbName]->setTableUpdateHandler(updateTableCallback);
dbMap[dbName]->setTransactionFinalizerHandler(onTransactionFinalizedCallback);
return SQLiteOPResult{
.type = SQLiteOk,
};
}
std::future<void> sqliteRefreshSchema(const std::string& dbName) {
if (dbMap.count(dbName) == 0) {
std::promise<void> promise;
promise.set_value();
return promise.get_future();
}
ConnectionPool* connection = dbMap[dbName];
return connection->refreshSchema();
}
SQLiteOPResult sqliteCloseDb(string const dbName) {
if (dbMap.count(dbName) == 0) {
return generateNotOpenResult(dbName);
}
ConnectionPool *connection = dbMap[dbName];
connection->closeAll();
dbMap.erase(dbName);
delete connection;
return SQLiteOPResult{
.type = SQLiteOk,
};
}
void sqliteCloseAll() {
for (auto const &x : dbMap) {
x.second->closeAll();
delete x.second;
}
dbMap.clear();
}
SQLiteOPResult sqliteQueueInContext(std::string dbName,
ConnectionLockId const contextId,
std::function<void(sqlite3 *)> task) {
if (dbMap.count(dbName) == 0) {
return generateNotOpenResult(dbName);
}
ConnectionPool *connection = dbMap[dbName];
return connection->queueInContext(contextId, task);
}
void sqliteReleaseLock(std::string const dbName,
ConnectionLockId const contextId) {
if (dbMap.count(dbName) == 0) {
// Do nothing if the lock does not actually exist
return;
}
ConnectionPool *connection = dbMap[dbName];
connection->closeContext(contextId);
}
SQLiteOPResult sqliteRequestLock(std::string const dbName,
ConnectionLockId const contextId,
ConcurrentLockType lockType) {
if (dbMap.count(dbName) == 0) {
return generateNotOpenResult(dbName);
}
ConnectionPool *connection = dbMap[dbName];
if (connection == nullptr) {
return SQLiteOPResult{
.type = SQLiteOk,
};
}
switch (lockType) {
case ConcurrentLockType::ReadLock:
connection->readLock(contextId);
break;
case ConcurrentLockType::WriteLock:
connection->writeLock(contextId);
break;
default:
break;
}
return SQLiteOPResult{
.type = SQLiteOk,
};
}
SQLiteOPResult sqliteAttachDb(string const mainDBName, string const docPath,
string const databaseToAttach,
string const alias) {
if (dbMap.count(mainDBName) == 0) {
return generateNotOpenResult(mainDBName);
}
ConnectionPool *connection = dbMap[mainDBName];
return connection->attachDatabase(databaseToAttach, docPath, alias);
}
SQLiteOPResult sqliteDetachDb(string const mainDBName, string const alias) {
if (dbMap.count(mainDBName) == 0) {
return generateNotOpenResult(mainDBName);
}
ConnectionPool *connection = dbMap[mainDBName];
return connection->detachDatabase(alias);
}
SQLiteOPResult sqliteRemoveDb(string const dbName, string const docPath) {
if (dbMap.count(dbName) == 1) {
SQLiteOPResult closeResult = sqliteCloseDb(dbName);
if (closeResult.type == SQLiteError) {
return closeResult;
}
}
string dbPath = get_db_path(dbName, docPath);
if (!file_exists(dbPath)) {
return SQLiteOPResult{
.type = SQLiteOk,
.errorMessage =
"[react-native-quick-sqlite]: Database file not found" + dbPath};
}
remove(dbPath.c_str());
return SQLiteOPResult{
.type = SQLiteOk,
};
}