sqlx_core_oldapi/sqlite/connection/
function.rs

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::ffi::{c_char, CString};
use std::os::raw::{c_int, c_void};
use std::sync::Arc;

use libsqlite3_sys::{
    sqlite3_context, sqlite3_create_function_v2, sqlite3_result_blob, sqlite3_result_double,
    sqlite3_result_error, sqlite3_result_int, sqlite3_result_int64, sqlite3_result_null,
    sqlite3_result_text, sqlite3_user_data, sqlite3_value, sqlite3_value_type,
    SQLITE_DETERMINISTIC, SQLITE_DIRECTONLY, SQLITE_OK, SQLITE_TRANSIENT, SQLITE_UTF8,
};

use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::{BoxDynError, Error};
use crate::sqlite::type_info::DataType;
use crate::sqlite::Sqlite;
use crate::sqlite::SqliteArgumentValue;
use crate::sqlite::SqliteTypeInfo;
use crate::sqlite::SqliteValue;
use crate::sqlite::{connection::handle::ConnectionHandle, SqliteError};
use crate::value::Value;

pub trait SqliteCallable: Send + Sync {
    unsafe fn call_boxed_closure(
        &self,
        ctx: *mut sqlite3_context,
        argc: c_int,
        argv: *mut *mut sqlite3_value,
    );
    // number of arguments
    fn arg_count(&self) -> i32;
}

pub struct SqliteFunctionCtx {
    ctx: *mut sqlite3_context,
    argument_values: Vec<SqliteValue>,
}

impl SqliteFunctionCtx {
    /// Creates a new `SqliteFunctionCtx` from the given raw SQLite function context.
    /// The context is used to access the arguments passed to the function.
    /// Safety: the context must be valid and argc must be the number of arguments passed to the function.
    unsafe fn new(ctx: *mut sqlite3_context, argc: c_int, argv: *mut *mut sqlite3_value) -> Self {
        let count = usize::try_from(argc).expect("invalid argument count");
        let argument_values = (0..count)
            .map(|i| {
                let raw = *argv.add(i);
                let data_type_code = sqlite3_value_type(raw);
                let value_type_info = SqliteTypeInfo(DataType::from_code(data_type_code));
                SqliteValue::new(raw, value_type_info)
            })
            .collect::<Vec<_>>();
        Self {
            ctx,
            argument_values,
        }
    }

    /// Returns the argument at the given index, or panics if the argument number is out of bounds or
    /// the argument cannot be decoded as the requested type.
    pub fn get_arg<'q, T: Decode<'q, Sqlite>>(&'q self, index: usize) -> T {
        self.try_get_arg::<T>(index)
            .expect("invalid argument index")
    }

    /// Returns the argument at the given index, or `None` if the argument number is out of bounds or
    /// the argument cannot be decoded as the requested type.
    pub fn try_get_arg<'q, T: Decode<'q, Sqlite>>(
        &'q self,
        index: usize,
    ) -> Result<T, BoxDynError> {
        if let Some(value) = self.argument_values.get(index) {
            let value_ref = value.as_ref();
            T::decode(value_ref)
        } else {
            Err("invalid argument index".into())
        }
    }

    pub fn set_result<'q, R: Encode<'q, Sqlite>>(&self, result: R) {
        unsafe {
            let mut arg_buffer: Vec<SqliteArgumentValue<'q>> = Vec::with_capacity(1);
            if let IsNull::Yes = result.encode(&mut arg_buffer) {
                sqlite3_result_null(self.ctx);
            } else {
                let arg = arg_buffer.pop().unwrap();
                match arg {
                    SqliteArgumentValue::Null => {
                        sqlite3_result_null(self.ctx);
                    }
                    SqliteArgumentValue::Text(text) => {
                        sqlite3_result_text(
                            self.ctx,
                            text.as_ptr() as *const c_char,
                            text.len() as c_int,
                            SQLITE_TRANSIENT(),
                        );
                    }
                    SqliteArgumentValue::Blob(blob) => {
                        sqlite3_result_blob(
                            self.ctx,
                            blob.as_ptr() as *const c_void,
                            blob.len() as c_int,
                            SQLITE_TRANSIENT(),
                        );
                    }
                    SqliteArgumentValue::Double(double) => {
                        sqlite3_result_double(self.ctx, double);
                    }
                    SqliteArgumentValue::Int(int) => {
                        sqlite3_result_int(self.ctx, int);
                    }
                    SqliteArgumentValue::Int64(int64) => {
                        sqlite3_result_int64(self.ctx, int64);
                    }
                }
            }
        }
    }

    pub fn set_error(&self, error_str: &str) {
        let error_str = CString::new(error_str).expect("invalid error string");
        unsafe {
            sqlite3_result_error(
                self.ctx,
                error_str.as_ptr(),
                error_str.as_bytes().len() as c_int,
            );
        }
    }
}

impl<F: Fn(&SqliteFunctionCtx) + Send + Sync> SqliteCallable for F {
    unsafe fn call_boxed_closure(
        &self,
        ctx: *mut sqlite3_context,
        argc: c_int,
        argv: *mut *mut sqlite3_value,
    ) {
        let ctx = SqliteFunctionCtx::new(ctx, argc, argv);
        (*self)(&ctx);
    }
    fn arg_count(&self) -> i32 {
        -1
    }
}

#[derive(Clone)]
pub struct Function {
    name: CString,
    func: Arc<dyn SqliteCallable>,
    /// the function always returns the same result given the same inputs
    pub deterministic: bool,
    /// the function may only be invoked from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in schema structures such as CHECK constraints, DEFAULT clauses, expression indexes, partial indexes, or generated columns.
    pub direct_only: bool,
    call:
        unsafe extern "C" fn(ctx: *mut sqlite3_context, argc: c_int, argv: *mut *mut sqlite3_value),
}

impl std::fmt::Debug for Function {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Function")
            .field("name", &self.name)
            .field("deterministic", &self.deterministic)
            .finish_non_exhaustive()
    }
}

impl Function {
    pub fn new<N, F>(name: N, func: F) -> Self
    where
        N: Into<Vec<u8>>,
        F: SqliteCallable + Send + Sync + 'static,
    {
        Function {
            name: CString::new(name).expect("invalid function name"),
            func: Arc::new(func),
            deterministic: false,
            direct_only: false,
            call: call_boxed_closure::<F>,
        }
    }

    pub(crate) fn create(&self, handle: &mut ConnectionHandle) -> Result<(), Error> {
        let raw_f = Arc::into_raw(Arc::clone(&self.func));
        let r = unsafe {
            sqlite3_create_function_v2(
                handle.as_ptr(),
                self.name.as_ptr(),
                self.func.arg_count(), // number of arguments
                self.sqlite_flags(),
                raw_f as *mut c_void,
                Some(self.call),
                None, // no step function for scalar functions
                None, // no final function for scalar functions
                None, // no need to free the function
            )
        };

        if r == SQLITE_OK {
            Ok(())
        } else {
            Err(Error::Database(Box::new(SqliteError::new(handle.as_ptr()))))
        }
    }

    fn sqlite_flags(&self) -> c_int {
        let mut flags = SQLITE_UTF8;
        if self.deterministic {
            flags |= SQLITE_DETERMINISTIC;
        }
        if self.direct_only {
            flags |= SQLITE_DIRECTONLY;
        }
        flags
    }

    pub fn deterministic(mut self) -> Self {
        self.deterministic = true;
        self
    }

    pub fn direct_only(mut self) -> Self {
        self.direct_only = true;
        self
    }
}

unsafe extern "C" fn call_boxed_closure<F: SqliteCallable>(
    ctx: *mut sqlite3_context,
    argc: c_int,
    argv: *mut *mut sqlite3_value,
) {
    let data = sqlite3_user_data(ctx);
    let boxed_f: *mut F = data as *mut F;
    debug_assert!(!boxed_f.is_null());
    let expected_argc = (*boxed_f).arg_count();
    debug_assert!(expected_argc == -1 || argc == expected_argc);
    (*boxed_f).call_boxed_closure(ctx, argc, argv);
}