oxisql-core 0.3.1

Core traits and types for OxiSQL — the COOLJAPAN Pure-Rust SQL facade
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Fluent query builder for constructing SQL strings with positional parameters.

use crate::{ToSqlValue, Value};

/// Sort direction for ORDER BY clauses.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum SortDirection {
    /// Ascending order (default).
    #[default]
    Asc,
    /// Descending order.
    Desc,
}

impl std::fmt::Display for SortDirection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SortDirection::Asc => write!(f, "ASC"),
            SortDirection::Desc => write!(f, "DESC"),
        }
    }
}

/// A fully-built SQL query: the SQL string and its bound parameter values.
#[derive(Debug, Clone, PartialEq)]
pub struct BuiltQuery {
    /// The SQL string with `$1`, `$2`, … placeholders.
    pub sql: String,
    /// Parameter values in order.
    pub params: Vec<Value>,
}

/// Fluent builder for SELECT queries.
///
/// # Example
///
/// ```rust
/// use oxisql_core::query::{SelectBuilder, SortDirection};
///
/// let q = SelectBuilder::new()
///     .columns(&["id", "name", "email"])
///     .from("users")
///     .where_eq("active", &true)
///     .order_by("name", Default::default())
///     .limit(20)
///     .offset(40)
///     .build();
///
/// assert_eq!(q.sql, "SELECT id, name, email FROM users WHERE active = $1 ORDER BY name ASC LIMIT 20 OFFSET 40");
/// ```
#[derive(Debug, Default, Clone)]
pub struct SelectBuilder {
    columns: Vec<String>,
    table: Option<String>,
    conditions: Vec<String>,
    params: Vec<Value>,
    order_by: Vec<(String, SortDirection)>,
    limit: Option<usize>,
    offset: Option<usize>,
}

impl SelectBuilder {
    /// Create a new empty SELECT builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add columns to SELECT. Call multiple times or pass a slice.
    pub fn columns(mut self, cols: &[&str]) -> Self {
        self.columns.extend(cols.iter().map(|s| s.to_string()));
        self
    }

    /// Set the FROM table.
    pub fn from(mut self, table: &str) -> Self {
        self.table = Some(table.to_string());
        self
    }

    /// Add a raw WHERE condition (e.g. `"id > $1"`). Call multiple times to AND them.
    pub fn where_raw(mut self, condition: &str) -> Self {
        self.conditions.push(condition.to_string());
        self
    }

    /// Add a `WHERE col = $N` condition, binding the value.
    pub fn where_eq(mut self, col: &str, val: &dyn ToSqlValue) -> Self {
        let n = self.params.len() + 1;
        self.conditions.push(format!("{col} = ${n}"));
        self.params.push(val.to_value());
        self
    }

    /// Add an ORDER BY clause.
    pub fn order_by(mut self, col: &str, dir: SortDirection) -> Self {
        self.order_by.push((col.to_string(), dir));
        self
    }

    /// Set LIMIT.
    pub fn limit(mut self, n: usize) -> Self {
        self.limit = Some(n);
        self
    }

    /// Set OFFSET.
    pub fn offset(mut self, n: usize) -> Self {
        self.offset = Some(n);
        self
    }

    /// Build the query into a [`BuiltQuery`].
    pub fn build(self) -> BuiltQuery {
        let cols = if self.columns.is_empty() {
            "*".to_string()
        } else {
            self.columns.join(", ")
        };
        let table = self.table.as_deref().unwrap_or("unknown");
        let mut sql = format!("SELECT {cols} FROM {table}");
        if !self.conditions.is_empty() {
            sql.push_str(" WHERE ");
            sql.push_str(&self.conditions.join(" AND "));
        }
        if !self.order_by.is_empty() {
            sql.push_str(" ORDER BY ");
            sql.push_str(
                &self
                    .order_by
                    .iter()
                    .map(|(c, d)| format!("{c} {d}"))
                    .collect::<Vec<_>>()
                    .join(", "),
            );
        }
        if let Some(l) = self.limit {
            sql.push_str(&format!(" LIMIT {l}"));
        }
        if let Some(o) = self.offset {
            sql.push_str(&format!(" OFFSET {o}"));
        }
        BuiltQuery {
            sql,
            params: self.params,
        }
    }
}

/// Fluent builder for INSERT queries.
///
/// # Example
///
/// ```rust
/// use oxisql_core::query::InsertBuilder;
///
/// let q = InsertBuilder::new()
///     .into_table("users")
///     .column("name", &"Alice")
///     .column("age", &30i64)
///     .build();
///
/// assert_eq!(q.sql, "INSERT INTO users (name, age) VALUES ($1, $2)");
/// ```
#[derive(Debug, Default, Clone)]
pub struct InsertBuilder {
    table: Option<String>,
    columns: Vec<String>,
    params: Vec<Value>,
}

impl InsertBuilder {
    /// Create a new empty INSERT builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the target table for the INSERT.
    pub fn into_table(mut self, table: &str) -> Self {
        self.table = Some(table.to_string());
        self
    }

    /// Add a column-value pair for the INSERT.
    pub fn column(mut self, col: &str, val: &dyn ToSqlValue) -> Self {
        self.columns.push(col.to_string());
        self.params.push(val.to_value());
        self
    }

    /// Build the query into a [`BuiltQuery`].
    pub fn build(self) -> BuiltQuery {
        let table = self.table.as_deref().unwrap_or("unknown");
        let n = self.columns.len();
        let cols = self.columns.join(", ");
        let placeholders = (1..=n)
            .map(|i| format!("${i}"))
            .collect::<Vec<_>>()
            .join(", ");
        let sql = format!("INSERT INTO {table} ({cols}) VALUES ({placeholders})");
        BuiltQuery {
            sql,
            params: self.params,
        }
    }
}

/// Fluent builder for UPDATE queries.
///
/// # Example
///
/// ```rust
/// use oxisql_core::query::UpdateBuilder;
///
/// let q = UpdateBuilder::new()
///     .table("users")
///     .set("email", &"new@example.com")
///     .where_eq("id", &42i64)
///     .build();
///
/// assert_eq!(q.sql, "UPDATE users SET email = $1 WHERE id = $2");
/// ```
#[derive(Debug, Default, Clone)]
pub struct UpdateBuilder {
    table: Option<String>,
    sets: Vec<String>,
    conditions: Vec<String>,
    params: Vec<Value>,
}

impl UpdateBuilder {
    /// Create a new empty UPDATE builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the target table for the UPDATE.
    pub fn table(mut self, t: &str) -> Self {
        self.table = Some(t.to_string());
        self
    }

    /// Add a `SET col = $N` assignment, binding the value.
    pub fn set(mut self, col: &str, val: &dyn ToSqlValue) -> Self {
        let n = self.params.len() + 1;
        self.sets.push(format!("{col} = ${n}"));
        self.params.push(val.to_value());
        self
    }

    /// Add a raw WHERE condition. Call multiple times to AND them.
    pub fn where_raw(mut self, cond: &str) -> Self {
        self.conditions.push(cond.to_string());
        self
    }

    /// Add a `WHERE col = $N` condition, binding the value.
    pub fn where_eq(mut self, col: &str, val: &dyn ToSqlValue) -> Self {
        let n = self.params.len() + 1;
        self.conditions.push(format!("{col} = ${n}"));
        self.params.push(val.to_value());
        self
    }

    /// Build the query into a [`BuiltQuery`].
    pub fn build(self) -> BuiltQuery {
        let table = self.table.as_deref().unwrap_or("unknown");
        let sets = self.sets.join(", ");
        let mut sql = format!("UPDATE {table} SET {sets}");
        if !self.conditions.is_empty() {
            sql.push_str(" WHERE ");
            sql.push_str(&self.conditions.join(" AND "));
        }
        BuiltQuery {
            sql,
            params: self.params,
        }
    }
}

/// Fluent builder for DELETE queries.
///
/// # Example
///
/// ```rust
/// use oxisql_core::query::DeleteBuilder;
///
/// let q = DeleteBuilder::new()
///     .from("users")
///     .where_eq("id", &99i64)
///     .build();
///
/// assert_eq!(q.sql, "DELETE FROM users WHERE id = $1");
/// ```
#[derive(Debug, Default, Clone)]
pub struct DeleteBuilder {
    table: Option<String>,
    conditions: Vec<String>,
    params: Vec<Value>,
}

impl DeleteBuilder {
    /// Create a new empty DELETE builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the target table for the DELETE.
    pub fn from(mut self, table: &str) -> Self {
        self.table = Some(table.to_string());
        self
    }

    /// Add a raw WHERE condition. Call multiple times to AND them.
    pub fn where_raw(mut self, cond: &str) -> Self {
        self.conditions.push(cond.to_string());
        self
    }

    /// Add a `WHERE col = $N` condition, binding the value.
    pub fn where_eq(mut self, col: &str, val: &dyn ToSqlValue) -> Self {
        let n = self.params.len() + 1;
        self.conditions.push(format!("{col} = ${n}"));
        self.params.push(val.to_value());
        self
    }

