Skip to content

Commit

Permalink
feat: continue stabilizing the sql query builder API
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-cne committed Apr 10, 2024
1 parent 5db2533 commit 946cd9b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 26 deletions.
49 changes: 24 additions & 25 deletions crates/application/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sea_query::{Query, SelectStatement};
use sea_query::{Expr, Query, SelectStatement, SimpleExpr};

use crate::{
iden::*,
Expand Down Expand Up @@ -49,6 +49,8 @@ impl CircuitQueryBuilder {
.and_results()
.and_drivers()
.and_constructors()
.and_grid()
.and_result()
.stmt
.paginate(page)
.per_page(limit)
Expand All @@ -63,44 +65,41 @@ impl CircuitQueryBuilder {
|| self.filter.year.is_some()
|| self.filter.round.is_some()
}
}

fn races_table(self) -> Self {
races_table(self, Self::one_of)
}

fn results_table(self) -> Self {
results_table(self, Self::one_of)
impl SqlBuilder for CircuitQueryBuilder {
fn stmt(&mut self) -> &mut sea_query::SelectStatement {
&mut self.stmt
}

fn drivers_table(self) -> Self {
drivers_table(self, |b| b.filter.driver_ref.is_some())
fn check_and_races(&self) -> bool {
self.one_of()
}

fn constructors_table(self) -> Self {
constructors_table(self, |s| s.filter.constructor_ref.is_some())
fn check_and_results(&self) -> bool {
self.one_of()
}

fn and_races(self) -> Self {
and_races(self, Self::one_of)
fn check_and_drivers(&self) -> Option<SimpleExpr> {
self.filter.driver_ref.as_ref().map(|d| Expr::value(&**d))
}

fn and_results(self) -> Self {
and_results(self, Self::one_of)
fn check_and_constructors(&self) -> Option<SimpleExpr> {
self.filter
.constructor_ref
.as_ref()
.map(|c| Expr::value(&**c))
}

fn and_drivers(self) -> Self {
and_drivers(self, |b| b.filter.driver_ref.as_ref().map(|d| d.0.clone()))
fn check_and_status(&self) -> Option<SimpleExpr> {
None
}

fn and_constructors(self) -> Self {
and_constructors(self, |b| {
b.filter.constructor_ref.as_ref().map(|d| d.0.clone())
})
fn check_and_grid(&self) -> Option<SimpleExpr> {
self.filter.grid.as_ref().map(|g| Expr::value(**g))
}
}

impl SqlBuilder for CircuitQueryBuilder {
fn stmt(&mut self) -> &mut sea_query::SelectStatement {
&mut self.stmt
fn check_and_result(&self) -> Option<SimpleExpr> {
self.filter.result.as_ref().map(|r| Expr::value(**r))
}
}
1 change: 1 addition & 0 deletions crates/application/src/iden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub(crate) enum Results {
Number,
Grid,
Position,
#[iden = "positionText"]
PositionText,
PositionOrder,
Points,
Expand Down
111 changes: 110 additions & 1 deletion crates/application/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,75 @@ use sea_query::{Expr, SimpleExpr};

use crate::iden::*;

pub(crate) trait SqlBuilder {
pub(crate) trait SqlBuilder: Sized {
fn stmt(&mut self) -> &mut sea_query::SelectStatement;
fn check_and_results(&self) -> bool;
fn check_and_races(&self) -> bool;
fn check_and_drivers(&self) -> Option<SimpleExpr>;
fn check_and_constructors(&self) -> Option<SimpleExpr>;
fn check_and_status(&self) -> Option<SimpleExpr>;
fn check_and_grid(&self) -> Option<SimpleExpr>;
fn check_and_result(&self) -> Option<SimpleExpr>;

fn check_races_table(&self) -> bool {
self.check_and_races()
}

fn check_results_table(&self) -> bool {
self.check_and_results()
}

fn check_drivers_table(&self) -> bool {
self.check_and_drivers().is_some()
}

fn check_constructors_table(&self) -> bool {
self.check_and_constructors().is_some()
}

fn races_table(self) -> Self {
races_table(self, Self::check_races_table)
}

fn results_table(self) -> Self {
results_table(self, Self::check_results_table)
}

fn drivers_table(self) -> Self {
drivers_table(self, Self::check_drivers_table)
}

fn constructors_table(self) -> Self {
constructors_table(self, Self::check_constructors_table)
}

fn and_races(self) -> Self {
and_races(self, Self::check_and_races)
}

fn and_results(self) -> Self {
and_results(self, Self::check_and_results)
}

fn and_drivers(self) -> Self {
and_drivers(self, Self::check_and_drivers)
}

fn and_constructors(self) -> Self {
and_constructors(self, Self::check_and_constructors)
}

fn and_status(self) -> Self {
and_status(self, Self::check_and_status)
}

fn and_grid(self) -> Self {
and_grid(self, Self::check_and_grid)
}

fn and_result(self) -> Self {
and_result(self, Self::check_and_result)
}
}

pub(crate) fn races_table<B, F>(mut builder: B, check: F) -> B
Expand Down Expand Up @@ -126,3 +193,45 @@ where
}
builder
}

pub(crate) fn and_status<B, F, V>(mut builder: B, check: F) -> B
where
B: SqlBuilder,
F: FnOnce(&B) -> Option<V>,
V: Into<SimpleExpr>,
{
if let Some(v) = check(&builder) {
builder
.stmt()
.and_where(Expr::col((Results::Table, Results::Rank)).eq(v));
}
builder
}

pub(crate) fn and_grid<B, F, V>(mut builder: B, check: F) -> B
where
B: SqlBuilder,
F: FnOnce(&B) -> Option<V>,
V: Into<SimpleExpr>,
{
if let Some(v) = check(&builder) {
builder
.stmt()
.and_where(Expr::col((Results::Table, Results::Grid)).eq(v));
}
builder
}

pub(crate) fn and_result<B, F, V>(mut builder: B, check: F) -> B
where
B: SqlBuilder,
F: FnOnce(&B) -> Option<V>,
V: Into<SimpleExpr>,
{
if let Some(v) = check(&builder) {
builder
.stmt()
.and_where(Expr::col((Results::Table, Results::PositionText)).eq(v));
}
builder
}

0 comments on commit 946cd9b

Please sign in to comment.