stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Parameter binding for SQL queries
//!
//! This module provides ergonomic parameter passing for SQL queries,
//! similar to rusqlite's `params!` macro.
//!
//! # Examples
//!
//! ```ignore
//! use stoolap::{Database, params, named_params};
//!
//! let db = Database::open("memory://")?;
//!
//! // Using params! macro (positional)
//! db.execute("INSERT INTO users VALUES ($1, $2, $3)", params![1, "Alice", 30])?;
//!
//! // Using tuple syntax (positional)
//! db.execute("INSERT INTO users VALUES ($1, $2)", (1, "Alice"))?;
//!
//! // Using named_params! macro
//! db.execute_named(
//!     "INSERT INTO users VALUES (:id, :name, :age)",
//!     named_params!{ id: 1, name: "Alice", age: 30 }
//! )?;
//! ```

use rustc_hash::FxHashMap;
use std::collections::HashMap;
use std::sync::Arc;

use chrono::{DateTime, Utc};
use smallvec::SmallVec;

use crate::common::SmartString;
use crate::core::Value;

/// Type alias for parameter vectors.
/// Uses SmallVec to avoid heap allocation for queries with ≤8 parameters (the common case).
pub type ParamVec = SmallVec<[Value; 8]>;

/// Trait for types that can be converted to SQL parameters
///
/// This trait is automatically implemented for common Rust types.
/// It enables the `params!` macro and tuple parameter syntax.
pub trait ToParam {
    /// Convert self into a Value for SQL parameter binding
    fn to_param(&self) -> Value;
}

// Implement ToParam for common types

impl ToParam for i64 {
    fn to_param(&self) -> Value {
        Value::Integer(*self)
    }
}

impl ToParam for i32 {
    fn to_param(&self) -> Value {
        Value::Integer(*self as i64)
    }
}

impl ToParam for i16 {
    fn to_param(&self) -> Value {
        Value::Integer(*self as i64)
    }
}

impl ToParam for i8 {
    fn to_param(&self) -> Value {
        Value::Integer(*self as i64)
    }
}

impl ToParam for u32 {
    fn to_param(&self) -> Value {
        Value::Integer(*self as i64)
    }
}

impl ToParam for u16 {
    fn to_param(&self) -> Value {
        Value::Integer(*self as i64)
    }
}

impl ToParam for u8 {
    fn to_param(&self) -> Value {
        Value::Integer(*self as i64)
    }
}

impl ToParam for usize {
    fn to_param(&self) -> Value {
        Value::Integer(*self as i64)
    }
}

impl ToParam for f64 {
    fn to_param(&self) -> Value {
        Value::Float(*self)
    }
}

impl ToParam for f32 {
    fn to_param(&self) -> Value {
        Value::Float(*self as f64)
    }
}

impl ToParam for bool {
    fn to_param(&self) -> Value {
        Value::Boolean(*self)
    }
}

impl ToParam for String {
    fn to_param(&self) -> Value {
        Value::Text(SmartString::new(self))
    }
}

impl ToParam for &str {
    fn to_param(&self) -> Value {
        Value::Text(SmartString::from(*self))
    }
}

impl ToParam for Arc<str> {
    fn to_param(&self) -> Value {
        Value::Text(SmartString::from(self.as_ref()))
    }
}

impl ToParam for DateTime<Utc> {
    fn to_param(&self) -> Value {
        Value::Timestamp(*self)
    }
}

impl ToParam for Value {
    fn to_param(&self) -> Value {
        self.clone()
    }
}

impl<T: ToParam> ToParam for Option<T> {
    fn to_param(&self) -> Value {
        match self {
            Some(v) => v.to_param(),
            None => Value::null_unknown(),
        }
    }
}

impl<T: ToParam> ToParam for &T {
    fn to_param(&self) -> Value {
        (*self).to_param()
    }
}

/// Trait for collections of parameters
///
/// This enables passing tuples, arrays, and slices as parameters.
pub trait Params {
    /// Convert into a ParamVec of Values.
    /// Uses SmallVec to avoid heap allocation for ≤4 parameters.
    fn into_params(self) -> ParamVec;
}

// Empty params
impl Params for () {
    fn into_params(self) -> ParamVec {
        ParamVec::new()
    }
}

// Slice of Value
impl Params for &[Value] {
    fn into_params(self) -> ParamVec {
        self.iter().cloned().collect()
    }
}

