reinhardt-query 0.1.0

SQL query builder for Reinhardt framework
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
//! Expr - The expression builder.
//!
//! This module provides [`Expr`], a builder for creating SQL expressions.
//! It provides a fluent API for building complex expressions.

use super::simple_expr::{CaseStatement, Keyword, SimpleExpr};
use crate::types::{ColumnRef, DynIden, IntoColumnRef, IntoIden, WindowStatement};
use crate::value::{IntoValue, Value};

/// Expression builder for creating SQL expressions.
///
/// `Expr` provides static methods to create expressions and instance methods
/// to chain operations on them.
///
/// # Example
///
/// ```rust,ignore
/// use reinhardt_query::Expr;
///
/// // Simple column reference
/// let col = Expr::col("name");
///
/// // Value expression
/// let val = Expr::val(42);
///
/// // Build complex expressions
/// let expr = Expr::col("age").gte(18).and(Expr::col("active").eq(true));
/// ```
#[derive(Debug, Clone)]
pub struct Expr(SimpleExpr);

impl Expr {
	/// Create an expression from a column reference.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_query::expr::Expr;
	///
	/// let expr = Expr::col("name");
	/// ```
	pub fn col<C>(col: C) -> Self
	where
		C: IntoColumnRef,
	{
		Self(SimpleExpr::Column(col.into_column_ref()))
	}

	/// Create a table-qualified column expression.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_query::expr::Expr;
	///
	/// let expr = Expr::tbl("users", "name");
	/// ```
	pub fn tbl<T, C>(table: T, col: C) -> Self
	where
		T: IntoIden,
		C: IntoIden,
	{
		Self(SimpleExpr::TableColumn(table.into_iden(), col.into_iden()))
	}

	/// Create a value expression.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_query::expr::Expr;
	///
	/// let expr = Expr::val(42);
	/// let expr2 = Expr::val("hello");
	/// ```
	pub fn val<V>(val: V) -> Self
	where
		V: IntoValue,
	{
		Self(SimpleExpr::Value(val.into_value()))
	}

	/// Create a custom SQL expression.
	///
	/// # Security Warning
	///
	/// **DO NOT** pass user input directly to this method. This method embeds the
	/// SQL string directly into the query without parameterization, which can lead
	/// to SQL injection vulnerabilities.
	///
	/// Use [`Expr::cust_with_values()`] for dynamic values instead.
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::expr::Expr;
	///
	/// // ✅ SAFE: Static SQL expression
	/// let expr = Expr::cust("NOW()");
	///
	/// // ❌ UNSAFE: User input
	/// // let expr = Expr::cust(&user_input);
	///
	/// // ✅ SAFE: Parameterized custom expression
	/// // let expr = Expr::cust_with_values("? + ?", [user_input, other_value]);
	/// ```
	pub fn cust<S>(sql: S) -> Self
	where
		S: Into<String>,
	{
		Self(SimpleExpr::Custom(sql.into()))
	}

	/// Create a custom SQL expression with value placeholders.
	///
	/// Use `?` as placeholders for the values.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_query::expr::Expr;
	///
	/// let expr = Expr::cust_with_values("? + ?", [1, 2]);
	/// ```
	pub fn cust_with_values<S, I, V>(sql: S, values: I) -> Self
	where
		S: Into<String>,
		I: IntoIterator<Item = V>,
		V: Into<SimpleExpr>,
	{
		Self(SimpleExpr::CustomWithExpr(
			sql.into(),
			values.into_iter().map(|v| v.into()).collect(),
		))
	}

	/// Create a tuple expression.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_query::expr::Expr;
	///
	/// let expr = Expr::tuple([Expr::val(1), Expr::val(2), Expr::val(3)]);
	/// ```
	pub fn tuple<I>(exprs: I) -> Self
	where
		I: IntoIterator<Item = Self>,
	{
		Self(SimpleExpr::Tuple(
			exprs.into_iter().map(|e| e.into_simple_expr()).collect(),
		))
	}

	/// Create an asterisk expression (`*`).
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_query::expr::Expr;
	///
	/// let expr = Expr::asterisk();
	/// ```
	pub fn asterisk() -> Self {
		Self(SimpleExpr::Asterisk)
	}

	/// Create a subquery expression.
	///
	/// This creates a standalone subquery that can be used in FROM or SELECT clauses.
	///
	/// # Example
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	///
	/// let subquery = Query::select().column("id").from("users");
	/// let expr = Expr::subquery(subquery);
	/// ```
	pub fn subquery(select: crate::query::SelectStatement) -> Self {
		Self(SimpleExpr::SubQuery(None, Box::new(select)))
	}

	/// Create an EXISTS subquery expression.
	///
	/// # Example
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	///
	/// let subquery = Query::select().column("id").from("orders").and_where(
	///     Expr::col(("orders", "user_id")).eq(Expr::col(("users", "id")))
	/// );
	/// let exists = Expr::exists(subquery);
	/// ```
	pub fn exists(select: crate::query::SelectStatement) -> Self {
		Self(SimpleExpr::SubQuery(
			Some(super::simple_expr::SubQueryOper::Exists),
			Box::new(select),
		))
	}

	/// Create a NOT EXISTS subquery expression.
	///
	/// # Example
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	///
	/// let subquery = Query::select().column("id").from("banned_users").and_where(
	///     Expr::col(("banned_users", "id")).eq(Expr::col(("users", "id")))
	/// );
	/// let not_exists = Expr::not_exists(subquery);
	/// ```
	pub fn not_exists(select: crate::query::SelectStatement) -> Self {
		Self(SimpleExpr::SubQuery(
			Some(super::simple_expr::SubQueryOper::NotExists),
			Box::new(select),
		))
	}

	/// Create an IN subquery expression.
	///
	/// # Example
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	///
	/// let subquery = Query::select().column("user_id").from("premium_users");
	/// let in_expr = Expr::col("id").in_subquery(subquery);
	/// ```
	pub fn in_subquery(self, select: crate::query::SelectStatement) -> Self {
		Self(SimpleExpr::Binary(
			Box::new(self.0),
			crate::types::BinOper::In,
			Box::new(SimpleExpr::SubQuery(None, Box::new(select))),
		))
	}

	/// Create a NOT IN subquery expression.
	///
	/// # Example
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	///
	/// let subquery = Query::select().column("user_id").from("banned_users");
	/// let not_in_expr = Expr::col("id").not_in_subquery(subquery);
	/// ```
	pub fn not_in_subquery(self, select: crate::query::SelectStatement) -> Self {
		Self(SimpleExpr::Binary(
			Box::new(self.0),
			crate::types::BinOper::NotIn,
			Box::new(SimpleExpr::SubQuery(None, Box::new(select))),
		))
	}

	/// Create a CASE expression.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_query::expr::{Expr, ExprTrait};
	///
	/// let case = Expr::case()
	///     .when(Expr::col("status").eq("active"), "Active")
	///     .when(Expr::col("status").eq("pending"), "Pending")
	///     .else_result("Unknown");
	/// ```
	pub fn case() -> CaseExprBuilder {
		CaseExprBuilder {
			case: CaseStatement::new(),
		}
	}

	/// Create a value expression (alias for [`Expr::val`]).
	pub fn value<V>(val: V) -> Self
	where
		V: IntoValue,
	{
		Self::val(val)
	}

	/// Create an expression from a DynIden column.
	pub fn expr_col(col: DynIden) -> Self {
		Self(SimpleExpr::Column(ColumnRef::Column(col)))
	}

	// Constant expressions

	/// Create a NULL constant expression.
	pub fn null() -> Self {
		Self(SimpleExpr::Constant(Keyword::Null))
	}

	/// Create a TRUE constant expression.
	pub fn constant_true() -> Self {
		Self(SimpleExpr::Constant(Keyword::True))
	}

	/// Create a FALSE constant expression.
	pub fn constant_false() -> Self {
		Self(SimpleExpr::Constant(Keyword::False))
	}

	/// Create a DEFAULT constant expression.
	// Intentional factory method for SQL DEFAULT keyword, not std::default::Default
	#[allow(clippy::should_implement_trait)]
	pub fn default() -> Self {
		Self(SimpleExpr::Constant(Keyword::Default))
	}

	/// Create a CURRENT_TIMESTAMP expression.
	pub fn current_timestamp() -> Self {
		Self(SimpleExpr::Constant(Keyword::CurrentTimestamp))
	}

