|
| 1 | +/// Provides a virtual filesystem implementation for SQLite based on the `file` |
| 2 | +/// and `clock` packages. |
| 3 | +/// |
| 4 | +/// This makes it easier to use SQLite in tests, as SQL constructs like |
| 5 | +/// `CURRENT_TIMESTAMP` will reflect the fake time of `package:clock`, allowing |
| 6 | +/// SQL logic relying on time to be tested reliably. Additionally, using |
| 7 | +/// `dart:clock` allows testing the IO behavior of SQLite databases if |
| 8 | +/// necessary. |
| 9 | +library; |
| 10 | + |
| 11 | +import 'dart:typed_data'; |
| 12 | + |
| 13 | +import 'package:clock/clock.dart'; |
| 14 | +import 'package:file/file.dart'; |
| 15 | +import 'package:sqlite3/common.dart'; |
| 16 | + |
| 17 | +final class TestSqliteFileSystem extends BaseVirtualFileSystem { |
| 18 | + static int _counter = 0; |
| 19 | + |
| 20 | + final FileSystem _fs; |
| 21 | + Directory? _createdTmp; |
| 22 | + int _tmpFileCounter = 0; |
| 23 | + |
| 24 | + TestSqliteFileSystem({required FileSystem fs, String? name}) |
| 25 | + : _fs = fs, |
| 26 | + super(name: name ?? 'dart-test-vfs-${_counter++}'); |
| 27 | + |
| 28 | + Directory get _tempDirectory { |
| 29 | + return _createdTmp ??= |
| 30 | + _fs.systemTempDirectory.createTempSync('dart-sqlite3-test'); |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + int xAccess(String path, int flags) { |
| 35 | + switch (flags) { |
| 36 | + case 0: |
| 37 | + // Exists |
| 38 | + return _fs.typeSync(path) == FileSystemEntityType.file ? 1 : 0; |
| 39 | + default: |
| 40 | + // Check readable and writable |
| 41 | + try { |
| 42 | + final file = _fs.file(path); |
| 43 | + file.openSync(mode: FileMode.write).closeSync(); |
| 44 | + return 1; |
| 45 | + } on IOException { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @override |
| 52 | + DateTime xCurrentTime() { |
| 53 | + return clock.now(); |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + void xDelete(String path, int syncDir) { |
| 58 | + _fs.file(path).deleteSync(); |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + String xFullPathName(String path) { |
| 63 | + return _fs.path.absolute(path); |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + XOpenResult xOpen(Sqlite3Filename path, int flags) { |
| 68 | + final fsPath = path.path ?? |
| 69 | + _tempDirectory.childFile((_tmpFileCounter++).toString()).absolute.path; |
| 70 | + final type = _fs.typeSync(fsPath); |
| 71 | + |
| 72 | + if (type != FileSystemEntityType.notFound && |
| 73 | + type != FileSystemEntityType.file) { |
| 74 | + throw VfsException(ErrorCodes.EINVAL); |
| 75 | + } |
| 76 | + |
| 77 | + if (flags & SqlFlag.SQLITE_OPEN_EXCLUSIVE != 0 && |
| 78 | + type != FileSystemEntityType.notFound) { |
| 79 | + throw VfsException(ErrorCodes.EEXIST); |
| 80 | + } |
| 81 | + if (flags & SqlFlag.SQLITE_OPEN_CREATE != 0 && |
| 82 | + type == FileSystemEntityType.notFound) { |
| 83 | + _fs.file(fsPath).createSync(); |
| 84 | + } |
| 85 | + |
| 86 | + final deleteOnClose = flags & SqlFlag.SQLITE_OPEN_DELETEONCLOSE != 0; |
| 87 | + final readonly = flags & SqlFlag.SQLITE_OPEN_READONLY != 0; |
| 88 | + final vsFile = _fs.file(fsPath); |
| 89 | + final file = |
| 90 | + vsFile.openSync(mode: readonly ? FileMode.read : FileMode.write); |
| 91 | + |
| 92 | + return ( |
| 93 | + file: _TestFile(vsFile, file, deleteOnClose), |
| 94 | + outFlags: readonly ? SqlFlag.SQLITE_OPEN_READONLY : 0, |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + void xSleep(Duration duration) {} |
| 100 | +} |
| 101 | + |
| 102 | +final class _TestFile implements VirtualFileSystemFile { |
| 103 | + final File _path; |
| 104 | + final RandomAccessFile _file; |
| 105 | + final bool _deleteOnClose; |
| 106 | + int _lockLevel = SqlFileLockingLevels.SQLITE_LOCK_NONE; |
| 107 | + |
| 108 | + _TestFile(this._path, this._file, this._deleteOnClose); |
| 109 | + |
| 110 | + @override |
| 111 | + void xClose() { |
| 112 | + _file.closeSync(); |
| 113 | + if (_deleteOnClose) { |
| 114 | + _path.deleteSync(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @override |
| 119 | + int get xDeviceCharacteristics => 0; |
| 120 | + |
| 121 | + @override |
| 122 | + int xFileSize() => _file.lengthSync(); |
| 123 | + |
| 124 | + @override |
| 125 | + void xRead(Uint8List target, int fileOffset) { |
| 126 | + _file.setPositionSync(fileOffset); |
| 127 | + final bytesRead = _file.readIntoSync(target); |
| 128 | + if (bytesRead < target.length) { |
| 129 | + target.fillRange(bytesRead, target.length, 0); |
| 130 | + throw VfsException(SqlExtendedError.SQLITE_IOERR_SHORT_READ); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @override |
| 135 | + void xSync(int flags) { |
| 136 | + _file.flushSync(); |
| 137 | + } |
| 138 | + |
| 139 | + @override |
| 140 | + void xTruncate(int size) { |
| 141 | + _file.truncateSync(size); |
| 142 | + } |
| 143 | + |
| 144 | + @override |
| 145 | + void xWrite(Uint8List buffer, int fileOffset) { |
| 146 | + _file |
| 147 | + ..setPositionSync(fileOffset) |
| 148 | + ..writeFromSync(buffer); |
| 149 | + } |
| 150 | + |
| 151 | + @override |
| 152 | + int xCheckReservedLock() { |
| 153 | + // RandomAccessFile doesn't appear to expose information on whether another |
| 154 | + // process is holding locks. |
| 155 | + return _lockLevel > SqlFileLockingLevels.SQLITE_LOCK_NONE ? 1 : 0; |
| 156 | + } |
| 157 | + |
| 158 | + @override |
| 159 | + void xLock(int mode) { |
| 160 | + if (_lockLevel >= mode) { |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + if (_lockLevel != SqlFileLockingLevels.SQLITE_LOCK_NONE) { |
| 165 | + // We want to upgrade our lock, which we do by releasing it and then |
| 166 | + // re-obtaining it. |
| 167 | + _file.unlockSync(); |
| 168 | + _lockLevel = SqlFileLockingLevels.SQLITE_LOCK_NONE; |
| 169 | + } |
| 170 | + |
| 171 | + final exclusive = mode > SqlFileLockingLevels.SQLITE_LOCK_SHARED; |
| 172 | + _file.lockSync( |
| 173 | + exclusive ? FileLock.blockingExclusive : FileLock.blockingShared); |
| 174 | + _lockLevel = mode; |
| 175 | + } |
| 176 | + |
| 177 | + @override |
| 178 | + void xUnlock(int mode) { |
| 179 | + if (_lockLevel < mode) { |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + _file.unlockSync(); |
| 184 | + if (mode != SqlFileLockingLevels.SQLITE_LOCK_NONE) { |
| 185 | + return xLock(mode); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments