ensemble 0.0.5

A Laravel-inspired ORM
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
use itertools::Itertools;
use rbs::Value;
use serde::Serialize;
use std::{
	collections::{HashMap, HashSet},
	fmt::Display,
};

use crate::{
	connection::{self, Database},
	value, Error, Model,
};

/// The Query Builder.
#[derive(Debug)]
pub struct Builder {
	table: String,
	join: Vec<Join>,
	order: Vec<Order>,
	limit: Option<usize>,
	offset: Option<usize>,
	r#where: Vec<WhereClause>,
	eager_load: HashSet<String>,
}

impl Builder {
	pub(crate) fn new(table: String) -> Self {
		Self {
			table,
			limit: None,
			offset: None,
			join: vec![],
			order: vec![],
			r#where: vec![],
			eager_load: HashSet::new(),
		}
	}

	/// Execute a raw SQL query and return the results.
	///
	/// # Safety
	///
	/// This method is unsafe because it allows for arbitrary SQL to be executed, which can lead to SQL injection.
	/// It is recommended to build queries using the methods provided by the query builder instead.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async unsafe fn raw_sql(sql: &str, bindings: Vec<Value>) -> Result<Vec<Value>, Error> {
		let mut conn = connection::get().await?;

		conn.get_values(sql, bindings)
			.await
			.map_err(|e| Error::Database(e.to_string()))
	}

	/// Set the table which the query is targeting.
	#[must_use]
	pub fn from(mut self, table: &str) -> Self {
		self.table = table.to_string();
		self
	}

	/// Apply the given callback to the builder if the provided condition is true.
	#[must_use]
	pub fn when(mut self, condition: bool, r#fn: impl FnOnce(Self) -> Self) -> Self {
		if condition {
			self = r#fn(self);
		}

		self
	}

	/// Apply the given callback to the builder if the provided [`Option`] is `Some`.
	#[must_use]
	pub fn when_some<T>(mut self, value: Option<T>, r#fn: impl FnOnce(Self, T) -> Self) -> Self {
		if let Some(value) = value {
			self = r#fn(self, value);
		}

		self
	}

	/// Add a basic where clause to the query.
	///
	/// # Panics
	///
	/// Panics if the provided value cannot be serialized.
	#[must_use]
	pub fn r#where<T, Op>(mut self, column: &str, operator: Op, value: T) -> Self
	where
		Op: Into<Operator>,
		T: serde::Serialize,
	{
		self.r#where.push(WhereClause::Simple(Where {
			boolean: Boolean::And,
			operator: operator.into(),
			column: Columns::escape(column),
			value: Some(value::for_db(value).unwrap()),
		}));

		self
	}

	/// Set the "limit" value of the query.
	#[must_use]
	pub const fn limit(mut self, take: usize) -> Self {
		self.limit = Some(take);
		self
	}

	/// Set the "offset" value of the query.
	#[must_use]
	pub const fn offset(mut self, skip: usize) -> Self {
		self.offset = Some(skip);
		self
	}

	/// Set the relationships that should be eager loaded.
	#[must_use]
	pub fn with<T: Into<EagerLoad>>(mut self, relations: T) -> Self {
		self.eager_load.extend(relations.into().list());

		self
	}

	/// Add an "or where" clause to the query.
	///
	/// # Panics
	///
	/// Panics if this is the first where clause.
	#[must_use]
	pub fn or_where<T, Op>(mut self, column: &str, op: Op, value: T) -> Self
	where
		T: Into<Value>,
		Op: Into<Operator>,
	{
		assert!(
			!self.r#where.is_empty(),
			"Cannot use or_where without a where clause."
		);

		self.r#where.push(WhereClause::Simple(Where {
			operator: op.into(),
			boolean: Boolean::Or,
			value: Some(value.into()),
			column: Columns::escape(column),
		}));

		self
	}

	/// Add a "where not null" clause to the query.
	#[must_use]
	pub fn where_not_null(mut self, column: &str) -> Self {
		self.r#where.push(WhereClause::Simple(Where {
			value: None,
			boolean: Boolean::And,
			operator: Operator::NotNull,
			column: Columns::escape(column),
		}));

		self
	}

	// Add a "where in" clause to the query.
	#[must_use]
	pub fn where_in<T>(mut self, column: &str, values: Vec<T>) -> Self
	where
		T: Into<Value>,
	{
		self.r#where.push(WhereClause::Simple(Where {
			boolean: Boolean::And,
			operator: Operator::In,
			column: Columns::escape(column),
			value: Some(Value::Array(values.into_iter().map(Into::into).collect())),
		}));

		self
	}

	/// Add a "where is null" clause to the query.
	#[must_use]
	pub fn where_null(mut self, column: &str) -> Self {
		self.r#where.push(WhereClause::Simple(Where {
			value: None,
			boolean: Boolean::And,
			operator: Operator::IsNull,
			column: Columns::escape(column),
		}));

		self
	}

	/// Add an inner join to the query.
	#[must_use]
	pub fn join<Op: Into<Operator>>(
		mut self,
		column: &str,
		first: &str,
		op: Op,
		second: &str,
	) -> Self {
		self.join.push(Join {
			operator: op.into(),
			r#type: JoinType::Inner,
			first: first.to_string(),
			second: second.to_string(),
			column: Columns::escape(column),
		});

		self
	}

	/// Add an "order by" clause to the query.
	#[must_use]
	pub fn order_by<Dir: Into<Direction>>(mut self, column: &str, direction: Dir) -> Self {
		self.order.push(Order {
			direction: direction.into(),
			column: Columns::escape(column),
		});

		self
	}

	/// Logically group a set of where clauses.
	#[must_use]
	pub fn where_group(mut self, r#fn: impl FnOnce(Self) -> Self) -> Self {
		let builder = r#fn(Self::new(self.table.clone()));

		self.r#where
			.push(WhereClause::Group(builder.r#where, Boolean::And));

		self
	}

	/// Get the SQL representation of the query.
	#[must_use]
	pub fn to_sql(&self, r#type: Type) -> String {
		let mut sql = match r#type {
			Type::Update => String::new(), // handled in update()
			Type::Delete => format!("DELETE FROM {}", self.table),
			Type::Select => format!("SELECT * FROM {}", self.table),
			Type::Count => format!("SELECT COUNT(*) FROM {}", self.table),
		};

		if !self.join.is_empty() {
			for join in &self.join {
				sql.push_str(&format!(
					" {} {} ON {} {} {}",
					join.r#type, join.column, join.first, join.operator, join.second
				));
			}
		}

		if !self.r#where.is_empty() {
			sql.push_str(" WHERE ");

			for (i, where_clause) in self.r#where.iter().enumerate() {
				sql.push_str(&where_clause.to_sql(i != 0));
			}
		}

		if !self.order.is_empty() {
			sql.push_str(" ORDER BY ");

			sql.push_str(
				&self
					.order
					.iter()
					.map(|order| format!("{} {}", order.column, order.direction))
					.join(", "),
			);
		}

		if let Some(take) = self.limit {
			sql.push_str(&format!(" LIMIT {take}"));
		}

		if let Some(skip) = self.offset {
			sql.push_str(&format!(" OFFSET {skip}"));
		}

		sql
	}

	/// Get the current query value bindings.
	#[must_use]
	pub fn get_bindings(&self) -> Vec<Value> {
		self.r#where
			.iter()
			.flat_map(WhereClause::get_bindings)
			.collect()
	}

	/// Retrieve the number of records that match the query constraints.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async fn count(self) -> Result<u64, Error> {
		let mut conn = connection::get().await?;

		let values = conn
			.get_values(&self.to_sql(Type::Count), self.get_bindings())
			.await
			.map_err(|e| Error::Database(e.to_string()))?;

		values
			.first()
			.and_then(|m| m.as_map())
			.and_then(|m| m.first())
			.and_then(|(_, v)| v.as_u64())
			.ok_or_else(|| {
				Error::Serialization(rbs::value::ext::Error::Syntax(
					"Failed to parse count value".to_string(),
				))
			})
	}

	/// Execute the query and return the first result.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async fn first<M: Model>(mut self) -> Result<Option<M>, Error> {
		self.limit = Some(1);
		let values = self.get::<M>().await?;

		Ok(values.into_iter().next())
	}

	/// Execute the query and return the results.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async fn get<M: Model>(self) -> Result<Vec<M>, Error> {
		let mut models = self
			._get()
			.await?
			.into_iter()
			.map(value::from::<M>)
			.collect::<Result<Vec<M>, rbs::Error>>()?;

		if models.is_empty() || self.eager_load.is_empty() {
			return Ok(models);
		}

		let model = M::default();
		for relation in self.eager_load {
			tracing::trace!(
				"Eager loading {relation} relation for {} models",
				models.len()
			);

			let rows = model
				.eager_load(&relation, models.iter().collect::<Vec<&M>>().as_slice())
				.get_rows()
				.await?;

			for model in &mut models {
				model.fill_relation(&relation, &rows)?;
			}
		}

		Ok(models)
	}

	/// Execute the query and return the results as a vector of rows.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub(crate) async fn get_rows(&self) -> Result<Vec<HashMap<String, Value>>, Error> {
		let values = self
			._get()
			.await?
			.into_iter()
			.map(|v| {
				let Value::Map(map) = v else { unreachable!() };

				map.into_iter()
					.map(|(k, v)| (k.into_string().unwrap_or_else(|| unreachable!()), v))
					.collect()
			})
			.collect();

		Ok(values)
	}

	/// Insert a new record into the database. Returns the ID of the inserted record, if applicable.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async fn insert<Id: for<'de> serde::Deserialize<'de>, T: Into<Columns> + Send>(
		&self,
		columns: T,
	) -> Result<Option<Id>, Error> {
		if self.limit.is_some()
			|| !self.join.is_empty()
			|| !self.order.is_empty()
			|| !self.r#where.is_empty()
		{
			return Err(Error::InvalidQuery);
		}

		let mut conn = connection::get().await?;
		let values: Vec<(String, Value)> = columns.into().0;

		let (sql, bindings) = (
			format!(
				"INSERT INTO {} ({}) VALUES ({})",
				self.table,
				values.iter().map(|(column, _)| column).join(", "),
				values.iter().map(|_| "?").join(", ")
			),
			values.into_iter().map(|(_, value)| value).collect(),
		);

		tracing::debug!(sql = sql.as_str(), bindings = ?bindings, "Executing INSERT SQL query");

		let result = conn
			.exec(&sql, bindings)
			.await
			.map_err(|e| Error::Database(e.to_string()))?;

		Ok(rbs::from_value(result.last_insert_id).ok())
	}

	/// Increment a column's value by a given amount. Returns the number of affected rows.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async fn increment(self, column: &str, amount: u64) -> Result<u64, Error> {
		let mut conn = connection::get().await?;
		let (sql, mut bindings) = (
			format!(
				"UPDATE {} SET {} = {} + ? {}",
				self.table,
				Columns::escape(column),
				Columns::escape(column),
				self.to_sql(Type::Update)
			),
			self.get_bindings(),
		);
		bindings.insert(0, amount.into());

		tracing::debug!(sql = sql.as_str(), bindings = ?bindings, "Executing UPDATE SQL query for increment");

		conn.exec(&sql, bindings)
			.await
			.map_err(|e| Error::Database(e.to_string()))
			.map(|r| r.rows_affected)
	}

	/// Update records in the database. Returns the number of affected rows.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async fn update<T: Into<Columns> + Send>(self, values: T) -> Result<u64, Error> {
		let mut conn = connection::get().await?;
		let values: Vec<(String, Value)> = values.into().0;

		let (sql, bindings) = (
			format!(
				"UPDATE {} SET {} {}",
				self.table,
				values
					.iter()
					.map(|(column, _)| format!("{column} = ?"))
					.join(", "),
				self.to_sql(Type::Update)
			),
			values
				.iter()
				.map(|(_, value)| value.clone())
				.chain(self.get_bindings())
				.collect(),
		);

		tracing::debug!(sql = sql.as_str(), bindings = ?bindings, "Executing UPDATE SQL query");

		conn.exec(&sql, bindings)
			.await
			.map_err(|e| Error::Database(e.to_string()))
			.map(|r| r.rows_affected)
	}

	/// Delete records from the database. Returns the number of affected rows.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async fn delete(self) -> Result<u64, Error> {
		let mut conn = connection::get().await?;
		let (sql, bindings) = (self.to_sql(Type::Delete), self.get_bindings());

		tracing::debug!(sql = sql.as_str(), bindings = ?bindings, "Executing DELETE SQL query");

		conn.exec(&sql, bindings)
			.await
			.map_err(|e| Error::Database(e.to_string()))
			.map(|r| r.rows_affected)
	}

	/// Run a truncate statement on the table. Returns the number of affected rows.
	///
	/// # Errors
	///
	/// Returns an error if the query fails, or if a connection to the database cannot be established.
	pub async fn truncate(self) -> Result<u64, Error> {
		let mut conn = connection::get().await?;
		let sql = format!("TRUNCATE TABLE {}", self.table);

		tracing::debug!(sql = sql.as_str(), "Executing TRUNCATE SQL query");

		conn.exec(&sql, vec![])
			.await
			.map_err(|e| Error::Database(e.to_string()))
			.map(|r| r.rows_affected)
	}
}

