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
use crate::error::Error;
use crate::postgres::query_field::{QueryWithParamsLoc};
use crate::postgres::query_token::{QueryTokens, QueryToken};
use crate::postgres::static_constant::PARAM_NOTATION;

#[derive(Debug)]
pub enum WhereOperator {
    Sql(String),
    ParameterLoc(usize),
    And,
    Or,
    Open,
    Close,
}

#[derive(Debug)]
pub struct QueryWheres(Vec<WhereOperator>);

impl Default for QueryWheres {
    fn default() -> Self {
        QueryWheres(Vec::new())
    }
}

impl From<QueryTokens> for QueryWheres {
    fn from(qtokens: QueryTokens) -> Self {
        let mut qw = Vec::with_capacity(qtokens.0.len());
        for token in qtokens.0 {
            let op = match token {
                QueryToken::Sql(s) => WhereOperator::Sql(s),
                QueryToken::ParameterLoc(p) => WhereOperator::ParameterLoc(p),
            };
            qw.push(op);
        }
        QueryWheres(qw)
    }
}

impl QueryWheres {
    pub fn build(&self, i: &mut i8) -> Result<QueryWithParamsLoc, Error> {
        let mut query = String::from("");
        let mut parameters = Vec::new();
        {
            for wo in &self.0 {
                match wo {
                    WhereOperator::Sql(s) => { query.push_str(&s); },
                    WhereOperator::ParameterLoc(p) => {
                        query.push_str(&format!("{}{}", PARAM_NOTATION, i));
                        parameters.push(p.clone());
                        *i += 1;
                    },
                    WhereOperator::And => { query.push_str(" AND "); },
                    WhereOperator::Or => { query.push_str(" OR "); },
                    WhereOperator::Open => { query.push_str("("); },
                    WhereOperator::Close => { query.push_str(")"); },
                }
            }
        }
        Ok(QueryWithParamsLoc {
            query: query,
            parameters_loc: parameters,
        })
    }
    pub fn len(&self) -> usize {
        self.0.len()
    }
    pub fn push(&mut self, field: WhereOperator) {
        self.0.push(field);
    }
    pub fn extend(&mut self, qwhere: QueryWheres) {
        self.0.extend(qwhere.0);
    }
}