	/// Create a CURRENT_DATE expression.
	pub fn current_date() -> Self {
		Self(SimpleExpr::Constant(Keyword::CurrentDate))
	}

	/// Create a CURRENT_TIME expression.
	pub fn current_time() -> Self {
		Self(SimpleExpr::Constant(Keyword::CurrentTime))
	}

	// Window function methods

	/// Apply a window specification to this expression.
	///
	/// This creates a window function call with an inline window specification.
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![Expr::col("department_id").into_simple_expr()],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// let expr = Expr::row_number().over(window);
	/// ```
	#[must_use]
	pub fn over(self, window: WindowStatement) -> SimpleExpr {
		SimpleExpr::Window {
			func: Box::new(self.0),
			window,
		}
	}

	/// Apply a named window to this expression.
	///
	/// This creates a window function call that references a named window
	/// defined in the WINDOW clause.
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	///
	/// let expr = Expr::row_number().over_named("w");
	/// ```
	#[must_use]
	pub fn over_named<T: IntoIden>(self, name: T) -> SimpleExpr {
		SimpleExpr::WindowNamed {
			func: Box::new(self.0),
			name: name.into_iden(),
		}
	}

	/// Create a ROW_NUMBER() window function.
	///
	/// Returns a sequential number for each row within a partition.
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// let expr = Expr::row_number().over(window);
	/// ```
	pub fn row_number() -> Self {
		Self(SimpleExpr::FunctionCall(
			"ROW_NUMBER".into_iden(),
			Vec::new(),
		))
	}

	/// Create a RANK() window function.
	///
	/// Returns the rank of each row within a partition, with gaps in ranking
	/// for tied values.
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// let expr = Expr::rank().over(window);
	/// ```
	pub fn rank() -> Self {
		Self(SimpleExpr::FunctionCall("RANK".into_iden(), Vec::new()))
	}

	/// Create a DENSE_RANK() window function.
	///
	/// Returns the rank of each row within a partition, without gaps in ranking
	/// for tied values.
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// let expr = Expr::dense_rank().over(window);
	/// ```
	pub fn dense_rank() -> Self {
		Self(SimpleExpr::FunctionCall(
			"DENSE_RANK".into_iden(),
			Vec::new(),
		))
	}

	/// Create an NTILE(n) window function.
	///
	/// Divides the rows in a partition into `buckets` number of groups.
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// let expr = Expr::ntile(4).over(window); // Divide into quartiles
	/// ```
	pub fn ntile(buckets: i64) -> Self {
		Self(SimpleExpr::FunctionCall(
			"NTILE".into_iden(),
			vec![SimpleExpr::Value(Value::BigInt(Some(buckets)))],
		))
	}

	/// Create a LEAD() window function.
	///
	/// Returns the value of the expression evaluated at the row that is `offset`
	/// rows after the current row within the partition.
	///
	/// # Arguments
	///
	/// * `expr` - The expression to evaluate
	/// * `offset` - Number of rows after the current row (default is 1 if `None`)
	/// * `default` - Default value if the lead row doesn't exist (default is NULL if `None`)
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// // Get next salary value
	/// let expr = Expr::lead(Expr::col("salary").into_simple_expr(), Some(1), None).over(window);
	/// ```
	pub fn lead(expr: SimpleExpr, offset: Option<i64>, default: Option<Value>) -> Self {
		let mut args = vec![expr];
		if let Some(off) = offset {
			args.push(SimpleExpr::Value(Value::BigInt(Some(off))));
			if let Some(def) = default {
				args.push(SimpleExpr::Value(def));
			}
		}
		Self(SimpleExpr::FunctionCall("LEAD".into_iden(), args))
	}