impl Builder {
	async fn _get(&self) -> Result<Vec<Value>, Error> {
		let mut conn = connection::get().await?;
		let (sql, bindings) = (self.to_sql(Type::Select), self.get_bindings());

		tracing::debug!(sql = sql.as_str(), bindings = ?bindings, "Executing SELECT SQL query");

		let values = conn
			.get_values(&sql, bindings)
			.await
			.map_err(|s| Error::Database(s.to_string()))?;

		Ok(values)
	}
}

pub enum EagerLoad {
	Single(String),
	Multiple(Vec<String>),
}

impl EagerLoad {
	#[must_use]
	pub fn list(self) -> Vec<String> {
		match self {
			Self::Single(value) => vec![value],
			Self::Multiple(value) => value,
		}
	}
}

impl From<&str> for EagerLoad {
	fn from(value: &str) -> Self {
		Self::Single(value.to_string())
	}
}

impl From<Vec<&str>> for EagerLoad {
	fn from(value: Vec<&str>) -> Self {
		Self::Multiple(value.iter().map(ToString::to_string).collect())
	}
}

pub struct Columns(Vec<(String, Value)>);

impl Columns {
	fn escape(column: &str) -> String {
		let parts = column.split('.');

		match connection::which_db() {
			Database::MySQL => parts
				.map(|part| format!("`{part}`"))
				.collect::<Vec<String>>()
				.join("."),
			Database::PostgreSQL => parts
				.map(|part| format!("\"{part}\""))
				.collect::<Vec<String>>()
				.join("."),
		}
	}
}

