deno_node_sqlite 0.2.0

Node sqlite compatibility for Deno
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
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
// Copyright 2018-2026 the Deno authors. MIT license.

use std::cell::Cell;
use std::cell::RefCell;
use std::rc::Rc;

use deno_core::GarbageCollected;
use deno_core::ToV8;
use deno_core::op2;
use deno_core::v8;
use deno_core::v8::GetPropertyNamesArgs;
use deno_core::v8_static_strings;
use rusqlite::ffi;

use super::SqliteError;
use super::validators;

// ECMA-262, 15th edition, 21.1.2.6. Number.MAX_SAFE_INTEGER (2^53-1)
const MAX_SAFE_JS_INTEGER: i64 = 9007199254740991;

pub struct RunStatementResult {
  last_insert_rowid: i64,
  changes: u64,
  use_big_ints: bool,
}

impl<'a> ToV8<'a> for RunStatementResult {
  type Error = SqliteError;

  fn to_v8(
    self,
    scope: &mut v8::PinScope<'a, '_>,
  ) -> Result<v8::Local<'a, v8::Value>, SqliteError> {
    v8_static_strings! {
      LAST_INSERT_ROW_ID = "lastInsertRowid",
      CHANGES = "changes",
    }

    let obj = v8::Object::new(scope);

    let last_insert_row_id_str = LAST_INSERT_ROW_ID.v8_string(scope).unwrap();
    let last_insert_row_id = if self.use_big_ints {
      v8::BigInt::new_from_i64(scope, self.last_insert_rowid).into()
    } else {
      v8::Number::new(scope, self.last_insert_rowid as f64).into()
    };

    obj
      .set(scope, last_insert_row_id_str.into(), last_insert_row_id)
      .unwrap();

    let changes_str = CHANGES.v8_string(scope).unwrap();
    let changes = if self.use_big_ints {
      v8::BigInt::new_from_u64(scope, self.changes).into()
    } else {
      v8::Number::new(scope, self.changes as f64).into()
    };

    obj.set(scope, changes_str.into(), changes).unwrap();
    Ok(obj.into())
  }
}

pub type InnerStatementPtr = Rc<Cell<Option<*mut ffi::sqlite3_stmt>>>;

pub trait StatementExecution {
  fn stmt_ptr(&self) -> Result<*mut ffi::sqlite3_stmt, SqliteError>;
  fn step(&self) -> Result<bool, SqliteError>;
  fn return_arrays(&self) -> bool;
  fn use_big_ints(&self) -> bool;
  fn check_bind_result(&self, r: i32) -> Result<(), SqliteError>;

  fn column_count(&self) -> Result<i32, SqliteError> {
    let raw = self.stmt_ptr()?;
    // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
    // as it lives as long as the StatementSync instance.
    let count = unsafe { ffi::sqlite3_column_count(raw) };
    Ok(count)
  }

  fn column_name(&self, index: i32) -> Result<&[u8], SqliteError> {
    let raw = self.stmt_ptr()?;
    // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
    // as it lives as long as the StatementSync instance.
    unsafe {
      let name = ffi::sqlite3_column_name(raw, index);
      Ok(std::ffi::CStr::from_ptr(name as _).to_bytes())
    }
  }

  fn column_value<'a>(
    &self,
    index: i32,
    scope: &mut v8::PinScope<'a, '_>,
  ) -> Result<v8::Local<'a, v8::Value>, SqliteError> {
    let raw = self.stmt_ptr()?;
    let use_big_ints = self.use_big_ints();
    // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
    // as it lives as long as the statement instance.
    unsafe {
      Ok(match ffi::sqlite3_column_type(raw, index) {
        ffi::SQLITE_INTEGER => {
          let value = ffi::sqlite3_column_int64(raw, index);
          if use_big_ints {
            v8::BigInt::new_from_i64(scope, value).into()
          } else if value.abs() <= MAX_SAFE_JS_INTEGER {
            v8::Number::new(scope, value as f64).into()
          } else {
            return Err(SqliteError::NumberTooLarge(value));
          }
        }
        ffi::SQLITE_FLOAT => {
          let value = ffi::sqlite3_column_double(raw, index);
          v8::Number::new(scope, value).into()
        }
        ffi::SQLITE_TEXT => {
          let value = ffi::sqlite3_column_text(raw, index);
          let value = std::ffi::CStr::from_ptr(value as _);
          v8::String::new_from_utf8(
            scope,
            value.to_bytes(),
            v8::NewStringType::Normal,
          )
          .unwrap()
          .into()
        }
        ffi::SQLITE_BLOB => {
          let value = ffi::sqlite3_column_blob(raw, index);
          let size = ffi::sqlite3_column_bytes(raw, index);
          let ab = if size == 0 {
            v8::ArrayBuffer::new(scope, 0)
          } else {
            let value =
              std::slice::from_raw_parts(value as *const u8, size as usize);
            let bs =
              v8::ArrayBuffer::new_backing_store_from_vec(value.to_vec())
                .make_shared();
            v8::ArrayBuffer::with_backing_store(scope, &bs)
          };
          v8::Uint8Array::new(scope, ab, 0, size as _).unwrap().into()
        }
        ffi::SQLITE_NULL => v8::null(scope).into(),
        _ => v8::undefined(scope).into(),
      })
    }
  }

  fn bind_value(
    &self,
    scope: &mut v8::PinScope<'_, '_>,
    value: v8::Local<v8::Value>,
    index: i32,
  ) -> Result<(), SqliteError> {
    let raw = self.stmt_ptr()?;
    let r = if value.is_number() {
      let value = value.number_value(scope).unwrap();

      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      // as it lives as long as the statement instance.
      unsafe { ffi::sqlite3_bind_double(raw, index, value) }
    } else if value.is_string() {
      let value = value.to_rust_string_lossy(scope);

      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      // as it lives as long as the statement instance.
      //
      // SQLITE_TRANSIENT is used to indicate that SQLite should make a copy of the data.
      unsafe {
        ffi::sqlite3_bind_text(
          raw,
          index,
          value.as_ptr() as *const _,
          value.len() as i32,
          ffi::SQLITE_TRANSIENT(),
        )
      }
    } else if value.is_null() {
      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      // as it lives as long as the statement instance.
      unsafe { ffi::sqlite3_bind_null(raw, index) }
    } else if value.is_array_buffer_view() {
      let value: v8::Local<v8::ArrayBufferView> = value.try_into().unwrap();
      let mut data = value.data();
      let mut size = value.byte_length();

      // data may be NULL if length is 0 or ab is detached. we need to pass a valid pointer
      // to sqlite3_bind_blob, so we use a static empty array in this case.
      if data.is_null() {
        static EMPTY: [u8; 0] = [];

        data = EMPTY.as_ptr() as *mut _;
        size = 0;
      }

      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      // as it lives as long as the statement instance.
      //
      // SQLITE_TRANSIENT is used to indicate that SQLite should make a copy of the data.
      unsafe {
        ffi::sqlite3_bind_blob(
          raw,
          index,
          data,
          size as i32,
          ffi::SQLITE_TRANSIENT(),
        )
      }
    } else if value.is_big_int() {
      let value: v8::Local<v8::BigInt> = value.try_into().unwrap();
      let (as_int, lossless) = value.i64_value();
      if !lossless {
        return Err(SqliteError::InvalidBindValue(
          "BigInt value is too large to bind",
        ));
      }

      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      // as it lives as long as the statement instance.
      unsafe { ffi::sqlite3_bind_int64(raw, index, as_int) }
    } else {
      return Err(SqliteError::InvalidBindType(index));
    };

    self.check_bind_result(r)
  }

  fn read_row<'a>(
    &self,
    scope: &mut v8::PinScope<'a, '_>,
  ) -> Result<Option<v8::Local<'a, v8::Value>>, SqliteError> {
    if self.step()? {
      return Ok(None);
    }

    let num_cols = self.column_count()?;
    let mut names = Vec::with_capacity(num_cols as usize);
    let mut values = Vec::with_capacity(num_cols as usize);

    for i in 0..num_cols {
      let name = self.column_name(i)?;
      let value = self.column_value(i, scope)?;
      let name =
        v8::String::new_from_utf8(scope, name, v8::NewStringType::Normal)
          .unwrap()
          .into();
      names.push(name);
      values.push(value);
    }

    if self.return_arrays() {
      let result = v8::Array::new_with_elements(scope, &values);
      Ok(Some(result.into()))
    } else {
      let null = v8::null(scope).into();
      let result =
        v8::Object::with_prototype_and_properties(scope, null, &names, &values);
      Ok(Some(result.into()))
    }
  }
}