	/// Create a LAG() window function.
	///
	/// Returns the value of the expression evaluated at the row that is `offset`
	/// rows before the current row within the partition.
	///
	/// # Arguments
	///
	/// * `expr` - The expression to evaluate
	/// * `offset` - Number of rows before the current row (default is 1 if `None`)
	/// * `default` - Default value if the lag row doesn't exist (default is NULL if `None`)
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// // Get previous salary value
	/// let expr = Expr::lag(Expr::col("salary").into_simple_expr(), Some(1), None).over(window);
	/// ```
	pub fn lag(expr: SimpleExpr, offset: Option<i64>, default: Option<Value>) -> Self {
		let mut args = vec![expr];
		if let Some(off) = offset {
			args.push(SimpleExpr::Value(Value::BigInt(Some(off))));
			if let Some(def) = default {
				args.push(SimpleExpr::Value(def));
			}
		}
		Self(SimpleExpr::FunctionCall("LAG".into_iden(), args))
	}

	/// Create a FIRST_VALUE() window function.
	///
	/// Returns the first value in a window frame.
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// let expr = Expr::first_value(Expr::col("salary").into_simple_expr()).over(window);
	/// ```
	pub fn first_value(expr: SimpleExpr) -> Self {
		Self(SimpleExpr::FunctionCall(
			"FIRST_VALUE".into_iden(),
			vec![expr],
		))
	}

	/// Create a LAST_VALUE() window function.
	///
	/// Returns the last value in a window frame.
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// let expr = Expr::last_value(Expr::col("salary").into_simple_expr()).over(window);
	/// ```
	pub fn last_value(expr: SimpleExpr) -> Self {
		Self(SimpleExpr::FunctionCall(
			"LAST_VALUE".into_iden(),
			vec![expr],
		))
	}

	/// Create an NTH_VALUE() window function.
	///
	/// Returns the value of the expression at the nth row of the window frame.
	///
	/// # Arguments
	///
	/// * `expr` - The expression to evaluate
	/// * `n` - The row number (1-based) within the frame
	///
	/// # Examples
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	/// use reinhardt_query::types::window::WindowStatement;
	///
	/// let window = WindowStatement {
	///     partition_by: vec![],
	///     order_by: vec![],
	///     frame: None,
	/// };
	///
	/// // Get the 3rd salary value in the frame
	/// let expr = Expr::nth_value(Expr::col("salary").into_simple_expr(), 3).over(window);
	/// ```
	pub fn nth_value(expr: SimpleExpr, n: i64) -> Self {
		Self(SimpleExpr::FunctionCall(
			"NTH_VALUE".into_iden(),
			vec![expr, SimpleExpr::Value(Value::BigInt(Some(n)))],
		))
	}

	// Conversion methods

	/// Convert this Expr into a SimpleExpr.
	#[must_use]
	pub fn into_simple_expr(self) -> SimpleExpr {
		self.0
	}

	/// Get a reference to the underlying SimpleExpr.
	#[must_use]
	pub fn as_simple_expr(&self) -> &SimpleExpr {
		&self.0
	}

	/// Create a binary operation expression.
	///
	/// # Example
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	///
	/// let expr = Expr::col("age").binary(BinOper::GreaterThan, Expr::val(18).into_simple_expr());
	/// ```
	pub fn binary<R>(self, op: crate::types::BinOper, right: R) -> SimpleExpr
	where
		R: Into<SimpleExpr>,
	{
		SimpleExpr::Binary(Box::new(self.0), op, Box::new(right.into()))
	}

	/// Create an equality expression between two columns.
	///
	/// This is equivalent to `self.eq(Expr::col(col))`.
	///
	/// # Example
	///
	/// ```rust,ignore
	/// use reinhardt_query::prelude::*;
	///
	/// let expr = Expr::col(("orders", "user_id")).equals(("users", "id"));
	/// ```
	pub fn equals<C>(self, col: C) -> SimpleExpr
	where
		C: IntoColumnRef,
	{
		SimpleExpr::Binary(
			Box::new(self.0),
			crate::types::BinOper::Equal,
			Box::new(SimpleExpr::Column(col.into_column_ref())),
		)
	}

	/// Create an aliased expression (AS).
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_query::expr::Expr;
	///
	/// let expr = Expr::col("name").expr_as("alias_name");
	/// ```
	#[must_use]
	pub fn expr_as<T: IntoIden>(self, alias: T) -> SimpleExpr {
		SimpleExpr::ExprAlias(Box::new(self.0), alias.into_iden())
	}
}

// Allow Expr to be converted into SimpleExpr
impl From<Expr> for SimpleExpr {
	fn from(e: Expr) -> Self {
		e.0
	}
}

