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
//! Prepared statements.

use std::mem;
use std::sync::Arc;
use std::sync::mpsc::Sender;

#[doc(inline)]
pub use postgres_shared::stmt::Column;

use StatementNew;
use types::Type;

/// A prepared statement.
pub struct Statement {
    close_sender: Sender<(u8, String)>,
    name: String,
    params: Vec<Type>,
    columns: Arc<Vec<Column>>,
}

impl StatementNew for Statement {
    fn new(close_sender: Sender<(u8, String)>,
           name: String,
           params: Vec<Type>,
           columns: Arc<Vec<Column>>)
           -> Statement {
        Statement {
            close_sender: close_sender,
            name: name,
            params: params,
            columns: columns,
        }
    }

    fn columns_arc(&self) -> &Arc<Vec<Column>> {
        &self.columns
    }

    fn name(&self) -> &str {
        &self.name
    }
}

impl Drop for Statement {
    fn drop(&mut self) {
        let name = mem::replace(&mut self.name, String::new());
        let _ = self.close_sender.send((b'S', name));
    }
}

impl Statement {
    /// Returns the types of query parameters for this statement.
    pub fn parameters(&self) -> &[Type] {
        &self.params
    }

    /// Returns information about the resulting columns for this statement.
    pub fn columns(&self) -> &[Column] {
        &self.columns
    }
}