cdbc_sqlite/types/
bytes.rs1use std::borrow::Cow;
2
3use cdbc::decode::Decode;
4use cdbc::encode::{Encode, IsNull};
5use cdbc::error::BoxDynError;
6use crate::type_info::DataType;
7use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
8use cdbc::types::Type;
9
10impl Type<Sqlite> for [u8] {
11 fn type_info() -> SqliteTypeInfo {
12 SqliteTypeInfo(DataType::Blob)
13 }
14
15 fn compatible(ty: &SqliteTypeInfo) -> bool {
16 matches!(ty.0, DataType::Blob | DataType::Text)
17 }
18}
19
20impl<'q> Encode<'q, Sqlite> for &'q [u8] {
21 fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
22 args.push(SqliteArgumentValue::Blob(Cow::Borrowed(self)));
23
24 IsNull::No
25 }
26}
27
28impl<'r> Decode<'r, Sqlite> for &'r [u8] {
29 fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
30 Ok(value.blob())
31 }
32}
33
34impl Type<Sqlite> for Vec<u8> {
35 fn type_info() -> SqliteTypeInfo {
36 <&[u8] as Type<Sqlite>>::type_info()
37 }
38
39 fn compatible(ty: &SqliteTypeInfo) -> bool {
40 <&[u8] as Type<Sqlite>>::compatible(ty)
41 }
42}
43
44impl<'q> Encode<'q, Sqlite> for Vec<u8> {
45 fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
46 args.push(SqliteArgumentValue::Blob(Cow::Owned(self)));
47
48 IsNull::No
49 }
50
51 fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
52 args.push(SqliteArgumentValue::Blob(Cow::Owned(self.clone())));
53
54 IsNull::No
55 }
56}
57
58impl<'r> Decode<'r, Sqlite> for Vec<u8> {
59 fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
60 Ok(value.blob().to_owned())
61 }
62}