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
use super::{ResultSet, Transaction, DBIO};
use crate::ast::*;

pub trait GetRow {
    fn get_result_row(&self) -> crate::Result<Vec<ParameterizedValue<'static>>>;
}

pub trait TakeRow {
    fn take_result_row(&mut self) -> crate::Result<Vec<ParameterizedValue<'static>>>;
}

pub trait ToColumnNames {
    fn to_column_names(&self) -> Vec<String>;
}

/// Represents a connection or a transaction that can be queried.
pub trait Queryable
where
    Self: Sync,
{
    /// Executes the given query and returns the ID of the last inserted row.
    fn execute<'a>(&'a self, q: Query<'a>) -> DBIO<'a, Option<Id>>;

    /// Executes the given query and returns the result set.
    fn query<'a>(&'a self, q: Query<'a>) -> DBIO<'a, ResultSet>;

    /// Executes a query given as SQL, interpolating the given parameters and
    /// returning a set of results.
    fn query_raw<'a>(&'a self, sql: &'a str, params: &'a [ParameterizedValue<'a>]) -> DBIO<'a, ResultSet>;

    /// Executes a query given as SQL, interpolating the given parameters and
    /// returning the number of affected rows.
    fn execute_raw<'a>(&'a self, sql: &'a str, params: &'a [ParameterizedValue<'a>]) -> DBIO<'a, u64>;

    /// Runs a command in the database, for queries that can't be run using
    /// prepared statements.
    fn raw_cmd<'a>(&'a self, cmd: &'a str) -> DBIO<'a, ()>;

    // For selecting data returning the results.
    fn select<'a>(&'a self, q: Select<'a>) -> DBIO<'a, ResultSet> {
        self.query(q.into())
    }

    /// For inserting data. Returns the ID of the last inserted row.
    fn insert<'a>(&'a self, q: Insert<'a>) -> DBIO<'a, Option<Id>> {
        self.execute(q.into())
    }

    /// For updating data.
    fn update<'a>(&'a self, q: Update<'a>) -> DBIO<'a, ()> {
        DBIO::new(async move {
            self.execute(q.into()).await?;
            Ok(())
        })
    }

    /// For deleting data.
    fn delete<'a>(&'a self, q: Delete<'a>) -> DBIO<'a, ()> {
        DBIO::new(async move {
            self.execute(q.into()).await?;
            Ok(())
        })
    }
}

/// A thing that can start a new transaction.
pub trait TransactionCapable: Queryable
where
    Self: Sized + Sync,
{
    /// Starts a new transaction
    fn start_transaction(&self) -> DBIO<Transaction> {
        DBIO::new(async move { Transaction::new(self).await })
    }
}