1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::ffi::CString;
use std::ptr;
use std::ptr::NonNull;
use crate::error::Error;
use libsqlite3_sys::{sqlite3, sqlite3_close, sqlite3_exec, sqlite3_last_insert_rowid, SQLITE_OK};
use crate::sqlite::SqliteError;
#[derive(Debug)]
pub(crate) struct ConnectionHandle(NonNull<sqlite3>);
#[derive(Clone, Debug)]
pub(crate) struct ConnectionHandleRaw(NonNull<sqlite3>);
unsafe impl Send for ConnectionHandle {}
unsafe impl Send for ConnectionHandleRaw {}
impl ConnectionHandle {
#[inline]
pub(super) unsafe fn new(ptr: *mut sqlite3) -> Self {
Self(NonNull::new_unchecked(ptr))
}
#[inline]
pub(crate) fn as_ptr(&self) -> *mut sqlite3 {
self.0.as_ptr()
}
pub(crate) fn as_non_null_ptr(&self) -> NonNull<sqlite3> {
self.0
}
#[inline]
pub(crate) fn to_raw(&self) -> ConnectionHandleRaw {
ConnectionHandleRaw(self.0)
}
pub(crate) fn last_insert_rowid(&mut self) -> i64 {
unsafe { sqlite3_last_insert_rowid(self.as_ptr()) }
}
pub(crate) fn exec(&mut self, query: impl Into<String>) -> Result<(), Error> {
let query = query.into();
let query = CString::new(query).map_err(|_| err_protocol!("query contains nul bytes"))?;
unsafe {
let status = sqlite3_exec(
self.as_ptr(),
query.as_ptr(),
None,
ptr::null_mut(),
ptr::null_mut(),
);
if status == SQLITE_OK {
Ok(())
} else {
Err(SqliteError::new(self.as_ptr()).into())
}
}
}
}
impl ConnectionHandleRaw {
pub(crate) fn as_ptr(&self) -> *mut sqlite3 {
self.0.as_ptr()
}
}
impl Drop for ConnectionHandle {
fn drop(&mut self) {
unsafe {
let status = sqlite3_close(self.0.as_ptr());
if status != SQLITE_OK {
panic!("{}", SqliteError::new(self.0.as_ptr()));
}
}
}
}