Skip to content

Commit

Permalink
Fix clippy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mara-schulke committed Nov 25, 2023
1 parent c608b10 commit d01959f
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 149 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [
]

[workspace.package]
version = "0.0.2"
version = "0.1.0"
license = "Apache-2.0"
edition = "2021"
authors = ["Mara Schulke <[email protected]>"]
Expand All @@ -15,8 +15,8 @@ repository = "https://github.com/hemisphere-studio/atmosphere"
keywords = ["sqlx", "postgres", "database", "orm", "backend"]

[workspace.dependencies]
atmosphere-core = { version = "=0.0.2", path = "atmosphere-core" }
atmosphere-macros = { version = "=0.0.2", path = "atmosphere-macros" }
atmosphere-core = { version = "=0.1.0", path = "atmosphere-core" }
atmosphere-macros = { version = "=0.1.0", path = "atmosphere-macros" }
async-trait = "0.1"
lazy_static = "1"
sqlx = { version = "0.7", features = ["chrono"] }
Expand Down
22 changes: 5 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ async fn main() -> sqlx::Result<()> {

user.save(&pool).await?;
user.delete(&pool).await?;
user.create(&pool).await?;

// Field Queries

assert!(
assert_eq!(
User::find(&0, &pool).await?,
User::find_by_email("[email protected]", &pool).await?
);
Expand Down Expand Up @@ -96,20 +97,7 @@ async fn main() -> sqlx::Result<()> {

Atmosphere introspects the `User` and `Post` structs at compile time and
generates `const` available type information about the schema into the `Table`
trait:

```rust
impl Table for User {
const SCHEMA: &str = "public"
const TABLE: &str = "user"
const PRIMARY_KEY: Column = Column { name: "id", ty: PrimaryKey, .. };
const FOREIGN_KEYS: &'static [Column; 0] = &[];
const DATA: &'static [Column; 2] = &[
Column { name: "name", ty: Value, .. },
Column { name: "email", ty: Value, .. }
];
}
```
trait.

## Roadmap

Expand All @@ -136,14 +124,14 @@ impl Table for User {
- [ ] Stabilize Traits
- [ ] Stabilize Query Generation
- [ ] Table Lenses (subsets / views)
- [ ] `validator` support
- [ ] Auto Timestamping

### Advanced
- [ ] Virtual Columns using (`#[virtual = "<sql>"]`)
- [ ] Soft Delete Support
- [ ] Attribute Macro (`#[query]`)
- [ ] Custom queries
- [ ] Auto Timestamping
- [ ] `validator` support

### Longterm
- [ ] Generate GraphQL + HTTP Servers?
Expand Down
8 changes: 6 additions & 2 deletions atmosphere-core/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ impl<'q> Bindable<'q> for QueryBuilder<'q, crate::Driver> {
}
}

/// Bind columns to SQL Queries
/// Trait for binding columns to SQL queries in the context of a specific table.
///
/// This trait should be implemented by table entities to enable the binding of their columns to
/// SQL queries. It provides a method to bind a single column, ensuring that the query correctly
/// reflects the structure and constraints of the table.
pub trait Bind: Table {
/// Bind a single column to the query
/// Binds a single column of the implementing table entity to a given query.
fn bind<'q, Q: Bindable<'q>>(&'q self, c: &'q Column<Self>, query: Q) -> Result<Q>;
}
50 changes: 22 additions & 28 deletions atmosphere-core/src/runtime/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ pub fn select_by<T: Bind>(c: Column<T>) -> Query<T> {

separated.push(T::PRIMARY_KEY.sql);

for ref fk in T::FOREIGN_KEYS {
for fk in T::FOREIGN_KEYS {
separated.push(fk.sql);
}

for ref data in T::DATA_COLUMNS {
for data in T::DATA_COLUMNS {
separated.push(data.sql);
}

for ref meta in T::TIMESTAMP_COLUMNS {
for meta in T::TIMESTAMP_COLUMNS {
separated.push(meta.sql);
}

Expand All @@ -133,15 +133,15 @@ pub fn select_all<T: Bind>() -> Query<T> {

separated.push(T::PRIMARY_KEY.sql);

for ref fk in T::FOREIGN_KEYS {
for fk in T::FOREIGN_KEYS {
separated.push(fk.sql);
}

for ref data in T::DATA_COLUMNS {
for data in T::DATA_COLUMNS {
separated.push(data.sql);
}

for ref meta in T::TIMESTAMP_COLUMNS {
for meta in T::TIMESTAMP_COLUMNS {
separated.push(meta.sql);
}

Expand Down Expand Up @@ -175,12 +175,12 @@ pub fn insert<T: Bind>() -> Query<T> {

for data in T::DATA_COLUMNS {
separated.push(data.sql.to_string());
bindings.push(Column::DataColumn(data));
bindings.push(Column::Data(data));
}

for meta in T::TIMESTAMP_COLUMNS {
separated.push(meta.sql.to_string());
bindings.push(Column::TimestampColumn(meta));
bindings.push(Column::Timestamp(meta));
}

separated.push_unseparated(")\nVALUES\n (");
Expand Down Expand Up @@ -217,21 +217,21 @@ pub fn update<T: Bind>() -> Query<T> {

let mut col = 2;

for ref fk in T::FOREIGN_KEYS {
for fk in T::FOREIGN_KEYS {
separated.push(format!("{} = ${col}", fk.sql));
bindings.push(Column::ForeignKey(fk));
col += 1;
}

for ref data in T::DATA_COLUMNS {
for data in T::DATA_COLUMNS {
separated.push(format!("{} = ${col}", data.sql));
bindings.push(Column::DataColumn(data));
bindings.push(Column::Data(data));
col += 1;
}

for ref meta in T::TIMESTAMP_COLUMNS {
for meta in T::TIMESTAMP_COLUMNS {
separated.push(format!("{} = ${col}", meta.sql));
bindings.push(Column::TimestampColumn(meta));
bindings.push(Column::Timestamp(meta));
col += 1;
}

Expand Down Expand Up @@ -261,15 +261,15 @@ pub fn upsert<T: Bind>() -> Query<T> {

let mut separated = builder.separated(",\n ");

for ref fk in T::FOREIGN_KEYS {
for fk in T::FOREIGN_KEYS {
separated.push(format!("{} = EXCLUDED.{}", fk.sql, fk.sql));
}

for ref data in T::DATA_COLUMNS {
for data in T::DATA_COLUMNS {
separated.push(format!("{} = EXCLUDED.{}", data.sql, data.sql));
}

for ref meta in T::TIMESTAMP_COLUMNS {
for meta in T::TIMESTAMP_COLUMNS {
separated.push(format!("{} = EXCLUDED.{}", meta.sql, meta.sql));
}

Expand Down Expand Up @@ -340,15 +340,9 @@ mod tests {
impl Bind for TestTable {
fn bind<'q, Q: Bindable<'q>>(&'q self, c: &'q Column<Self>, query: Q) -> crate::Result<Q> {
match c.field() {
"id" => {
return Ok(query.dyn_bind(&self.id));
}
"fk" => {
return Ok(query.dyn_bind(&self.fk));
}
"data" => {
return Ok(query.dyn_bind(&self.data));
}
"id" => Ok(query.dyn_bind(self.id)),
"fk" => Ok(query.dyn_bind(self.fk)),
"data" => Ok(query.dyn_bind(self.data)),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -387,7 +381,7 @@ mod tests {
Bindings(vec![
Column::PrimaryKey(&TestTable::PRIMARY_KEY),
Column::ForeignKey(&TestTable::FOREIGN_KEYS[0]),
Column::DataColumn(&TestTable::DATA_COLUMNS[0]),
Column::Data(&TestTable::DATA_COLUMNS[0]),
])
);
}
Expand All @@ -408,7 +402,7 @@ mod tests {
Bindings(vec![
Column::PrimaryKey(&TestTable::PRIMARY_KEY),
Column::ForeignKey(&TestTable::FOREIGN_KEYS[0]),
Column::DataColumn(&TestTable::DATA_COLUMNS[0]),
Column::Data(&TestTable::DATA_COLUMNS[0]),
])
);
}
Expand All @@ -429,7 +423,7 @@ mod tests {
Bindings(vec![
Column::PrimaryKey(&TestTable::PRIMARY_KEY),
Column::ForeignKey(&TestTable::FOREIGN_KEYS[0]),
Column::DataColumn(&TestTable::DATA_COLUMNS[0]),
Column::Data(&TestTable::DATA_COLUMNS[0]),
])
);
}
Expand Down
2 changes: 1 addition & 1 deletion atmosphere-core/src/schema/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
{
let query = crate::runtime::sql::insert::<T>();

hooks::execute(HookStage::PreBind, &query, HookInput::Row(&mut self)).await?;
hooks::execute(HookStage::PreBind, &query, HookInput::Row(self)).await?;

let mut builder = sqlx::query(query.sql());

Expand Down
2 changes: 1 addition & 1 deletion atmosphere-core/src/schema/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ where
hooks::execute(
hooks::HookStage::PreBind,
&query,
hooks::HookInput::Row(&mut self),
hooks::HookInput::Row(self),
)
.await?;

Expand Down
32 changes: 16 additions & 16 deletions atmosphere-core/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ pub mod column {
/// A foreign key
ForeignKey(&'static ForeignKey<T>),
/// A data column
DataColumn(&'static DataColumn<T>),
Data(&'static DataColumn<T>),
/// A timestamp column
TimestampColumn(&'static TimestampColumn<T>),
Timestamp(&'static TimestampColumn<T>),
}

impl<T: Table> Clone for Column<T> {
fn clone(&self) -> Self {
match self {
Self::PrimaryKey(pk) => Self::PrimaryKey(*pk),
Self::ForeignKey(fk) => Self::ForeignKey(*fk),
Self::DataColumn(data) => Self::DataColumn(*data),
Self::TimestampColumn(ts) => Self::TimestampColumn(*ts),
Self::Data(data) => Self::Data(*data),
Self::Timestamp(ts) => Self::Timestamp(*ts),
}
}
}
Expand All @@ -99,17 +99,17 @@ pub mod column {
match self {
Self::PrimaryKey(pk) => pk.field,
Self::ForeignKey(fk) => fk.field,
Self::DataColumn(data) => data.field,
Self::TimestampColumn(ts) => ts.field,
Self::Data(data) => data.field,
Self::Timestamp(ts) => ts.field,
}
}

pub const fn sql(&self) -> &'static str {
match self {
Self::PrimaryKey(pk) => pk.sql,
Self::ForeignKey(fk) => fk.sql,
Self::DataColumn(data) => data.sql,
Self::TimestampColumn(ts) => ts.sql,
Self::Data(data) => data.sql,
Self::Timestamp(ts) => ts.sql,
}
}
}
Expand Down Expand Up @@ -169,14 +169,14 @@ pub mod column {
Column::ForeignKey(self)
}

/// # Safety
//
/// We do treat this foreign key as a column of another table. This is not
/// smart to do - but can become necessary when doing complex joins. This
/// is memory safe as Self<A> and Self<B> have the exact same memory layout,
/// we do not store any data (A or B) but only a `PhantomData` instance which
/// is here transmuted.
pub const unsafe fn transmute<I: Table>(&'static self) -> &'static ForeignKey<I> {
// SAFETY:
//
// We do treat this foreign key as a column of another table. This is not
// smart to do - but can become necessary when doing complex joins. This
// is memory safe as Self<A> and Self<B> have the exact same memory layout,
// we do not store any data (A or B) but only a `PhantomData` instance which
// is here transmuted.
std::mem::transmute(self)
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ pub mod column {
}

pub const fn as_col(&'static self) -> Column<T> {
Column::DataColumn(self)
Column::Data(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion atmosphere-core/src/schema/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
{
let query = crate::runtime::sql::select_by::<T>(T::PRIMARY_KEY.as_col());

hooks::execute(HookStage::PreBind, &query, HookInput::Row(&mut self)).await?;
hooks::execute(HookStage::PreBind, &query, HookInput::Row(self)).await?;

let mut sql = sqlx::query_as(query.sql());

Expand Down
4 changes: 2 additions & 2 deletions atmosphere-core/src/schema/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ where
{
let query = crate::runtime::sql::update::<T>();

hooks::execute(HookStage::PreBind, &query, HookInput::Row(&mut self)).await?;
hooks::execute(HookStage::PreBind, &query, HookInput::Row(self)).await?;

let mut sql = sqlx::query(query.sql());

Expand Down Expand Up @@ -91,7 +91,7 @@ where
{
let query = crate::runtime::sql::upsert::<T>();

hooks::execute(HookStage::PreBind, &query, HookInput::Row(&mut self)).await?;
hooks::execute(HookStage::PreBind, &query, HookInput::Row(self)).await?;

let mut sql = sqlx::query(query.sql());

Expand Down
Loading

0 comments on commit d01959f

Please sign in to comment.