forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.rs
More file actions
149 lines (127 loc) · 3.92 KB
/
Copy pathclient.rs
File metadata and controls
149 lines (127 loc) · 3.92 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use fallible_iterator::FallibleIterator;
use futures::{Async, Future, Poll, Stream};
use std::io::{self, Read};
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
use tokio_postgres::types::{ToSql, Type};
#[cfg(feature = "runtime")]
use tokio_postgres::Socket;
use tokio_postgres::{Error, Row, SimpleQueryMessage};
#[cfg(feature = "runtime")]
use crate::Config;
use crate::{CopyOutReader, QueryIter, SimpleQueryIter, Statement, ToStatement, Transaction};
pub struct Client(tokio_postgres::Client);
impl Client {
#[cfg(feature = "runtime")]
pub fn connect<T>(params: &str, tls_mode: T) -> Result<Client, Error>
where
T: MakeTlsConnect<Socket> + 'static + Send,
T::TlsConnect: Send,
T::Stream: Send,
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
params.parse::<Config>()?.connect(tls_mode)
}
#[cfg(feature = "runtime")]
pub fn configure() -> Config {
Config::new()
}
pub fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.0.prepare(query).wait().map(Statement)
}
pub fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
self.0.prepare_typed(query, types).wait().map(Statement)
}
pub fn execute<T>(&mut self, query: &T, params: &[&dyn ToSql]) -> Result<u64, Error>
where
T: ?Sized + ToStatement,
{
let statement = query.__statement(self)?;
self.0.execute(&statement.0, params).wait()
}
pub fn query<T>(&mut self, query: &T, params: &[&dyn ToSql]) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement,
{
self.query_iter(query, params)?.collect()
}
pub fn query_iter<T>(
&mut self,
query: &T,
params: &[&dyn ToSql],
) -> Result<QueryIter<'_>, Error>
where
T: ?Sized + ToStatement,
{
let statement = query.__statement(self)?;
Ok(QueryIter::new(self.0.query(&statement.0, params)))
}
pub fn copy_in<T, R>(
&mut self,
query: &T,
params: &[&dyn ToSql],
reader: R,
) -> Result<u64, Error>
where
T: ?Sized + ToStatement,
R: Read,
{
let statement = query.__statement(self)?;
self.0
.copy_in(&statement.0, params, CopyInStream(reader))
.wait()
}
pub fn copy_out<T>(
&mut self,
query: &T,
params: &[&dyn ToSql],
) -> Result<CopyOutReader<'_>, Error>
where
T: ?Sized + ToStatement,
{
let statement = query.__statement(self)?;
let stream = self.0.copy_out(&statement.0, params);
CopyOutReader::new(stream)
}
pub fn simple_query(&mut self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
self.simple_query_iter(query)?.collect()
}
pub fn simple_query_iter(&mut self, query: &str) -> Result<SimpleQueryIter<'_>, Error> {
Ok(SimpleQueryIter::new(self.0.simple_query(query)))
}
pub fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
self.simple_query("BEGIN")?;
Ok(Transaction::new(self))
}
pub fn is_closed(&self) -> bool {
self.0.is_closed()
}
pub fn get_ref(&self) -> &tokio_postgres::Client {
&self.0
}
pub fn get_mut(&mut self) -> &mut tokio_postgres::Client {
&mut self.0
}
pub fn into_inner(self) -> tokio_postgres::Client {
self.0
}
}
impl From<tokio_postgres::Client> for Client {
fn from(c: tokio_postgres::Client) -> Client {
Client(c)
}
}
struct CopyInStream<R>(R);
impl<R> Stream for CopyInStream<R>
where
R: Read,
{
type Item = Vec<u8>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Option<Vec<u8>>, io::Error> {
let mut buf = vec![];
match self.0.by_ref().take(4096).read_to_end(&mut buf)? {
0 => Ok(Async::Ready(None)),
_ => Ok(Async::Ready(Some(buf))),
}
}
}