Skip to content

Commit

Permalink
feat: dumped diesel for mysql
Browse files Browse the repository at this point in the history
Dumped diesel ORM for the mysql driver in rust
  • Loading branch information
thibault-cne committed Apr 10, 2024
1 parent 13526ba commit 10d5d2c
Show file tree
Hide file tree
Showing 10 changed files with 612 additions and 728 deletions.
1,206 changes: 553 additions & 653 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ dotenvy = "0.15.7"
proc-macro2 = "1.0.78"
quote = "1.0.35"
syn = { version = "2.0.48", features = ["parsing"] }
sea-orm = { version = "^0.12.0", features = ["sqlx-mysql", "macros"] }
sea-query = "0.30.7"
sea-query = { version = "0.30.7", features = ["backend-mysql"] }
sea-query-diesel = { version = "0.1.0", features = [
"mysql",
"with-chrono",
"with-json",
"with-uuid",
"with-time",
"with-rust_decimal",
"with-bigdecimal",
] }
r2d2 = "0.8.10"
diesel = { version = "2.1.0", features = ["mysql", "chrono", "r2d2"] }
diesel = { version = "2.1.5", features = ["mysql", "chrono", "r2d2"] }
mysql = { version = "25.0.0" }
2 changes: 1 addition & 1 deletion bin/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
rocket.workspace = true
dotenvy.workspace = true
sea-orm.workspace = true
mysql.workspace = true

api-lib.workspace = true
infrastructure.workspace = true
24 changes: 6 additions & 18 deletions bin/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
#[macro_use]
extern crate rocket;

use dotenvy::dotenv;
use sea_orm::ConnectOptions;

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
#[launch]
fn rocket() -> _ {
dotenv().ok();

let opt = database_options();

rocket::build()
.mount("/api", api_lib::handlers::handlers())
.manage(sea_orm::Database::connect(opt).await.unwrap())
.launch()
.await?;

Ok(())
}

fn database_options() -> ConnectOptions {
let database_url = std::env::var("DATABASE_URL").unwrap();

let mut opt = ConnectOptions::new(database_url);
opt.max_connections(100).min_connections(10);
opt
.manage(mysql::Pool::new("mysql://user:password@localhost:3306/f1db").unwrap())
}
2 changes: 1 addition & 1 deletion crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ diesel.workspace = true
serde.workspace = true
r2d2.workspace = true
serde_json.workspace = true
sea-query-diesel.workspace = true
mysql.workspace = true

application.workspace = true
infrastructure.workspace = true
Expand Down
14 changes: 8 additions & 6 deletions crates/api/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use diesel::{Connection, RunQueryDsl};
use mysql::prelude::Queryable;
use mysql::Pool;
use rocket::serde::json::Json;
use rocket::{get, routes, State};

Expand All @@ -8,17 +10,17 @@ use shared::prelude::*;