#[derive(Debug)]
pub struct StatementSync {
  pub inner: InnerStatementPtr,
  pub db: Rc<RefCell<Option<rusqlite::Connection>>>,
  pub statements: Rc<RefCell<Vec<InnerStatementPtr>>>,
  pub ignore_next_sqlite_error: Rc<Cell<bool>>,

  pub return_arrays: Cell<bool>,
  pub use_big_ints: Cell<bool>,
  pub allow_bare_named_params: Cell<bool>,
  pub allow_unknown_named_params: Cell<bool>,

  pub is_iter_finished: Cell<bool>,
}

impl Drop for StatementSync {
  fn drop(&mut self) {
    let mut statements = self.statements.borrow_mut();
    let mut finalized_stmt = None;

    if let Some(pos) = statements
      .iter()
      .position(|stmt| Rc::ptr_eq(stmt, &self.inner))
    {
      let stmt = statements.remove(pos);
      finalized_stmt = stmt.get();
      stmt.set(None);
    }

    if let Some(ptr) = finalized_stmt {
      // SAFETY: `ptr` is a valid pointer to a sqlite3_stmt.
      unsafe {
        ffi::sqlite3_finalize(ptr);
      }
    }
  }
}

pub(crate) fn check_error_code(
  r: i32,
  db: *mut ffi::sqlite3,
) -> Result<(), SqliteError> {
  if r != ffi::SQLITE_OK {
    // SAFETY: lifetime of the connection is guaranteed by reference
    // counting.
    let err_message = unsafe { ffi::sqlite3_errmsg(db) };

    if !err_message.is_null() {
      // SAFETY: `err_msg` is a valid pointer to a null-terminated string.
      let err_message = unsafe { std::ffi::CStr::from_ptr(err_message) }
        .to_string_lossy()
        .into_owned();
      // SAFETY: `err_str` is a valid pointer to a null-terminated string.
      let err_str = unsafe { std::ffi::CStr::from_ptr(ffi::sqlite3_errstr(r)) }
        .to_string_lossy()
        .into_owned();

      return Err(SqliteError::SqliteSysError {
        message: err_message,
        errcode: r as _,
        errstr: err_str,
      });
    }
  }

  Ok(())
}

pub(crate) fn check_error_code2(r: i32) -> Result<(), SqliteError> {
  // SAFETY: `ffi::sqlite3_errstr` is a valid function that returns a pointer to a null-terminated
  // string.
  let err_str = unsafe { std::ffi::CStr::from_ptr(ffi::sqlite3_errstr(r)) }
    .to_string_lossy()
    .into_owned();

  Err(SqliteError::SqliteSysError {
    message: err_str.clone(),
    errcode: r as _,
    errstr: err_str,
  })
}

// SAFETY: we're sure this can be GCed
unsafe impl GarbageCollected for StatementSync {
  fn trace(&self, _visitor: &mut deno_core::v8::cppgc::Visitor) {}

  fn get_name(&self) -> &'static std::ffi::CStr {
    c"StatementSync"
  }
}

