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
use std::any::Any;
use std::time::Instant;

use crate::{ClickhouseDialect, Condition, DeleteClause, Field, InsertClause, InsertValue, LimitClause, PostgresDialect, Selectable, SelectClause, UpdateClause, WhereClause};
use crate::client::{ClientManager, QueryResult};
use crate::error::*;

#[derive(Debug)]
pub enum QueryType {
    Insert,
    Delete,
    Update,
    Select,
}

pub struct QueryBuilder {
    pub query_type: QueryType,
    pub dialect: String,
    pub insert_clause: InsertClause,
    pub delete_clause: DeleteClause,
    pub update_clause: UpdateClause,
    pub where_clause: WhereClause,
    pub select_clause: SelectClause,
    pub limit_clause: LimitClause,
}

pub trait ToSql {
    fn to_sql(&self) -> Result<String>;
}

impl ToSql for QueryBuilder {
    fn to_sql(&self) -> Result<String> {
        // TODO: implement Dialect trait
        let dialect: Box<dyn ToString> = match self.dialect.as_str() {
            // TODO: dialect name -> const
            "postgres" => Box::new(PostgresDialect::new(&self)),
            "clickhouse" => Box::new(ClickhouseDialect::new(&self)),
            _ => unimplemented!(),
        };

        Ok(dialect.to_string())
    }
}

impl QueryBuilder {
    pub fn insert(dialect: String) -> Self {
        Self {
            query_type: QueryType::Insert,
            dialect,
            insert_clause: InsertClause::default(),
            delete_clause: DeleteClause::default(),
            update_clause: UpdateClause::default(),
            where_clause: WhereClause::default(),
            select_clause: SelectClause::default(),
            limit_clause: Default::default(),
        }
    }

    pub fn delete(dialect: String) -> Self {
        Self {
            query_type: QueryType::Delete,
            dialect,
            insert_clause: InsertClause::default(),
            delete_clause: DeleteClause::default(),
            update_clause: UpdateClause::default(),
            where_clause: WhereClause::default(),
            select_clause: SelectClause::default(),
            limit_clause: Default::default(),
        }
    }

    pub fn update(dialect: String) -> Self {
        Self {
            query_type: QueryType::Update,
            dialect,
            insert_clause: InsertClause::default(),
            delete_clause: DeleteClause::default(),
            update_clause: UpdateClause::default(),
            where_clause: WhereClause::default(),
            select_clause: SelectClause::default(),
            limit_clause: Default::default(),
        }
    }

    pub fn select_(dialect: String, values: Vec<Selectable>) -> Self {
        Self {
            query_type: QueryType::Select,
            dialect,
            insert_clause: InsertClause::default(),
            delete_clause: DeleteClause::default(),
            update_clause: UpdateClause::default(),
            where_clause: WhereClause::default(),
            select_clause: SelectClause::from_values(values),
            limit_clause: Default::default(),
        }
    }

    pub fn select(&mut self, values: Vec<Selectable>) -> &mut Self {
        self.select_clause.values.extend(values);

        self
    }

    pub fn model(&mut self, table_name: String, fields: Vec<Field>) -> &mut Self {
        match &self.query_type {
            QueryType::Insert => {
                self.insert_clause.table_name = table_name;
                self.insert_clause.fields = fields;
            },
            QueryType::Delete => {
                self.delete_clause.table_name = table_name;
            },
            QueryType::Update => {
                self.update_clause.table_name = table_name;
            },
            QueryType::Select => {
                self.select_clause.table_name = table_name;
            }
        }

        self
    }

    pub fn values(&mut self, values: Vec<InsertValue>) -> &mut Self {
        match &self.query_type {
            QueryType::Insert => {
                self.insert_clause.values = values;
            },
            _ => unimplemented!(),
        }

        self
    }

    pub fn filter(&mut self, conditions: Vec<Condition>) -> &mut Self {
        for c in conditions {
            self.where_clause.conditions.push(c);
        }

        self
    }

    pub fn set<T: Any + Clone>(&mut self, field: Field, value: &T) -> &mut Self {
        match &self.query_type {
            QueryType::Update => {
                self.update_clause.values.insert(field, Box::new(value.clone()));

                // self.update_clause.values = values;
            },
            _ => unimplemented!(),
        };

        self
    }

    pub fn limit(&mut self, limit: i32) -> &mut Self {
        self.limit_clause.limit = Some(limit);

        self
    }

    pub fn offset(&mut self, offset: i32) -> &mut Self {
        self.limit_clause.offset = Some(offset);

        self
    }

    pub fn exec(&self, client_manager: &mut ClientManager) -> Result<QueryResult> {
        let client = client_manager.get_client(&self.dialect).unwrap();
        let query = self.to_sql().unwrap();
        let now = Instant::now();

        let result = client.query(query.as_str());

        println!("Compiled query({}, {}ms): {}", self.dialect, now.elapsed().as_millis(), query);

        result
    }
}