#[get("/<series>/circuits?<param..>")]
pub fn circuits(
db: &State<ConnectionPool>,
db: &State<Pool>,
series: Series,
param: shared::parameters::GetCircuitsParameter,
) -> Result<()> {
let pool = &mut db.from_series(series).get().unwrap();
let mut conn = db.get_conn().unwrap();

let query = application::circuit::CircuitQueryBuilder::filter(param.into())
.build()
.unwrap();
let query = application::circuit::CircuitQueryBuilder::filter(param.into()).build();

let res = pool.transaction(|conn| query.load::<shared::models::(conn)).unwrap();
let res: Vec<application::models::Circuits> = conn.query(query).unwrap();

println!("{:?}", res);

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions crates/application/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ edition = "2021"
sea-query.workspace = true
sea-query-diesel.workspace = true
diesel.workspace = true
mysql.workspace = true

shared.workspace = true
27 changes: 13 additions & 14 deletions crates/application/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl CircuitQueryBuilder {
Circuits::Name,
Circuits::Location,
Circuits::Country,
Circuits::Latitude,
Circuits::Longitude,
Circuits::Altitude,
Circuits::Lat,
Circuits::Lng,
Circuits::Alt,
Circuits::Url,
]
.into_iter()
Expand All @@ -32,13 +32,8 @@ impl CircuitQueryBuilder {
Self { stmt, filter }
}

pub fn build(self) -> QueryResult<SeaQuery<diesel::mysql::Mysql>> {
self.races_table()
.results_table()
.drivers_table()
.constructors_table()
.stmt
.build_diesel::<diesel::mysql::Mysql>()
pub fn build(self) -> String {
self.stmt.to_string(sea_query::MysqlQueryBuilder)
}

fn one_of(&self) -> bool {
Expand All @@ -53,7 +48,8 @@ impl CircuitQueryBuilder {

fn races_table(mut self) -> Self {
if self.one_of() {
self.stmt.left_join(
self.stmt.join(
sea_query::JoinType::Join,
Races::Table,
Expr::col((Circuits::Table, Circuits::CircuitId))
.equals((Races::Table, Races::CircuitId)),
Expand All @@ -64,7 +60,8 @@ impl CircuitQueryBuilder {

fn results_table(mut self) -> Self {
if self.one_of() {
self.stmt.left_join(
self.stmt.join(
sea_query::JoinType::Join,
Results::Table,
Expr::col((Races::Table, Races::CircuitId))
.equals((Results::Table, Results::RaceId)),
Expand All @@ -75,7 +72,8 @@ impl CircuitQueryBuilder {

fn drivers_table(mut self) -> Self {
if self.filter.driver_ref.is_some() {
self.stmt.left_join(
self.stmt.join(
sea_query::JoinType::Join,
Drivers::Table,
Expr::col((Results::Table, Results::DriverId))
.equals((Drivers::Table, Drivers::DriverId)),
Expand All @@ -86,7 +84,8 @@ impl CircuitQueryBuilder {

fn constructors_table(mut self) -> Self {
if self.filter.constructor_ref.is_some() {
self.stmt.left_join(
self.stmt.join(
sea_query::JoinType::Join,
Constructors::Table,
Expr::col((Results::Table, Results::ConstructorId))
.equals((Constructors::Table, Constructors::ConstructorId)),
Expand Down
14 changes: 11 additions & 3 deletions crates/application/src/iden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@ use sea_query::Iden;
#[derive(Iden)]
pub(crate) enum Circuits {
Table,
#[iden = "circuitId"]
CircuitId,
#[iden = "circuitRef"]
CircuitRef,
Name,
Location,
Country,
Latitude,
Longitude,
Altitude,
Lat,
Lng,
Alt,
Url,
}

#[derive(Iden)]
pub(crate) enum Races {
Table,
#[iden = "raceId"]
RaceId,
Year,
Round,
#[iden = "circuitId"]
CircuitId,
Name,
Date,
Expand All @@ -40,8 +44,11 @@ pub(crate) enum Races {
#[derive(Iden)]
pub(crate) enum Results {
Table,
#[iden = "resultId"]
ResultId,
#[iden = "raceId"]
RaceId,
#[iden = "driverId"]
DriverId,
ConstructorId,
Number,
Expand All @@ -63,6 +70,7 @@ pub(crate) enum Results {
#[derive(Iden)]
pub(crate) enum Drivers {
Table,
#[iden = "driverId"]
DriverId,
DriverRef,
Number,
Expand Down
41 changes: 15 additions & 26 deletions crates/application/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
diesel::table! {
circuits (circuit_id) {
#[sql_name = "circuitId"]
circuit_id -> Integer,
#[sql_name = "circuitRef"]
#[max_length = 255]
circuit_ref -> Varchar,
#[sql_name = "circuitName"]
#[max_length = 255]
circuit_name -> Varchar,
#[sql_name = "circuitLocation"]
#[max_length = 255]
circuit_location -> Nullable<Varchar>,
#[sql_name = "circuitCountry"]
#[max_length = 255]
country -> Nullable<Varchar>,
#[sql_name = "circuitLat"]
lat -> Nullable<Float>,
#[sql_name = "circuitLng"]
lng -> Nullable<Float>,
#[sql_name = "circuitAlt"]
alt -> Nullable<Integer>,
#[sql_name = "circuitUrl"]
#[max_length = 255]
url -> Varchar,
}
use mysql::prelude::*;

#[derive(FromRow, Debug)]
pub struct Circuits {
#[mysql(rename = "circuitId")]
circuit_id: i32,
#[mysql(rename = "circuitRef")]
circuit_ref: String,
name: String,
location: Option<String>,
country: Option<String>,
lat: Option<f32>,
lng: Option<f32>,
alt: Option<i32>,
url: String,
}

0 comments on commit 10d5d2c

Please sign in to comment.