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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

pub const SQLITE_NO_PARAM: &[SqlBindableRef] = &[];
pub type SqlBindableRef<'a> = &'a (dyn SqlBindable + 'a);
pub type SqlBindableBox<'a> = Box<dyn SqlBindable + 'a>;

pub struct SqliteConnection {
    info: SqliteConnectionInfo,
    connection: Mutex<Connection>,
    cached_statements: Mutex<StatementCache>,
}

impl MallocSizeOf for SqliteConnection {
    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { unimplemented!() }
}

pub struct SqliteConnectionInfo {
    pub readonly: bool,
    pub path: PathBuf,
    pub open_flags: OpenFlags,
}

type StatementCache = HashMap<String, ScopedStatement>;

impl Drop for SqliteConnection {
    fn drop(&mut self) {
        // Clear all associated statement otherwise the sqlite connection will
        // be left open because sqlite3_close returns BUSY.
        // https://www.sqlite.org/c3ref/close.html
        self.cached_statements.get_mut().clear();
        // We would like to check return value of sqlite close, and run close_v2
        // when necessary. After that we'd like to prevent Connection's drop
        // from running. To do so we open a new Connection and overwrite
        // self.connection.
        //
        // When we can't open a new connection successfully, give up and simply
        // let Connection close the db. However we lose the ability to
        // run close_v2() if close fails.
        unsafe {
            if let Ok(new_connection) = Connection::open_with_flags(
                self.info.path.clone(),
                Self::default_open_flags().set_read_write(),
            ) {
                self.connection.get_mut().remove_busy_handler().ok();
                if self.close().is_err() {
                    error!(
                        "Closing sqlite connection while still being used.
                         The sqlite connection will be closed when all pending
                         resources are released. However it suggests that the
                         code may not managing object ownership and lifetime
                         of sqlite execution well."
                    );
                    self.close_v2().ok();
                }

                std::ptr::write(self.connection.get_mut(), new_connection);
            }
        }
    }
}

unsafe impl Send for SqliteConnection {}
unsafe impl Sync for SqliteConnection {}

impl SqliteConnection {
    pub fn close(&mut self) -> Result<()> {
        match unsafe { sqlite_ffi::sqlite3_close(self.get_db_mut().as_raw()) } {
            sqlite_ffi::SQLITE_OK => Ok(()),
            code => bail!(sqlite::Error {
                code: Some(code as isize),
                message: None
            }),
        }
    }

    pub fn close_v2(&mut self) -> Result<()> {
        match unsafe {
            sqlite_ffi::sqlite3_close_v2(self.get_db_mut().as_raw())
        } {
            sqlite_ffi::SQLITE_OK => Ok(()),
            code => bail!(sqlite::Error {
                code: Some(code as isize),
                message: None
            }),
        }
    }

    pub fn default_open_flags() -> OpenFlags {
        // The sqlite library didn't provide a function to construct customary
        // open_flags.
        unsafe {
            std::mem::transmute::<i32, OpenFlags>(
                sqlite_ffi::SQLITE_OPEN_NOMUTEX
                    // TODO: check if SHARED_CACHE improves the performance or not.
                    | sqlite_ffi::SQLITE_OPEN_SHAREDCACHE
                    | sqlite_ffi::SQLITE_OPEN_URI,
            )
        }
    }

    /// If `unsafe_mode` is true, data loss or database corruption may happen if
    /// the process crashes, so it should only be used for write-once
    /// databases where an unfinished temporary database will be removed
    /// after process restart.
    pub fn create_and_init<P: AsRef<Path>>(
        path: P, unsafe_mode: bool,
    ) -> Result<()> {
        let conn = Connection::open_with_flags(
            &path,
            Self::default_open_flags().set_read_write().set_create(),
        )?;
        if unsafe_mode {
            conn.execute("PRAGMA journal_mode=OFF")?;
            conn.execute("PRAGMA synchronous=OFF")?;
        } else {
            conn.execute("PRAGMA journal_mode=WAL")?;
        }
        // Prevent other processes from accessing the db.
        // The "-shm" file will not be created,
        // see https://www.sqlite.org/tempfiles.html#shared_memory_files.
        conn.execute("PRAGMA locking_mode=EXCLUSIVE")?;
        Ok(())
    }

    pub fn create_and_open<P: AsRef<Path>>(
        path: P, open_flags: OpenFlags, unsafe_mode: bool,
    ) -> Result<Self> {
        Self::create_and_init(path.as_ref(), unsafe_mode)?;
        Self::open(path, false, open_flags)
    }

    pub fn open<P: AsRef<Path>>(
        path: P, readonly: bool, open_flags: OpenFlags,
    ) -> Result<Self> {
        let conn_open_flags = if readonly {
            open_flags.set_read_only()
        } else {
            open_flags.set_read_write()
        };

        Ok(Self {
            info: SqliteConnectionInfo {
                readonly,
                path: path.as_ref().to_path_buf(),
                open_flags,
            },
            connection: Mutex::new(Connection::open_with_flags(
                path,
                conn_open_flags,
            )?),
            cached_statements: Mutex::new(HashMap::new()),
        })
    }

    pub fn try_clone(&self) -> Result<Self> {
        Self::open(&self.info.path, self.info.readonly, self.info.open_flags)
    }

    pub fn prepare<'db>(
        db: &'db mut Connection, statement_cache: &'db mut StatementCache,
        sql: &str,
    ) -> Result<&'db mut ScopedStatement> {
        // Actually safe. I don't want an unnecessary to_string() for the sql.
        // But the borrow-checker doesn't seem to understand branch very well.
        Ok(unsafe {
            let maybe_statement = statement_cache
                .get_mut(sql)
                .map(|x| x as *mut ScopedStatement);
            if maybe_statement.is_some() {
                &mut *maybe_statement.unwrap()
            } else {
                statement_cache.entry(sql.to_string()).or_insert(
                    // This is safe because we store the
                    // ScopedStatement in the same struct where the
                    // connection is.
                    ScopedStatement::new(db.prepare(sql)?),
                )
            }
        })
    }

    /// The statement must be created with the db. Then the statement is a mut
    /// borrow of db, so it's guaranteed that the db is only used by one
    /// thread.
    pub fn execute_locked<'db, 'p, Param: Borrow<dyn SqlBindable + 'p>>(
        statement: &'db mut ScopedStatement, params: &[Param],
    ) -> Result<MaybeRows<'db>> {
        Ok(MaybeRows(statement.execute(params)?))
    }

    pub fn execute<'p, Param: Borrow<dyn SqlBindable + 'p>>(
        &mut self, sql: &str, params: &[Param],
    ) -> Result<MaybeRows<'_>> {
        let db = self.connection.get_mut();
        let statement =
            Self::prepare(db, self.cached_statements.get_mut(), sql)?;

        Self::execute_locked(statement, params)
    }

    pub fn get_db_mut(&mut self) -> &mut Connection {
        self.connection.get_mut()
    }

    pub fn lock_db(&self) -> MutexGuard<Connection> { self.connection.lock() }

    pub fn lock_statement_cache(&self) -> MutexGuard<StatementCache> {
        self.cached_statements.lock()
    }

    pub fn possible_temporary_files(db_path: &str) -> Vec<String> {
        let mut paths = vec![];
        paths.push(Self::wal_path(db_path));
        paths.push(Self::shm_path(db_path));

        paths
    }

    fn wal_path(db_path: &str) -> String { db_path.to_string() + "-wal" }

    fn shm_path(db_path: &str) -> String { db_path.to_string() + "-shm" }
}

