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
use std::collections::HashMap;

use hyper::{self, client::HttpConnector};

use self::error::Result;
pub use self::{
    insert::Insert, inserter::Inserter, introspection::Reflection, query::Query, watch::Watch,
};

mod buflist;
pub mod error;
mod insert;
mod inserter;
mod introspection;
mod query;
mod response;
mod rowbinary;
mod sql_builder;
mod watch;

mod sealed {
    pub trait Sealed {}
}

#[derive(Clone)]
pub struct Client {
    client: hyper::Client<HttpConnector>,

    url: String,
    database: Option<String>,
    user: Option<String>,
    password: Option<String>,
    options: HashMap<String, String>,
}

impl Default for Client {
    fn default() -> Self {
        Self {
            client: hyper::Client::new(),
            url: String::new(),
            database: None,
            user: None,
            password: None,
            options: HashMap::new(),
        }
    }
}

impl Client {
    // TODO: use `url` crate?
    pub fn with_url(mut self, url: impl Into<String>) -> Self {
        self.url = url.into();
        self
    }

    pub fn with_database(mut self, database: impl Into<String>) -> Self {
        self.database = Some(database.into());
        self
    }

    pub fn with_user(mut self, user: impl Into<String>) -> Self {
        self.user = Some(user.into());
        self
    }

    pub fn with_password(mut self, password: impl Into<String>) -> Self {
        self.password = Some(password.into());
        self
    }

    pub fn with_option(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.options.insert(name.into(), value.into());
        self
    }

    pub fn insert<T: Reflection>(&self, table: &str) -> Result<Insert<T>> {
        Insert::new(self, table)
    }

    pub fn inserter<T: Reflection>(&self, table: &str) -> Result<Inserter<T>> {
        Inserter::new(self, table)
    }

    pub fn query(&self, query: &str) -> Query {
        Query::new(self, query)
    }

    pub fn watch(&self, query: &str) -> Watch {
        Watch::new(self, query)
    }
}