    /// Build the query into a [`BuiltQuery`].
    pub fn build(self) -> BuiltQuery {
        let table = self.table.as_deref().unwrap_or("unknown");
        let mut sql = format!("DELETE FROM {table}");
        if !self.conditions.is_empty() {
            sql.push_str(" WHERE ");
            sql.push_str(&self.conditions.join(" AND "));
        }
        BuiltQuery {
            sql,
            params: self.params,
        }
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

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

    #[test]
    fn select_all_from_table() {
        let q = SelectBuilder::new().from("users").build();
        assert_eq!(q.sql, "SELECT * FROM users");
        assert!(q.params.is_empty());
    }

    #[test]
    fn select_columns_with_where_and_limit() {
        let q = SelectBuilder::new()
            .columns(&["id", "name"])
            .from("users")
            .where_eq("active", &true)
            .limit(10)
            .build();
        assert_eq!(
            q.sql,
            "SELECT id, name FROM users WHERE active = $1 LIMIT 10"
        );
        assert_eq!(q.params, vec![Value::Bool(true)]);
    }

    #[test]
    fn select_order_by_multiple() {
        let q = SelectBuilder::new()
            .from("items")
            .order_by("price", SortDirection::Desc)
            .order_by("name", SortDirection::Asc)
            .build();
        assert_eq!(q.sql, "SELECT * FROM items ORDER BY price DESC, name ASC");
    }

    #[test]
    fn select_with_offset() {
        let q = SelectBuilder::new()
            .from("logs")
            .limit(20)
            .offset(40)
            .build();
        assert_eq!(q.sql, "SELECT * FROM logs LIMIT 20 OFFSET 40");
    }

    #[test]
    fn insert_single_row() {
        let q = InsertBuilder::new()
            .into_table("users")
            .column("name", &"Alice")
            .column("age", &30i64)
            .build();
        assert_eq!(q.sql, "INSERT INTO users (name, age) VALUES ($1, $2)");
        assert_eq!(q.params.len(), 2);
    }

    #[test]
    fn update_with_where() {
        let q = UpdateBuilder::new()
            .table("users")
            .set("email", &"new@example.com")
            .where_eq("id", &42i64)
            .build();
        assert_eq!(q.sql, "UPDATE users SET email = $1 WHERE id = $2");
        assert_eq!(q.params.len(), 2);
    }

    #[test]
    fn delete_with_condition() {
        let q = DeleteBuilder::new()
            .from("users")
            .where_eq("id", &99i64)
            .build();
        assert_eq!(q.sql, "DELETE FROM users WHERE id = $1");
        assert_eq!(q.params[0], Value::I64(99));
    }

    #[test]
    fn multiple_where_conditions_joined_with_and() {
        let q = SelectBuilder::new()
            .from("orders")
            .where_eq("status", &"active")
            .where_eq("user_id", &5i64)
            .build();
        assert_eq!(
            q.sql,
            "SELECT * FROM orders WHERE status = $1 AND user_id = $2"
        );
        assert_eq!(q.params.len(), 2);
    }

    #[test]
    fn select_no_table_falls_back_to_unknown() {
        let q = SelectBuilder::new().columns(&["x"]).build();
        assert_eq!(q.sql, "SELECT x FROM unknown");
    }

    #[test]
    fn update_no_table_falls_back_to_unknown() {
        let q = UpdateBuilder::new().set("x", &1i64).build();
        assert_eq!(q.sql, "UPDATE unknown SET x = $1");
    }

    #[test]
    fn delete_no_conditions() {
        let q = DeleteBuilder::new().from("logs").build();
        assert_eq!(q.sql, "DELETE FROM logs");
        assert!(q.params.is_empty());
    }

    #[test]
    fn sort_direction_default_is_asc() {
        assert_eq!(SortDirection::default(), SortDirection::Asc);
    }

    #[test]
    fn sort_direction_display() {
        assert_eq!(format!("{}", SortDirection::Asc), "ASC");
        assert_eq!(format!("{}", SortDirection::Desc), "DESC");
    }

    #[test]
    fn where_raw_passthrough() {
        let q = SelectBuilder::new()
            .from("events")
            .where_raw("created_at > NOW()")
            .build();
        assert_eq!(q.sql, "SELECT * FROM events WHERE created_at > NOW()");
        assert!(q.params.is_empty());
    }

    #[test]
    fn insert_empty_columns() {
        let q = InsertBuilder::new().into_table("empty_table").build();
        assert_eq!(q.sql, "INSERT INTO empty_table () VALUES ()");
    }

    #[test]
    fn built_query_clone_and_eq() {
        let q1 = SelectBuilder::new().from("t").build();
        let q2 = q1.clone();
        assert_eq!(q1, q2);
    }
}