tank_core/
data_set.rs

1use crate::{
2    Driver, Executor, Expression, Query, Result, RowLabeled,
3    stream::Stream,
4    writer::{Context, SqlWriter},
5};
6
7pub trait DataSet {
8    /// Must qualify the column names with the table name
9    fn qualified_columns() -> bool
10    where
11        Self: Sized;
12    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, buff: &mut String);
13    fn select<'s, Item, Cols, Exec, Expr>(
14        &'s self,
15        columns: Cols,
16        executor: &'s mut Exec,
17        condition: &Expr,
18        limit: Option<u32>,
19    ) -> impl Stream<Item = Result<RowLabeled>> + 's
20    where
21        Self: Sized,
22        Item: Expression,
23        Cols: IntoIterator<Item = Item> + Clone,
24        Exec: Executor,
25        Expr: Expression,
26    {
27        let mut query = String::with_capacity(1024);
28        executor
29            .driver()
30            .sql_writer()
31            .write_select(&mut query, columns, self, condition, limit);
32        executor.fetch(query.into())
33    }
34    fn prepare<Item, Cols, Exec, Expr>(
35        &self,
36        columns: Cols,
37        executor: &mut Exec,
38        condition: &Expr,
39        limit: Option<u32>,
40    ) -> impl Future<Output = Result<Query<Exec::Driver>>>
41    where
42        Self: Sized,
43        Item: Expression,
44        Cols: IntoIterator<Item = Item> + Clone,
45        Exec: Executor,
46        Expr: Expression,
47    {
48        let mut query = String::with_capacity(1024);
49        executor
50            .driver()
51            .sql_writer()
52            .write_select(&mut query, columns, self, condition, limit);
53        executor.prepare(query.into())
54    }
55}
56
57impl DataSet for &dyn DataSet {
58    fn qualified_columns() -> bool
59    where
60        Self: Sized,
61    {
62        unreachable!("Cannot call static qualified_columns on a dyn object directly");
63    }
64    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, buff: &mut String) {
65        (*self).write_query(writer, context, buff)
66    }
67}