impl StatementExecution for StatementSync {
  fn stmt_ptr(&self) -> Result<*mut ffi::sqlite3_stmt, SqliteError> {
    let ptr = self.inner.get();
    match ptr {
      Some(p) => Ok(p),
      None => Err(SqliteError::StatementFinalized),
    }
  }

  fn step(&self) -> Result<bool, SqliteError> {
    let raw = self.stmt_ptr()?;
    // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
    // as it lives as long as the StatementSync instance.
    unsafe {
      let r = ffi::sqlite3_step(raw);
      if r == ffi::SQLITE_DONE {
        return Ok(true);
      }
      if r != ffi::SQLITE_ROW {
        self.check_error_code_impl(r)?;
      }
    }

    Ok(false)
  }

  fn return_arrays(&self) -> bool {
    self.return_arrays.get()
  }

  fn use_big_ints(&self) -> bool {
    self.use_big_ints.get()
  }

  fn check_bind_result(&self, r: i32) -> Result<(), SqliteError> {
    self.check_error_code_impl(r)
  }
}

impl StatementSync {
  fn assert_statement_finalized(&self) -> Result<(), SqliteError> {
    if self.inner.get().is_none() {
      return Err(SqliteError::StatementFinalized);
    }
    Ok(())
  }

  // Clear the prepared statement back to its initial state.
  fn reset(&self) -> Result<(), SqliteError> {
    let raw = self.stmt_ptr()?;
    // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
    // as it lives as long as the StatementSync instance.
    let r = unsafe { ffi::sqlite3_reset(raw) };

    self.check_error_code_impl(r)
  }

  fn check_error_code_impl(&self, r: i32) -> Result<(), SqliteError> {
    if r != ffi::SQLITE_OK {
      if self.ignore_next_sqlite_error.get() {
        self.ignore_next_sqlite_error.set(false);
        return Ok(());
      }
      let db = self.db.borrow();
      let db = db.as_ref().ok_or(SqliteError::AlreadyClosed)?;

      // SAFETY: db.handle() is valid
      unsafe {
        check_error_code(r, db.handle())?;
      }
    }

    Ok(())
  }

