Skip to main content

sqlx_cloudflare_d1/
value.rs

1use std::borrow::Cow;
2
3use sqlx_cloudflare_core::{AsValue, Value as CoreValue};
4use sqlx_core::value::{Value, ValueRef};
5
6use crate::{D1TypeInfo, D1};
7
8/// A value read out of a D1 result, already converted from JavaScript.
9///
10/// Owned, because D1 returns the whole result set as JavaScript values and
11/// there is nothing on the Rust side for a row to borrow from.
12///
13/// It wraps [`D1ArgumentValue`](crate::D1ArgumentValue): a value read and
14/// a value bound are the same enum, as in sqlx's `Any` driver.
15#[derive(Debug, Clone, PartialEq)]
16pub struct D1Value(pub(crate) CoreValue);
17
18impl Value for D1Value {
19    type Database = D1;
20
21    fn as_ref(&self) -> D1ValueRef<'_> {
22        D1ValueRef(&self.0)
23    }
24
25    fn type_info(&self) -> Cow<'_, D1TypeInfo> {
26        Cow::Owned(self.0.type_info())
27    }
28
29    fn is_null(&self) -> bool {
30        self.0.is_null()
31    }
32}
33
34/// A borrowed [`D1Value`], which is what `Decode` implementations read.
35#[derive(Debug, Clone, Copy)]
36pub struct D1ValueRef<'r>(pub(crate) &'r CoreValue);
37
38impl<'r> AsValue<'r> for D1ValueRef<'r> {
39    fn as_value(&self) -> &'r CoreValue {
40        self.0
41    }
42}
43
44impl ValueRef<'_> for D1ValueRef<'_> {
45    type Database = D1;
46
47    fn to_owned(&self) -> D1Value {
48        D1Value(self.0.clone())
49    }
50
51    fn type_info(&self) -> Cow<'_, D1TypeInfo> {
52        Cow::Owned(self.0.type_info())
53    }
54
55    fn is_null(&self) -> bool {
56        self.0.is_null()
57    }
58}