// Vec of Value
impl Params for Vec<Value> {
    fn into_params(self) -> ParamVec {
        self.into_iter().collect()
    }
}

// ParamVec (SmallVec) — zero-cost passthrough, no conversion needed
impl Params for ParamVec {
    fn into_params(self) -> ParamVec {
        self
    }
}

// Array of Value
impl<const N: usize> Params for [Value; N] {
    fn into_params(self) -> ParamVec {
        self.into_iter().collect()
    }
}

// Tuple implementations for 1-12 elements
macro_rules! impl_params_for_tuple {
    ($($idx:tt: $T:ident),+) => {
        impl<$($T: ToParam),+> Params for ($($T,)+) {
            fn into_params(self) -> ParamVec {
                smallvec::smallvec![$(self.$idx.to_param()),+]
            }
        }
    };
}

impl_params_for_tuple!(0: T0);
impl_params_for_tuple!(0: T0, 1: T1);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3, 4: T4);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5, 6: T6);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5, 6: T6, 7: T7);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5, 6: T6, 7: T7, 8: T8);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5, 6: T6, 7: T7, 8: T8, 9: T9);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5, 6: T6, 7: T7, 8: T8, 9: T9, 10: T10);
impl_params_for_tuple!(0: T0, 1: T1, 2: T2, 3: T3, 4: T4, 5: T5, 6: T6, 7: T7, 8: T8, 9: T9, 10: T10, 11: T11);

/// Create a parameter list for SQL queries
///
/// This macro provides a convenient way to create parameter lists without
/// manually wrapping each value in `Value::from()`.
///
/// # Examples
///
/// ```ignore
/// use stoolap::{Database, params};
///
/// let db = Database::open("memory://")?;
/// db.execute("CREATE TABLE users (id INTEGER, name TEXT, age INTEGER)", ())?;
///
/// // Insert with params
/// db.execute(
///     "INSERT INTO users VALUES ($1, $2, $3)",
///     params![1, "Alice", 30]
/// )?;
///
/// // Query with params
/// let rows = db.query(
///     "SELECT * FROM users WHERE age > $1",
///     params![25]
/// )?;
///
/// // Mixed types work seamlessly
/// db.execute(
///     "INSERT INTO users VALUES ($1, $2, $3)",
///     params![2, String::from("Bob"), 25]
/// )?;
/// ```
#[macro_export]
macro_rules! params {
    () => {
        ()
    };
    ($($param:expr),+ $(,)?) => {
        ($($param,)+)
    };
}

/// Named parameters for SQL queries
///
/// This struct holds named parameter bindings that can be used with
/// the `:name` syntax in SQL queries.
///
/// # Examples
///
/// ```ignore
/// use stoolap::{Database, NamedParams, named_params};
///
/// let db = Database::open("memory://")?;
/// db.execute("CREATE TABLE users (id INTEGER, name TEXT)", ())?;
///
/// // Using the named_params! macro
/// db.execute_named(
///     "INSERT INTO users VALUES (:id, :name)",
///     named_params!{ id: 1, name: "Alice" }
/// )?;
///
/// // Building NamedParams manually
/// let params = NamedParams::new()
///     .add("id", 2)
///     .add("name", "Bob");
/// db.execute_named("INSERT INTO users VALUES (:id, :name)", params)?;
/// ```
#[derive(Debug, Clone, Default)]
pub struct NamedParams {
    params: FxHashMap<String, Value>,
}

impl NamedParams {
    /// Create empty named params
    pub fn new() -> Self {
        Self {
            params: FxHashMap::default(),
        }
    }

    /// Add a named parameter (builder style)
    pub fn add<T: ToParam>(mut self, name: impl Into<String>, value: T) -> Self {
        self.params.insert(name.into(), value.to_param());
        self
    }

    /// Insert a named parameter
    pub fn insert<T: ToParam>(&mut self, name: impl Into<String>, value: T) {
        self.params.insert(name.into(), value.to_param());
    }

    /// Get the underlying FxHashMap
    pub fn into_inner(self) -> FxHashMap<String, Value> {
        self.params
    }

    /// Get a reference to the underlying FxHashMap
    pub fn as_map(&self) -> &FxHashMap<String, Value> {
        &self.params
    }
}

impl From<HashMap<String, Value>> for NamedParams {
    fn from(params: HashMap<String, Value>) -> Self {
        Self {
            params: params.into_iter().collect(),
        }
    }
}

