sqlite3_ext 0.2.0

Build loadable extensions for SQLite using Rust
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use crate::{ffi, sqlite3_match_version, sqlite3_require_version, types::*, value::*};
use std::{ffi::CStr, ptr};

/// Information about a query plan.
///
/// This struct contains all of the information about a query plan that SQLite is attempting on
/// the virtual table. The struct will be passed to
/// [VTab::best_index](super::VTab::best_index).
///
/// This struct is both an input and an output. The virtual table implementation should examine
/// the [constraints](Self::constraints) and [order_by](Self::order_by) fields, decide on the
/// best query plan, and then set the results using [IndexInfoConstraint::set_argv_index],
/// [set_estimated_cost](Self::set_estimated_cost), and the other methods.
#[repr(transparent)]
pub struct IndexInfo {
    base: ffi::sqlite3_index_info,
}

impl IndexInfo {
    pub fn constraints(&self) -> IndexInfoConstraintIterator<'_> {
        IndexInfoConstraintIterator::new(self)
    }

    pub fn order_by(&self) -> IndexInfoOrderByIterator<'_> {
        IndexInfoOrderByIterator::new(self)
    }

    /// Get the "distinct mode" of the query.
    ///
    /// Requires SQLite 3.38.0. On earlier versions, this function will always return
    /// [DistinctMode::Ordered].
    pub fn distinct_mode(&self) -> DistinctMode {
        sqlite3_match_version! {
            3_038_000 => {
                let ret = unsafe { ffi::sqlite3_vtab_distinct(&self.base as *const _ as _) };
                DistinctMode(ret)
            },
            _ => DistinctMode(0),
        }
    }

    /// Retrieve the value previously set by
    /// [set_index_num](Self::set_index_num).
    pub fn index_num(&self) -> i32 {
        self.base.idxNum
    }

    /// Set the index number of this query plan. This is an arbitrary value which will be
    /// passed to [VTabCursor::filter](super::VTabCursor::filter).
    pub fn set_index_num(&mut self, val: i32) {
        self.base.idxNum = val;
    }

    /// Retrieve the value previously set by
    /// [set_index_str](Self::set_index_str).
    pub fn index_str(&self) -> Option<&str> {
        if self.base.idxStr.is_null() {
            None
        } else {
            let cstr = unsafe { CStr::from_ptr(self.base.idxStr) };
            cstr.to_str().ok()
        }
    }

    /// Set the index string of this query plan. This is an arbitrary value which will be
    /// passed to [VTabCursor::filter](super::VTabCursor::filter).
    ///
    /// This function can fail if SQLite is not able to allocate memory for the string.
    pub fn set_index_str(&mut self, val: Option<&str>) -> Result<()> {
        if self.base.needToFreeIdxStr != 0 {
            unsafe { ffi::sqlite3_free(self.base.idxStr as _) };
        }
        match val {
            None => {
                self.base.idxStr = ptr::null_mut();
                self.base.needToFreeIdxStr = 0;
            }
            Some(x) => {
                self.base.idxStr = ffi::str_to_sqlite3(x)?;
                self.base.needToFreeIdxStr = 1;
            }
        }
        Ok(())
    }

    /// Set the index string without copying.
    pub fn set_index_str_static(&mut self, val: &'static CStr) {
        if self.base.needToFreeIdxStr != 0 {
            unsafe { ffi::sqlite3_free(self.base.idxStr as _) };
        }
        self.base.idxStr = val.as_ptr() as _;
        self.base.needToFreeIdxStr = 0;
    }

    /// Retrieve the value previously set by
    /// [set_order_by_consumed](Self::set_order_by_consumed).
    pub fn order_by_consumed(&self) -> bool {
        self.base.orderByConsumed != 0
    }

    /// Indicate that the virtual table orders all returned rows according to the
    /// [order_by](Self::order_by) fields.
    ///
    /// If this is the case, then SQLite can omit reordering the results of the query, which may
    /// improve performance. It is never necessary to use the order_by information, but virtual
    /// tables may opt to use it as a performance optimization.
    ///
    /// The virtual table may, if desired, additionally use the
    /// [distinct_mode](Self::distinct_mode) method to find out if ordering requirements are
    /// relaxed by the particular query.
    pub fn set_order_by_consumed(&mut self, val: bool) {
        self.base.orderByConsumed = val as _;
    }

    /// Retrieve the value previously set by
    /// [set_estimated_cost](Self::set_estimated_cost).
    pub fn estimated_cost(&self) -> f64 {
        self.base.estimatedCost
    }

    pub fn set_estimated_cost(&mut self, val: f64) {
        self.base.estimatedCost = val;
    }

    /// Retrieve the value previously set by
    /// [set_estimated_rows](Self::set_estimated_rows).
    ///
    /// Requires SQLite 3.8.2.
    pub fn estimated_rows(&self) -> Result<i64> {
        sqlite3_require_version!(3_008_002, Ok(self.base.estimatedRows))
    }

    /// Requires SQLite 3.8.2. On earlier versions of SQLite, this function is a harmless
    /// no-op.
    pub fn set_estimated_rows(&mut self, val: i64) {
        let _ = val;
        sqlite3_match_version! {
            3_008_220 => self.base.estimatedRows = val,
            _ => (),
        }
    }

    /// Retrieve the value previously set by
    /// [set_scan_flags](Self::set_scan_flags).
    ///
    /// Requires SQLite 3.9.0.
    pub fn scan_flags(&self) -> Result<usize> {
        sqlite3_require_version!(3_009_000, Ok(self.base.idxFlags as _))
    }

    /// Requires SQLite 3.9.0. On earlier versions of SQLite, this function is a harmless
    /// no-op.
    pub fn set_scan_flags(&mut self, val: usize) {
        let _ = val;
        sqlite3_match_version! {
            3_009_000 => self.base.idxFlags = val as _,
            _ => (),
        }
    }

    /// Requires SQLite 3.10.0.
    pub fn columns_used(&self) -> Result<u64> {
        sqlite3_require_version!(3_010_000, Ok(self.base.colUsed))
    }
}