// Allow creating Expr from SimpleExpr
impl From<SimpleExpr> for Expr {
	fn from(e: SimpleExpr) -> Self {
		Self(e)
	}
}

impl From<&str> for Expr {
	fn from(s: &str) -> Self {
		Expr::val(s)
	}
}

impl crate::value::IntoValue for Expr {
	fn into_value(self) -> crate::value::Value {
		match self.0 {
			SimpleExpr::Value(v) => v,
			_ => panic!("Cannot convert non-value Expr to Value"),
		}
	}
}

/// Builder for CASE expressions.
#[derive(Debug, Clone)]
pub struct CaseExprBuilder {
	case: CaseStatement,
}

impl CaseExprBuilder {
	/// Add a WHEN clause.
	#[must_use]
	pub fn when<C, R>(mut self, condition: C, result: R) -> Self
	where
		C: Into<SimpleExpr>,
		R: Into<SimpleExpr>,
	{
		self.case = self.case.when(condition, result);
		self
	}

	/// Set the ELSE clause and return the built Expr.
	#[must_use]
	pub fn else_result<E>(mut self, result: E) -> Expr
	where
		E: Into<SimpleExpr>,
	{
		self.case = self.case.else_result(result);
		Expr(SimpleExpr::Case(Box::new(self.case)))
	}

	/// Build the CASE expression without an ELSE clause.
	#[must_use]
	pub fn build(self) -> Expr {
		Expr(SimpleExpr::Case(Box::new(self.case)))
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::value::Value;
	use rstest::rstest;

	#[rstest]
	fn test_expr_col() {
		let expr = Expr::col("name");
		assert!(matches!(expr.0, SimpleExpr::Column(_)));
	}

	#[rstest]
	fn test_expr_tbl() {
		let expr = Expr::tbl("users", "name");
		assert!(matches!(expr.0, SimpleExpr::TableColumn(_, _)));
	}

	#[rstest]
	fn test_expr_val() {
		let expr = Expr::val(42);
		assert!(matches!(expr.0, SimpleExpr::Value(Value::Int(Some(42)))));
	}

	#[rstest]
	fn test_expr_cust() {
		let expr = Expr::cust("NOW()");
		if let SimpleExpr::Custom(s) = expr.0 {
			assert_eq!(s, "NOW()");
		} else {
			panic!("Expected Custom variant");
		}
	}

	#[rstest]
	fn test_expr_cust_with_values() {
		let expr = Expr::cust_with_values("? + ?", [1i32, 2i32]);
		assert!(matches!(expr.0, SimpleExpr::CustomWithExpr(_, _)));
	}

	#[rstest]
	fn test_expr_tuple() {
		let expr = Expr::tuple([Expr::val(1), Expr::val(2), Expr::val(3)]);
		if let SimpleExpr::Tuple(v) = expr.0 {
			assert_eq!(v.len(), 3);
		} else {
			panic!("Expected Tuple variant");
		}
	}

	#[rstest]
	fn test_expr_asterisk() {
		let expr = Expr::asterisk();
		assert!(matches!(expr.0, SimpleExpr::Asterisk));
	}

	#[rstest]
	fn test_expr_null() {
		let expr = Expr::null();
		assert!(matches!(expr.0, SimpleExpr::Constant(Keyword::Null)));
	}

	#[rstest]
	fn test_expr_current_timestamp() {
		let expr = Expr::current_timestamp();
		assert!(matches!(
			expr.0,
			SimpleExpr::Constant(Keyword::CurrentTimestamp)
		));
	}

	#[rstest]
	fn test_case_expr_builder() {
		let expr = Expr::case()
			.when(true, 1i32)
			.when(false, 0i32)
			.else_result(-1i32);

		assert!(matches!(expr.0, SimpleExpr::Case(_)));
	}

	#[rstest]
	fn test_case_expr_without_else() {
		let expr = Expr::case().when(true, 1i32).build();

		assert!(matches!(expr.0, SimpleExpr::Case(_)));
	}

	#[rstest]
	fn test_expr_into_simple_expr() {
		let expr = Expr::val(42);
		let simple = expr.into_simple_expr();
		assert!(matches!(simple, SimpleExpr::Value(Value::Int(Some(42)))));
	}
}