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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use futures::future::Future;
use mysql_common::{
packets::{parse_ok_packet, RawPacket},
row::new_row,
value::{read_bin_values, read_text_values},
};
use std::sync::Arc;
use self::{
query_result::QueryResult,
stmt::Stmt,
transaction::{Transaction, TransactionOptions},
};
use crate::{
connection_like::ConnectionLike, consts::Command, error::*, prelude::FromRow, BoxFuture,
Column, Conn, Params, Row,
};
pub mod query_result;
pub mod stmt;
pub mod transaction;
pub trait Protocol: Send + 'static {
fn read_result_set_row(packet: &RawPacket, columns: Arc<Vec<Column>>) -> Result<Row>;
fn is_last_result_set_packet<T>(conn_like: &T, packet: &RawPacket) -> bool
where
T: ConnectionLike,
{
parse_ok_packet(&*packet.0, conn_like.get_capabilities()).is_ok()
}
}
pub struct TextProtocol;
pub struct BinaryProtocol;
impl Protocol for TextProtocol {
fn read_result_set_row(packet: &RawPacket, columns: Arc<Vec<Column>>) -> Result<Row> {
read_text_values(&*packet.0, columns.len())
.map(|values| new_row(values, columns))
.map_err(Into::into)
}
}
impl Protocol for BinaryProtocol {
fn read_result_set_row(packet: &RawPacket, columns: Arc<Vec<Column>>) -> Result<Row> {
read_bin_values(&*packet.0, &*columns)
.map(|values| new_row(values, columns))
.map_err(Into::into)
}
fn is_last_result_set_packet<T>(conn_like: &T, packet: &RawPacket) -> bool
where
T: ConnectionLike,
{
(parse_ok_packet(&*packet.0, conn_like.get_capabilities()).is_ok() && packet.0[0] == 0xFE)
}
}
pub trait Queryable: ConnectionLike
where
Self: Sized + 'static,
{
fn ping(self) -> BoxFuture<Self> {
let fut = self
.write_command_data(Command::COM_PING, &[])
.and_then(|this| this.read_packet())
.map(|(this, _)| this);
Box::new(fut)
}
fn disconnect(mut self) -> BoxFuture<()> {
self.on_disconnect();
let fut = self.write_command_data(Command::COM_QUIT, &[]).map(|_| ());
Box::new(fut)
}
fn query<Q: AsRef<str>>(self, query: Q) -> BoxFuture<QueryResult<Self, TextProtocol>> {
let fut = self
.write_command_data(Command::COM_QUERY, query.as_ref().as_bytes())
.and_then(|conn_like| conn_like.read_result_set(None));
Box::new(fut)
}
fn first<Q, R>(self, query: Q) -> BoxFuture<(Self, Option<R>)>
where
Q: AsRef<str>,
R: FromRow,
{
let fut = self
.query(query)
.and_then(|result| result.collect_and_drop::<Row>())
.map(|(this, mut rows)| {
if rows.len() > 1 {
(this, Some(FromRow::from_row(rows.swap_remove(0))))
} else {
(this, rows.pop().map(FromRow::from_row))
}
});
Box::new(fut)
}
fn drop_query<Q: AsRef<str>>(self, query: Q) -> BoxFuture<Self> {
let fut = self.query(query).and_then(|result| result.drop_result());
Box::new(fut)
}
fn prepare<Q: AsRef<str>>(self, query: Q) -> BoxFuture<Stmt<Self>> {
let fut = self
.prepare_stmt(query)
.map(|(this, inner_stmt, stmt_cache_result)| {
stmt::new(this, inner_stmt, stmt_cache_result)
});
Box::new(fut)
}
fn prep_exec<Q, P>(self, query: Q, params: P) -> BoxFuture<QueryResult<Self, BinaryProtocol>>
where
Q: AsRef<str>,
P: Into<Params>,
{
let params: Params = params.into();
let fut = self
.prepare(query)
.and_then(|stmt| stmt.execute(params))
.map(|result| {
let (stmt, columns, _) = query_result::disassemble(result);
let (conn_like, cached) = stmt.unwrap();
query_result::assemble(conn_like, columns, cached)
});
Box::new(fut)
}
fn first_exec<Q, P, R>(self, query: Q, params: P) -> BoxFuture<(Self, Option<R>)>
where
Q: AsRef<str>,
P: Into<Params>,
R: FromRow,
{
let fut = self
.prep_exec(query, params)
.and_then(|result| result.collect_and_drop::<Row>())
.map(|(this, mut rows)| {
if rows.len() > 1 {
(this, Some(FromRow::from_row(rows.swap_remove(0))))
} else {
(this, rows.pop().map(FromRow::from_row))
}
});
Box::new(fut)
}
fn drop_exec<Q, P>(self, query: Q, params: P) -> BoxFuture<Self>
where
Q: AsRef<str>,
P: Into<Params>,
{
let fut = self
.prep_exec(query, params)
.and_then(|result| result.drop_result());
Box::new(fut)
}
fn batch_exec<Q, I, P>(self, query: Q, params_iter: I) -> BoxFuture<Self>
where
Q: AsRef<str>,
I: IntoIterator<Item = P> + Send + 'static,
I::IntoIter: Send + 'static,
Params: From<P>,
P: Send + 'static,
{
let fut = self
.prepare(query)
.and_then(|stmt| stmt.batch(params_iter))
.and_then(|stmt| stmt.close());
Box::new(fut)
}
fn start_transaction(self, options: TransactionOptions) -> BoxFuture<Transaction<Self>> {
Box::new(transaction::new(self, options))
}
}
impl Queryable for Conn {}
impl<T: Queryable + ConnectionLike> Queryable for Transaction<T> {}