forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_inference.rs
More file actions
59 lines (49 loc) · 2.13 KB
/
Copy pathschema_inference.rs
File metadata and controls
59 lines (49 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use syn;
use quote;
use database_url::extract_database_url;
use diesel_infer_schema::*;
use util::{get_options_from_input, get_option, get_optional_option};
pub fn derive_infer_schema(input: syn::MacroInput) -> quote::Tokens {
fn bug() -> ! {
panic!("This is a bug. Please open a Github issue \
with your invocation of `infer_schema`!");
}
let options = get_options_from_input("infer_schema_options", &input.attrs, bug)
.unwrap_or_else(|| bug());
let database_url = extract_database_url(get_option(&options, "database_url", bug)).unwrap();
let schema_name = get_optional_option(&options, "schema_name");
let schema_name = schema_name.as_ref().map(|s| &**s);
let table_names = load_table_names(&database_url, schema_name)
.expect(&format!("Could not load table names from database `{}`{}",
database_url,
if let Some(name) = schema_name {
format!(" with schema `{}`", name)
} else {
"".into()
}
));
let tables = table_names.iter()
.map(|table| {
let mod_ident = syn::Ident::new(format!("infer_{}", table.name));
let table_name = table.to_string();
quote! {
mod #mod_ident {
infer_table_from_schema!(#database_url, #table_name);
}
pub use self::#mod_ident::*;
}
});
handle_schema(tables, schema_name)
}
pub fn derive_infer_table_from_schema(input: syn::MacroInput) -> quote::Tokens {
fn bug() -> ! {
panic!("This is a bug. Please open a Github issue \
with your invocation of `infer_table_from_schema`!");
}
let options = get_options_from_input("infer_table_from_schema_options", &input.attrs, bug)
.unwrap_or_else(|| bug());
let database_url = extract_database_url(get_option(&options, "database_url", bug)).unwrap();
let table_name = get_option(&options, "table_name", bug);
expand_infer_table_from_schema(&database_url, &table_name.parse().unwrap())
.expect(&format!("Could not infer table {}", table_name))
}