-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from SeaQL/sea-query-derive
Sea query derive
- Loading branch information
Showing
14 changed files
with
435 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
[workspace] | ||
members = [ | ||
".", | ||
"sea-query-derive", | ||
] | ||
|
||
[package] | ||
name = "sea-query" | ||
version = "0.4.0" | ||
authors = ["Billy Chan <[email protected]>"] | ||
version = "0.5.0" | ||
authors = [ "Billy Chan <[email protected]>" ] | ||
edition = "2018" | ||
description = "A database agnostic runtime query builder for Rust" | ||
license = "MIT OR Apache-2.0" | ||
documentation = "https://docs.rs/sea-query" | ||
repository = "https://github.com/SeaQL/Sea-Query" | ||
categories = ["database"] | ||
keywords = ["database", "sql", "mysql", "postgres", "sqlite"] | ||
repository = "https://github.com/SeaQL/sea-query" | ||
categories = [ "database" ] | ||
keywords = [ "database", "sql", "mysql", "postgres", "sqlite" ] | ||
|
||
[lib] | ||
name = "sea_query" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
serde_json = "1.0" | ||
sea-query-derive = { version = "0.1.0", path = "sea-query-derive", default-features = false, optional = true } | ||
|
||
[dev-dependencies] | ||
async-std = "1.8" | ||
sqlx = { version = "0.4.0", default-features = false, features = [ "runtime-async-std-native-tls", "macros", "any", "mysql", "postgres", "sqlite", "tls", "migrate", "decimal" ] } | ||
|
||
[features] | ||
sqlx-driver = [] | ||
default = [ "derive" ] | ||
sqlx-driver = [ ] | ||
derive = [ "sea-query-derive" ] | ||
|
||
[[example]] | ||
name = "sqlx" | ||
required-features = ["sqlx-driver"] | ||
required-features = [ "sqlx-driver" ] | ||
|
||
[[example]] | ||
name = "derive" | ||
required-features = [ "derive" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use sea_query::Iden; | ||
|
||
#[derive(Iden)] | ||
enum User { | ||
Table, | ||
Id, | ||
FirstName, | ||
LastName, | ||
Email, | ||
} | ||
|
||
#[derive(Iden)] | ||
// Outer iden attributes overrides what's used for "Table"... | ||
#[iden = "user"] | ||
enum Custom { | ||
Table, | ||
#[iden = "my_id"] | ||
Id, | ||
#[iden = "name"] | ||
FirstName, | ||
#[iden = "surname"] | ||
LastName, | ||
// Custom casing if needed | ||
#[iden = "EMail"] | ||
Email, | ||
} | ||
|
||
#[derive(Iden)] | ||
enum Something { | ||
// ...the Table can also be overwritten like this | ||
#[iden = "something_else"] | ||
Table, | ||
Id, | ||
AssetName, | ||
UserId, | ||
} | ||
|
||
#[derive(Iden)] | ||
pub struct SomeType; | ||
#[derive(Iden)] | ||
#[iden = "another_name"] | ||
pub struct CustomName; | ||
|
||
fn main() { | ||
println!("Default field names"); | ||
assert_eq!(dbg!(Iden::to_string(&User::Table)), "user"); | ||
assert_eq!(dbg!(Iden::to_string(&User::Id)), "id"); | ||
assert_eq!(dbg!(Iden::to_string(&User::FirstName)), "first_name"); | ||
assert_eq!(dbg!(Iden::to_string(&User::LastName)), "last_name"); | ||
assert_eq!(dbg!(Iden::to_string(&User::Email)), "email"); | ||
|
||
println!("Custom field names"); | ||
assert_eq!(dbg!(Iden::to_string(&Custom::Table)), "user"); | ||
assert_eq!(dbg!(Iden::to_string(&Custom::Id)), "my_id"); | ||
assert_eq!(dbg!(Iden::to_string(&Custom::FirstName)), "name"); | ||
assert_eq!(dbg!(Iden::to_string(&Custom::LastName)), "surname"); | ||
assert_eq!(dbg!(Iden::to_string(&Custom::Email)), "EMail"); | ||
|
||
println!("Single custom field name"); | ||
assert_eq!(dbg!(Iden::to_string(&Something::Table)), "something_else"); | ||
assert_eq!(dbg!(Iden::to_string(&Something::Id)), "id"); | ||
assert_eq!(dbg!(Iden::to_string(&Something::AssetName)), "asset_name"); | ||
assert_eq!(dbg!(Iden::to_string(&Something::UserId)), "user_id"); | ||
|
||
println!("Unit structs"); | ||
assert_eq!(dbg!(Iden::to_string(&SomeType)), "some_type"); | ||
assert_eq!(dbg!(Iden::to_string(&CustomName)), "another_name"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "sea-query-derive" | ||
version = "0.1.0" | ||
authors = [ "Follpvosten <[email protected]>" ] | ||
edition = "2018" | ||
description = "Derive macro for sea-query's Iden trait" | ||
license = "MIT OR Apache-2.0" | ||
documentation = "https://docs.rs/sea-query" | ||
repository = "https://github.com/SeaQL/sea-query" | ||
categories = [ "database" ] | ||
keywords = [ "database", "sql", "mysql", "postgres", "sqlite" ] | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = { version = "1", default-features = false, features = [ "derive", "parsing", "proc-macro", "printing" ] } | ||
quote = "1" | ||
sea-query = "0.4" | ||
heck = "0.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use heck::SnakeCase; | ||
use proc_macro::{self, TokenStream}; | ||
use quote::{quote, quote_spanned}; | ||
use syn::{parse_macro_input, Attribute, DataEnum, DataStruct, DeriveInput, Fields, Meta, Variant}; | ||
|
||
fn get_iden_attr(attrs: &[Attribute]) -> Option<syn::Lit> { | ||
for attr in attrs { | ||
let name_value = match attr.parse_meta() { | ||
Ok(Meta::NameValue(nv)) => nv, | ||
_ => continue, | ||
}; | ||
if name_value.path.is_ident("iden") { | ||
return Some(name_value.lit); | ||
} | ||
} | ||
None | ||
} | ||
|
||
#[proc_macro_derive(Iden, attributes(iden))] | ||
pub fn derive_iden(input: TokenStream) -> TokenStream { | ||
let DeriveInput { | ||
ident, data, attrs, .. | ||
} = parse_macro_input!(input); | ||
let table_name = match get_iden_attr(&attrs) { | ||
Some(lit) => quote! { #lit }, | ||
None => { | ||
let normalized = ident.to_string().to_snake_case(); | ||
quote! { #normalized } | ||
} | ||
}; | ||
|
||
// Currently we only support enums and unit structs | ||
let variants = | ||
match data { | ||
syn::Data::Enum(DataEnum { variants, .. }) => variants, | ||
syn::Data::Struct(DataStruct { | ||
fields: Fields::Unit, | ||
.. | ||
}) => { | ||
return quote! { | ||
impl sea_query::Iden for #ident { | ||
fn unquoted(&self, s: &mut dyn sea_query::Write) { | ||
write!(s, #table_name).unwrap(); | ||
} | ||
} | ||
} | ||
.into() | ||
} | ||
_ => return quote_spanned! { | ||
ident.span() => compile_error!("you can only derive Iden on enums or unit structs"); | ||
} | ||
.into(), | ||
}; | ||
if variants.is_empty() { | ||
return TokenStream::new(); | ||
} | ||
|
||
let variant = variants | ||
.iter() | ||
.map(|Variant { ident, .. }| quote! { #ident }); | ||
let name = variants.iter().map(|v| { | ||
if let Some(lit) = get_iden_attr(&v.attrs) { | ||
// If the user supplied a name, just adapt it. | ||
return quote! { #lit }; | ||
} | ||
if v.ident == "Table" { | ||
table_name.clone() | ||
} else { | ||
let ident = v.ident.to_string().to_snake_case(); | ||
quote! { #ident } | ||
} | ||
}); | ||
|
||
let output = quote! { | ||
impl sea_query::Iden for #ident { | ||
fn unquoted(&self, s: &mut dyn sea_query::Write) { | ||
write!(s, "{}", match self { | ||
#(Self::#variant => #name),* | ||
}).unwrap(); | ||
} | ||
} | ||
}; | ||
output.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.