v2 / vlib / db / sqlite / vfs_lowlevel.c.v
234 lines · 186 sloc · 7.32 KB · bbecb3eeeb122074bc23eee2ac11c8fa0657e08b
Raw
1module sqlite
2
3type Sig1 = fn (&C.sqlite3_file, &i64) int // https://github.com/vlang/v/issues/16291
4
5type Sig2 = fn (&Sqlite3_file, &int) int // https://github.com/vlang/v/issues/16291
6
7pub type Sqlite3_file = C.sqlite3_file
8pub type Sqlite3_vfs = C.sqlite3_vfs
9
10// https://www.sqlite.org/c3ref/vfs.html
11type 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.
15type Sqlite3_file_op_fn = fn (&Sqlite3_file) int
16
17type Sqlite3_file_rw_fn = fn (&Sqlite3_file, voidptr, int, i64) int
18
19type Sqlite3_file_truncate_fn = fn (&Sqlite3_file, i64) int
20
21type Sqlite3_file_flag_fn = fn (&Sqlite3_file, int) int
22
23type Sqlite3_file_control_fn = fn (&Sqlite3_file, int, voidptr) int
24
25type Sqlite3_file_shm_map_fn = fn (&Sqlite3_file, int, int, int, &voidptr) int
26
27type Sqlite3_file_shm_lock_fn = fn (&Sqlite3_file, int, int, int) int
28
29type Sqlite3_file_shm_barrier_fn = fn (&Sqlite3_file)
30
31type Sqlite3_file_fetch_fn = fn (&Sqlite3_file, i64, int, &voidptr) int
32
33type Sqlite3_file_unfetch_fn = fn (&Sqlite3_file, i64, voidptr) int
34
35type Sqlite3_vfs_open_fn = fn (&Sqlite3_vfs, &char, &Sqlite3_file, int, &int) int
36
37type Sqlite3_vfs_delete_fn = fn (&Sqlite3_vfs, &char, int) int
38
39type Sqlite3_vfs_access_fn = fn (&Sqlite3_vfs, &char, int, &int) int
40
41type Sqlite3_vfs_fullpathname_fn = fn (&Sqlite3_vfs, &char, int, &char) int
42
43type Sqlite3_vfs_dlopen_fn = fn (&Sqlite3_vfs, &char) voidptr
44
45type Sqlite3_vfs_dlerror_fn = fn (&Sqlite3_vfs, int, &char)
46
47type Sqlite3_vfs_dlsym_fn = fn (&Sqlite3_vfs, voidptr, &char) voidptr
48
49type Sqlite3_vfs_dlclose_fn = fn (&Sqlite3_vfs, voidptr)
50
51type Sqlite3_vfs_randomness_fn = fn (&Sqlite3_vfs, int, &char) int
52
53type Sqlite3_vfs_sleep_fn = fn (&Sqlite3_vfs, int) int
54
55type Sqlite3_vfs_current_time_fn = fn (&Sqlite3_vfs, &f64) int
56
57type Sqlite3_vfs_get_last_error_fn = fn (&Sqlite3_vfs, int, &char) int
58
59type Sqlite3_vfs_current_time_i64_fn = fn (&Sqlite3_vfs, &i64) int
60
61type Sqlite3_vfs_set_system_call_fn = fn (&Sqlite3_vfs, &char, Fn_sqlite3_syscall_ptr) int
62
63type Sqlite3_vfs_get_system_call_fn = fn (&Sqlite3_vfs, &char) Fn_sqlite3_syscall_ptr
64
65type Sqlite3_vfs_next_system_call_fn = fn (&Sqlite3_vfs, &char) &char
66
67// https://www.sqlite.org/c3ref/file.html
68pub struct C.sqlite3_file {
69pub 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]
75pub struct C.sqlite3_io_methods {
76mut:
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
102pub type Sqlite3_io_methods = C.sqlite3_io_methods
103
104@[heap]
105pub struct C.sqlite3_vfs {
106pub 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
137fn C.sqlite3_vfs_find(&char) &C.sqlite3_vfs
138fn C.sqlite3_vfs_register(&C.sqlite3_vfs, i32) i32
139fn 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
143pub 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
152pub 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
164pub 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
172pub 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
180fn C.sqlite3_open_v2(&char, &&C.sqlite3, i32, &char) i32
181
182// https://www.sqlite.org/c3ref/c_open_autoproxy.html
183pub 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).
201pub 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