kaccy-db 0.2.0

Database layer for Kaccy Protocol - PostgreSQL, Redis, and distributed caching
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
489
490
491
492
493
//! Dynamic query builder with type-safe construction and SQL injection prevention.
//!
//! This module provides:
//! - Type-safe query construction
//! - Runtime filter composition
//! - SQL injection prevention

use std::fmt::Write;

/// SQL operator for filters
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operator {
    /// Equal (=)
    Eq,
    /// Not equal (<>)
    Ne,
    /// Greater than (>)
    Gt,
    /// Greater than or equal (>=)
    Gte,
    /// Less than (<)
    Lt,
    /// Less than or equal (<=)
    Lte,
    /// LIKE
    Like,
    /// ILIKE (case-insensitive)
    ILike,
    /// IN
    In,
    /// NOT IN
    NotIn,
    /// IS NULL
    IsNull,
    /// IS NOT NULL
    IsNotNull,
}

impl Operator {
    /// Get the SQL representation
    pub fn as_sql(&self) -> &str {
        match self {
            Operator::Eq => "=",
            Operator::Ne => "<>",
            Operator::Gt => ">",
            Operator::Gte => ">=",
            Operator::Lt => "<",
            Operator::Lte => "<=",
            Operator::Like => "LIKE",
            Operator::ILike => "ILIKE",
            Operator::In => "IN",
            Operator::NotIn => "NOT IN",
            Operator::IsNull => "IS NULL",
            Operator::IsNotNull => "IS NOT NULL",
        }
    }
}

/// Filter condition
#[derive(Debug, Clone)]
pub struct Filter {
    /// Column name
    pub column: String,
    /// Operator
    pub operator: Operator,
    /// Value (None for IS NULL/IS NOT NULL)
    pub value: Option<FilterValue>,
}

/// Filter value type
#[derive(Debug, Clone)]
pub enum FilterValue {
    /// String value
    String(String),
    /// Integer value
    Int(i64),
    /// Float value
    Float(f64),
    /// Boolean value
    Bool(bool),
    /// Array of strings
    StringArray(Vec<String>),
    /// Array of integers
    IntArray(Vec<i64>),
}

impl Filter {
    /// Create a new filter
    pub fn new(column: String, operator: Operator, value: Option<FilterValue>) -> Self {
        Self {
            column,
            operator,
            value,
        }
    }

    /// Create an equality filter
    pub fn eq<T: Into<FilterValue>>(column: String, value: T) -> Self {
        Self::new(column, Operator::Eq, Some(value.into()))
    }

    /// Create a not-equal filter
    pub fn ne<T: Into<FilterValue>>(column: String, value: T) -> Self {
        Self::new(column, Operator::Ne, Some(value.into()))
    }

    /// Create a greater-than filter
    pub fn gt<T: Into<FilterValue>>(column: String, value: T) -> Self {
        Self::new(column, Operator::Gt, Some(value.into()))
    }

    /// Create an IN filter
    pub fn in_values(column: String, values: Vec<String>) -> Self {
        Self::new(column, Operator::In, Some(FilterValue::StringArray(values)))
    }

    /// Create an IS NULL filter
    pub fn is_null(column: String) -> Self {
        Self::new(column, Operator::IsNull, None)
    }

    /// Convert to SQL with parameterized value
    pub fn to_sql(&self, param_index: &mut usize) -> String {
        let mut sql = format!("{} {}", self.column, self.operator.as_sql());

        match (&self.operator, &self.value) {
            (Operator::IsNull | Operator::IsNotNull, _) => {
                // No value needed
            }
            (Operator::In | Operator::NotIn, Some(FilterValue::StringArray(values))) => {
                let placeholders: Vec<String> = values
                    .iter()
                    .map(|_| {
                        let placeholder = format!("${}", param_index);
                        *param_index += 1;
                        placeholder
                    })
                    .collect();
                write!(sql, " ({})", placeholders.join(", ")).unwrap();
            }
            (Operator::In | Operator::NotIn, Some(FilterValue::IntArray(values))) => {
                let placeholders: Vec<String> = values
                    .iter()
                    .map(|_| {
                        let placeholder = format!("${}", param_index);
                        *param_index += 1;
                        placeholder
                    })
                    .collect();
                write!(sql, " ({})", placeholders.join(", ")).unwrap();
            }
            (_, Some(_)) => {
                write!(sql, " ${}", param_index).unwrap();
                *param_index += 1;
            }
            _ => {}
        }

        sql
    }
}