#[allow(clippy::fallible_impl_from)]
impl From<Value> for Columns {
	fn from(value: Value) -> Self {
		match value {
			Value::Map(map) => Self(
				map.into_iter()
					.map(|(column, value)| (Self::escape(&column.into_string().unwrap()), value))
					.collect(),
			),
			_ => panic!("The provided value is not a map."),
		}
	}
}

impl<T: Serialize> From<Vec<(&str, T)>> for Columns {
	fn from(values: Vec<(&str, T)>) -> Self {
		Self(
			values
				.iter()
				.map(|(column, value)| (Self::escape(column), value::for_db(value).unwrap()))
				.collect(),
		)
	}
}
impl<T: Serialize> From<&[(&str, T)]> for Columns {
	fn from(values: &[(&str, T)]) -> Self {
		Self(
			values
				.iter()
				.map(|(column, value)| (Self::escape(column), value::for_db(value).unwrap()))
				.collect(),
		)
	}
}

/// Available sort directions.
#[derive(Debug)]
pub enum Direction {
	Ascending,
	Descending,
}

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

impl From<String> for Direction {
	fn from(value: String) -> Self {
		value.as_str().into()
	}
}

#[allow(clippy::fallible_impl_from)]
impl From<&str> for Direction {
	fn from(value: &str) -> Self {
		match value.to_uppercase().as_str() {
			"ASC" | "ASCENDING" => Self::Ascending,
			"DESC" | "DESCENDING" => Self::Descending,

			_ => panic!("Invalid direction {value}"),
		}
	}
}