/// Upstream didn't implement Bindable for trait object, which makes passing
/// an array of params impossible. Therefore we define a new trait and implement
/// Bindable for its trait object.
pub trait SqlBindable {
    fn bind(&self, statement: &mut Statement, i: usize) -> sqlite::Result<()>;
}

/// To implement SqlBindable for String, Vec<[u8]>, etc, whichever is Deref to a
/// Bindable.
pub trait SqlDerefBindable<'a> {
    type Type: Bindable;

    fn as_bindable(&'a self) -> Self::Type;
}

impl<'a, T: ?Sized + 'a + Deref> SqlDerefBindable<'a> for T
where &'a T::Target: Bindable
{
    type Type = &'a T::Target;

    fn as_bindable(&'a self) -> Self::Type { self.deref() }
}

impl SqlBindable for i64 {
    fn bind(&self, statement: &mut Statement, i: usize) -> sqlite::Result<()> {
        Bindable::bind(*self as i64, statement, i)
    }
}

impl<'a, T: 'a + Deref> SqlBindable for Pin<T>
where for<'x> &'x T::Target: Bindable
{
    fn bind(&self, statement: &mut Statement, i: usize) -> sqlite::Result<()> {
        Bindable::bind(&**self, statement, i)
    }
}

impl<'a, T: 'a + ?Sized> SqlBindable for &'a T
where T: SqlDerefBindable<'a>
{
    fn bind(&self, statement: &mut Statement, i: usize) -> sqlite::Result<()> {
        Bindable::bind(self.as_bindable(), statement, i)
    }
}

pub trait SqlReadable: SqlReadableIntoSelf + Sized {
    fn from_column(row: &Statement<'_>, column: usize) -> Result<Self>;
}

impl SqlReadable for Vec<u8> {
    fn from_column(row: &Statement<'_>, column: usize) -> Result<Self> {
        Ok(Self::read(row, column)?)
    }
}

impl SqlReadable for Box<[u8]> {
    fn from_column(row: &Statement<'_>, column: usize) -> Result<Self> {
        Ok(Vec::<u8>::read(row, column)?.into_boxed_slice())
    }
}

impl SqlReadable for i64 {
    fn from_column(row: &Statement<'_>, column: usize) -> Result<Self> {
        Ok(i64::read(row, column)?)
    }
}

/// This trait can be made into trait object.
pub trait SqlReadableIntoSelf {
    fn read_into_self(
        &mut self, row: &Statement<'_>, column: usize,
    ) -> Result<()>;
}

impl<T: SqlReadable> SqlReadableIntoSelf for T {
    fn read_into_self(
        &mut self, row: &Statement<'_>, column: usize,
    ) -> Result<()> {
        Ok(*self = Self::from_column(row, column)?)
    }
}

impl<'a> Bindable for &'a dyn SqlBindable {
    fn bind(self, statement: &mut Statement, i: usize) -> sqlite::Result<()> {
        self.bind(statement, i)
    }
}

type MaybeUnfinishedStatement<'db> = Option<&'db mut Statement<'db>>;

#[derive(Default)]
pub struct MaybeRows<'db>(MaybeUnfinishedStatement<'db>);

impl<'db> MaybeRows<'db> {
    pub fn finish_ignore_rows(&mut self) -> Result<()> {
        while Self::next(&mut self.0)?.is_some() {}
        Ok(())
    }

    pub fn map<Item, F: FnMut(&Statement<'db>) -> Item>(
        mut self, f: F,
    ) -> MappedRows<'db, F> {
        MappedRows {
            maybe_rows: self.0.take(),
            f,
        }
    }

    pub fn statement_ref<'a>(
        maybe_statement: &'a mut MaybeUnfinishedStatement<'db>,
    ) -> &'a Statement<'db> {
        maybe_statement.as_ref().unwrap()
    }

    pub fn next<'a>(
        maybe_statement: &'a mut MaybeUnfinishedStatement<'db>,
    ) -> Result<Option<&'a Statement<'db>>> {
        let state = match maybe_statement {
            None => return Ok(None),
            Some(statement) => statement.next()?,
        };

        match state {
            State::Row => Ok(Some(Self::statement_ref(maybe_statement))),
            State::Done => {
                *maybe_statement = None;
                Ok(None)
            }
        }
    }
}

pub struct ConnectionWithRowParser<Connection, RowParser>(
    pub Connection,
    pub RowParser,
);

pub struct MappedRows<'db, F> {
    /// If Rows is default constructible, we could remove the option and
    /// default construct it for the empty database.
    maybe_rows: MaybeUnfinishedStatement<'db>,
    f: F,
}

impl<'db, Item, F: FnMut(&Statement<'db>) -> Item> MappedRows<'db, F> {
    pub fn expect_one_row(&mut self) -> Result<Option<Item>> {
        if self.maybe_rows.is_none() {
            Ok(None)
        } else {
            let row_mapped =
                (self.f)(MaybeRows::statement_ref(&mut self.maybe_rows));
            if MaybeRows::next(&mut self.maybe_rows)?.is_none() {
                Ok(Some(row_mapped))
            } else {
                bail!(Error::DbValueError)
            }
        }
    }
}

impl<'db, Item, F: FnMut(&Statement<'db>) -> Result<Item>> FallibleIterator
    for MappedRows<'db, F>
{
    type Error = Error;
    type Item = Item;

    fn next(&mut self) -> Result<Option<Self::Item>> {
        if self.maybe_rows.is_none() {
            Ok(None)
        } else {
            let value =
                (self.f)(MaybeRows::statement_ref(&mut self.maybe_rows))?;
            MaybeRows::next(&mut self.maybe_rows)?;
            Ok(Some(value))
        }
    }
}

/// The ScopedStatement struct is a wrapper solely meant to store
/// cached statement object together with the connection because it's
/// otherwise too painful for the user to maintain especially in multi-threaded
/// environment.
///
/// The ScopedStatement should only be stored in the struct where the
/// corresponding sqlite Connection is.
///
/// The ScopedStatement should in general not be extended. Any more code added
/// should examined carefully so that the database connection can not be used in
/// more than one thread at the same time.
pub struct ScopedStatement {
    stmt: Statement<'static>,
}

impl ScopedStatement {
    /// This method is unsafe because it extended the lifetime of Statement to
    /// infinity.
    unsafe fn new<'a>(scoped_statement: Statement<'a>) -> Self {
        Self {
            stmt: std::mem::transmute::<Statement<'a>, Statement<'static>>(
                scoped_statement,
            ),
        }
    }

    fn as_mut(&mut self) -> &mut Statement<'_> {
        unsafe {
            std::mem::transmute::<&mut Statement<'static>, &mut Statement<'_>>(
                &mut self.stmt,
            )
        }
    }

    /// It is essential to have `&mut self` in order to execute. As required by
    /// sqlite "Multi-thread mode" https://sqlite.org/threadsafe.html, there can
    /// be only one active use of a database connection.
    ///
    /// Even binding an variable modifies the error field in database
    /// connection.
    ///
    /// We should never allow a mutable Statement returned from a
    /// SqliteConnection reference because then we can not prevent using
    /// database connection from multiple threads.
    fn bind<'p, Param: Borrow<dyn SqlBindable + 'p>>(
        &mut self, params: &[Param],
    ) -> Result<()> {
        self.stmt.reset().ok();
        for i in 0..params.len() {
            // Sqlite index starts at 1.
            self.stmt.bind(i + 1, params[i].borrow())?
        }
        Ok(())
    }

    fn execute<'p, Param: Borrow<dyn SqlBindable + 'p>>(
        &mut self, params: &[Param],
    ) -> Result<MaybeUnfinishedStatement<'_>> {
        self.bind(params)?;

        // FIXME: Should we wait for the first row to become available?
        let result = self.stmt.next();
        match result {
            Ok(State::Done) => Ok(None),
            Ok(State::Row) => Ok(Some(self.as_mut())),
            Err(e) => {
                bail!(e);
            }
        }
    }
}

pub trait ValueReadImpl<Kind: ?Sized>: Sized {
    fn from_row_impl(row: &Statement<'_>, value_column: usize) -> Result<Self>;
    fn from_kv_row_impl(
        row: &Statement<'_>, key: &mut dyn SqlReadableIntoSelf,
    ) -> Result<Self>;
}

pub trait ValueRead {
    type Kind: ?Sized;
}

impl ValueRead for () {
    type Kind = ();
}

impl ValueRead for Box<[u8]> {
    type Kind = dyn SqlReadableIntoSelf;
}

impl ValueReadImpl<()> for () {
    fn from_row_impl(
        _row: &Statement<'_>, _value_column: usize,
    ) -> Result<Self> {
        Ok(())
    }

    fn from_kv_row_impl(
        row: &Statement<'_>, key: &mut dyn SqlReadableIntoSelf,
    ) -> Result<Self> {
        key.read_into_self(row, 0)?;
        Ok(())
    }
}

impl<ValueType: SqlReadable> ValueReadImpl<dyn SqlReadableIntoSelf>
    for ValueType
{
    fn from_row_impl(row: &Statement<'_>, value_column: usize) -> Result<Self> {
        ValueType::from_column(row, value_column)
    }

    fn from_kv_row_impl(
        row: &Statement<'_>, key: &mut dyn SqlReadableIntoSelf,
    ) -> Result<Self> {
        key.read_into_self(row, 0)?;
        ValueType::from_column(row, 1)
    }
}

impl<
        ValueType: Default + TupleIndexExt + TupleIterate<dyn SqlReadableIntoSelf>,
    > ValueRead for ValueType
{
    type Kind = dyn ElementSatisfy<dyn SqlReadableIntoSelf>;
}

impl<
        ValueType: Default + TupleIndexExt + TupleIterate<dyn SqlReadableIntoSelf>,
    > ValueReadImpl<dyn ElementSatisfy<dyn SqlReadableIntoSelf>> for ValueType
{
    fn from_row_impl(
        row: &Statement<'_>, value_column: usize,
    ) -> Result<ValueType> {
        let mut result = Ok(ValueType::default());

        struct Load<'r, 'db, ValueType> {
            t: &'r mut ValueType,
            row: &'r Statement<'db>,
            error: Option<Error>,
            value_column: usize,
        }

        impl<ValueType: TupleIndexExt>
            IterCallFamilyTrait<ValueType, dyn SqlReadableIntoSelf>
            for &mut Load<'_, '_, ValueType>
        {
            fn iter_step<
                Index: OfElementSatisfiesOnTuple<ValueType, dyn SqlReadableIntoSelf>,
            >(
                &mut self, _: &'static Index, index: usize,
            ) {
                match self.error {
                    None => {
                        let column_read_result = ElementSatisfy::<
                            dyn SqlReadableIntoSelf,
                        >::to_constrain_object_mut(
                            Index::getter_for_tuple_mut(self.t).get_mut_impl(),
                        )
                        .read_into_self(self.row, index + self.value_column);

                        if column_read_result.is_err() {
                            self.error = column_read_result.err()
                        }
                    }
                    _ => { /* Skip */ }
                }
            }
        }

        let mut loader = Load {
            t: result.as_mut().unwrap(),
            row,
            error: None,
            value_column,
        };
        ValueType::iterate(&mut loader);

        match loader.error {
            Some(e) => bail!(e),
            None => result,
        }
    }

    fn from_kv_row_impl(
        row: &Statement<'_>, key: &mut dyn SqlReadableIntoSelf,
    ) -> Result<Self> {
        key.read_into_self(row, 0)?;
        Self::from_row_impl(row, 1)
    }
}

pub trait SqlBindableValue {
    type Kind: ?Sized;
}

impl SqlBindableValue for () {
    type Kind = ();
}

impl SqlBindableValue for [u8] {
    /// Use SqlBindable for all single element.
    type Kind = dyn SqlBindable;
}

impl SqlBindableValue for i64 {
    type Kind = dyn SqlBindable;
}

impl<
        ValueType: Default + TupleIndexExt + TupleIterate<dyn SqlReadableIntoSelf>,
    > SqlBindableValue for ValueType
{
    type Kind = dyn ElementSatisfy<dyn BindValueAppendImpl<dyn SqlBindable>>;
}

impl<
        ValueType: Default + TupleIndexExt + TupleIterate<dyn SqlReadableIntoSelf>,
    > DbValueType for ValueType
{
    type Type = ValueType;
}

/// This trait should be implemented for all types implement SqlBindableValue.
pub trait BindValueAppendImpl<Kind: ?Sized> {
    fn make_bind_list(&self) -> Vec<SqlBindableBox<'_>>;
}

// FIXME: Clone? Is it possible to impl it for &T?
impl<T: 'static + SqlBindableValue + SqlBindable + Clone>
    BindValueAppendImpl<dyn SqlBindable> for T
{
    fn make_bind_list(&self) -> Vec<SqlBindableBox<'_>> {
        vec![Box::new(self.clone())]
    }
}

impl BindValueAppendImpl<()> for () {
    fn make_bind_list(&self) -> Vec<Box<dyn SqlBindable>> { vec![] }
}

impl BindValueAppendImpl<dyn SqlBindable> for [u8] {
    fn make_bind_list(&self) -> Vec<SqlBindableBox<'_>> {
        vec![Box::new(Pin::new(self))]
    }
}

impl BindValueAppendImpl<dyn SqlBindable> for Box<[u8]> {
    fn make_bind_list(&self) -> Vec<SqlBindableBox<'_>> {
        vec![Box::new(Pin::new(self.deref()))]
    }
}

impl<
        ValueType: TupleIndexExt + TupleIterate<dyn BindValueAppendImpl<dyn SqlBindable>>,
    >
    BindValueAppendImpl<
        dyn ElementSatisfy<dyn BindValueAppendImpl<dyn SqlBindable>>,
    > for ValueType
{
    fn make_bind_list(&self) -> Vec<SqlBindableBox<'_>> {
        struct Append<'x, ValueType> {
            v: &'x ValueType,
            l: *mut Vec<SqlBindableBox<'x>>,
        }

        impl<'x, ValueType>
            IterCallFamilyTrait<
                ValueType,
                dyn BindValueAppendImpl<dyn SqlBindable>,
            > for Append<'x, ValueType>
        {
            // FIXME: we shoull note that, if Self contains a mut pointer of
            // FIXME: a Tuple, then iter_step can only return a
            // FIXME: contrain object of the current lifetime.
            // FIXME: we must find a way to make it possible to get an
            // FIXME: constrain_object from the same lifetime.
            fn iter_step<
                Index: OfElementSatisfiesOnTuple<
                    ValueType,
                    dyn BindValueAppendImpl<dyn SqlBindable>,
                >,
            >(
                &mut self, _: &'static Index, _: usize,
            ) {
                let mut to_append = ElementSatisfy::<
                    dyn BindValueAppendImpl<dyn SqlBindable>,
                >::to_constrain_object(
                    Index::getter_for_tuple(self.v).get_impl(),
                )
                .make_bind_list();
                unsafe { &mut *self.l }.append(&mut to_append);
            }
        }

        let mut bind_list =
            Vec::<SqlBindableBox<'_>>::with_capacity(ValueType::size_tuple());
        {
            let append = Append {
                v: self,
                l: &mut bind_list,
            };
            ValueType::iterate(append);
        }

        bind_list
    }
}

use super::super::{
    super::{storage_db::key_value_db::DbValueType, utils::tuple::*},
    errors::*,
};
use fallible_iterator::FallibleIterator;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use parking_lot::{Mutex, MutexGuard};
use sqlite::{Bindable, Connection, OpenFlags, Readable, State, Statement};
use sqlite3_sys as sqlite_ffi;
use std::{
    borrow::Borrow,
    collections::HashMap,
    ops::Deref,
    path::{Path, PathBuf},
    pin::Pin,
};