Skip to content

Commit

Permalink
Merge main into sweep/add-sweep-config
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Dec 14, 2023
2 parents ae6fbb8 + 7882039 commit dfcaa15
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
6 changes: 3 additions & 3 deletions ensemble/docs/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Database tables are often related to one another. For example, a blog post may have many comments or an order could be related to the user who placed it. Ensemble makes managing and working with these relationships easy, with native support for the three most common:

- [One To One](#one-to-one)
- [One To Many](#one-to-many)
- [Many To Many](#many-to-many-relationships)
- [One To One](#one-to-one)
- [One To Many](#one-to-many)
- [Many To Many](#many-to-many-relationships)

## Defining Relationships

Expand Down
6 changes: 3 additions & 3 deletions ensemble/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ pub async fn get() -> Result<Connection, ConnectError> {
}
}

#[cfg(any(feature = "mysql", feature = "postgres"))]
pub enum Database {
MySQL,
PostgreSQL,
}

#[cfg(any(feature = "mysql", feature = "postgres"))]
impl Database {
pub const fn is_mysql(&self) -> bool {
matches!(self, Self::MySQL)
Expand All @@ -97,8 +95,10 @@ impl Database {
}
}

#[cfg(any(feature = "mysql", feature = "postgres"))]
pub const fn which_db() -> Database {
#[cfg(all(not(feature = "mysql"), not(feature = "postgres")))]
panic!("Either the `mysql` or `postgres` feature must be enabled to use `ensemble`.");

#[cfg(all(feature = "mysql", feature = "postgres"))]
panic!("Both the `mysql` and `postgres` features are enabled. Please enable only one of them.");

Expand Down
56 changes: 33 additions & 23 deletions ensemble/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::{
fmt::Display,
};

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

/// The Query Builder.
#[derive(Debug)]
Expand Down Expand Up @@ -92,7 +95,7 @@ impl Builder {
self.r#where.push(WhereClause::Simple(Where {
boolean: Boolean::And,
operator: operator.into(),
column: column.to_string(),
column: Columns::escape(column),
value: Some(value::for_db(value).unwrap()),
}));

Expand Down Expand Up @@ -141,7 +144,7 @@ impl Builder {
operator: op.into(),
boolean: Boolean::Or,
value: Some(value.into()),
column: column.to_string(),
column: Columns::escape(column),
}));

self
Expand All @@ -153,8 +156,8 @@ impl Builder {
self.r#where.push(WhereClause::Simple(Where {
value: None,
boolean: Boolean::And,
column: column.to_string(),
operator: Operator::NotNull,
column: Columns::escape(column),
}));

self
Expand All @@ -169,7 +172,7 @@ impl Builder {
self.r#where.push(WhereClause::Simple(Where {
boolean: Boolean::And,
operator: Operator::In,
column: column.to_string(),
column: Columns::escape(column),
value: Some(Value::Array(values.into_iter().map(Into::into).collect())),
}));

Expand All @@ -182,8 +185,8 @@ impl Builder {
self.r#where.push(WhereClause::Simple(Where {
value: None,
boolean: Boolean::And,
column: column.to_string(),
operator: Operator::IsNull,
column: Columns::escape(column),
}));

self
Expand All @@ -200,10 +203,10 @@ impl Builder {
) -> Self {
self.join.push(Join {
operator: op.into(),
first: first.to_string(),
column: column.to_string(),
r#type: JoinType::Inner,
first: first.to_string(),
second: second.to_string(),
column: Columns::escape(column),
});

self
Expand All @@ -213,8 +216,8 @@ impl Builder {
#[must_use]
pub fn order_by<Dir: Into<Direction>>(mut self, column: &str, direction: Dir) -> Self {
self.order.push(Order {
column: column.to_string(),
direction: direction.into(),
column: Columns::escape(column),
});

self
Expand Down Expand Up @@ -254,7 +257,7 @@ impl Builder {
sql.push_str(" WHERE ");

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

Expand Down Expand Up @@ -430,8 +433,10 @@ impl Builder {
let mut conn = connection::get().await?;
let (sql, mut bindings) = (
format!(
"UPDATE {} SET {column} = {column} + ? {}",
"UPDATE {} SET {} = {} + ? {}",
self.table,
Columns::escape(column),
Columns::escape(column),
self.to_sql(Type::Update)
),
self.get_bindings(),
Expand Down Expand Up @@ -560,13 +565,22 @@ impl From<Vec<&str>> for EagerLoad {

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

impl Columns {
fn escape(column: &str) -> String {
match connection::which_db() {
Database::MySQL => format!("`{column}`"),
Database::PostgreSQL => format!("\"{column}\""),
}
}
}

#[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)| (column.into_string().unwrap(), value))
.map(|(column, value)| (Self::escape(&column.into_string().unwrap()), value))
.collect(),
),
_ => panic!("The provided value is not a map."),
Expand All @@ -579,7 +593,7 @@ impl<T: Serialize> From<Vec<(&str, T)>> for Columns {
Self(
values
.iter()
.map(|(column, value)| ((*column).to_string(), value::for_db(value).unwrap()))
.map(|(column, value)| (Self::escape(column), value::for_db(value).unwrap()))
.collect(),
)
}
Expand All @@ -589,7 +603,7 @@ impl<T: Serialize> From<&[(&str, T)]> for Columns {
Self(
values
.iter()
.map(|(column, value)| ((*column).to_string(), value::for_db(value).unwrap()))
.map(|(column, value)| (Self::escape(column), value::for_db(value).unwrap()))
.collect(),
)
}
Expand Down Expand Up @@ -683,17 +697,13 @@ impl WhereClause {
let mut sql = String::new();

for (i, where_clause) in where_clauses.iter().enumerate() {
sql.push_str(&format!("({})", where_clause.to_sql(false)));

if i != where_clauses.len() - 1 {
sql.push_str(" AND ");
}
sql.push_str(&where_clause.to_sql(i != 0));
}

if add_boolean {
format!("{boolean} {sql}")
format!(" {boolean} ({sql})")
} else {
sql
format!("({sql})")
}
},
}
Expand Down Expand Up @@ -741,7 +751,7 @@ impl Where {
);

if add_boolean {
format!("{sql} {} ", self.boolean)
format!(" {} {sql} ", self.boolean)
} else {
sql
}
Expand Down Expand Up @@ -839,7 +849,7 @@ impl From<&str> for Operator {
}
}

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

0 comments on commit dfcaa15

Please sign in to comment.