/// An order clause.
#[derive(Debug)]
struct Order {
	column: String,
	direction: Direction,
}

/// Available join types.
#[derive(Debug)]
enum JoinType {
	/// The `INNER JOIN` type.
	Inner,
}

impl Display for JoinType {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::Inner => write!(f, "INNER JOIN"),
		}
	}
}

#[derive(Debug, Clone, Copy)]
pub enum Type {
	Count,
	Select,
	Update,
	Delete,
}

/// A join clause.
#[derive(Debug)]
struct Join {
	column: String,
	first: String,
	second: String,
	r#type: JoinType,
	operator: Operator,
}

#[derive(Debug)]
enum WhereClause {
	Simple(Where),
	Group(Vec<WhereClause>, Boolean),
}

impl WhereClause {
	fn to_sql(&self, add_boolean: bool) -> String {
		match self {
			Self::Simple(where_clause) => where_clause.to_sql(add_boolean),
			Self::Group(where_clauses, boolean) => {
				let mut sql = String::new();

				for (i, where_clause) in where_clauses.iter().enumerate() {
					sql.push_str(&where_clause.to_sql(i != 0));
				}

				if add_boolean {
					format!(" {boolean} ({sql})")
				} else {
					format!("({sql})")
				}
			},
		}
	}

	fn get_bindings(&self) -> Vec<Value> {
		match self {
			Self::Simple(where_clause) => where_clause
				.value
				.clone()
				.into_iter()
				.flat_map(|v| match v {
					Value::Array(array) => array,
					_ => vec![v],
				})
				.collect(),
			Self::Group(where_clauses, _) => {
				where_clauses.iter().flat_map(Self::get_bindings).collect()
			},
		}
	}
}

