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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
extern crate libsqlite3_sys as ffi;
use std::ffi::{CStr, CString};
use std::io::{stderr, Write};
use std::os::raw as libc;
use std::ptr::NonNull;
use std::{mem, ptr, slice, str};
use super::serialized_value::SerializedValue;
use result::Error::DatabaseError;
use result::*;
#[allow(missing_debug_implementations, missing_copy_implementations)]
pub struct RawConnection {
pub(crate) internal_connection: NonNull<ffi::sqlite3>,
}
impl RawConnection {
pub fn establish(database_url: &str) -> ConnectionResult<Self> {
let mut conn_pointer = ptr::null_mut();
let database_url = CString::new(database_url)?;
let connection_status =
unsafe { ffi::sqlite3_open(database_url.as_ptr(), &mut conn_pointer) };
match connection_status {
ffi::SQLITE_OK => {
let conn_pointer = unsafe { NonNull::new_unchecked(conn_pointer) };
Ok(RawConnection {
internal_connection: conn_pointer,
})
}
err_code => {
let message = super::error_message(err_code);
Err(ConnectionError::BadConnection(message.into()))
}
}
}
pub fn exec(&self, query: &str) -> QueryResult<()> {
let mut err_msg = ptr::null_mut();
let query = CString::new(query)?;
let callback_fn = None;
let callback_arg = ptr::null_mut();
unsafe {
ffi::sqlite3_exec(
self.internal_connection.as_ptr(),
query.as_ptr(),
callback_fn,
callback_arg,
&mut err_msg,
);
}
if err_msg.is_null() {
Ok(())
} else {
let msg = convert_to_string_and_free(err_msg);
let error_kind = DatabaseErrorKind::__Unknown;
Err(DatabaseError(error_kind, Box::new(msg)))
}
}
pub fn rows_affected_by_last_query(&self) -> usize {
unsafe { ffi::sqlite3_changes(self.internal_connection.as_ptr()) as usize }
}
pub fn register_sql_function<F>(
&self,
fn_name: &str,
num_args: usize,
deterministic: bool,
f: F,
) -> QueryResult<()>
where
F: FnMut(&Self, &[*mut ffi::sqlite3_value]) -> QueryResult<SerializedValue>
+ Send
+ 'static,
{
let fn_name = CString::new(fn_name)?;
let mut flags = ffi::SQLITE_UTF8;
if deterministic {
flags |= ffi::SQLITE_DETERMINISTIC;
}
let callback_fn = Box::into_raw(Box::new(f));
let result = unsafe {
ffi::sqlite3_create_function_v2(
self.internal_connection.as_ptr(),
fn_name.as_ptr(),
num_args as _,
flags,
callback_fn as *mut _,
Some(run_custom_function::<F>),
None,
None,
Some(destroy_boxed_fn::<F>),
)
};
if result == ffi::SQLITE_OK {
Ok(())
} else {
let error_message = super::error_message(result);
Err(DatabaseError(
DatabaseErrorKind::__Unknown,
Box::new(error_message.to_string()),
))
}
}
}
impl Drop for RawConnection {
fn drop(&mut self) {
use std::thread::panicking;
let close_result = unsafe { ffi::sqlite3_close(self.internal_connection.as_ptr()) };
if close_result != ffi::SQLITE_OK {
let error_message = super::error_message(close_result);
if panicking() {
write!(
stderr(),
"Error closing SQLite connection: {}",
error_message
)
.expect("Error writing to `stderr`");
} else {
panic!("Error closing SQLite connection: {}", error_message);
}
}
}
}
fn convert_to_string_and_free(err_msg: *const libc::c_char) -> String {
let msg = unsafe {
let bytes = CStr::from_ptr(err_msg).to_bytes();
str::from_utf8_unchecked(bytes).into()
};
unsafe { ffi::sqlite3_free(err_msg as *mut libc::c_void) };
msg
}
#[allow(warnings)]
extern "C" fn run_custom_function<F>(
ctx: *mut ffi::sqlite3_context,
num_args: libc::c_int,
value_ptr: *mut *mut ffi::sqlite3_value,
) where
F: FnMut(&RawConnection, &[*mut ffi::sqlite3_value]) -> QueryResult<SerializedValue>
+ Send
+ 'static,
{
static NULL_DATA_ERR: &str = "An unknown error occurred. sqlite3_user_data returned a null pointer. This should never happen.";
static NULL_CONN_ERR: &str = "An unknown error occurred. sqlite3_context_db_handle returned a null pointer. This should never happen.";
unsafe {
let data_ptr = ffi::sqlite3_user_data(ctx);
let data_ptr = data_ptr as *mut F;
let f = match data_ptr.as_mut() {
Some(f) => f,
None => {
ffi::sqlite3_result_error(
ctx,
NULL_DATA_ERR.as_ptr() as *const _ as *const _,
NULL_DATA_ERR.len() as _,
);
return;
}
};
let args = slice::from_raw_parts(value_ptr, num_args as _);
let conn = match NonNull::new(ffi::sqlite3_context_db_handle(ctx)) {
Some(conn) => RawConnection {
internal_connection: conn,
},
None => {
ffi::sqlite3_result_error(
ctx,
NULL_DATA_ERR.as_ptr() as *const _ as *const _,
NULL_DATA_ERR.len() as _,
);
return;
}
};
match f(&conn, args) {
Ok(value) => value.result_of(ctx),
Err(e) => {
let msg = e.to_string();
ffi::sqlite3_result_error(ctx, msg.as_ptr() as *const _, msg.len() as _);
}
}
mem::forget(conn);
}
}
extern "C" fn destroy_boxed_fn<F>(data: *mut libc::c_void)
where
F: FnMut(&RawConnection, &[*mut ffi::sqlite3_value]) -> QueryResult<SerializedValue>
+ Send
+ 'static,
{
let ptr = data as *mut F;
unsafe { Box::from_raw(ptr) };
}