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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright (c) 2016 Anatoly Ikorsky
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use conn::Conn;
use conn::futures::new_raw_query_result::NewRawQueryResult;
use conn::futures::query_result::futures::*;
use conn::futures::read_packet::ReadPacket;
use conn::stmt::InnerStmt;
use conn::stmt::new_stmt;
use conn::stmt::Stmt;
use consts;
use either::Either;
use either::Left;
use either::Right;
use errors::*;
use lib_futures::Async;
use lib_futures::Async::Ready;
use lib_futures::Future;
use lib_futures::Poll;
use lib_futures::stream::Stream;
use proto::Column;
use proto::OkPacket;
use proto::Packet;
use proto::PacketType;
use proto::Row;
use std::borrow::Cow;
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::sync::Arc;
use value::FromRow;
use value::Value;


pub mod futures;


/// Result set of a query or statement execution.
///
/// It represents query result which has been collected.
/// It could be dereferenced into it's query result to get access to `QueryResult` methods.
pub struct ResultSet<T, Q>(pub Vec<T>, Q);

impl<T, Q: QueryResult> ResultSet<T, Q> {
    pub fn as_ref(&self) -> &[T] {
        &*self.0
    }
}

impl<T, Q: QueryResult> Deref for ResultSet<T, Q> {
    type Target = Q;

    fn deref(&self) -> &Self::Target {
        &self.1
    }
}

impl<T, Q: QueryResult> IntoIterator for ResultSet<T, Q> {
    type Item = T;
    type IntoIter = ::std::vec::IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

/// MySql protocol.
#[derive(Eq, PartialEq, Copy, Clone, Debug, Hash)]
pub enum Protocol {
    /// MySql text protocol.
    Text,
    /// MySql binary protocol (i.e. prepared statements).
    Binary,
}

/// Result kind of a query result.
pub trait ResultKind {
    /// Output of this result kind.
    ///
    /// For `TextResult` it is either next `TextQueryResult` of a multi-result set or `Conn` on
    /// which query was executed.
    ///
    /// For `BinaryResult` it is `Stmt` which was executed.
    type Output: Sized;

    /// A way to determine protocol at runtime.
    fn protocol() -> Protocol;
}

pub trait InnerResultKind: ResultKind {
    fn read_values(packet: Packet, cols: &Arc<Vec<Column>>) -> Result<Vec<Value>>;
    fn handle_next_query_result(next: RawQueryResult<Self>) -> Self::Output;
    fn handle_end(conn: Conn, inner_stmt: Option<InnerStmt>) -> Self::Output;
}

/// Binary protocol result (i.e. statement execution result).
pub struct BinaryResult;

impl ResultKind for BinaryResult {
    type Output = Stmt;

    fn protocol() -> Protocol {
        Protocol::Binary
    }
}

impl InnerResultKind for BinaryResult {
    fn read_values(packet: Packet, cols: &Arc<Vec<Column>>) -> Result<Vec<Value>> {
        Value::from_bin_payload(packet.as_ref(), cols)
    }

    fn handle_next_query_result(_: RawQueryResult<Self>) -> Self::Output {
        panic!("Binary protocol query can't produce multi-result set");
    }

    fn handle_end(conn: Conn, inner_stmt: Option<InnerStmt>) -> Self::Output {
        new_stmt(inner_stmt.unwrap(), conn)
    }
}

/// Text protocol result (i.e. query execution result).
pub struct TextResult;

impl ResultKind for TextResult {
    type Output = Either<TextQueryResult, Conn>;

    fn protocol() -> Protocol {
        Protocol::Text
    }
}

impl InnerResultKind for TextResult {
    fn read_values(packet: Packet, cols: &Arc<Vec<Column>>) -> Result<Vec<Value>> {
        Value::from_payload(packet.as_ref(), cols.len())
    }

    fn handle_next_query_result(next: RawQueryResult<Self>) -> Self::Output {
        Left(next.into())
    }

    fn handle_end(conn: Conn, _: Option<InnerStmt>) -> Self::Output {
        Right(conn)
    }
}

enum Step<K: ResultKind + ?Sized> {
    ReadPacket(ReadPacket),
    NextResult(NewRawQueryResult<K>),
    Done(Conn),
    Consumed,
}

enum Out<K: ResultKind + ?Sized> {
    ReadPacket((Conn, Packet)),
    NextResult(RawQueryResult<K>),
    Done,
}

/// Raw query result implementation parametrized by it's kind.
pub struct RawQueryResult<K: ResultKind + ?Sized> {
    step: Step<K>,
    columns: Arc<Vec<Column>>,
    ok_packet: Option<OkPacket>,
    inner_stmt: Option<InnerStmt>,
    _phantom: PhantomData<K>,
}

pub fn new_raw<K: ?Sized, T>(mut conn: Conn,
                             cols: T,
                             ok_packet: Option<OkPacket>,
                             inner_stmt: Option<InnerStmt>)
                             -> RawQueryResult<K>
    where T: Into<Arc<Vec<Column>>>,
          K: ResultKind,
{
    let cols = cols.into();
    let step = if cols.len() == 0 {
        Step::Done(conn)
    } else {
        conn.has_result = Some((cols.clone(), ok_packet.clone(), inner_stmt.clone()));
        Step::ReadPacket(conn.read_packet())
    };

    RawQueryResult {
        step: step,
        columns: cols,
        ok_packet: ok_packet,
        inner_stmt: inner_stmt,
        _phantom: PhantomData,
    }
}

impl<K: ResultKind> RawQueryResult<K> {
    fn either_poll(&mut self) -> Result<Async<Option<Out<K>>>> {
        match self.step {
            Step::ReadPacket(ref mut fut) => {
                let val = try_ready!(fut.poll());
                Ok(Ready(Some(Out::ReadPacket(val))))
            },
            Step::NextResult(ref mut fut) => {
                let val = try_ready!(fut.poll());
                Ok(Ready(Some(Out::NextResult(val))))
            },
            Step::Done(_) => Ok(Ready(Some(Out::Done))),
            Step::Consumed => Ok(Ready(None)),
        }
    }
}

impl<K: ResultKind + InnerResultKind> Stream for RawQueryResult<K> {
    type Item = Either<Row, K::Output>;
    type Error = Error;

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        match try_ready!(self.either_poll()) {
            Some(Out::ReadPacket((mut conn, packet))) => {
                if packet.is(PacketType::Eof) {
                    if conn.status.contains(consts::SERVER_MORE_RESULTS_EXISTS) {
                        self.step =
                            Step::NextResult(conn.handle_result_set::<K>(self.inner_stmt.clone()));
                        self.poll()
                    } else {
                        conn.has_result = None;
                        self.step = Step::Done(conn);
                        self.poll()
                    }
                } else {
                    let values = K::read_values(packet, &self.columns)?;
                    let row = Row::new(values, self.columns.clone());
                    self.step = Step::ReadPacket(conn.read_packet());
                    Ok(Ready(Some(Left(row))))
                }
            },
            Some(Out::NextResult(query_result)) => {
                let output = K::handle_next_query_result(query_result);
                self.step = Step::Consumed;
                Ok(Ready(Some(Right(output))))
            },
            Some(Out::Done) => {
                if let Step::Done(mut conn) = mem::replace(&mut self.step, Step::Consumed) {
                    conn.has_result = None;
                    let output = K::handle_end(conn, self.inner_stmt.clone());
                    Ok(Ready(Some(Right(output))))
                } else {
                    unreachable!();
                }
            },
            None => Ok(Ready(None)),
        }
    }
}

/// Final result of a query result.
pub trait QueryResultOutput {
    /// Type of query result for this query result output.
    type Result: QueryResult;
    /// Output of a query result output.
    ///
    /// It is `Conn` for text protocol or `Stmt` fot binary protocol.
    type Output;

    /// A way to generically extract possible next result and output of query result output.
    ///
    /// Returns (previous result, Either<next result, output>)
    fn into_next_or_output(self,
                           prev: Self::Result)
                           -> (Self::Result, Either<Self::Result, Self::Output>);
}

impl QueryResultOutput for Stmt {
    type Result = BinQueryResult;
    type Output = Stmt;

    fn into_next_or_output(self,
                           prev: BinQueryResult)
                           -> (Self::Result, Either<Self::Result, Self::Output>) {
        (prev, Right(self))
    }
}

impl QueryResultOutput for Either<TextQueryResult, Conn> {
    type Result = TextQueryResult;
    type Output = Conn;

    fn into_next_or_output(self,
                           prev: TextQueryResult)
                           -> (Self::Result, Either<Self::Result, Self::Output>) {
        (prev, self)
    }
}

/// Query result which was not consumed yet.
pub trait UnconsumedQueryResult: InnerQueryResult {
    type Output: QueryResultOutput;

    /// Returns future which collects result set of this query result.
    ///
    /// It is parametrized by `R` and internally calls `R::from_row(Row)` on each row.
    fn collect<R>(self) -> Collect<R, Self>
        where Self: Sized,
              R: FromRow,
    {
        new_collect(self)
    }

    /// It returns future that collects all result sets of a multi-result set.
    ///
    /// Since only text protocol result could be a multi-result set this method makes sense only for
    /// `TextQueryResult`.
    fn collect_all(self) -> CollectAll<Self>
        where Self: Sized,
    {
        new_collect_all(self)
    }

    /// It returns future that maps every `Row` of this query result to `U`.
    fn map<F, U>(self, fun: F) -> Map<F, U, Self>
        where Self: Sized,
              F: FnMut(Row) -> U,
    {
        new_map(self, fun)
    }

