Skip to main content

sqll/from_column/
alloc.rs

1use alloc::string::String;
2use alloc::vec::Vec;
3
4use crate::ty;
5use crate::{FromUnsizedColumn, Result, Statement};
6
7use super::FromColumn;
8
9/// [`FromColumn`] implementation which returns a newly allocated [`String`].
10///
11/// For a more memory-efficient way of reading bytes, consider using the
12/// [`FromUnsizedColumn`] implementation for [`str`].
13///
14/// # Examples
15///
16/// ```
17/// use sqll::Connection;
18///
19/// let c = Connection::open_in_memory()?;
20///
21/// c.execute(r#"
22///     CREATE TABLE users (name TEXT);
23///
24///     INSERT INTO users (name) VALUES ('Alice'), ('Bob');
25/// "#)?;
26///
27/// let mut stmt = c.prepare("SELECT name FROM users")?;
28///
29/// assert_eq!(stmt.next::<String>()?, Some(String::from("Alice")));
30/// assert_eq!(stmt.next::<String>()?, Some(String::from("Bob")));
31/// assert_eq!(stmt.next::<String>()?, None);
32/// # Ok::<_, sqll::Error>(())
33/// ```
34///
35/// Automatic conversion being denied:
36///
37/// ```
38/// use sqll::{Connection, Code};
39///
40/// let c = Connection::open_in_memory()?;
41///
42/// c.execute(r#"
43///     CREATE TABLE users (id INTEGER);
44///
45///     INSERT INTO users (id) VALUES (1), (2);
46/// "#)?;
47///
48/// let mut stmt = c.prepare("SELECT id FROM users")?;
49///
50/// let e = stmt.next::<String>().unwrap_err();
51/// assert_eq!(e.code(), Code::MISMATCH);
52/// # Ok::<_, sqll::Error>(())
53/// ```
54impl FromColumn<'_> for String {
55    type Type = ty::Text;
56
57    #[inline]
58    fn from_column(stmt: &Statement, index: ty::Text) -> Result<Self> {
59        let mut s = String::with_capacity(index.len());
60        s.push_str(<_>::from_unsized_column(stmt, index)?);
61        Ok(s)
62    }
63}
64
65/// [`FromColumn`] implementation which returns a newly allocated [`Vec`].
66///
67/// For a more memory-efficient way of reading bytes, consider using the
68/// [`FromUnsizedColumn`] implementation for a byte slice.
69///
70/// # Examples
71///
72/// ```
73/// use sqll::Connection;
74///
75/// let c = Connection::open_in_memory()?;
76///
77/// c.execute(r#"
78///     CREATE TABLE users (blob BLOB);
79///
80///     INSERT INTO users (blob) VALUES (X'aabb'), (X'bbcc');
81/// "#)?;
82///
83/// let mut stmt = c.prepare("SELECT blob FROM users")?;
84///
85/// assert_eq!(stmt.next::<Vec<u8>>()?, Some(vec![0xaa, 0xbb]));
86/// assert_eq!(stmt.next::<Vec<u8>>()?, Some(vec![0xbb, 0xcc]));
87/// assert_eq!(stmt.next::<Vec<u8>>()?, None);
88/// # Ok::<_, sqll::Error>(())
89/// ```
90///
91/// Automatic conversion being denied:
92///
93/// ```
94/// use sqll::{Connection, Code};
95///
96/// let c = Connection::open_in_memory()?;
97///
98/// c.execute(r#"
99///     CREATE TABLE users (id INTEGER);
100///
101///     INSERT INTO users (id) VALUES (1), (2);
102/// "#)?;
103///
104/// let mut stmt = c.prepare("SELECT id FROM users")?;
105///
106/// let e = stmt.next::<Vec::<u8>>().unwrap_err();
107/// assert_eq!(e.code(), Code::MISMATCH);
108/// # Ok::<_, sqll::Error>(())
109/// ```
110impl FromColumn<'_> for Vec<u8> {
111    type Type = ty::Blob;
112
113    #[inline]
114    fn from_column(stmt: &Statement, index: ty::Blob) -> Result<Self> {
115        let mut buf = Vec::with_capacity(index.len());
116        buf.extend_from_slice(<_>::from_unsized_column(stmt, index)?);
117        Ok(buf)
118    }
119}