Skip to content

Commit

Permalink
Provide a syntax extension free alternative to #[insertable_into]
Browse files Browse the repository at this point in the history
This provides a pure stable alternative to the `#[insertable_into]`
annotation. The intention is to change the annotation to call this
macro, rather than `impl Insertable` directly. However, there are some
unaddressed issues for that, and I will submit that as a separate PR to
attempt to keep the PR size reasonable.

The tests for this are a bit messy, as the actual test body doesn't
change much -- Just the struct definition. I moved the most common case
out into a macro, but I opted to just leave the duplication for the
remaining 4-5 cases that didn't fit, instead of trying to make it so dry
it chafes.

We will continue to support syntex as an option on stable, as we can
provide much better error messages from a procedural macro. I would like
to improve the error messages in some cases if possible though (in
particular, we want to handle the case where a unit struct is passed or
where a tuple struct has unannotated fields).

The structure of the macro is intended to be compatible with the
`custom_derive` crate. This is untested, but will be fully tested once
I've moved all our annotations to stable macros. The goal is for any
struct definition to be copy pasted into this macro, and the macro
parses the struct body to create the proper implementation. For
sufficiently large structs, we can hit the recursion limit, but there's
really no way around that. People will just need to bump the limit.

One case that this macro *doesn't* handle is when there are annotations
on struct fields other than `#[column_name]`. I had originally planned
to handle these, but I realized that the only recognized annotation that
could be there on stable is `#[cfg]`, and we are *not* handling cfg
attributes. We might handle that in the future, but it'd look *really*
ugly.

Related to #99
  • Loading branch information
sgrif committed Apr 24, 2016
1 parent 9c8716c commit d8ce020
Show file tree
Hide file tree
Showing 12 changed files with 787 additions and 158 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/

## Unreleased

### Added

* The `Insertable!` macro can now be used instead of `#[insertable_into]` for
those wishing to avoid syntax extensions from `diesel_codegen`. See
http://docs.diesel.rs/diesel/macro.Insertable!.html for details.

### Changed

* `infer_schema!` on SQLite now accepts a larger range of type names
Expand Down
37 changes: 9 additions & 28 deletions diesel/src/doctest_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,17 @@ struct NewUser {
name: String,
}

struct NewUserValues {
name: String,
}

impl<DB> InsertValues<DB> for NewUserValues where
DB: diesel::backend::Backend,
{
fn column_names(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult {
out.push_sql("name");
Ok(())
}

fn values_clause(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult {
out.push_sql(&format!("('{}')", self.name));
Ok(())
}

fn values_bind_params(&self, _out: &mut DB::BindCollector) -> QueryResult<()> {
Ok(())
impl NewUser {
pub fn new(name: &str) -> Self {
NewUser {
name: name.into(),
}
}
}

impl<'a, DB> Insertable<users::table, DB> for &'a NewUser where
DB: diesel::backend::Backend,
{
type Values = NewUserValues;

fn values(self) -> Self::Values {
NewUserValues {
name: self.name.clone(),
}
Insertable! {
(users)
struct NewUser {
name: String,
}
}
3 changes: 1 addition & 2 deletions diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#[macro_use]
mod macros;
#[macro_use]
pub mod query_builder;

pub mod associations;
pub mod backend;
Expand All @@ -16,6 +14,7 @@ pub mod connection;
pub mod expression;
#[doc(hidden)]
pub mod persistable;
pub mod query_builder;
#[macro_use]
pub mod types;

Expand Down
Loading

0 comments on commit d8ce020

Please sign in to comment.