impl From<String> for FilterValue {
    fn from(s: String) -> Self {
        FilterValue::String(s)
    }
}

impl From<&str> for FilterValue {
    fn from(s: &str) -> Self {
        FilterValue::String(s.to_string())
    }
}

impl From<i64> for FilterValue {
    fn from(i: i64) -> Self {
        FilterValue::Int(i)
    }
}

impl From<i32> for FilterValue {
    fn from(i: i32) -> Self {
        FilterValue::Int(i as i64)
    }
}

impl From<bool> for FilterValue {
    fn from(b: bool) -> Self {
        FilterValue::Bool(b)
    }
}

impl From<f64> for FilterValue {
    fn from(f: f64) -> Self {
        FilterValue::Float(f)
    }
}

/// Logical operator for combining filters
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogicalOp {
    /// AND
    And,
    /// OR
    Or,
}

impl LogicalOp {
    /// Get the SQL representation
    pub fn as_sql(&self) -> &str {
        match self {
            LogicalOp::And => "AND",
            LogicalOp::Or => "OR",
        }
    }
}

/// Order direction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderDirection {
    /// Ascending
    Asc,
    /// Descending
    Desc,
}

impl OrderDirection {
    /// Get the SQL representation
    pub fn as_sql(&self) -> &str {
        match self {
            OrderDirection::Asc => "ASC",
            OrderDirection::Desc => "DESC",
        }
    }
}

/// Order by clause
#[derive(Debug, Clone)]
pub struct OrderBy {
    /// Column name
    pub column: String,
    /// Direction
    pub direction: OrderDirection,
}

impl OrderBy {
    /// Create a new order by clause
    pub fn new(column: String, direction: OrderDirection) -> Self {
        Self { column, direction }
    }

    /// Create ascending order
    pub fn asc(column: String) -> Self {
        Self::new(column, OrderDirection::Asc)
    }

    /// Create descending order
    pub fn desc(column: String) -> Self {
        Self::new(column, OrderDirection::Desc)
    }

    /// Convert to SQL
    pub fn to_sql(&self) -> String {
        format!("{} {}", self.column, self.direction.as_sql())
    }
}

/// Dynamic query builder
#[derive(Debug, Clone)]
pub struct QueryBuilder {
    table: String,
    columns: Vec<String>,
    filters: Vec<Filter>,
    logical_op: LogicalOp,
    order_by: Vec<OrderBy>,
    limit: Option<i64>,
    offset: Option<i64>,
}

impl QueryBuilder {
    /// Create a new query builder for a table
    pub fn new(table: String) -> Self {
        Self {
            table,
            columns: vec!["*".to_string()],
            filters: Vec::new(),
            logical_op: LogicalOp::And,
            order_by: Vec::new(),
            limit: None,
            offset: None,
        }
    }

    /// Select specific columns
    pub fn select(mut self, columns: Vec<String>) -> Self {
        self.columns = columns;
        self
    }

    /// Add a filter
    pub fn filter(mut self, filter: Filter) -> Self {
        self.filters.push(filter);
        self
    }

    /// Add multiple filters
    pub fn filters(mut self, filters: Vec<Filter>) -> Self {
        self.filters.extend(filters);
        self
    }

    /// Set the logical operator for combining filters (AND/OR)
    pub fn logical_op(mut self, op: LogicalOp) -> Self {
        self.logical_op = op;
        self
    }

    /// Add an order by clause
    pub fn order_by(mut self, order: OrderBy) -> Self {
        self.order_by.push(order);
        self
    }

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

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

