Skip to content

Commit cc91b27

Browse files
committed
Support custom VFS on native platforms
1 parent 3f3c71a commit cc91b27

25 files changed

+786
-65
lines changed

sqlite3/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.5.0
2+
3+
- Allow registering custom virtual file systems on all platforms. Previously,
4+
this was only supported on the web.
5+
16
## 2.4.7
27

38
- Web: Improve performance of in-memory and IndexedDB file system implementations.

sqlite3/assets/sqlite3.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,81 @@ int sqlite3_auto_extension(void *xEntryPoint);
130130

131131
// Database configuration
132132
int sqlite3_db_config(sqlite3 *db, int op, ...);
133+
134+
// VFS
135+
typedef struct sqlite3_file sqlite3_file;
136+
137+
struct sqlite3_io_methods {
138+
int iVersion;
139+
int (*xClose)(sqlite3_file*);
140+
int (*xRead)(sqlite3_file*, void*, int iAmt, int64_t iOfst);
141+
int (*xWrite)(sqlite3_file*, const void*, int iAmt, int64_t iOfst);
142+
int (*xTruncate)(sqlite3_file*, int64_t size);
143+
int (*xSync)(sqlite3_file*, int flags);
144+
int (*xFileSize)(sqlite3_file*, int64_t *pSize);
145+
int (*xLock)(sqlite3_file*, int);
146+
int (*xUnlock)(sqlite3_file*, int);
147+
int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
148+
int (*xFileControl)(sqlite3_file*, int op, void *pArg);
149+
int (*xSectorSize)(sqlite3_file*);
150+
int (*xDeviceCharacteristics)(sqlite3_file*);
151+
/* Methods above are valid for version 1 */
152+
int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void **);
153+
int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
154+
void (*xShmBarrier)(sqlite3_file*);
155+
int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
156+
/* Methods above are valid for version 2 */
157+
int (*xFetch)(sqlite3_file*, int64_t iOfst, int iAmt, void **pp);
158+
int (*xUnfetch)(sqlite3_file*, int64_t iOfst, void *p);
159+
/* Methods above are valid for version 3 */
160+
/* Additional methods may be added in future releases */
161+
};
162+
163+
struct sqlite3_file {
164+
const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
165+
};
166+
167+
typedef struct sqlite3_vfs sqlite3_vfs;
168+
typedef void (*sqlite3_syscall_ptr)(void);
169+
typedef const char *sqlite3_filename;
170+
171+
struct sqlite3_vfs {
172+
int iVersion; /* Structure version number (currently 3) */
173+
int szOsFile; /* Size of subclassed sqlite3_file */
174+
int mxPathname; /* Maximum file pathname length */
175+
sqlite3_vfs *pNext; /* Next registered VFS */
176+
const char *zName; /* Name of this virtual file system */
177+
void *pAppData; /* Pointer to application-specific data */
178+
int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
179+
int flags, int *pOutFlags);
180+
int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
181+
int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
182+
int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
183+
void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
184+
void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
185+
void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
186+
void (*xDlClose)(sqlite3_vfs*, void*);
187+
int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
188+
int (*xSleep)(sqlite3_vfs*, int microseconds);
189+
int (*xCurrentTime)(sqlite3_vfs*, double*);
190+
int (*xGetLastError)(sqlite3_vfs*, int, char *);
191+
/*
192+
** The methods above are in version 1 of the sqlite_vfs object
193+
** definition. Those that follow are added in version 2 or later
194+
*/
195+
int (*xCurrentTimeInt64)(sqlite3_vfs*, int64_t*);
196+
/*
197+
** The methods above are in versions 1 and 2 of the sqlite_vfs object.
198+
** Those below are for version 3 and greater.
199+
*/
200+
int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
201+
sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
202+
const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
203+
/*
204+
** The methods above are in versions 1 through 3 of the sqlite_vfs object.
205+
** New fields may be appended in future versions. The iVersion
206+
** value will increment whenever this happens.
207+
*/
208+
};
209+
int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
210+
int sqlite3_vfs_unregister(sqlite3_vfs*);

sqlite3/lib/common.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export 'src/sqlite3.dart';
1111
export 'src/statement.dart'
1212
show CommonPreparedStatement, StatementParameters, CustomStatementParameter;
1313
export 'src/vfs.dart';
14+
export 'src/in_memory_vfs.dart' show InMemoryFileSystem;

sqlite3/lib/src/ffi/api.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract interface class Sqlite3 implements CommonSqlite3 {
3131
Database fromPointer(Pointer<void> database);
3232

3333
@override
34-
Database openInMemory();
34+
Database openInMemory({String? vfs});
3535

3636
/// Opens a new in-memory database and copies another database into it
3737
/// https://www.sqlite.org/c3ref/backup_finish.html

0 commit comments

Comments
 (0)