Skip to content

Commit

Permalink
Support custom VFS on native platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Nov 19, 2024
1 parent 3f3c71a commit cc91b27
Show file tree
Hide file tree
Showing 25 changed files with 786 additions and 65 deletions.
5 changes: 5 additions & 0 deletions sqlite3/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.5.0

- Allow registering custom virtual file systems on all platforms. Previously,
this was only supported on the web.

## 2.4.7

- Web: Improve performance of in-memory and IndexedDB file system implementations.
Expand Down
78 changes: 78 additions & 0 deletions sqlite3/assets/sqlite3.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,81 @@ int sqlite3_auto_extension(void *xEntryPoint);

// Database configuration
int sqlite3_db_config(sqlite3 *db, int op, ...);

// VFS
typedef struct sqlite3_file sqlite3_file;

struct sqlite3_io_methods {
int iVersion;
int (*xClose)(sqlite3_file*);
int (*xRead)(sqlite3_file*, void*, int iAmt, int64_t iOfst);
int (*xWrite)(sqlite3_file*, const void*, int iAmt, int64_t iOfst);
int (*xTruncate)(sqlite3_file*, int64_t size);
int (*xSync)(sqlite3_file*, int flags);
int (*xFileSize)(sqlite3_file*, int64_t *pSize);
int (*xLock)(sqlite3_file*, int);
int (*xUnlock)(sqlite3_file*, int);
int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
int (*xFileControl)(sqlite3_file*, int op, void *pArg);
int (*xSectorSize)(sqlite3_file*);
int (*xDeviceCharacteristics)(sqlite3_file*);
/* Methods above are valid for version 1 */
int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void **);
int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
void (*xShmBarrier)(sqlite3_file*);
int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
/* Methods above are valid for version 2 */
int (*xFetch)(sqlite3_file*, int64_t iOfst, int iAmt, void **pp);
int (*xUnfetch)(sqlite3_file*, int64_t iOfst, void *p);
/* Methods above are valid for version 3 */
/* Additional methods may be added in future releases */
};

struct sqlite3_file {
const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
};

typedef struct sqlite3_vfs sqlite3_vfs;
typedef void (*sqlite3_syscall_ptr)(void);
typedef const char *sqlite3_filename;

struct sqlite3_vfs {
int iVersion; /* Structure version number (currently 3) */
int szOsFile; /* Size of subclassed sqlite3_file */
int mxPathname; /* Maximum file pathname length */
sqlite3_vfs *pNext; /* Next registered VFS */
const char *zName; /* Name of this virtual file system */
void *pAppData; /* Pointer to application-specific data */
int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
int flags, int *pOutFlags);
int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
void (*xDlClose)(sqlite3_vfs*, void*);
int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
int (*xSleep)(sqlite3_vfs*, int microseconds);
int (*xCurrentTime)(sqlite3_vfs*, double*);
int (*xGetLastError)(sqlite3_vfs*, int, char *);
/*
** The methods above are in version 1 of the sqlite_vfs object
** definition. Those that follow are added in version 2 or later
*/
int (*xCurrentTimeInt64)(sqlite3_vfs*, int64_t*);
/*
** The methods above are in versions 1 and 2 of the sqlite_vfs object.
** Those below are for version 3 and greater.
*/
int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
/*
** The methods above are in versions 1 through 3 of the sqlite_vfs object.
** New fields may be appended in future versions. The iVersion
** value will increment whenever this happens.
*/
};
int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
int sqlite3_vfs_unregister(sqlite3_vfs*);
1 change: 1 addition & 0 deletions sqlite3/lib/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export 'src/sqlite3.dart';
export 'src/statement.dart'
show CommonPreparedStatement, StatementParameters, CustomStatementParameter;
export 'src/vfs.dart';
export 'src/in_memory_vfs.dart' show InMemoryFileSystem;
2 changes: 1 addition & 1 deletion sqlite3/lib/src/ffi/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract interface class Sqlite3 implements CommonSqlite3 {
Database fromPointer(Pointer<void> database);

@override
Database openInMemory();
Database openInMemory({String? vfs});

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

0 comments on commit cc91b27

Please sign in to comment.