1use crate::conversion::FromRow;
4use crate::error::Result;
5use crate::protocol::backend::query::{CommandComplete, DataRow, RowDescription};
6use crate::state::action::AsyncMessage;
7
8pub trait TextHandler {
21 fn result_start(&mut self, cols: RowDescription<'_>) -> Result<()> {
23 let _ = cols;
24 Ok(())
25 }
26
27 fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()>;
29
30 fn result_end(&mut self, complete: CommandComplete<'_>) -> Result<()> {
32 let _ = complete;
33 Ok(())
34 }
35}
36
37pub trait BinaryHandler {
44 fn result_start(&mut self, cols: RowDescription<'_>) -> Result<()> {
46 let _ = cols;
47 Ok(())
48 }
49
50 fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()>;
52
53 fn result_end(&mut self, complete: CommandComplete<'_>) -> Result<()> {
55 let _ = complete;
56 Ok(())
57 }
58}
59
60#[derive(Debug, Default)]
62pub struct DropHandler {
63 rows_affected: Option<u64>,
64}
65
66impl DropHandler {
67 pub fn new() -> Self {
69 Self::default()
70 }
71
72 pub fn rows_affected(&self) -> Option<u64> {
74 self.rows_affected
75 }
76}
77
78impl TextHandler for DropHandler {
79 fn row(&mut self, _cols: RowDescription<'_>, _row: DataRow<'_>) -> Result<()> {
80 Ok(())
81 }
82
83 fn result_end(&mut self, complete: CommandComplete<'_>) -> Result<()> {
84 self.rows_affected = complete.rows_affected();
85 Ok(())
86 }
87}
88
89impl BinaryHandler for DropHandler {
90 fn row(&mut self, _cols: RowDescription<'_>, _row: DataRow<'_>) -> Result<()> {
91 Ok(())
92 }
93
94 fn result_end(&mut self, complete: CommandComplete<'_>) -> Result<()> {
95 self.rows_affected = complete.rows_affected();
96 Ok(())
97 }
98}
99
100#[derive(Default)]
112pub struct CollectHandler<T> {
113 rows: Vec<T>,
114}
115
116impl<T> CollectHandler<T> {
117 pub fn new() -> Self {
119 Self { rows: Vec::new() }
120 }
121
122 pub fn rows(&self) -> &[T] {
124 &self.rows
125 }
126
127 pub fn into_rows(self) -> Vec<T> {
129 self.rows
130 }
131
132 pub fn len(&self) -> usize {
134 self.rows.len()
135 }
136
137 pub fn is_empty(&self) -> bool {
139 self.rows.is_empty()
140 }
141}
142
143impl<T: for<'a> FromRow<'a>> TextHandler for CollectHandler<T> {
144 fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
145 let typed_row = T::from_row_text(cols.fields(), row)?;
146 self.rows.push(typed_row);
147 Ok(())
148 }
149}
150
151impl<T: for<'a> FromRow<'a>> BinaryHandler for CollectHandler<T> {
152 fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
153 let typed_row = T::from_row_binary(cols.fields(), row)?;
154 self.rows.push(typed_row);
155 Ok(())
156 }
157}
158
159#[derive(Default)]
161pub struct FirstRowHandler<T> {
162 row: Option<T>,
163}
164
165impl<T> FirstRowHandler<T> {
166 pub fn new() -> Self {
168 Self { row: None }
169 }
170
171 pub fn get(&self) -> Option<&T> {
173 self.row.as_ref()
174 }
175
176 pub fn into_row(self) -> Option<T> {
178 self.row
179 }
180}
181
182impl<T: for<'a> FromRow<'a>> TextHandler for FirstRowHandler<T> {
183 fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
184 if self.row.is_none() {
185 let typed_row = T::from_row_text(cols.fields(), row)?;
186 self.row = Some(typed_row);
187 }
188 Ok(())
189 }
190}
191
192impl<T: for<'a> FromRow<'a>> BinaryHandler for FirstRowHandler<T> {
193 fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
194 if self.row.is_none() {
195 let typed_row = T::from_row_binary(cols.fields(), row)?;
196 self.row = Some(typed_row);
197 }
198 Ok(())
199 }
200}
201
202pub trait AsyncMessageHandler: Send {
237 fn handle(&mut self, msg: &AsyncMessage);
239}
240
241impl<F: FnMut(&AsyncMessage) + Send> AsyncMessageHandler for F {
242 fn handle(&mut self, msg: &AsyncMessage) {
243 self(msg)
244 }
245}