| 1 | module sqlite |
| 2 | |
| 3 | type Sig1 = fn (&C.sqlite3_file, &i64) int // https://github.com/vlang/v/issues/16291 |
| 4 | |
| 5 | type Sig2 = fn (&Sqlite3_file, &int) int // https://github.com/vlang/v/issues/16291 |
| 6 | |
| 7 | pub type Sqlite3_file = C.sqlite3_file |
| 8 | pub type Sqlite3_vfs = C.sqlite3_vfs |
| 9 | |
| 10 | // https://www.sqlite.org/c3ref/vfs.html |
| 11 | type Fn_sqlite3_syscall_ptr = fn () |
| 12 | |
| 13 | // Keep these callback signatures named so v2 does not need to parse qualified |
| 14 | // type names inside inline `fn (...)` struct fields. |
| 15 | type Sqlite3_file_op_fn = fn (&Sqlite3_file) int |
| 16 | |
| 17 | type Sqlite3_file_rw_fn = fn (&Sqlite3_file, voidptr, int, i64) int |
| 18 | |
| 19 | type Sqlite3_file_truncate_fn = fn (&Sqlite3_file, i64) int |
| 20 | |
| 21 | type Sqlite3_file_flag_fn = fn (&Sqlite3_file, int) int |
| 22 | |
| 23 | type Sqlite3_file_control_fn = fn (&Sqlite3_file, int, voidptr) int |
| 24 | |
| 25 | type Sqlite3_file_shm_map_fn = fn (&Sqlite3_file, int, int, int, &voidptr) int |
| 26 | |
| 27 | type Sqlite3_file_shm_lock_fn = fn (&Sqlite3_file, int, int, int) int |
| 28 | |
| 29 | type Sqlite3_file_shm_barrier_fn = fn (&Sqlite3_file) |
| 30 | |
| 31 | type Sqlite3_file_fetch_fn = fn (&Sqlite3_file, i64, int, &voidptr) int |
| 32 | |
| 33 | type Sqlite3_file_unfetch_fn = fn (&Sqlite3_file, i64, voidptr) int |
| 34 | |
| 35 | type Sqlite3_vfs_open_fn = fn (&Sqlite3_vfs, &char, &Sqlite3_file, int, &int) int |
| 36 | |
| 37 | type Sqlite3_vfs_delete_fn = fn (&Sqlite3_vfs, &char, int) int |
| 38 | |
| 39 | type Sqlite3_vfs_access_fn = fn (&Sqlite3_vfs, &char, int, &int) int |
| 40 | |
| 41 | type Sqlite3_vfs_fullpathname_fn = fn (&Sqlite3_vfs, &char, int, &char) int |
| 42 | |
| 43 | type Sqlite3_vfs_dlopen_fn = fn (&Sqlite3_vfs, &char) voidptr |
| 44 | |
| 45 | type Sqlite3_vfs_dlerror_fn = fn (&Sqlite3_vfs, int, &char) |
| 46 | |
| 47 | type Sqlite3_vfs_dlsym_fn = fn (&Sqlite3_vfs, voidptr, &char) voidptr |
| 48 | |
| 49 | type Sqlite3_vfs_dlclose_fn = fn (&Sqlite3_vfs, voidptr) |
| 50 | |
| 51 | type Sqlite3_vfs_randomness_fn = fn (&Sqlite3_vfs, int, &char) int |
| 52 | |
| 53 | type Sqlite3_vfs_sleep_fn = fn (&Sqlite3_vfs, int) int |
| 54 | |
| 55 | type Sqlite3_vfs_current_time_fn = fn (&Sqlite3_vfs, &f64) int |
| 56 | |
| 57 | type Sqlite3_vfs_get_last_error_fn = fn (&Sqlite3_vfs, int, &char) int |
| 58 | |
| 59 | type Sqlite3_vfs_current_time_i64_fn = fn (&Sqlite3_vfs, &i64) int |
| 60 | |
| 61 | type Sqlite3_vfs_set_system_call_fn = fn (&Sqlite3_vfs, &char, Fn_sqlite3_syscall_ptr) int |
| 62 | |
| 63 | type Sqlite3_vfs_get_system_call_fn = fn (&Sqlite3_vfs, &char) Fn_sqlite3_syscall_ptr |
| 64 | |
| 65 | type Sqlite3_vfs_next_system_call_fn = fn (&Sqlite3_vfs, &char) &char |
| 66 | |
| 67 | // https://www.sqlite.org/c3ref/file.html |
| 68 | pub struct C.sqlite3_file { |
| 69 | pub mut: |
| 70 | pMethods &C.sqlite3_io_methods // Methods for an open file |
| 71 | } |
| 72 | |
| 73 | // https://www.sqlite.org/c3ref/io_methods.html |
| 74 | @[heap] |
| 75 | pub struct C.sqlite3_io_methods { |
| 76 | mut: |
| 77 | // version 1 and later fields |
| 78 | iVersion int |
| 79 | |
| 80 | xClose Sqlite3_file_op_fn |
| 81 | xRead Sqlite3_file_rw_fn |
| 82 | xWrite Sqlite3_file_rw_fn |
| 83 | xTruncate Sqlite3_file_truncate_fn |
| 84 | xSync Sqlite3_file_flag_fn |
| 85 | xFileSize Sig1 |
| 86 | xLock Sqlite3_file_flag_fn |
| 87 | xUnlock Sqlite3_file_flag_fn |
| 88 | xCheckReservedLock Sig2 |
| 89 | xFileControl Sqlite3_file_control_fn |
| 90 | xSectorSize Sqlite3_file_op_fn |
| 91 | xDeviceCharacteristics Sqlite3_file_op_fn |
| 92 | // version 2 and later fields |
| 93 | xShmMap Sqlite3_file_shm_map_fn |
| 94 | xShmLock Sqlite3_file_shm_lock_fn |
| 95 | xShmBarrier Sqlite3_file_shm_barrier_fn |
| 96 | xShmUnmap Sqlite3_file_flag_fn |
| 97 | // version 3 and later fields |
| 98 | xFetch Sqlite3_file_fetch_fn |
| 99 | xUnfetch Sqlite3_file_unfetch_fn |
| 100 | } |
| 101 | |
| 102 | pub type Sqlite3_io_methods = C.sqlite3_io_methods |
| 103 | |
| 104 | @[heap] |
| 105 | pub struct C.sqlite3_vfs { |
| 106 | pub mut: |
| 107 | // version 1 and later fields |
| 108 | iVersion int // Structure version number (currently 3) |
| 109 | szOsFile int // Size of subclassed sqlite3_file |
| 110 | mxPathname int // Maximum file pathname length |
| 111 | pNext &Sqlite3_vfs // Next registered VFS |
| 112 | zName &char // Name of this virtual file system |
| 113 | pAppData voidptr // Pointer to application-specific data |
| 114 | |
| 115 | xOpen Sqlite3_vfs_open_fn |
| 116 | xDelete Sqlite3_vfs_delete_fn |
| 117 | |
| 118 | xAccess Sqlite3_vfs_access_fn |
| 119 | xFullPathname Sqlite3_vfs_fullpathname_fn |
| 120 | xDlOpen Sqlite3_vfs_dlopen_fn |
| 121 | xDlError Sqlite3_vfs_dlerror_fn |
| 122 | xDlSym Sqlite3_vfs_dlsym_fn // to fn accepting void and returning |
| 123 | xDlClose Sqlite3_vfs_dlclose_fn |
| 124 | xRandomness Sqlite3_vfs_randomness_fn |
| 125 | xSleep Sqlite3_vfs_sleep_fn |
| 126 | xCurrentTime Sqlite3_vfs_current_time_fn |
| 127 | xGetLastError Sqlite3_vfs_get_last_error_fn |
| 128 | // version two and later only fields |
| 129 | xCurrentTimeInt64 Sqlite3_vfs_current_time_i64_fn |
| 130 | // version three and later only fields |
| 131 | xSetSystemCall Sqlite3_vfs_set_system_call_fn |
| 132 | xGetSystemCall Sqlite3_vfs_get_system_call_fn |
| 133 | xNextSystemCall Sqlite3_vfs_next_system_call_fn |
| 134 | } |
| 135 | |
| 136 | // https://www.sqlite.org/c3ref/vfs_find.html |
| 137 | fn C.sqlite3_vfs_find(&char) &C.sqlite3_vfs |
| 138 | fn C.sqlite3_vfs_register(&C.sqlite3_vfs, i32) i32 |
| 139 | fn C.sqlite3_vfs_unregister(&C.sqlite3_vfs) i32 |
| 140 | |
| 141 | // get_vfs Requests sqlite to return instance of VFS with given name. |
| 142 | // when such vfs is not known, `none` is returned |
| 143 | pub fn get_vfs(name string) !&Sqlite3_vfs { |
| 144 | res := C.sqlite3_vfs_find(name.str) |
| 145 | if res != unsafe { nil } { |
| 146 | return res |
| 147 | } |
| 148 | return error('sqlite3_vfs_find returned 0') |
| 149 | } |
| 150 | |
| 151 | // get_default_vfs Asks sqlite for default VFS instance |
| 152 | pub fn get_default_vfs() !&Sqlite3_vfs { |
| 153 | res := C.sqlite3_vfs_find(unsafe { nil }) |
| 154 | if res != unsafe { nil } { |
| 155 | return res |
| 156 | } |
| 157 | return error('sqlite3_vfs_find(0) returned 0') |
| 158 | } |
| 159 | |
| 160 | // register_as_nondefault Asks sqlite to register VFS passed in receiver argument as the known VFS. |
| 161 | // more info about VFS: https://www.sqlite.org/c3ref/vfs.html |
| 162 | // 'not TODOs' to prevent corruption: https://sqlite.org/howtocorrupt.html |
| 163 | // example VFS: https://www.sqlite.org/src/doc/trunk/src/test_demovfs.c |
| 164 | pub fn (mut v Sqlite3_vfs) register_as_nondefault() ! { |
| 165 | res := C.sqlite3_vfs_register(v, 0) |
| 166 | if sqlite_ok != res { |
| 167 | return error('sqlite3_vfs_register returned ${res}') |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // unregister Requests sqlite to stop using VFS as passed in receiver argument |
| 172 | pub fn (mut v Sqlite3_vfs) unregister() ! { |
| 173 | res := C.sqlite3_vfs_unregister(v) |
| 174 | if sqlite_ok != res { |
| 175 | return error('sqlite3_vfs_unregister returned ${res}') |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // https://www.sqlite.org/c3ref/open.html |
| 180 | fn C.sqlite3_open_v2(&char, &&C.sqlite3, i32, &char) i32 |
| 181 | |
| 182 | // https://www.sqlite.org/c3ref/c_open_autoproxy.html |
| 183 | pub enum OpenModeFlag { |
| 184 | readonly = 0x00000001 |
| 185 | readwrite = 0x00000002 |
| 186 | create = 0x00000004 |
| 187 | uri = 0x00000040 |
| 188 | memory = 0x00000080 |
| 189 | nomutex = 0x00008000 |
| 190 | fullmutex = 0x00010000 |
| 191 | sharedcache = 0x00020000 |
| 192 | privatecache = 0x00040000 |
| 193 | exrescode = 0x02000000 |
| 194 | nofollow = 0x01000000 |
| 195 | } |
| 196 | |
| 197 | // connect_full Opens connection to sqlite database. It gives more control than `open`. |
| 198 | // Flags give control over readonly and create decisions. Specific VFS can be chosen. |
| 199 | // Pass '' for vfs_name, if you want to use the default VFS for the current platform, |
| 200 | // (which is equivalent to 'win32' on Windows, and 'unix' on !Windows systems). |
| 201 | pub fn connect_full(path string, mode_flags []OpenModeFlag, vfs_name string) !DB { |
| 202 | db := &C.sqlite3(unsafe { nil }) |
| 203 | path_cstr := sqlite_cstring(path) |
| 204 | defer { |
| 205 | unsafe { free(path_cstr) } |
| 206 | } |
| 207 | |
| 208 | mut flags := 0 |
| 209 | |
| 210 | for flag in mode_flags { |
| 211 | flags = flags | int(flag) |
| 212 | } |
| 213 | |
| 214 | mut pstr := unsafe { &char(0) } |
| 215 | if vfs_name != '' { |
| 216 | pstr = sqlite_cstring(vfs_name) |
| 217 | } |
| 218 | defer { |
| 219 | if vfs_name != '' { |
| 220 | unsafe { free(pstr) } |
| 221 | } |
| 222 | } |
| 223 | code := C.sqlite3_open_v2(path_cstr, &db, flags, pstr) |
| 224 | if code != 0 { |
| 225 | return &SQLError{ |
| 226 | msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) } |
| 227 | code: code |
| 228 | } |
| 229 | } |
| 230 | return DB{ |
| 231 | conn: db |
| 232 | is_open: true |
| 233 | } |
| 234 | } |
| 235 | |