Skip to main content

sqlx_cloudflare_do/
value.rs

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