use crate::conversion::FromRow;
use crate::error::Result;
use crate::protocol::backend::query::{CommandComplete, DataRow, RowDescription};
use crate::state::action::AsyncMessage;
pub trait SimpleHandler {
fn result_start(&mut self, cols: RowDescription<'_>) -> Result<()> {
let _ = cols;
Ok(())
}
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()>;
fn result_end(&mut self, complete: CommandComplete<'_>) -> Result<()> {
let _ = complete;
Ok(())
}
}
pub trait ExtendedHandler {
fn result_start(&mut self, cols: RowDescription<'_>) -> Result<()> {
let _ = cols;
Ok(())
}
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()>;
fn result_end(&mut self, complete: CommandComplete<'_>) -> Result<()> {
let _ = complete;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct DropHandler {
rows_affected: Option<u64>,
}
impl DropHandler {
pub fn new() -> Self {
Self::default()
}
pub fn rows_affected(&self) -> Option<u64> {
self.rows_affected
}
}
impl SimpleHandler for DropHandler {
fn row(&mut self, _cols: RowDescription<'_>, _row: DataRow<'_>) -> Result<()> {
Ok(())
}
fn result_end(&mut self, complete: CommandComplete<'_>) -> Result<()> {
self.rows_affected = complete.rows_affected();
Ok(())
}
}
impl ExtendedHandler for DropHandler {
fn row(&mut self, _cols: RowDescription<'_>, _row: DataRow<'_>) -> Result<()> {
Ok(())
}
fn result_end(&mut self, complete: CommandComplete<'_>) -> Result<()> {
self.rows_affected = complete.rows_affected();
Ok(())
}
}
#[derive(Default)]
pub struct CollectHandler<T> {
rows: Vec<T>,
}
impl<T> CollectHandler<T> {
pub fn new() -> Self {
Self { rows: Vec::new() }
}
pub fn rows(&self) -> &[T] {
&self.rows
}
pub fn into_rows(self) -> Vec<T> {
self.rows
}
pub fn len(&self) -> usize {
self.rows.len()
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
}
impl<T: for<'a> FromRow<'a>> SimpleHandler for CollectHandler<T> {
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
let typed_row = T::from_row_text(cols.fields(), row)?;
self.rows.push(typed_row);
Ok(())
}
}
impl<T: for<'a> FromRow<'a>> ExtendedHandler for CollectHandler<T> {
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
let typed_row = T::from_row_binary(cols.fields(), row)?;
self.rows.push(typed_row);
Ok(())
}
}
#[derive(Default)]
pub struct FirstRowHandler<T> {
row: Option<T>,
}
impl<T> FirstRowHandler<T> {
pub fn new() -> Self {
Self { row: None }
}
pub fn get(&self) -> Option<&T> {
self.row.as_ref()
}
pub fn into_row(self) -> Option<T> {
self.row
}
}
impl<T: for<'a> FromRow<'a>> SimpleHandler for FirstRowHandler<T> {
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
if self.row.is_none() {
let typed_row = T::from_row_text(cols.fields(), row)?;
self.row = Some(typed_row);
}
Ok(())
}
}
impl<T: for<'a> FromRow<'a>> ExtendedHandler for FirstRowHandler<T> {
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
if self.row.is_none() {
let typed_row = T::from_row_binary(cols.fields(), row)?;
self.row = Some(typed_row);
}
Ok(())
}
}
pub struct ForEachHandler<T, F> {
f: F,
_marker: std::marker::PhantomData<T>,
}
impl<T, F> ForEachHandler<T, F>
where
F: FnMut(T) -> Result<()>,
{
pub fn new(f: F) -> Self {
Self {
f,
_marker: std::marker::PhantomData,
}
}
}
impl<T: for<'a> FromRow<'a>, F: FnMut(T) -> Result<()>> SimpleHandler for ForEachHandler<T, F> {
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
let typed_row = T::from_row_text(cols.fields(), row)?;
(self.f)(typed_row)
}
}
impl<T: for<'a> FromRow<'a>, F: FnMut(T) -> Result<()>> ExtendedHandler for ForEachHandler<T, F> {
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
let typed_row = T::from_row_binary(cols.fields(), row)?;
(self.f)(typed_row)
}
}
pub struct ForEachRefHandler<Row, F> {
f: F,
_marker: std::marker::PhantomData<Row>,
}
impl<Row, F> ForEachRefHandler<Row, F> {
pub fn new(f: F) -> Self {
Self {
f,
_marker: std::marker::PhantomData,
}
}
}
impl<Row, F> ExtendedHandler for ForEachRefHandler<Row, F>
where
Row: for<'a> crate::conversion::ref_row::RefFromRow<'a>,
F: for<'a> FnMut(&'a Row) -> Result<()>,
{
fn row(&mut self, cols: RowDescription<'_>, row: DataRow<'_>) -> Result<()> {
let parsed = Row::ref_from_row_binary(cols.fields(), row)?;
(self.f)(parsed)
}
}
pub trait AsyncMessageHandler: Send {
fn handle(&mut self, msg: &AsyncMessage);
}
impl<F: FnMut(&AsyncMessage) + Send> AsyncMessageHandler for F {
fn handle(&mut self, msg: &AsyncMessage) {
self(msg)
}
}