    /// Build the SQL query
    pub fn build(&self) -> String {
        let mut sql = format!("SELECT {} FROM {}", self.columns.join(", "), self.table);

        if !self.filters.is_empty() {
            sql.push_str(" WHERE ");
            let mut param_index = 1;

            for (i, filter) in self.filters.iter().enumerate() {
                if i > 0 {
                    write!(sql, " {} ", self.logical_op.as_sql()).unwrap();
                }
                write!(sql, "{}", filter.to_sql(&mut param_index)).unwrap();
            }
        }

        if !self.order_by.is_empty() {
            sql.push_str(" ORDER BY ");
            let order_clauses: Vec<String> = self.order_by.iter().map(|o| o.to_sql()).collect();
            sql.push_str(&order_clauses.join(", "));
        }

        if let Some(limit) = self.limit {
            write!(sql, " LIMIT {}", limit).unwrap();
        }

        if let Some(offset) = self.offset {
            write!(sql, " OFFSET {}", offset).unwrap();
        }

        sql
    }
}

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

    #[test]
    fn test_operator_as_sql() {
        assert_eq!(Operator::Eq.as_sql(), "=");
        assert_eq!(Operator::Ne.as_sql(), "<>");
        assert_eq!(Operator::Gt.as_sql(), ">");
        assert_eq!(Operator::Like.as_sql(), "LIKE");
        assert_eq!(Operator::In.as_sql(), "IN");
        assert_eq!(Operator::IsNull.as_sql(), "IS NULL");
    }

    #[test]
    fn test_filter_eq() {
        let filter = Filter::eq("name".to_string(), "John");
        assert_eq!(filter.column, "name");
        assert_eq!(filter.operator, Operator::Eq);
    }

    #[test]
    fn test_filter_is_null() {
        let filter = Filter::is_null("deleted_at".to_string());
        assert_eq!(filter.column, "deleted_at");
        assert_eq!(filter.operator, Operator::IsNull);
        assert!(filter.value.is_none());
    }

    #[test]
    fn test_filter_to_sql() {
        let mut param_index = 1;
        let filter = Filter::eq("age".to_string(), 25);
        let sql = filter.to_sql(&mut param_index);
        assert_eq!(sql, "age = $1");
        assert_eq!(param_index, 2);
    }

    #[test]
    fn test_filter_to_sql_is_null() {
        let mut param_index = 1;
        let filter = Filter::is_null("deleted_at".to_string());
        let sql = filter.to_sql(&mut param_index);
        assert_eq!(sql, "deleted_at IS NULL");
        assert_eq!(param_index, 1); // No parameter added
    }

    #[test]
    fn test_order_by() {
        let order = OrderBy::asc("created_at".to_string());
        assert_eq!(order.to_sql(), "created_at ASC");

        let order = OrderBy::desc("updated_at".to_string());
        assert_eq!(order.to_sql(), "updated_at DESC");
    }

    #[test]
    fn test_query_builder_simple() {
        let query = QueryBuilder::new("users".to_string()).build();
        assert_eq!(query, "SELECT * FROM users");
    }

    #[test]
    fn test_query_builder_with_filter() {
        let query = QueryBuilder::new("users".to_string())
            .filter(Filter::eq("email".to_string(), "test@example.com"))
            .build();

        assert!(query.contains("SELECT * FROM users"));
        assert!(query.contains("WHERE email = $1"));
    }

    #[test]
    fn test_query_builder_with_multiple_filters() {
        let query = QueryBuilder::new("users".to_string())
            .filter(Filter::eq("active".to_string(), true))
            .filter(Filter::gt("age".to_string(), 18))
            .build();

        assert!(query.contains("WHERE active = $1 AND age > $2"));
    }

    #[test]
    fn test_query_builder_with_order() {
        let query = QueryBuilder::new("users".to_string())
            .order_by(OrderBy::desc("created_at".to_string()))
            .build();

        assert!(query.contains("ORDER BY created_at DESC"));
    }

    #[test]
    fn test_query_builder_with_limit_offset() {
        let query = QueryBuilder::new("users".to_string())
            .limit(10)
            .offset(20)
            .build();

        assert!(query.contains("LIMIT 10"));
        assert!(query.contains("OFFSET 20"));
    }

    #[test]
    fn test_query_builder_full() {
        let query = QueryBuilder::new("users".to_string())
            .select(vec![
                "id".to_string(),
                "name".to_string(),
                "email".to_string(),
            ])
            .filter(Filter::eq("active".to_string(), true))
            .filter(Filter::gt("age".to_string(), 18))
            .order_by(OrderBy::desc("created_at".to_string()))
            .limit(10)
            .offset(0)
            .build();

        assert!(query.contains("SELECT id, name, email FROM users"));
        assert!(query.contains("WHERE active = $1 AND age > $2"));
        assert!(query.contains("ORDER BY created_at DESC"));
        assert!(query.contains("LIMIT 10"));
        assert!(query.contains("OFFSET 0"));
    }
}