forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.cpp
More file actions
358 lines (296 loc) · 7.71 KB
/
sqlite.cpp
File metadata and controls
358 lines (296 loc) · 7.71 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* @file sqlite.cpp
* @brief SQLite DB access layer
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK 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.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega.h"
#ifdef USE_SQLITE
namespace mega {
SqliteDbAccess::SqliteDbAccess(string* path)
{
if (path)
{
dbpath = *path;
}
}
SqliteDbAccess::~SqliteDbAccess()
{
}
DbTable* SqliteDbAccess::open(PrnGen &rng, FileSystemAccess* fsaccess, string* name, bool recycleLegacyDB, bool checkAlwaysTransacted)
{
//Each table will use its own database object and its own file
sqlite3* db;
string dbfile;
ostringstream legacyoss;
legacyoss << dbpath;
legacyoss << "megaclient_statecache";
legacyoss << LEGACY_DB_VERSION;
legacyoss << "_" << *name << ".db";
string legacydbpath = legacyoss.str();
ostringstream newoss;
newoss << dbpath;
newoss << "megaclient_statecache";
newoss << DB_VERSION;
newoss << "_" << *name << ".db";
string currentdbpath = newoss.str();
auto fa = fsaccess->newfileaccess();
auto locallegacydbpath = LocalPath::fromPath(legacydbpath, *fsaccess);
bool legacydbavailable = fa->fopen(locallegacydbpath);
fa.reset();
if (legacydbavailable)
{
if (currentDbVersion == LEGACY_DB_VERSION)
{
LOG_debug << "Using a legacy DB";
dbfile = legacydbpath;
}
else
{
if (!recycleLegacyDB)
{
LOG_debug << "Legacy DB is outdated. Deleting.";
fsaccess->unlinklocal(locallegacydbpath);
}
else
{
LOG_debug << "Trying to recycle a legacy DB";
auto localcurrentdbpath = LocalPath::fromPath(currentdbpath, *fsaccess);
if (fsaccess->renamelocal(locallegacydbpath, localcurrentdbpath, false))
{
auto localsuffix = LocalPath::fromPath("-shm", *fsaccess);
auto oldfile = locallegacydbpath + localsuffix;
auto newfile = localcurrentdbpath + localsuffix;
fsaccess->renamelocal(oldfile, newfile, true);
localsuffix = LocalPath::fromPath("-wal", *fsaccess);
oldfile = locallegacydbpath + localsuffix;
newfile = localcurrentdbpath + localsuffix;
fsaccess->renamelocal(oldfile, newfile, true);
LOG_debug << "Legacy DB recycled";
}
else
{
LOG_debug << "Unable to recycle legacy DB. Deleting.";
fsaccess->unlinklocal(locallegacydbpath);
}
}
}
}
if (!dbfile.size())
{
LOG_debug << "Using an upgraded DB";
dbfile = currentdbpath;
currentDbVersion = DB_VERSION;
}
int rc;
rc = sqlite3_open(dbfile.c_str(), &db);
if (rc)
{
return NULL;
}
#if !(TARGET_OS_IPHONE)
sqlite3_exec(db, "PRAGMA journal_mode=WAL;", NULL, NULL, NULL);
#endif
const char *sql = "CREATE TABLE IF NOT EXISTS statecache (id INTEGER PRIMARY KEY ASC NOT NULL, content BLOB NOT NULL)";
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (rc)
{
return NULL;
}
return new SqliteDbTable(rng, db, fsaccess, &dbfile, checkAlwaysTransacted);
}
SqliteDbTable::SqliteDbTable(PrnGen &rng, sqlite3* cdb, FileSystemAccess *fs, string *filepath, bool checkAlwaysTransacted)
: DbTable(rng, checkAlwaysTransacted)
{
db = cdb;
pStmt = NULL;
fsaccess = fs;
dbfile = *filepath;
}
SqliteDbTable::~SqliteDbTable()
{
resetCommitter();
if (!db)
{
return;
}
if (pStmt)
{
sqlite3_finalize(pStmt);
}
abort();
sqlite3_close(db);
LOG_debug << "Database closed " << dbfile;
}
// set cursor to first record
void SqliteDbTable::rewind()
{
if (!db)
{
return;
}
if (pStmt)
{
sqlite3_reset(pStmt);
}
else
{
sqlite3_prepare(db, "SELECT id, content FROM statecache", -1, &pStmt, NULL);
}
}
// retrieve next record through cursor
bool SqliteDbTable::next(uint32_t* index, string* data)
{
if (!db)
{
return false;
}
if (!pStmt)
{
return false;
}
int rc = sqlite3_step(pStmt);
if (rc != SQLITE_ROW)
{
sqlite3_finalize(pStmt);
pStmt = NULL;
return false;
}
*index = sqlite3_column_int(pStmt, 0);
data->assign((char*)sqlite3_column_blob(pStmt, 1), sqlite3_column_bytes(pStmt, 1));
return true;
}
// retrieve record by index
bool SqliteDbTable::get(uint32_t index, string* data)
{
if (!db)
{
return false;
}
checkTransaction();
sqlite3_stmt *stmt;
bool result = false;
if (sqlite3_prepare(db, "SELECT content FROM statecache WHERE id = ?", -1, &stmt, NULL) == SQLITE_OK)
{
if (sqlite3_bind_int(stmt, 1, index) == SQLITE_OK)
{
if (sqlite3_step(stmt) == SQLITE_ROW)
{
data->assign((char*)sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
result = true;
}
}
}
sqlite3_finalize(stmt);
return result;
}
// add/update record by index
bool SqliteDbTable::put(uint32_t index, char* data, unsigned len)
{
if (!db)
{
return false;
}
checkTransaction();
sqlite3_stmt *stmt;
bool result = false;
if (sqlite3_prepare(db, "INSERT OR REPLACE INTO statecache (id, content) VALUES (?, ?)", -1, &stmt, NULL) == SQLITE_OK)
{
if (sqlite3_bind_int(stmt, 1, index) == SQLITE_OK)
{
if (sqlite3_bind_blob(stmt, 2, data, len, SQLITE_STATIC) == SQLITE_OK)
{
if (sqlite3_step(stmt) == SQLITE_DONE)
{
result = true;
}
}
}
}
sqlite3_finalize(stmt);
return result;
}
// delete record by index
bool SqliteDbTable::del(uint32_t index)
{
if (!db)
{
return false;
}
checkTransaction();
char buf[64];
sprintf(buf, "DELETE FROM statecache WHERE id = %" PRIu32, index);
return !sqlite3_exec(db, buf, 0, 0, NULL);
}
// truncate table
void SqliteDbTable::truncate()
{
if (!db)
{
return;
}
checkTransaction();
sqlite3_exec(db, "DELETE FROM statecache", 0, 0, NULL);
}
// begin transaction
void SqliteDbTable::begin()
{
if (!db)
{
return;
}
LOG_debug << "DB transaction BEGIN " << dbfile;
sqlite3_exec(db, "BEGIN", 0, 0, NULL);
}
// commit transaction
void SqliteDbTable::commit()
{
if (!db)
{
return;
}
LOG_debug << "DB transaction COMMIT " << dbfile;
sqlite3_exec(db, "COMMIT", 0, 0, NULL);
}
// abort transaction
void SqliteDbTable::abort()
{
if (!db)
{
return;
}
LOG_debug << "DB transaction ROLLBACK " << dbfile;
sqlite3_exec(db, "ROLLBACK", 0, 0, NULL);
}
void SqliteDbTable::remove()
{
if (!db)
{
return;
}
if (pStmt)
{
sqlite3_finalize(pStmt);
}
abort();
sqlite3_close(db);
db = NULL;
auto localpath = LocalPath::fromPath(dbfile, *fsaccess);
fsaccess->unlinklocal(localpath);
}
} // namespace
#endif