-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogiovfs.cpp
More file actions
83 lines (73 loc) · 3.07 KB
/
logiovfs.cpp
File metadata and controls
83 lines (73 loc) · 3.07 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
// 1. Include <SQLiteVfs.hpp> header
// If you are building an extension DLL, make sure to include
// <sqlite3ext.h> and call SQLITE_EXTENSION_INIT1 first.
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1
#include <SQLiteVfs.hpp>
#include <iostream>
using namespace sqlitevfs;
using namespace std;
// 2. Implement your own `SQLiteFileImpl` subclass.
// Override any IO methods necessary. Reference: https://www.sqlite.org/c3ref/io_methods.html
// Default implementation will forward execution to the `original_file` opened by `SQLiteVfsImpl::xOpen`.
// Default constructor will be called before `SQLiteVfsImpl::xOpen`.
// Destructor will be called right after `xClose`, or after a failed `SQLiteVfsImpl::xOpen`.
struct LogIOFileShim : public SQLiteFileImpl {
sqlite3_filename zName;
LogIOFileShim() {
cout << "> Constructing file!" << endl;
}
int xRead(void *p, int iAmt, sqlite3_int64 iOfst) override {
cout << "> READ " << zName << " " << iAmt << " bytes starting at " << iOfst << endl;
return SQLiteFileImpl::xRead(p, iAmt, iOfst);
}
int xWrite(const void *p, int iAmt, sqlite3_int64 iOfst) override {
cout << "> WRITE " << zName << " " << iAmt << " bytes starting at " << iOfst << endl;
return SQLiteFileImpl::xWrite(p, iAmt, iOfst);
}
int xClose() override {
cout << "> CLOSE " << zName << endl;
return SQLiteFileImpl::xClose();
}
~LogIOFileShim() {
cout << "> Destroying file!" << endl;
}
};
// 3. Implement your own `SQLiteVfsImpl<>` subclass.
// Pass your SQLiteFileImpl subclass as template parameter.
// Override any methods necessary. Reference: https://www.sqlite.org/c3ref/vfs.html
// Default implementation will forward execution to the `original_vfs` passed in `SQLiteVfs` construtor.
// Notice that `xOpen` receives a `SQLiteFile<LogIOFileImpl> *` instead of `sqlite3_file`.
// `file` is guaranteed to be fully constructed before this method is called.
struct LogIOVfsShim : public SQLiteVfsImpl<LogIOFileShim> {
int xOpen(sqlite3_filename zName, SQLiteFile<LogIOFileShim> *file, int flags, int *pOutFlags) override {
int result = SQLiteVfsImpl::xOpen(zName, file, flags, pOutFlags);
file->implementation.zName = zName;
if (result == SQLITE_OK) {
cout << "> OPENED '" << zName << "'" << endl;
}
else {
cout << "> ERROR OPENING '" << zName << "': " << sqlite3_errstr(result) << endl;
}
return result;
}
int xDelete(const char *zName, int syncDir) override {
cout << "> DELETE " << zName << endl;
return SQLiteVfsImpl::xDelete(zName, syncDir);
}
};
// If implementing an extension DLL, export the function SQLite expects
extern "C" int sqlite3_logiovfs_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
SQLITE_EXTENSION_INIT2(pApi);
// 4. Create a `SQLiteVfs<>`.
// Pass your `SQLiteVfsImpl<>` subclass as template parameter.
static SQLiteVfs<LogIOVfsShim> logiovfs("logiovfs");
// 5. Register your newly created VFS.
// Optionally make it the default VFS.
int rc = logiovfs.register_vfs(false);
if (rc == SQLITE_OK) {
rc = SQLITE_OK_LOAD_PERMANENTLY;
}
return rc;
}
// 6. (optional) Unregister your VFS using `your_vfs.unregister_vfs()`