#[derive(Copy, Clone)]
pub struct IndexInfoConstraint<'a> {
    index_info: &'a IndexInfo,
    position: usize,
}

impl IndexInfoConstraint<'_> {
    fn constraint(&self) -> &ffi::sqlite3_index_info_sqlite3_index_constraint {
        unsafe { &*self.index_info.base.aConstraint.add(self.position) }
    }

    fn usage(&self) -> &ffi::sqlite3_index_info_sqlite3_index_constraint_usage {
        unsafe { &mut *self.index_info.base.aConstraintUsage.add(self.position) }
    }

    fn usage_mut(&mut self) -> &mut ffi::sqlite3_index_info_sqlite3_index_constraint_usage {
        unsafe { &mut *self.index_info.base.aConstraintUsage.add(self.position) }
    }

    /// Return the column being constrained. The value is a 0-based index of columns as declared by
    /// [connect](super::VTab::connect) / [create](super::CreateVTab::create). The rowid column is
    /// index -1.
    pub fn column(&self) -> i32 {
        self.constraint().iColumn as _
    }

    /// Return the type of constraint.
    pub fn op(&self) -> ConstraintOp {
        ConstraintOp::from_sqlite(self.constraint().op)
    }

    /// [IndexInfo::constraints] contains information about all constraints that apply to
    /// the virtual table, but some of the constraints might not be usable because of the
    /// way tables are ordered in a join. The best_index method must therefore only
    /// consider constraints that for which this method returns true.
    pub fn usable(&self) -> bool {
        self.constraint().usable != 0
    }

    /// Returns the right-hand side of the constraint.
    ///
    /// This routine attempts to retrieve the value of the right-hand operand of the constraint if
    /// that operand is known. If the operand is not known, then Err([SQLITE_NOTFOUND]) is returned.
    /// This method can return another error type if something goes wrong.
    ///
    /// This method is usually only successful if the right-hand operand of a constraint is a
    /// literal value in the original SQL statement. If the right-hand operand is an expression or
    /// a reference to some other column or a host parameter, then this method will probably return
    /// Err(SQLITE_NOTFOUND).
    ///
    /// Some constraints, such as [ConstraintOp::IsNull], have no right-hand operand. For such
    /// constraints, this method always returns Err(SQLITE_NOTFOUND).
    ///
    /// Requires SQLite 3.38.0. On earlier versions of SQLite, Err(SQLITE_NOTFOUND) is always
    /// returned.
    pub fn rhs(&self) -> Result<&ValueRef> {
        sqlite3_match_version! {
            3_038_000 => unsafe {
                let mut ret: *mut ffi::sqlite3_value = ptr::null_mut();
                Error::from_sqlite(ffi::sqlite3_vtab_rhs_value(
                    &self.index_info.base as *const _ as _,
                    self.position as _,
                    &mut ret,
                ))?;
                Ok(&*(ret as *const crate::value::ValueRef))
            },
            _ => Err(SQLITE_NOTFOUND),
        }
    }

    /// Return the collation to use for text comparisons on this column.
    ///
    /// See [the SQLite documentation](https://www.sqlite.org/c3ref/vtab_collation.html)
    /// for more details.
    pub fn collation(&self) -> Result<&str> {
        sqlite3_require_version!(3_022_000, {
            let ret = unsafe {
                CStr::from_ptr(ffi::sqlite3_vtab_collation(
                    &self.index_info.base as *const _ as _,
                    self.position as _,
                ))
            };
            Ok(ret.to_str()?)
        })
    }

    /// Retrieve the value previously set using [set_argv_index](Self::set_argv_index).
    pub fn argv_index(&self) -> Option<u32> {
        match self.usage().argvIndex {
            0 => None,
            x => Some((x - 1) as _),
        }
    }

    /// Set the desired index for [filter](super::VTabCursor::filter)'s argv.
    ///
    /// Exactly one entry in the IndexInfo should be set to 0, another to 1, another to 2,
    /// and so forth up to as many or as few as the best_index method wants. The EXPR of
    /// the corresponding constraints will then be passed in as the argv[] parameters to
    /// filter.
    pub fn set_argv_index(&mut self, idx: Option<u32>) {
        self.usage_mut().argvIndex = match idx {
            None => 0,
            Some(i) => (i + 1) as _,
        };
    }

    /// Retrieve the value previously set by [set_omit](Self::set_omit).
    pub fn omit(&self) -> bool {
        self.usage().omit != 0
    }

    /// Advise SQLite that this constraint is validated by the virtual table
    /// implementation. SQLite may skip performing its own check in some cases. It is
    /// generally a hint and not a requirement, but a notable exception is for
    /// [ConstraintOp::Offset], which is always honored. See [the SQLite
    /// documentation](https://www.sqlite.org/vtab.html#omit_constraint_checking_in_bytecode)
    /// for more details.
    pub fn set_omit(&mut self, val: bool) {
        self.usage_mut().omit = val as _;
    }

    /// Check if all values in this IN constraint are able to be processed simultaneously.
    /// If this method returns true, then a call to
    /// [set_value_list_wanted](Self::set_value_list_wanted) would also return true.
    ///
    /// Requires SQLite 3.38.0. On earlier versions, this function will always return
    /// false.
    pub fn value_list_available(&self) -> bool {
        sqlite3_match_version! {
            3_038_000 => unsafe {
                ffi::sqlite3_vtab_in(
                    &self.index_info.base as *const _ as _,
                    self.position as _,
                    -1,
                ) != 0
            },
            _ => false,
        }
    }

    /// Instruct SQLite to return all values in an IN constraint simultaneously.
    ///
    /// A constraint on a virtual table in the form of "column IN (...)" is communicated to
    /// [VTab::best_index](super::VTab::best_index) as a [ConstraintOp::Eq] constraint. If
    /// the virtual table wants to use this constraint, it must use
    /// [set_argv_index](Self::set_argv_index) to assign the constraint to an argument.
    /// Then, SQLite will invoke [VTabCursor::filter](super::VTabCursor::filter) once for
    /// each value on the right-hand side of the IN operator. Thus, the virtual table only
    /// sees a single value from the right-hand side of the IN operator at a time.
    ///
    /// In some cases, however, it would be advantageous for the virtual table to see all values on the right-hand of the IN operator all at once. This method enables this feature.
    ///
    /// Calling this method with true will request [ValueList](crate::ValueList)
    /// processing. In order for ValueList processing to work:
    ///
    /// 1. this constraint must be assigned an argv index using
    ///    [set_argv_index](Self::set_argv_index);
    /// 2. this method is called with true; and
    /// 3. SQLite is able to provide all values simultaneously.
    ///
    /// If all of these criteria are met, then the corresponding argument passed to
    /// [VTabCursor::filter](super::VTabCursor::filter) will appear to be SQL NULL, but
    /// accessible using [ValueList](crate::ValueList). If this facility is requested but
    /// this method returns false, then VTabCursor::filter will be invoked multiple times
    /// with each different value of the constraint, as normal.
    ///
    /// This method always returns the same value that
    /// [value_list_available](Self::value_list_available) would. Calling this method with
    /// false cancels a previous request for a ValueList.
    ///
    /// See [the SQLite documentation](https://www.sqlite.org/c3ref/vtab_in.html) for more
    /// details.
    ///
    /// Requires SQLite 3.38.0. On earlier versions, this function will always return
    /// false.
    pub fn set_value_list_wanted(&mut self, val: bool) -> bool {
        let _ = val;
        sqlite3_match_version! {
            3_038_000 => unsafe {
                ffi::sqlite3_vtab_in(
                    &self.index_info.base as *const _ as _,
                    self.position as _,
                    if val { 1 } else { 0 },
                ) != 0
            },
            _ => false,
        }
    }
}

#[derive(Copy, Clone)]
pub struct IndexInfoOrderBy<'a> {
    index_info: &'a IndexInfo,
    position: usize,
}

impl IndexInfoOrderBy<'_> {
    fn base(&self) -> &ffi::sqlite3_index_info_sqlite3_index_orderby {
        unsafe { &*self.index_info.base.aOrderBy.add(self.position) }
    }

    pub fn column(&self) -> i32 {
        self.base().iColumn as _
    }

    pub fn desc(&self) -> bool {
        self.base().desc != 0
    }
}

macro_rules! make_iterator(
    ($t:ident, $n:ident) => {
        paste::paste! {
            pub struct [<$t Iterator>]<'a> {
                current: $t<'a>,
            }

            impl<'a> [<$t Iterator>]<'a> {
                fn new(index_info: &'a IndexInfo) -> Self {
                    Self {
                        current: $t {
                            index_info,
                            position: usize::MAX,
                        },
                    }
                }
            }

            impl<'a> Iterator for [<$t Iterator>]<'a> {
                type Item = $t<'a>;

                fn next(&mut self) -> Option<Self::Item> {
                    let pos = self.current.position.wrapping_add(1);
                    if pos < self.current.index_info.base.$n as usize {
                        self.current.position = pos;
                        Some(self.current)
                    } else {
                        None
                    }
                }

                fn size_hint(&self) -> (usize, Option<usize>) {
                    let remaining = self.current.index_info.base.$n as usize
                        - self.current.position.wrapping_add(1);
                    (remaining, Some(remaining))
                }
            }
        }
    }
);

make_iterator!(IndexInfoConstraint, nConstraint);
make_iterator!(IndexInfoOrderBy, nOrderBy);

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum ConstraintOp {
    Eq,
    GT,
    LE,
    LT,
    GE,
    Match,
    Like,         /* 3.10.0 and later */
    Glob,         /* 3.10.0 and later */
    Regexp,       /* 3.10.0 and later */
    NE,           /* 3.21.0 and later */
    IsNot,        /* 3.21.0 and later */
    IsNotNull,    /* 3.21.0 and later */
    IsNull,       /* 3.21.0 and later */
    Is,           /* 3.21.0 and later */
    Limit,        /* 3.38.0 and later */
    Offset,       /* 3.38.0 and later */
    Function(u8), /* 3.25.0 and later */
}

impl ConstraintOp {
    pub(crate) fn assert_valid_function_constraint(&self) {
        if let ConstraintOp::Function(val) = *self {
            if val >= 150 {
                return;
            }
        }
        panic!("invalid function constraint")
    }

    fn from_sqlite(val: u8) -> ConstraintOp {
        match val as _ {
            2 => ConstraintOp::Eq,
            4 => ConstraintOp::GT,
            8 => ConstraintOp::LE,
            16 => ConstraintOp::LT,
            32 => ConstraintOp::GE,
            64 => ConstraintOp::Match,
            65 => ConstraintOp::Like,
            66 => ConstraintOp::Glob,
            67 => ConstraintOp::Regexp,
            68 => ConstraintOp::NE,
            69 => ConstraintOp::IsNot,
            70 => ConstraintOp::IsNotNull,
            71 => ConstraintOp::IsNull,
            72 => ConstraintOp::Is,
            73 => ConstraintOp::Limit,
            74 => ConstraintOp::Offset,
            150..=255 => ConstraintOp::Function(val),
            _ => panic!("invalid constraint op"),
        }
    }
}

/// Describes the requirements of the virtual table query.
///
/// This value is retured by [IndexInfo::distinct_mode]. It provides the virtual table
/// implementation with additional information about how the query planner wants the output to be
/// returned. If the virtual table implementation can meet the requirements, then it may consume
/// the [order_by](IndexInfo::order_by) fields using [IndexInfo::set_order_by_consumed].
///
/// For the purposes of comparing virtual table output values to see if the values are same
/// value for sorting purposes, two NULL values are considered to be the same. In other words,
/// the comparison operator is "IS" (or "IS NOT DISTINCT FROM") and not "==".
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Copy)]
pub struct DistinctMode(pub i32);

impl DistinctMode {
    /// Indicates that sqlite doesn't need complete ordering from the virtual table.
    ///
    /// If this method returns true, then sqlite still expects that all rows with the same value in
    /// all of the [order_by](IndexInfo::order_by) columns are adjacent to one another. If this is
    /// not achievable, then it is not valid to consume the order_by columns.
    ///
    /// This returns true for GROUP BY and DISTINCT queries.
    pub fn may_return_unordered(&self) -> bool {
        self.0 == 1 || self.0 == 2
    }

    /// Indicates that sqlite doesn't need duplicated rows to be included in the result set.
    ///
    /// This returns true for DISTINCT queries.
    pub fn may_omit_duplicates(&self) -> bool {
        self.0 == 2 || self.0 == 3
    }
}

impl From<i32> for DistinctMode {
    fn from(val: i32) -> Self {
        Self(val)
    }
}

impl std::fmt::Debug for IndexInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        let mut ds = f.debug_struct("IndexInfo");
        ds.field(
            "constraints",
            &self.constraints().collect::<Vec<_>>().as_slice(),
        )
        .field("order_by", &self.order_by().collect::<Vec<_>>().as_slice())
        .field("index_num", &self.index_num())
        .field("index_str", &self.index_str())
        .field("order_by_consumed", &self.order_by_consumed())
        .field("estimated_cost", &self.estimated_cost());
        self.estimated_rows()
            .map(|v| ds.field("estimated_rows", &v))
            .ok();
        self.scan_flags().map(|v| ds.field("scan_flags", &v)).ok();
        self.columns_used()
            .map(|v| ds.field("columns_used", &v))
            .ok();
        ds.finish()
    }
}

impl std::fmt::Debug for IndexInfoConstraint<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        let mut ds = f.debug_struct("IndexInfoConstraint");
        ds.field("column", &self.column())
            .field("op", &self.op())
            .field("usable", &self.usable());
        sqlite3_match_version! {
            3_038_000 => {
                ds.field("rhs", &self.rhs());
            }
            _ => (),
        }
        sqlite3_match_version! {
            3_022_000 => {
                ds.field("collation", &self.collation());
            }
            _ => (),
        }
        ds.field("argv_index", &self.argv_index())
            .field("omit", &self.omit())
            .finish()
    }
}

impl std::fmt::Debug for IndexInfoOrderBy<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        f.debug_struct("IndexInfoOrderBy")
            .field("column", &self.column())
            .field("desc", &self.desc())
            .finish()
    }
}

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

    #[test]
    fn distinct_mode() {
        assert!(!DistinctMode(0).may_return_unordered());
        assert!(!DistinctMode(0).may_omit_duplicates());
        assert!(DistinctMode(1).may_return_unordered());
        assert!(!DistinctMode(1).may_omit_duplicates());
        assert!(DistinctMode(2).may_return_unordered());
        assert!(DistinctMode(2).may_omit_duplicates());
        assert!(!DistinctMode(3).may_return_unordered());
        assert!(DistinctMode(3).may_omit_duplicates());
    }
}