/// Create named parameters for SQL queries
///
/// This macro provides a convenient way to create named parameter bindings
/// for use with the `:name` syntax in SQL queries.
///
/// # Examples
///
/// ```ignore
/// use stoolap::{Database, named_params};
///
/// let db = Database::open("memory://")?;
/// db.execute("CREATE TABLE users (id INTEGER, name TEXT, active BOOLEAN)", ())?;
///
/// // Insert with named params
/// db.execute_named(
///     "INSERT INTO users VALUES (:id, :name, :active)",
///     named_params!{ id: 1, name: "Alice", active: true }
/// )?;
///
/// // Query with named params
/// let rows = db.query_named(
///     "SELECT * FROM users WHERE name = :name",
///     named_params!{ name: "Alice" }
/// )?;
/// ```
#[macro_export]
macro_rules! named_params {
    () => {
        $crate::NamedParams::new()
    };
    ($($name:ident : $value:expr),+ $(,)?) => {
        {
            let mut params = $crate::NamedParams::new();
            $(
                params.insert(stringify!($name), $value);
            )+
            params
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_to_param_integers() {
        assert_eq!(42i64.to_param(), Value::Integer(42));
        assert_eq!(42i32.to_param(), Value::Integer(42));
        assert_eq!(42i16.to_param(), Value::Integer(42));
        assert_eq!(42i8.to_param(), Value::Integer(42));
        assert_eq!(42u32.to_param(), Value::Integer(42));
        assert_eq!(42u16.to_param(), Value::Integer(42));
        assert_eq!(42u8.to_param(), Value::Integer(42));
    }

    #[test]
    fn test_to_param_floats() {
        assert_eq!(3.5f64.to_param(), Value::Float(3.5));
        assert_eq!(3.5f32.to_param(), Value::Float(3.5f32 as f64));
    }

    #[test]
    fn test_to_param_strings() {
        assert_eq!("hello".to_param(), Value::text("hello"));
        assert_eq!(String::from("world").to_param(), Value::text("world"));
    }

    #[test]
    fn test_to_param_bool() {
        assert_eq!(true.to_param(), Value::Boolean(true));
        assert_eq!(false.to_param(), Value::Boolean(false));
    }

    #[test]
    fn test_to_param_option() {
        assert_eq!(Some(42i64).to_param(), Value::Integer(42));
        assert!(Option::<i64>::None.to_param().is_null());
    }

    #[test]
    fn test_params_empty() {
        let params: ParamVec = ().into_params();
        assert!(params.is_empty());
    }

    #[test]
    fn test_params_tuple() {
        let params = (1i64, "hello", 3.5f64).into_params();
        assert_eq!(params.len(), 3);
        assert_eq!(params[0], Value::Integer(1));
        assert_eq!(params[1], Value::text("hello"));
        assert_eq!(params[2], Value::Float(3.5));
    }

    #[test]
    fn test_params_macro() {
        let p = params![1, "hello", 3.5];
        let params = p.into_params();
        assert_eq!(params.len(), 3);
        assert_eq!(params[0], Value::Integer(1));
        assert_eq!(params[1], Value::text("hello"));
        assert_eq!(params[2], Value::Float(3.5));
    }

    #[test]
    fn test_params_macro_empty() {
        let p = params![];
        let params: ParamVec = p.into_params();
        assert!(params.is_empty());
    }

    #[test]
    fn test_params_with_option() {
        let name: Option<&str> = Some("Alice");
        let age: Option<i32> = None;
        let params = (1i64, name, age).into_params();

        assert_eq!(params.len(), 3);
        assert_eq!(params[0], Value::Integer(1));
        assert_eq!(params[1], Value::text("Alice"));
        assert!(params[2].is_null());
    }

    #[test]
    fn test_params_from_param_vec() {
        // ParamVec -> ParamVec should be zero-cost identity
        let mut pv = ParamVec::new();
        pv.push(Value::Integer(1));
        pv.push(Value::text("hello"));
        pv.push(Value::Float(3.5));

        let result = pv.into_params();
        assert_eq!(result.len(), 3);
        assert_eq!(result[0], Value::Integer(1));
        assert_eq!(result[1], Value::text("hello"));
        assert_eq!(result[2], Value::Float(3.5));
    }

    #[test]
    fn test_params_from_empty_param_vec() {
        let pv = ParamVec::new();
        let result = pv.into_params();
        assert!(result.is_empty());
    }
}