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
use crateType;
use craterepeat;
use crate::;
/// This allows a type to be constructed from a [`Statement`] using [`next`],
/// [`iter`], or [`row`].
///
/// This is typically implemented with the [`Row` derive].
///
/// [`iter`]: Statement::iter
/// [`next`]: Statement::next
/// [`row`]: Statement::row
/// [`Row` derive]: derive@crate::Row
///
/// # Safety
///
/// The caller must ensure that the implementation of `from_row` only reads
/// distinct rows. Attempting to read the same row multiple times may lead to
/// invalidation of the column value leading to undefined behavior.
///
/// This is guaranteed by the [`Row` derive] and any internal implementations.
///
/// [`Row` derive]: derive@crate::Row
///
/// # Examples
///
/// The simplest implementation for [`Row`] is provided by tuples.
///
/// ```
/// use sqll::{Connection, Row};
///
/// let mut c = Connection::open_in_memory()?;
///
/// c.execute(r#"
/// CREATE TABLE users (name TEXT, age INTEGER);
///
/// INSERT INTO users VALUES ('Alice', 42);
/// INSERT INTO users VALUES ('Bob', 72);
/// "#)?;
///
/// let mut results = c.prepare("SELECT name, age FROM users ORDER BY age")?;
///
/// while let Some((name, age)) = results.next::<(&str, u32)>()? {
/// println!("{name} is {age} years old");
/// }
/// # Ok::<_, sqll::Error>(())
/// ```
///
/// It can also be derived on a custom struct:
///
/// ```
/// use sqll::{Connection, Row};
///
/// #[derive(Row)]
/// struct Person<'stmt> {
/// name: &'stmt str,
/// age: u32,
/// }
///
/// #[derive(Row)]
/// struct PersonTuple<'stmt>(&'stmt str, u32);
///
/// let mut c = Connection::open_in_memory()?;
///
/// c.execute(r#"
/// CREATE TABLE users (name TEXT, age INTEGER);
///
/// INSERT INTO users VALUES ('Alice', 42);
/// INSERT INTO users VALUES ('Bob', 72);
/// "#)?;
///
/// let mut results = c.prepare("SELECT name, age FROM users ORDER BY age")?;
///
/// while let Some(person) = results.next::<Person<'_>>()? {
/// println!("{} is {} years old", person.name, person.age);
/// }
///
/// results.reset()?;
///
/// while let Some(PersonTuple(name, age)) = results.next::<PersonTuple<'_>>()? {
/// println!("{name} is {age} years old");
/// }
/// # Ok::<_, sqll::Error>(())
/// ```
///
/// Convert into an owned type:
///
/// ```
/// use sqll::{Connection, Row};
///
/// #[derive(Row)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// #[derive(Row)]
/// struct PersonTuple(String, u32);
///
/// let mut c = Connection::open_in_memory()?;
///
/// c.execute(r#"
/// CREATE TABLE users (name TEXT, age INTEGER);
///
/// INSERT INTO users VALUES ('Alice', 42);
/// INSERT INTO users VALUES ('Bob', 72);
/// "#)?;
///
/// let mut stmt = c.prepare("SELECT name, age FROM users ORDER BY age")?;
///
/// while stmt.step()?.is_row() {
/// let person = stmt.row::<Person>()?;
/// println!("{} is {} years old", person.name, person.age);
///
/// let PersonTuple(name, age) = stmt.row::<PersonTuple>()?;
/// println!("{name} is {age} years old");
/// }
/// # Ok::<_, sqll::Error>(())
/// ```
pub unsafe
unsafe
repeat!;