  // Bind the parameters to the prepared statement.
  fn bind_params(
    &self,
    scope: &mut v8::PinScope<'_, '_>,
    params: Option<&v8::FunctionCallbackArguments>,
  ) -> Result<(), SqliteError> {
    let raw = self.stmt_ptr()?;
    // Reset the prepared statement to its initial state.
    // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
    unsafe {
      let r = ffi::sqlite3_clear_bindings(raw);
      self.check_error_code_impl(r)?;
    }

    let mut anon_start = 0;

    if let Some(params) = params {
      let param0 = params.get(0);

      if param0.is_object() && !param0.is_array_buffer_view() {
        let obj = v8::Local::<v8::Object>::try_from(param0).unwrap();
        let keys = obj
          .get_property_names(scope, GetPropertyNamesArgs::default())
          .unwrap();

        // Allow specifying named parameters without the SQLite prefix character to improve
        // ergonomics. This can be disabled with `StatementSync#setAllowBareNamedParams`.
        let mut bare_named_params = std::collections::HashMap::new();
        if self.allow_bare_named_params.get() {
          // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
          let param_count = unsafe { ffi::sqlite3_bind_parameter_count(raw) };
          for i in 1..=param_count {
            // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
            let full_name = unsafe {
              let name = ffi::sqlite3_bind_parameter_name(raw, i);
              if name.is_null() {
                continue;
              }
              std::ffi::CStr::from_ptr(name).to_bytes()
            };
            let bare_name = &full_name[1..];

            let e = bare_named_params.insert(bare_name, i);
            if let Some(existing_index) = e {
              let bare_name_str = std::str::from_utf8(bare_name)?;
              let full_name_str = std::str::from_utf8(full_name)?;

              // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
              unsafe {
                let existing_full_name =
                  ffi::sqlite3_bind_parameter_name(raw, existing_index);
                let existing_full_name_str =
                  std::ffi::CStr::from_ptr(existing_full_name).to_str()?;

                return Err(SqliteError::DuplicateNamedParameter(
                  bare_name_str.to_string(),
                  existing_full_name_str.to_string(),
                  full_name_str.to_string(),
                ));
              }
            }
          }
        }

        let len = keys.length();
        for j in 0..len {
          let key = keys.get_index(scope, j).unwrap();
          let key_str = key.to_rust_string_lossy(scope);
          let key_c = std::ffi::CString::new(key_str).unwrap();

          // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
          let mut r = unsafe {
            ffi::sqlite3_bind_parameter_index(raw, key_c.as_ptr() as *const _)
          };
          if r == 0 {
            let lookup = bare_named_params.get(key_c.as_bytes());
            if let Some(index) = lookup {
              r = *index;
            }

            if r == 0 {
              if self.allow_unknown_named_params.get() {
                continue;
              }

              return Err(SqliteError::UnknownNamedParameter(
                key_c.into_string().unwrap(),
              ));
            }
          }

          let value = obj.get(scope, key).unwrap();
          self.bind_value(scope, value, r)?;
        }

        anon_start += 1;
      }

      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
      let sql_param_count = unsafe { ffi::sqlite3_bind_parameter_count(raw) };

      let mut positional_idx = 1;
      for i in anon_start..params.length() {
        // Find the next positional parameter slot.
        // Skip named parameters (:name, $name, @name) but include anonymous (?)
        // and numbered (?NNN) parameters.
        while positional_idx <= sql_param_count {
          // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
          let name_ptr =
            unsafe { ffi::sqlite3_bind_parameter_name(raw, positional_idx) };
          if name_ptr.is_null()
            // SAFETY: short-circuiting guarantees name_ptr is non-null here
            || unsafe { *name_ptr as u8 == b'?' }
          {
            break;
          }
          // Named parameter (:name, $name, @name) - skip it
          positional_idx += 1;
        }

        let value = params.get(i);

        self.bind_value(scope, value, positional_idx)?;

        positional_idx += 1;
      }
    }

    Ok(())
  }
}

struct ResetGuard<'a>(&'a StatementSync);

impl Drop for ResetGuard<'_> {
  fn drop(&mut self) {
    let _ = self.0.reset();
  }
}

// Represents a single prepared statement. Cannot be initialized directly via constructor.
// Instances are created using `DatabaseSync#prepare`.
//
// A prepared statement is an efficient binary representation of the SQL used to create it.
#[op2]
impl StatementSync {
  #[constructor]
  #[cppgc]
  fn new(_: bool) -> Result<StatementSync, SqliteError> {
    Err(SqliteError::InvalidConstructor)
  }

