min_sqlite3_sys/bindings.rs
1//! This module contains binding data-types and functions for SQLite.
2//!
3//! All the functions are wrapped with Rust functions due to safety.
4//! So, they are not accessible by outer crates.
5
6#![allow(dead_code)]
7
8use std::{mem, os};
9
10/// This enumeration is the list of the possible status outcomes for the
11/// SQL statement execution on SQLite3.
12#[non_exhaustive]
13#[repr(i32)]
14#[derive(Debug, PartialEq, Copy, Clone)]
15pub enum SqlitePrimaryResult {
16 /// Indicates the actual result id from SQLITE as an inner value.
17 Other(i32) = -1,
18 /// The SQLITE_OK result code means that the operation was
19 /// successful and that there were no errors. Most other
20 /// result codes indicate an error.
21 Ok = 0,
22 /// The SQLITE_ERROR result code is a generic error code
23 /// that is used when no other more specific error code
24 /// is available.
25 Error = 1,
26 /// The SQLITE_INTERNAL result code indicates an internal
27 /// malfunction. In a working version of SQLite, an application
28 /// should never see this result code. If application does
29 /// encounter this result code, it shows that there is a bug
30 /// in the database engine.
31 ///
32 /// SQLite does not currently generate this result code.
33 /// However, application-defined SQL functions or virtual
34 /// tables, or VFSes, or other extensions might cause this
35 /// result code to be returned.
36 Internal = 2,
37 /// The SQLITE_PERM result code indicates that the requested
38 /// access mode for a newly created database could not be provided.
39 Perm = 3,
40 /// The SQLITE_ABORT result code indicates that an operation was
41 /// aborted prior to completion, usually be application request.
42 /// See also: SQLITE_INTERRUPT.
43 ///
44 /// If the callback function to sqlite3_exec() returns non-zero,
45 /// then sqlite3_exec() will return SQLITE_ABORT.
46 ///
47 /// If a ROLLBACK operation occurs on the same database connection
48 /// as a pending read or write, then the pending read or write may
49 /// fail with an SQLITE_ABORT or SQLITE_ABORT_ROLLBACK error.
50 ///
51 /// In addition to being a result code, the SQLITE_ABORT value is
52 /// also used as a conflict resolution mode returned from the
53 /// sqlite3_vtab_on_conflict() interface.
54 Abort = 4,
55 /// The SQLITE_BUSY result code indicates that the database file
56 /// could not be written (or in some cases read) because of
57 /// concurrent activity by some other database connection, usually
58 /// a database connection in a separate process.
59 ///
60 /// For example, if process A is in the middle of a large write transaction
61 /// and at the same time process B attempts to start a new write transaction,
62 /// process B will get back an SQLITE_BUSY result because SQLite only supports
63 /// one writer at a time. Process B will need to wait for process A to finish
64 /// its transaction before starting a new transaction. The sqlite3_busy_timeout()
65 /// and sqlite3_busy_handler() interfaces and the busy_timeout pragma are available
66 /// to process B to help it deal with SQLITE_BUSY errors.
67 ///
68 /// An SQLITE_BUSY error can occur at any point in a transaction: when the
69 /// transaction is first started, during any write or update operations,
70 /// or when the transaction commits. To avoid encountering SQLITE_BUSY
71 /// errors in the middle of a transaction, the application can use BEGIN
72 /// IMMEDIATE instead of just BEGIN to start a transaction. The BEGIN
73 /// IMMEDIATE command might itself return SQLITE_BUSY, but if it succeeds,
74 /// then SQLite guarantees that no subsequent operations on the same database
75 /// through the next COMMIT will return SQLITE_BUSY.
76 ///
77 /// See also: SQLITE_BUSY_RECOVERY and SQLITE_BUSY_SNAPSHOT.
78 ///
79 /// The SQLITE_BUSY result code differs from SQLITE_LOCKED in that SQLITE_BUSY
80 /// indicates a conflict with a separate database connection, probably in a
81 /// separate process, whereas SQLITE_LOCKED indicates a conflict within the
82 /// same database connection (or sometimes a database connection with a shared cache).
83 Busy = 5,
84 /// The SQLITE_LOCKED result code indicates that a write operation could not
85 /// continue because of a conflict within the same database connection or a
86 /// conflict with a different database connection that uses a shared cache.
87 ///
88 /// For example, a DROP TABLE statement cannot be run while another thread
89 /// is reading from that table on the same database connection because
90 /// dropping the table would delete the table out from under the concurrent reader.
91 ///
92 /// The SQLITE_LOCKED result code differs from SQLITE_BUSY in that SQLITE_LOCKED
93 /// indicates a conflict on the same database connection (or on a connection with
94 /// a shared cache) whereas SQLITE_BUSY indicates a conflict with a different
95 /// database connection, probably in a different process.
96 Locked = 6,
97 /// The SQLITE_NOMEM result code indicates that SQLite was unable to allocate all
98 /// the memory it needed to complete the operation. In other words, an internal
99 /// call to sqlite3_malloc() or sqlite3_realloc() has failed in a case where the
100 /// memory being allocated was required in order to continue the operation.
101 NoMem = 7,
102 /// The SQLITE_READONLY result code is returned when an attempt is made to alter
103 /// some data for which the current database connection does not have write permission.
104 Readonly = 8,
105 /// The SQLITE_INTERRUPT result code indicates that an operation was interrupted
106 /// by the sqlite3_interrupt() interface. See also: SQLITE_ABORT
107 Interrupt = 9,
108 /// The SQLITE_IOERR result code says that the operation could not finish because
109 /// the operating system reported an I/O error.
110 ///
111 /// A full disk drive will normally give an SQLITE_FULL
112 /// error rather than an SQLITE_IOERR error.
113 ///
114 /// There are many different extended result codes for I/O errors that
115 /// identify the specific I/O operation that failed.
116 IoErr = 10,
117 /// The SQLITE_CORRUPT result code indicates that the database file has been
118 /// corrupted.See the How To Corrupt Your Database Files for further discussion
119 /// on how corruption can occur.
120 Corrupt = 11,
121 /// The SQLITE_NOTFOUND result code is exposed in three ways:
122 /// 1. SQLITE_NOTFOUND can be returned by the sqlite3_file_control() interface
123 /// to indicate that the file control opcode passed as the third argument
124 /// was not recognized by the underlying VFS.
125 ///
126 /// 2. SQLITE_NOTFOUND can also be returned by the xSetSystemCall() method
127 /// of an sqlite3_vfs object.
128 ///
129 /// 3. SQLITE_NOTFOUND an be returned by sqlite3_vtab_rhs_value() to
130 /// indicate that the right-hand operand of a constraint is not
131 /// available to the xBestIndex method that made the call.
132 ///
133 /// The SQLITE_NOTFOUND result code is also used internally by the SQLite
134 /// implementation, but those internal uses are not exposed to the application.
135 NotFound = 12,
136 /// The SQLITE_FULL result code indicates that a write could not complete
137 /// because the disk is full. Note that this error can occur when trying
138 /// to write information into the main database file, or it can also occur
139 /// when writing into temporary disk files.
140 ///
141 /// Sometimes applications encounter this error even though there is an
142 /// abundance of primary disk space because the error occurs when writing
143 /// into temporary disk files on a system where temporary files are stored
144 /// on a separate partition with much less space that the primary disk.
145 Full = 13,
146 /// The SQLITE_CANTOPEN result code indicates that SQLite was unable to
147 /// open a file. The file in question might be a primary database file
148 /// or one of several temporary disk files.
149 CantOpen = 14,
150 /// The SQLITE_PROTOCOL result code indicates a problem with the file
151 /// locking protocol used by SQLite. The SQLITE_PROTOCOL error is
152 /// currently only returned when using WAL mode and attempting to
153 /// start a new transaction. There is a race condition that can occur
154 /// when two separate database connections both try to start a transaction
155 /// at the same time in WAL mode. The loser of the race backs off and
156 /// tries again, after a brief delay. If the same connection loses the
157 /// locking race dozens of times over a span of multiple seconds, it will
158 /// eventually give up and return SQLITE_PROTOCOL. The SQLITE_PROTOCOL
159 /// error should appear in practice very, very rarely, and only when
160 /// there are many separate processes all competing intensely to write
161 /// to the same database.
162 Protocol = 15,
163 /// The SQLITE_EMPTY result code is not currently used.
164 Empty = 16,
165 /// The SQLITE_SCHEMA result code indicates that the database schema has
166 /// changed. This result code can be returned from sqlite3_step() for a
167 /// prepared statement that was generated using sqlite3_prepare() or
168 /// sqlite3_prepare16(). If the database schema was changed by some other
169 /// process in between the time that the statement was prepared and the
170 /// time the statement was run, this error can result.
171 ///
172 /// If a prepared statement is generated from sqlite3_prepare_v2() then
173 /// the statement is automatically re-prepared if the schema changes,
174 /// up to SQLITE_MAX_SCHEMA_RETRY times (default: 50). The sqlite3_step()
175 /// interface will only return SQLITE_SCHEMA back to the application if
176 /// the failure persists after these many retries.
177 Schema = 17,
178 /// The SQLITE_TOOBIG error code indicates that a string or BLOB was too
179 /// large. The default maximum length of a string or BLOB in SQLite is
180 /// 1,000,000,000 bytes. This maximum length can be changed at compile-time
181 /// using the SQLITE_MAX_LENGTH compile-time option, or at run-time using
182 /// the sqlite3_limit(db,SQLITE_LIMIT_LENGTH,...) interface. The SQLITE_TOOBIG
183 /// error results when SQLite encounters a string or BLOB that exceeds the
184 /// compile-time or run-time limit.
185 ///
186 /// The SQLITE_TOOBIG error code can also result when an oversized SQL statement
187 /// is passed into one of the sqlite3_prepare_v2() interfaces. The maximum
188 /// length of an SQL statement defaults to a much smaller value of 1,000,000,000
189 /// bytes. The maximum SQL statement length can be set at compile-time using
190 /// SQLITE_MAX_SQL_LENGTH or at run-time using sqlite3_limit(db,SQLITE_LIMIT_SQL_LENGTH,...).
191 TooBig = 18,
192 /// The SQLITE_CONSTRAINT error code means that an SQL constraint violation
193 /// occurred while trying to process an SQL statement. Additional information
194 /// about the failed constraint can be found by consulting the accompanying
195 /// error message (returned via sqlite3_errmsg() or sqlite3_errmsg16())
196 /// or by looking at the extended error code.
197 ///
198 /// The SQLITE_CONSTRAINT code can also be used as the return value from the
199 /// xBestIndex() method of a virtual table implementation. When xBestIndex()
200 /// returns SQLITE_CONSTRAINT, that indicates that the particular combination
201 /// of inputs submitted to xBestIndex() cannot result in a usable query plan
202 /// and should not be given further consideration.
203 Constrait = 19,
204 /// The SQLITE_MISMATCH error code indicates a datatype mismatch.
205 ///
206 /// SQLite is normally very forgiving about mismatches between the type
207 /// of a value and the declared type of the container in which that value
208 /// is to be stored. For example, SQLite allows the application to store
209 /// a large BLOB in a column with a declared type of BOOLEAN. But in a
210 /// few cases, SQLite is strict about types. The SQLITE_MISMATCH error is
211 /// returned in those few cases when the types do not match.
212 ///
213 /// The rowid of a table must be an integer. Attempt to set the rowid to
214 /// anything other than an integer (or a NULL which will be automatically
215 /// converted into the next available integer rowid) results in an SQLITE_MISMATCH
216 /// error.
217 MisMatch = 20,
218 /// The SQLITE_MISUSE return code might be returned if the application uses any
219 /// SQLite interface in a way that is undefined or unsupported. For example,
220 /// using a prepared statement after that prepared statement has been finalized
221 /// might result in an SQLITE_MISUSE error.
222 ///
223 /// SQLite tries to detect misuse and report the misuse using this result
224 /// code. However, there is no guarantee that the detection of misuse will
225 /// be successful. Misuse detection is probabilistic. Applications should
226 /// never depend on an SQLITE_MISUSE return value.
227 ///
228 /// If SQLite ever returns SQLITE_MISUSE from any interface, that means
229 /// that the application is incorrectly coded and needs to be fixed. Do
230 /// not ship an application that sometimes returns SQLITE_MISUSE from a
231 /// standard SQLite interface because that application contains potentially
232 /// serious bugs.
233 Misuse = 21,
234 /// The SQLITE_NOLFS error can be returned on systems that do not support
235 /// large files when the database grows to be larger than what the filesystem
236 /// can handle. "NOLFS" stands for "NO Large File Support".
237 NoLfs = 22,
238 /// The SQLITE_AUTH error is returned when the authorizer callback indicates
239 /// that an SQL statement being prepared is not authorized.
240 Auth = 23,
241 /// The SQLITE_FORMAT error code is not currently used by SQLite.
242 Format = 24,
243 /// The SQLITE_RANGE error indices that the parameter number argument to one
244 /// of the sqlite3_bind routines or the column number in one of the
245 /// sqlite3_column routines is out of range.
246 Range = 25,
247 /// When attempting to open a file, the SQLITE_NOTADB error indicates that
248 /// the file being opened does not appear to be an SQLite database file.
249 NotADB = 26,
250 /// The SQLITE_NOTICE result code is not returned by any C/C++ interface.
251 /// However, SQLITE_NOTICE (or rather one of its extended error codes) is
252 /// sometimes used as the first argument in an sqlite3_log() callback to
253 /// indicate that an unusual operation is taking place.
254 Notice = 27,
255 /// The SQLITE_WARNING result code is not returned by any C/C++ interface.
256 /// However, SQLITE_WARNING (or rather one of its extended error codes) is
257 /// sometimes used as the first argument in an sqlite3_log() callback to
258 /// indicate that an unusual and possibly ill-advised operation is taking
259 /// place.
260 Warning = 28,
261}
262
263impl From<i32> for SqlitePrimaryResult {
264 fn from(value: i32) -> Self {
265 match value {
266 0 => Self::Ok,
267 1 => Self::Error,
268 2 => Self::Internal,
269 3 => Self::Perm,
270 4 => Self::Abort,
271 5 => Self::Busy,
272 6 => Self::Locked,
273 7 => Self::NoMem,
274 8 => Self::Readonly,
275 9 => Self::Interrupt,
276 10 => Self::IoErr,
277 11 => Self::Corrupt,
278 12 => Self::NotFound,
279 13 => Self::Full,
280 14 => Self::CantOpen,
281 15 => Self::Protocol,
282 16 => Self::Empty,
283 17 => Self::Schema,
284 18 => Self::TooBig,
285 19 => Self::Constrait,
286 20 => Self::MisMatch,
287 21 => Self::Misuse,
288 22 => Self::NoLfs,
289 23 => Self::Auth,
290 24 => Self::Format,
291 25 => Self::Range,
292 26 => Self::NotADB,
293 27 => Self::Notice,
294 28 => Self::Warning,
295 other_id => Self::Other(other_id),
296 }
297 }
298}
299
300/// Binder of sqlite3 from C source
301#[repr(C)]
302#[derive(Copy, Clone)]
303pub(crate) struct sqlite3 {
304 __: [u8; 0],
305}
306
307/// Binder of sqlite3_stmt from C source
308#[repr(C)]
309#[derive(Copy, Clone)]
310pub struct sqlite3_stmt {
311 __: [u8; 0],
312}
313
314/// Binder of SQLITE_NULL from C source
315pub(crate) const COLUMN_NULL: u32 = 5;
316
317#[inline(always)]
318pub fn sqlite_transient() -> Option<unsafe extern "C" fn(lifetime: *mut os::raw::c_void)> {
319 Some(unsafe { mem::transmute(-1_isize) })
320}
321
322#[inline(always)]
323pub fn sqlite_static() -> Option<unsafe extern "C" fn(lifetime: *mut os::raw::c_void)> {
324 None
325}
326
327extern "C" {
328 pub(crate) fn sqlite3_open(
329 file_path: *const os::raw::c_char,
330 db: *mut *mut sqlite3,
331 ) -> os::raw::c_int;
332
333 pub(crate) fn sqlite3_close(db: *mut sqlite3) -> os::raw::c_int;
334
335 pub(crate) fn sqlite3_exec(
336 db: *mut sqlite3,
337 sql_statement: *const os::raw::c_char,
338 callback: Option<
339 unsafe extern "C" fn(
340 a: *mut os::raw::c_void,
341 b: os::raw::c_int,
342 c: *mut *mut os::raw::c_char,
343 d: *mut *mut os::raw::c_char,
344 ) -> os::raw::c_int,
345 >,
346 callback_a: *mut os::raw::c_void,
347 errmsg: *mut *mut os::raw::c_char,
348 ) -> os::raw::c_int;
349
350 pub(crate) fn sqlite3_prepare_v2(
351 db: *mut sqlite3,
352 sql_statement: *const os::raw::c_char,
353 n_byte: os::raw::c_int,
354 pp_stmt: *mut *mut sqlite3_stmt,
355 pz_tail: *mut *const os::raw::c_char,
356 ) -> os::raw::c_int;
357
358 pub(crate) fn sqlite3_step(stmt: *mut sqlite3_stmt) -> os::raw::c_int;
359
360 pub(crate) fn sqlite3_finalize(smtm: *mut sqlite3_stmt) -> os::raw::c_int;
361
362 pub(crate) fn sqlite3_column_blob(
363 smtm: *mut sqlite3_stmt,
364 col_index: os::raw::c_int,
365 ) -> *const os::raw::c_void;
366
367 pub(crate) fn sqlite3_column_double(smtm: *mut sqlite3_stmt, col_index: os::raw::c_int) -> f64;
368
369 pub(crate) fn sqlite3_column_text(
370 stmt: *mut sqlite3_stmt,
371 col_index: os::raw::c_int,
372 ) -> *const os::raw::c_uchar;
373
374 pub(crate) fn sqlite3_column_int64(
375 stmt: *mut sqlite3_stmt,
376 col_index: os::raw::c_int,
377 ) -> os::raw::c_longlong;
378
379 pub(crate) fn sqlite3_column_bytes(
380 stmt: *mut sqlite3_stmt,
381 col_index: os::raw::c_int,
382 ) -> os::raw::c_int;
383
384 pub fn sqlite3_bind_blob(
385 stmt: *mut sqlite3_stmt,
386 col_index: os::raw::c_int,
387 val: *const os::raw::c_void,
388 val_bytes: os::raw::c_int,
389 val_lifetime: Option<unsafe extern "C" fn(lifetime: *mut os::raw::c_void)>,
390 ) -> os::raw::c_int;
391
392 pub fn sqlite3_bind_zeroblob64(
393 stmt: *mut sqlite3_stmt,
394 col_index: os::raw::c_int,
395 val: os::raw::c_ulonglong,
396 ) -> os::raw::c_int;
397
398 pub fn sqlite3_bind_double(
399 stmt: *mut sqlite3_stmt,
400 col_index: os::raw::c_int,
401 val: f64,
402 ) -> os::raw::c_int;
403
404 pub fn sqlite3_bind_text(
405 stmt: *mut sqlite3_stmt,
406 col_index: os::raw::c_int,
407 val: *const os::raw::c_char,
408 val_bytes: os::raw::c_int,
409 val_lifetime: Option<unsafe extern "C" fn(lifetime: *mut os::raw::c_void)>,
410 ) -> os::raw::c_int;
411
412 pub fn sqlite3_bind_int64(
413 stmt: *mut sqlite3_stmt,
414 col_index: os::raw::c_int,
415 val: os::raw::c_longlong,
416 ) -> os::raw::c_int;
417
418 pub fn sqlite3_bind_null(stmt: *mut sqlite3_stmt, col_index: os::raw::c_int) -> os::raw::c_int;
419
420 pub fn sqlite3_column_type(
421 stmt: *mut sqlite3_stmt,
422 col_index: os::raw::c_int,
423 ) -> os::raw::c_int;
424}