/// A where clause.
#[derive(Debug)]
struct Where {
	column: String,
	boolean: Boolean,
	operator: Operator,
	value: Option<Value>,
}

impl Where {
	fn to_sql(&self, add_boolean: bool) -> String {
		let sql = format!(
			"{} {} {}",
			self.column,
			self.operator,
			self.value.as_ref().map_or_else(String::new, |value| {
				value.as_array().map_or_else(
					|| "?".to_string(),
					|value| format!("({})", value.iter().map(|_| "?").join(", ")),
				)
			})
		);

		if add_boolean {
			format!(" {} {sql} ", self.boolean)
		} else {
			sql
		}
	}
}

/// Available operators for where clauses.
#[derive(Debug)]
pub enum Operator {
	/// The `IN` operator.
	In,
	/// The `LIKE` operator.
	Like,
	/// The `NOT IN` operator.
	NotIn,
	/// The `=` operator.
	Equals,
	/// The `IS NULL` operator.
	IsNull,
	/// The `IS NOT NULL` operator.
	NotNull,
	/// The `BETWEEN` operator.
	Between,
	/// The `NOT LIKE` operator.
	NotLike,
	/// The `<` operator.
	LessThan,
	/// The `<>` operator.
	NotEquals,
	/// The `NOT BETWEEN` operator.
	NotBetween,
	/// The `>` operator.
	GreaterThan,
	/// The `<=` operator.
	LessOrEqual,
	/// The `>=` operator.
	GreaterOrEqual,
}

impl Display for Operator {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(
			f,
			"{}",
			match self {
				Self::In => "IN",
				Self::Equals => "=",
				Self::Like => "LIKE",
				Self::LessThan => "<",
				Self::NotIn => "NOT IN",
				Self::NotEquals => "<>",
				Self::GreaterThan => ">",
				Self::LessOrEqual => "<=",
				Self::IsNull => "IS NULL",
				Self::Between => "BETWEEN",
				Self::NotLike => "NOT LIKE",
				Self::GreaterOrEqual => ">=",
				Self::NotNull => "IS NOT NULL",
				Self::NotBetween => "NOT BETWEEN",
			}
		)
	}
}

impl From<String> for Operator {
	fn from(value: String) -> Self {
		value.as_str().into()
	}
}
impl From<char> for Operator {
	fn from(value: char) -> Self {
		value.to_string().into()
	}
}

#[allow(clippy::fallible_impl_from)]
impl From<&str> for Operator {
	fn from(value: &str) -> Self {
		match value.to_uppercase().as_str() {
			"IN" => Self::In,
			"=" => Self::Equals,
			"LIKE" => Self::Like,
			"<" => Self::LessThan,
			"NOT IN" => Self::NotIn,
			"!=" => Self::NotEquals,
			">" => Self::GreaterThan,
			"<=" => Self::LessOrEqual,
			"BETWEEN" => Self::Between,
			"NOT LIKE" => Self::NotLike,
			">=" => Self::GreaterOrEqual,
			"NOT BETWEEN" => Self::NotBetween,

			_ => panic!("Invalid operator {value}"),
		}
	}
}

#[derive(Debug, Clone, Copy)]
enum Boolean {
	And,
	Or,
}

impl Display for Boolean {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::Or => write!(f, "OR"),
			Self::And => write!(f, "AND"),
		}
	}
}

impl AsRef<Self> for Builder {
	fn as_ref(&self) -> &Self {
		self
	}
}