swadhinbiswas WIP save-point: production hardening, H3 removal, fmt gate, and accumulated phase work · 7/15/2026
2080d40
282 lines · 8.1 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Basic query builder for constructing SQL statements.
//!
//! Provides `Select`, `Insert`, `Update`, and `Delete` builders.
//! These are simple string builders — they don't provide compile-time
//! query checking (use raw `sqlx::query!()` for that).

/// Build a SELECT query.
#[derive(Debug, Clone)]
pub struct Select {
    table: String,
    columns: Vec<String>,
    conditions: Vec<String>,
    order_by: Option<String>,
    limit: Option<u64>,
    offset: Option<u64>,
}

impl Select {
    pub fn new(table: impl Into<String>) -> Self {
        Self {
            table: table.into(),
            columns: vec!["*".into()],
            conditions: Vec::new(),
            order_by: None,
            limit: None,
            offset: None,
        }
    }

    pub fn columns(mut self, columns: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.columns = columns.into_iter().map(|c| c.into()).collect();
        self
    }

    pub fn r#where(mut self, condition: impl Into<String>) -> Self {
        self.conditions.push(condition.into());
        self
    }

    pub fn order_by(mut self, column: impl Into<String>) -> Self {
        self.order_by = Some(column.into());
        self
    }

    pub fn limit(mut self, limit: u64) -> Self {
        self.limit = Some(limit);
        self
    }

    pub fn offset(mut self, offset: u64) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Build the SQL string.
    pub fn build(&self) -> String {
        let cols = self.columns.join(", ");
        let mut sql = format!("SELECT {} FROM {}", cols, self.table);
        if !self.conditions.is_empty() {
            sql.push_str(" WHERE ");
            sql.push_str(&self.conditions.join(" AND "));
        }
        if let Some(ref ob) = self.order_by {
            sql.push_str(" ORDER BY ");
            sql.push_str(ob);
        }
        if let Some(limit) = self.limit {
            sql.push_str(" LIMIT ");
            sql.push_str(&limit.to_string());
        }
        if let Some(offset) = self.offset {
            sql.push_str(" OFFSET ");
            sql.push_str(&offset.to_string());
        }
        sql
    }

    pub fn build_count(&self) -> String {
        let mut sql = format!("SELECT COUNT(*) as count FROM {}", self.table);
        if !self.conditions.is_empty() {
            sql.push_str(" WHERE ");
            sql.push_str(&self.conditions.join(" AND "));
        }
        sql
    }
}

/// Build an INSERT query.
#[derive(Debug, Clone)]
pub struct Insert {
    table: String,
    columns: Vec<String>,
    values: Vec<Vec<String>>,
    returning: Option<Vec<String>>,
}

impl Insert {
    pub fn new(table: impl Into<String>) -> Self {
        Self { table: table.into(), columns: Vec::new(), values: Vec::new(), returning: None }
    }

    pub fn columns(mut self, columns: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.columns = columns.into_iter().map(|c| c.into()).collect();
        self
    }

    pub fn values(mut self, values: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.values.push(values.into_iter().map(|v| v.into()).collect());
        self
    }

    pub fn returning(mut self, columns: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.returning = Some(columns.into_iter().map(|c| c.into()).collect());
        self
    }

    /// Build the SQL string.
    pub fn build(&self) -> String {
        let cols = self.columns.join(", ");
        let placeholders: Vec<String> = self
            .values
            .iter()
            .map(|row| {
                let params: Vec<String> = row.iter().map(|_| "?".to_string()).collect();
                format!("({})", params.join(", "))
            })
            .collect();
        let mut sql =
            format!("INSERT INTO {} ({}) VALUES {}", self.table, cols, placeholders.join(", "));
        if let Some(ref ret) = self.returning {
            sql.push_str(" RETURNING ");
            sql.push_str(&ret.join(", "));
        }
        sql
    }
}

/// Build an UPDATE query.
#[derive(Debug, Clone)]
pub struct Update {
    table: String,
    sets: Vec<String>,
    conditions: Vec<String>,
    returning: Option<Vec<String>>,
}