  // Executes a prepared statement and returns the first result as an object.
  //
  // The prepared statement does not return any results, this method returns undefined.
  // Optionally, parameters can be bound to the prepared statement.
  #[reentrant]
  fn get<'a>(
    &self,
    scope: &mut v8::PinScope<'a, '_>,
    #[varargs] params: Option<&v8::FunctionCallbackArguments>,
  ) -> Result<v8::Local<'a, v8::Value>, SqliteError> {
    self.reset()?;

    self.bind_params(scope, params)?;

    let _reset = ResetGuard(self);

    let entry = self.read_row(scope)?;
    let result = entry.unwrap_or_else(|| v8::undefined(scope).into());

    Ok(result)
  }

  // Executes a prepared statement and returns an object summarizing the resulting
  // changes.
  //
  // Optionally, parameters can be bound to the prepared statement.
  fn run(
    &self,
    scope: &mut v8::PinScope<'_, '_>,
    #[varargs] params: Option<&v8::FunctionCallbackArguments>,
  ) -> Result<RunStatementResult, SqliteError> {
    let db = self.db.borrow();
    let db = db.as_ref().ok_or(SqliteError::AlreadyClosed)?;

    self.bind_params(scope, params)?;

    let reset = ResetGuard(self);

    self.step()?;
    // Reset to return correct change metadata.
    drop(reset);

    Ok(RunStatementResult {
      last_insert_rowid: db.last_insert_rowid(),
      changes: db.changes(),
      use_big_ints: self.use_big_ints.get(),
    })
  }

  // Executes a prepared statement and returns all results as an array of objects.
  //
  // If the prepared statement does not return any results, this method returns an empty array.
  // Optionally, parameters can be bound to the prepared statement.
  fn all<'a>(
    &self,
    scope: &mut v8::PinScope<'a, '_>,
    #[varargs] params: Option<&v8::FunctionCallbackArguments>,
  ) -> Result<v8::Local<'a, v8::Array>, SqliteError> {
    let mut arr = vec![];

    self.bind_params(scope, params)?;

    let _reset = ResetGuard(self);
    while let Some(result) = self.read_row(scope)? {
      arr.push(result);
    }

    let arr = v8::Array::new_with_elements(scope, &arr);
    Ok(arr)
  }

  fn iterate<'a>(
    &self,
    scope: &mut v8::PinScope<'a, '_>,
    #[varargs] params: Option<&v8::FunctionCallbackArguments>,
  ) -> Result<v8::Local<'a, v8::Object>, SqliteError> {
    macro_rules! v8_static_strings {
      ($($ident:ident = $str:literal),* $(,)?) => {
        $(
          pub static $ident: deno_core::FastStaticString = deno_core::ascii_str!($str);
        )*
      };
    }

    v8_static_strings! {
      ITERATOR = "Iterator",
      PROTOTYPE = "prototype",
      NEXT = "next",
      RETURN = "return",
      DONE = "done",
      VALUE = "value",
      __STATEMENT_REF = "__statement_ref",
    }

    self.reset()?;

    self.bind_params(scope, params)?;

    let iterate_next = |scope: &mut v8::PinScope<'_, '_>,
                        args: v8::FunctionCallbackArguments,
                        mut rv: v8::ReturnValue| {
      let context = v8::Local::<v8::External>::try_from(args.data())
        .expect("Iterator#next expected external data");
      // SAFETY: `context` is a valid pointer to a StatementSync instance
      let statement = unsafe { &mut *(context.value() as *mut StatementSync) };

      let names = &[
        DONE.v8_string(scope).unwrap().into(),
        VALUE.v8_string(scope).unwrap().into(),
      ];

      if statement.is_iter_finished.get() {
        let values =
          &[v8::Boolean::new(scope, true).into(), v8::null(scope).into()];
        let null = v8::null(scope).into();
        let result =
          v8::Object::with_prototype_and_properties(scope, null, names, values);
        rv.set(result.into());
        return;
      }

      let Ok(Some(row)) = statement.read_row(scope) else {
        let _ = statement.reset();
        statement.is_iter_finished.set(true);

        let values =
          &[v8::Boolean::new(scope, true).into(), v8::null(scope).into()];
        let null = v8::null(scope).into();
        let result =
          v8::Object::with_prototype_and_properties(scope, null, names, values);
        rv.set(result.into());
        return;
      };

      let values = &[v8::Boolean::new(scope, false).into(), row];
      let null = v8::null(scope).into();
      let result =
        v8::Object::with_prototype_and_properties(scope, null, names, values);
      rv.set(result.into());
    };

    let iterate_return = |scope: &mut v8::PinScope<'_, '_>,
                          args: v8::FunctionCallbackArguments,
                          mut rv: v8::ReturnValue| {
      let context = v8::Local::<v8::External>::try_from(args.data())
        .expect("Iterator#return expected external data");
      // SAFETY: `context` is a valid pointer to a StatementSync instance
      let statement = unsafe { &mut *(context.value() as *mut StatementSync) };

      statement.is_iter_finished.set(true);
      let _ = statement.reset();

      let names = &[
        DONE.v8_string(scope).unwrap().into(),
        VALUE.v8_string(scope).unwrap().into(),
      ];
      let values =
        &[v8::Boolean::new(scope, true).into(), v8::null(scope).into()];

      let null = v8::null(scope).into();
      let result =
        v8::Object::with_prototype_and_properties(scope, null, names, values);
      rv.set(result.into());
    };

    let external = v8::External::new(scope, self as *const _ as _);
    let next_func = v8::Function::builder(iterate_next)
      .data(external.into())
      .build(scope)
      .expect("Failed to create Iterator#next function");
    let return_func = v8::Function::builder(iterate_return)
      .data(external.into())
      .build(scope)
      .expect("Failed to create Iterator#return function");

    let global = scope.get_current_context().global(scope);
    let iter_str = ITERATOR.v8_string(scope).unwrap();
    let js_iterator: v8::Local<v8::Object> = {
      global
        .get(scope, iter_str.into())
        .unwrap()
        .try_into()
        .unwrap()
    };

    let proto_str = PROTOTYPE.v8_string(scope).unwrap();
    let js_iterator_proto = js_iterator.get(scope, proto_str.into()).unwrap();

    let names = &[
      NEXT.v8_string(scope).unwrap().into(),
      RETURN.v8_string(scope).unwrap().into(),
      __STATEMENT_REF.v8_string(scope).unwrap().into(),
    ];

    // Get the cppgc wrapper object to keep the statement alive
    // We store a reference to the statement object on the iterator to prevent
    // the GC from collecting it while the iterator is still in use.
    let statement_ref = if let Some(args) = params {
      args.this().into()
    } else {
      v8::undefined(scope).into()
    };

    let values = &[next_func.into(), return_func.into(), statement_ref];
    let iterator = v8::Object::with_prototype_and_properties(
      scope,
      js_iterator_proto,
      names,
      values,
    );

    self.is_iter_finished.set(false);

    Ok(iterator)
  }

  #[fast]
  #[undefined]
  fn set_allow_bare_named_parameters(
    &self,
    #[validate(validators::allow_bare_named_params_bool)] enabled: bool,
  ) -> Result<(), SqliteError> {
    self.assert_statement_finalized()?;
    self.allow_bare_named_params.set(enabled);
    Ok(())
  }

  #[fast]
  #[undefined]
  fn set_allow_unknown_named_parameters(
    &self,
    #[validate(validators::allow_unknown_named_params_bool)] enabled: bool,
  ) -> Result<(), SqliteError> {
    self.assert_statement_finalized()?;
    self.allow_unknown_named_params.set(enabled);
    Ok(())
  }

  #[fast]
  #[undefined]
  fn set_read_big_ints(
    &self,
    #[validate(validators::read_big_ints_bool)] enabled: bool,
  ) -> Result<(), SqliteError> {
    self.assert_statement_finalized()?;
    self.use_big_ints.set(enabled);
    Ok(())
  }

  #[fast]
  #[undefined]
  fn set_return_arrays(
    &self,
    #[validate(validators::return_arrays_bool)] enabled: bool,
  ) -> Result<(), SqliteError> {
    self.assert_statement_finalized()?;
    self.return_arrays.set(enabled);
    Ok(())
  }

  #[getter]
  #[rename("sourceSQL")]
  #[string]
  fn source_sql(&self) -> Result<String, SqliteError> {
    let inner = self.stmt_ptr()?;
    // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
    // as it lives as long as the StatementSync instance.
    let source_sql = unsafe {
      let raw = ffi::sqlite3_sql(inner);
      std::ffi::CStr::from_ptr(raw as _)
        .to_string_lossy()
        .into_owned()
    };
    Ok(source_sql)
  }

  #[getter]
  #[rename("expandedSQL")]
  #[string]
  fn expanded_sql(&self) -> Result<String, SqliteError> {
    let inner = self.stmt_ptr()?;
    // SAFETY: `inner` is a valid pointer to a sqlite3_stmt
    // as it lives as long as the StatementSync instance.
    unsafe {
      let raw = ffi::sqlite3_expanded_sql(inner);
      if raw.is_null() {
        return Err(SqliteError::InvalidExpandedSql);
      }
      let sql = std::ffi::CStr::from_ptr(raw as _)
        .to_string_lossy()
        .into_owned();
      ffi::sqlite3_free(raw as _);
      Ok(sql)
    }
  }

  fn columns<'a>(
    &self,
    scope: &mut v8::PinScope<'a, '_>,
  ) -> Result<v8::Local<'a, v8::Array>, SqliteError> {
    v8_static_strings! {
      NAME = "name",
      COLUMN = "column",
      TABLE = "table",
      DATABASE = "database",
      TYPE = "type",
    }

    let column_count = self.column_count()?;
    let mut columns = Vec::with_capacity(column_count as usize);

    // Pre-create property keys
    let name_key = NAME.v8_string(scope).unwrap().into();
    let column_key = COLUMN.v8_string(scope).unwrap().into();
    let table_key = TABLE.v8_string(scope).unwrap().into();
    let database_key = DATABASE.v8_string(scope).unwrap().into();
    let type_key = TYPE.v8_string(scope).unwrap().into();

    let keys = &[name_key, column_key, table_key, database_key, type_key];
    let raw = self.stmt_ptr()?;

    for i in 0..column_count {
      // name: The name of the column in the result set
      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      let name = unsafe {
        let name_ptr = ffi::sqlite3_column_name(raw, i);
        if !name_ptr.is_null() {
          let name_cstr = std::ffi::CStr::from_ptr(name_ptr as _);
          v8::String::new_from_utf8(
            scope,
            name_cstr.to_bytes(),
            v8::NewStringType::Normal,
          )
          .unwrap()
          .into()
        } else {
          v8::null(scope).into()
        }
      };

      // column: The unaliased name of the column in the origin table
      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      let column = unsafe {
        let column_ptr = ffi::sqlite3_column_origin_name(raw, i);
        if !column_ptr.is_null() {
          let column_cstr = std::ffi::CStr::from_ptr(column_ptr as _);
          v8::String::new_from_utf8(
            scope,
            column_cstr.to_bytes(),
            v8::NewStringType::Normal,
          )
          .unwrap()
          .into()
        } else {
          v8::null(scope).into()
        }
      };

      // table: The unaliased name of the origin table
      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      let table = unsafe {
        let table_ptr = ffi::sqlite3_column_table_name(raw, i);
        if !table_ptr.is_null() {
          let table_cstr = std::ffi::CStr::from_ptr(table_ptr as _);
          v8::String::new_from_utf8(
            scope,
            table_cstr.to_bytes(),
            v8::NewStringType::Normal,
          )
          .unwrap()
          .into()
        } else {
          v8::null(scope).into()
        }
      };

      // database: The unaliased name of the origin database
      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      let database = unsafe {
        let database_ptr = ffi::sqlite3_column_database_name(raw, i);
        if !database_ptr.is_null() {
          let database_cstr = std::ffi::CStr::from_ptr(database_ptr as _);
          v8::String::new_from_utf8(
            scope,
            database_cstr.to_bytes(),
            v8::NewStringType::Normal,
          )
          .unwrap()
          .into()
        } else {
          v8::null(scope).into()
        }
      };

      // type: The declared data type of the column
      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt
      let col_type = unsafe {
        let type_ptr = ffi::sqlite3_column_decltype(raw, i);
        if !type_ptr.is_null() {
          let type_cstr = std::ffi::CStr::from_ptr(type_ptr as _);
          v8::String::new_from_utf8(
            scope,
            type_cstr.to_bytes(),
            v8::NewStringType::Normal,
          )
          .unwrap()
          .into()
        } else {
          v8::null(scope).into()
        }
      };

      let values = &[name, column, table, database, col_type];
      let null = v8::null(scope).into();
      let obj =
        v8::Object::with_prototype_and_properties(scope, null, keys, values);

      columns.push(obj.into());
    }

    Ok(v8::Array::new_with_elements(scope, &columns))
  }
}