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
//! The common interface for all database backends.
use async_trait::async_trait;
use std::{
    borrow::{Borrow, BorrowMut},
    result::Result,
};

use crate::{
    error::ToqlError, from_row::FromRow, key::Key, keyed::Keyed, page::Page,
    page_counts::PageCounts, query::Query,
};

use count::Count;
use delete::Delete;
use fields::Fields;
use insert::Insert;
use load::Load;
use paths::Paths;
use update::Update;

pub mod count;
pub mod delete;
pub mod fields;
pub mod insert;
pub mod load;
pub mod paths;
pub mod update;

/// Every database provider implements this trait.
/// This ensures that the user of Toql can easily switch databases.
///
/// The trait can also be used to write database independend code. This however requires more trait bounds
/// and is usually less ergonomic than passing for a specific database type to the functions.
///
/// ### Example
/// The following code shows the function signature to load an entity `MyUser` from a any database:
///
/// ```rust, ignore
/// use toql_core::{error::ToqlError, toql_api::ToqlApi, load::Load, from_row::FromRow};
/// use toql_derive::Toql;
///
/// struct MyError;
///
/// #[derive(Toql)]
/// struct MyUser {
///    #[toql(key)]
///    id: u64,
///    name: String
/// }
///
/// async fn load_user<R, E, A>(toql: &mut A) -> std::result::Result<Vec<MyUser>, MyError>
/// where A: ToqlApi<Row=R, Error = E>,
///     E: From<ToqlError>,
///     MyUser: Load<R, E>,
///     <User as Keyed>::Key: FromRow<R, E>,  // Needed until rust-lang/rfcs#2289 is resolved
///     MyError: From<E>
/// {
///        let users = toql.load_many().await?;
///        Ok(users)
/// }
/// ```
#[async_trait]
pub trait ToqlApi
where
    Self::Error: From<ToqlError>,
{
    type Row;
    type Error;

    async fn insert_one<T>(&mut self, entity: &mut T, paths: Paths) -> Result<(), Self::Error>
    where
        T: Insert;

    async fn insert_many<T, Q>(
        &mut self,
        entities: &mut [Q],
        paths: Paths,
    ) -> Result<(), Self::Error>
    where
        T: Insert,
        Q: BorrowMut<T> + Send;

    async fn update_one<T>(&mut self, entity: &mut T, fields: Fields) -> Result<(), Self::Error>
    where
        T: Update + Keyed;

    async fn update_many<T, Q>(
        &mut self,
        entities: &mut [Q],
        fields: Fields,
    ) -> Result<(), Self::Error>
    where
        T: Update + Keyed,
        Q: BorrowMut<T> + Send + Sync;

    async fn load_one<T, B>(&mut self, query: B) -> Result<T, Self::Error>
    where
        T: Load<Self::Row, Self::Error>,
        B: Borrow<Query<T>> + Send + Sync,
        <T as Keyed>::Key: FromRow<Self::Row, Self::Error>;

    async fn load_many<T, B>(&mut self, query: B) -> Result<Vec<T>, Self::Error>
    where
        T: Load<Self::Row, Self::Error>,
        B: Borrow<Query<T>> + Send + Sync,
        <T as Keyed>::Key: FromRow<Self::Row, Self::Error>;

    async fn load_page<T, B>(
        &mut self,
        query: B,
        page: Page,
    ) -> Result<(Vec<T>, Option<PageCounts>), Self::Error>
    where
        T: Load<Self::Row, Self::Error>,
        B: Borrow<Query<T>> + Send + Sync,
        <T as Keyed>::Key: FromRow<Self::Row, Self::Error>;

    async fn count<T, B>(&mut self, query: B) -> Result<u64, Self::Error>
    where
        T: Count,
        B: Borrow<Query<T>> + Send + Sync;

    async fn delete_one<K>(&mut self, key: K) -> Result<(), Self::Error>
    where
        K: Key + Into<Query<<K as Key>::Entity>> + Send,
        <K as Key>::Entity: Send + Delete;

    async fn delete_many<T, B>(&mut self, query: B) -> Result<(), Self::Error>
    where
        T: Delete,
        B: Borrow<Query<T>> + Send + Sync;
}