|
@@ -1,5 +1,5 @@
|
|
|
use proc_macro::TokenStream;
|
|
|
-use quote::{quote};
|
|
|
+use quote::{quote, format_ident};
|
|
|
use syn::parse_macro_input;
|
|
|
|
|
|
use convert_case::{Case, Casing};
|
|
@@ -31,27 +31,39 @@ pub(crate) fn do_make_index(
|
|
|
let input = parse_macro_input!(tokens as MakeIndexParams);
|
|
|
|
|
|
let index_struct_name = input.name;
|
|
|
+ let index_sql_name = format!("{}", index_struct_name).to_case(Case::Snake);
|
|
|
|
|
|
let columns = input.columns.clone().into_iter();
|
|
|
|
|
|
- let index_sql_name = format!("{}", index_struct_name).to_case(Case::Snake);
|
|
|
-
|
|
|
let unique = input.unique.is_some();
|
|
|
|
|
|
- let first_column = columns.clone().next();
|
|
|
+ let first_column = columns.clone().next().unwrap();
|
|
|
+
|
|
|
+ let columns_array_name = format_ident!("INDEX_COLUMN_NAMES_{}", index_struct_name.to_string().to_case(Case::ScreamingSnake));
|
|
|
+ let column_count = columns.clone().len();
|
|
|
|
|
|
quote!{
|
|
|
- pub struct #index_struct_name {}
|
|
|
- // type #index_entity_type_name = <#column_type_path as #microrm_ref::entity::EntityColumn>::Entity;
|
|
|
- // type IndexType = <#first_column as #microrm_ref::entity::EntityColumn>::Entity;
|
|
|
+ pub struct #index_struct_name ();
|
|
|
+
|
|
|
+ #microrm_ref::re_export::lazy_static::lazy_static!{
|
|
|
+ static ref #columns_array_name : [&'static str; #column_count] = {
|
|
|
+ use #microrm_ref::entity::EntityColumn;
|
|
|
+ [
|
|
|
+ #( #columns . name() ),*
|
|
|
+ ]
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
impl #microrm_ref::entity::Index for #index_struct_name {
|
|
|
- type IndexedEntity = <#first_column as #microrm_ref::entity::EntityColumn>::Entity;
|
|
|
fn index_name() -> &'static str {
|
|
|
#index_sql_name
|
|
|
}
|
|
|
- fn columns() -> &'static [&'static dyn #microrm_ref::entity::EntityColumn<Entity = Self::IndexedEntity>] where Self: Sized {
|
|
|
- &[#(&#columns),*]
|
|
|
+ fn table_name() -> &'static str {
|
|
|
+ use #microrm_ref::entity::EntityColumn;
|
|
|
+ #first_column.table_name()
|
|
|
+ }
|
|
|
+ fn column_names() -> &'static [&'static str] {
|
|
|
+ #columns_array_name.as_ref()
|
|
|
}
|
|
|
fn unique() -> bool where Self: Sized {
|
|
|
#unique
|