    /// It returns future that reduce rows of this query result to an instance of `A`.
    fn reduce<A, F>(self, init: A, fun: F) -> Reduce<A, F, Self>
        where Self: Sized,
              F: FnMut(A, Row) -> A,
    {
        new_reduce(self, init, fun)
    }

    /// It returns future that applies `F` to every `Row` of this query result.
    fn for_each<F>(self, fun: F) -> ForEach<F, Self>
        where Self: Sized,
              F: FnMut(Row),
    {
        new_for_each(self, fun)
    }

    /// It returns future that drops result and resolves to wrapped `Conn` on `Stmt`.
    fn drop_result(self) -> DropResult<Self>
        where Self: Sized,
    {
        new_drop_result(self)
    }
}

/// Inner methods of `QueryResult`.
pub trait InnerQueryResult: QueryResult {
    /// Polls another row of this query result.
    fn poll(&mut self) -> Result<Async<Either<Row, Self::Output>>> where Self: UnconsumedQueryResult;

    fn ok_packet_ref(&self) -> Option<&OkPacket>;
}

/// Common methods of all query results.
pub trait QueryResult {
    /// Affected rows value returned by a server, if any.
    fn affected_rows(&self) -> Option<u64>
        where Self: InnerQueryResult,
    {
        self.ok_packet_ref().map(OkPacket::affected_rows)
    }

    /// Last insert id value returned by a server, if any.
    fn last_insert_id(&self) -> Option<u64>
        where Self: InnerQueryResult,
    {
        self.ok_packet_ref().map(OkPacket::last_insert_id)
    }

    /// Warnings count value returned by a server, if any.
    fn warnings(&self) -> Option<u16>
        where Self: InnerQueryResult,
    {
        self.ok_packet_ref().map(OkPacket::warnings)
    }

    /// Bytes of info string returned by a server, if any.
    fn info_bytes(&self) -> Option<&[u8]>
        where Self: InnerQueryResult,
    {
        self.ok_packet_ref().map(OkPacket::info_bytes)
    }

    /// Info bytes lossy converted to UTF-8, if any.
    fn info(&self) -> Option<Cow<str>>
        where Self: InnerQueryResult,
    {
        self.ok_packet_ref().map(OkPacket::info)
    }

    /// Bytes of session state changed value returned by a server, if any.
    fn session_state_changes_bytes(&self) -> Option<&[u8]>
        where Self: InnerQueryResult,
    {
        self.ok_packet_ref().and_then(OkPacket::session_state_changes_bytes)
    }

    /// Session state changed value lossy converted to UTF-8, if any.
    fn session_state_changes(&self) -> Option<Cow<str>>
        where Self: InnerQueryResult,
    {
        self.ok_packet_ref().and_then(OkPacket::session_state_changes)
    }
}

/// Uncollected result of a query execution.
pub struct TextQueryResult(RawQueryResult<TextResult>);

#[doc(hidden)]
impl From<RawQueryResult<TextResult>> for TextQueryResult {
    fn from(raw_query_result: RawQueryResult<TextResult>) -> Self {
        TextQueryResult(raw_query_result)
    }
}

impl QueryResult for TextQueryResult {}

impl UnconsumedQueryResult for TextQueryResult {
    type Output = <TextResult as ResultKind>::Output;
}

impl InnerQueryResult for TextQueryResult {
    fn poll(&mut self) -> Result<Async<Either<Row, <Self as UnconsumedQueryResult>::Output>>>
        where Self: UnconsumedQueryResult,
    {
        let result = try_ready!(self.0.poll());
        Ok(Ready(result.expect("Can't poll consumed query result")))
    }

    fn ok_packet_ref(&self) -> Option<&OkPacket> {
        self.0.ok_packet.as_ref()
    }
}

/// Uncollected result of a statement execution.
pub struct BinQueryResult(RawQueryResult<BinaryResult>);

#[doc(hidden)]
impl From<RawQueryResult<BinaryResult>> for BinQueryResult {
    fn from(raw_query_result: RawQueryResult<BinaryResult>) -> Self {
        BinQueryResult(raw_query_result)
    }
}

impl QueryResult for BinQueryResult {}

impl UnconsumedQueryResult for BinQueryResult {
    type Output = <BinaryResult as ResultKind>::Output;
}

impl InnerQueryResult for BinQueryResult {
    #[doc(hidden)]
    fn poll(&mut self) -> Result<Async<Either<Row, <Self as UnconsumedQueryResult>::Output>>>
        where Self: UnconsumedQueryResult,
    {
        let result = try_ready!(self.0.poll());
        Ok(Ready(result.expect("Can't poll consumed query result")))
    }

    #[doc(hidden)]
    fn ok_packet_ref(&self) -> Option<&OkPacket> {
        self.0.ok_packet.as_ref()
    }
}