impl Update {
    pub fn new(table: impl Into<String>) -> Self {
        Self { table: table.into(), sets: Vec::new(), conditions: Vec::new(), returning: None }
    }

    pub fn set(mut self, column: impl Into<String>, placeholder: impl Into<String>) -> Self {
        self.sets.push(format!("{} = {}", column.into(), placeholder.into()));
        self
    }

    pub fn r#where(mut self, condition: impl Into<String>) -> Self {
        self.conditions.push(condition.into());
        self
    }

    pub fn returning(mut self, columns: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.returning = Some(columns.into_iter().map(|c| c.into()).collect());
        self
    }

    pub fn build(&self) -> String {
        let sets = self.sets.join(", ");
        let mut sql = format!("UPDATE {} SET {}", self.table, sets);
        if !self.conditions.is_empty() {
            sql.push_str(" WHERE ");
            sql.push_str(&self.conditions.join(" AND "));
        }
        if let Some(ref ret) = self.returning {
            sql.push_str(" RETURNING ");
            sql.push_str(&ret.join(", "));
        }
        sql
    }
}

/// Build a DELETE query.
#[derive(Debug, Clone)]
pub struct Delete {
    table: String,
    conditions: Vec<String>,
    returning: Option<Vec<String>>,
}

impl Delete {
    pub fn new(table: impl Into<String>) -> Self {
        Self { table: table.into(), conditions: Vec::new(), returning: None }
    }

    pub fn r#where(mut self, condition: impl Into<String>) -> Self {
        self.conditions.push(condition.into());
        self
    }

    pub fn returning(mut self, columns: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.returning = Some(columns.into_iter().map(|c| c.into()).collect());
        self
    }

    pub fn build(&self) -> String {
        let mut sql = format!("DELETE FROM {}", self.table);
        if !self.conditions.is_empty() {
            sql.push_str(" WHERE ");
            sql.push_str(&self.conditions.join(" AND "));
        }
        if let Some(ref ret) = self.returning {
            sql.push_str(" RETURNING ");
            sql.push_str(&ret.join(", "));
        }
        sql
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_select_all() {
        let q = Select::new("users").build();
        assert_eq!(q, "SELECT * FROM users");
    }

    #[test]
    fn test_select_with_where() {
        let q = Select::new("users")
            .columns(["id", "name", "email"])
            .r#where("id = ?")
            .r#where("active = true")
            .build();
        assert_eq!(q, "SELECT id, name, email FROM users WHERE id = ? AND active = true");
    }

    #[test]
    fn test_select_with_limit_offset() {
        let q = Select::new("users").order_by("name ASC").limit(10).offset(20).build();
        assert_eq!(q, "SELECT * FROM users ORDER BY name ASC LIMIT 10 OFFSET 20");
    }

    #[test]
    fn test_insert() {
        let q = Insert::new("users")
            .columns(["name", "email"])
            .values(["Alice", "[email protected]"])
            .build();
        assert_eq!(q, "INSERT INTO users (name, email) VALUES (?, ?)");
    }

    #[test]
    fn test_insert_returning() {
        let q = Insert::new("users")
            .columns(["name", "email"])
            .values(["Alice", "[email protected]"])
            .returning(["id"])
            .build();
        assert_eq!(q, "INSERT INTO users (name, email) VALUES (?, ?) RETURNING id");
    }

    #[test]
    fn test_update() {
        let q = Update::new("users").set("name", "?").r#where("id = ?").build();
        assert_eq!(q, "UPDATE users SET name = ? WHERE id = ?");
    }

    #[test]
    fn test_delete() {
        let q = Delete::new("users").r#where("id = ?").build();
        assert_eq!(q, "DELETE FROM users WHERE id = ?");
    }

    #[test]
    fn test_select_build_count() {
        let q = Select::new("users").r#where("active = true").build_count();
        assert_eq!(q, "SELECT COUNT(*) as count FROM users WHERE active = true");
    }
}