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
use r2d2;
use std::cell::RefCell;

use cluster::{GetCompressor, GetConnection};
use error;
use frame::parser::from_connection;
use frame::{Flag, Frame, IntoBytes};
use query::{Query, QueryParams, QueryParamsBuilder, QueryValues};
use transport::CDRSTransport;

pub trait QueryExecutor<
  T: CDRSTransport + 'static,
  M: r2d2::ManageConnection<Connection = RefCell<T>, Error = error::Error> + Sized,
>: GetConnection<T, M> + GetCompressor<'static>
{
  fn query_with_params_tw<Q: ToString>(
    &self,
    query: Q,
    query_params: QueryParams,
    with_tracing: bool,
    with_warnings: bool,
  ) -> error::Result<Frame> {
    let query = Query {
      query: query.to_string(),
      params: query_params,
    };

    let mut flags = vec![];

    if with_tracing {
      flags.push(Flag::Tracing);
    }

    if with_warnings {
      flags.push(Flag::Warning);
    }

    let query_frame = Frame::new_query(query, flags).into_cbytes();
    let ref compression = self.get_compressor();

    self
      .get_connection()
      .ok_or(error::Error::from("Unable to get transport"))
      .and_then(|transport_cell| {
        let write_res = transport_cell
          .borrow_mut()
          .write(query_frame.as_slice())
          .map_err(error::Error::from);
        write_res.map(|_| transport_cell)
      })
      .and_then(|transport_cell| from_connection(&transport_cell, compression))
  }

  /// Executes a query with default parameters:
  /// * TDB
  fn query<Q: ToString>(&self, query: Q) -> error::Result<Frame> {
    self.query_tw(query, false, false)
  }

  /// Executes a query with ability to trace it and see warnings, and default parameters:
  /// * TBD
  fn query_tw<Q: ToString>(
    &self,
    query: Q,
    with_tracing: bool,
    with_warnings: bool,
  ) -> error::Result<Frame> {
    let query_params = QueryParamsBuilder::new().finalize();
    self.query_with_params_tw(query, query_params, with_tracing, with_warnings)
  }

  /// Executes a query with bounded values (either with or without names).
  fn query_with_values<Q: ToString, V: Into<QueryValues>>(
    &self,
    query: Q,
    values: V,
  ) -> error::Result<Frame> {
    self.query_with_values_tw(query, values, false, false)
  }

  /// Executes a query with bounded values (either with or without names)
  /// and ability to see warnings, trace a request and default parameters.
  fn query_with_values_tw<Q: ToString, V: Into<QueryValues>>(
    &self,
    query: Q,
    values: V,
    with_tracing: bool,
    with_warnings: bool,
  ) -> error::Result<Frame> {
    let query_params_builder = QueryParamsBuilder::new();
    let query_params = query_params_builder.values(values.into()).finalize();
    self.query_with_params_tw(query, query_params, with_tracing, with_warnings)
  }

  /// Executes a query with query params without warnings and tracing.
  fn query_with_params<Q: ToString>(
    &self,
    query: Q,
    query_params: QueryParams,
  ) -> error::Result<Frame> {
    self.query_with_params_tw(query, query_params, false, false)
  }
}