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
use std::sync::Arc;

use crate::proto::client::WeakClient;
use crate::types::Type;
use crate::Column;

pub struct StatementInner {
    client: WeakClient,
    name: String,
    params: Vec<Type>,
    columns: Vec<Column>,
}

impl Drop for StatementInner {
    fn drop(&mut self) {
        if let Some(client) = self.client.upgrade() {
            client.close_statement(&self.name);
        }
    }
}

#[derive(Clone)]
pub struct Statement(Arc<StatementInner>);

impl Statement {
    pub fn new(
        client: WeakClient,
        name: String,
        params: Vec<Type>,
        columns: Vec<Column>,
    ) -> Statement {
        Statement(Arc::new(StatementInner {
            client,
            name,
            params,
            columns,
        }))
    }

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

    pub fn params(&self) -> &[Type] {
        &self.0.params
    }

    pub fn columns(&self) -> &[Column] {
        &self.0.columns
    }
}