| Crates.io | table-creator-macro2 |
| lib.rs | table-creator-macro2 |
| version | 0.2.5 |
| created_at | 2025-05-16 06:06:48.38373+00 |
| updated_at | 2025-07-15 06:01:21.292182+00 |
| description | A simple macro to generate a specific sql when we have a structure. |
| homepage | |
| repository | https://github.com/heyaranzheng/table-creator-macro2 |
| max_upload_size | |
| id | 1676128 |
| size | 61,588 |
for example:
#[derive(TableCreator)]
struct Mystruct <'somelifetime, SomeType>{
#[table_ignore]
a: i32,
b: String,
c: &'Somelifetime SomeType
#[primary_key]
d: String,
}
printfln!("{}", Mystruct::<'somelifetime, SomeType>::sql_create();
printfln!("{}", Mystruct::<'somelifetime, SomeType>::sql_insert();
the output is :
Note:
The elements , with #[table_ignore], will be ignored, and will be dealed as a key if #[primary_key].
And the table name always the lower case. If the struct name is "Abc", then "abc" will be the nameof the table.
You can also use the default "SERIAL PRIMARY KEY" named "id", without marking the field with atribute [primary_key].
For example:
#[derive(TableCreator)]
struct Mystruct <'somelifetime, SomeType>{
#[table_ignore]
a: i32,
b: String,
c: &'somelifetime, SomeType,
//#[primary_key]
d: String,
}
printfln!("{}", Mystruct::<'somelifetime, SomeType>::sql_create();
printfln!("{}", Mystruct::<'somelifetime, SomeType>::sql_insert();
the output is:
This crate also provided a way to generate the arguments to insert a row for *postgres *if you use sqlx.
Add this in your** Cargo.toml** firstly:
[features]
sqlx-support = ["table-creator-macro2/sqlx-support"]
default = ["sqlx-support"]
Then you can create args for postgres like this :
let mystruct = MyStruct { .....}
let args = mystruct.to_sql_value();
This args will ignore the element which marked with #[table_ignore].
It just support some sql types: