table-creator-macro2-0.2.1 has been yanked.
A simple macro to generate a specified sql when we have a struct A.
For example:
#[derive(TableCreator)]
struct Mystruct <'a, T>{
#[table_ignore]
a: i32,
b: String,
c: &'a T
#[primary_key]
d: String,
}
printfln!("{}", Mystruct::<'somelifetime, SomeType>::sql_create();
printfln!("{}", Mystruct::<'somelifetime, SomeType>::sql_insert();
the output is :
- CREATE TABLE IF NOT EXISTS mystruct (b TEXT , c SomeMatchedSqlType, d PRIMARY KEY TEXT)
- INSERT INTO TABLE mystruct (b, c, d) VALUES ( ?, ?, ?)
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 name
of the table.
You can also use the default "SERIAL PRIMARY KEY" named "id", without making the field with
atribute [primary_key].
For example:
#[derive(TableCreator)]
struct Mystruct <'a, T>{
#[table_ignore]
a: i32,
b: String,
c: &'a T
//#[primary_key]
d: String,
}
printfln!("{}", Mystruct::<'somelifetime, SomeType>::sql_create();
printfln!("{}", Mystruct::<'somelifetime, SomeType>::sql_insert();
the output is:
- CREATE TABLE IF NOT EXISTS mystruct (b TEXT , c SomeMatchedSqlType, d TEXT, id SERIAL PRIMARY KEY)
- INSERT INTO TABLE mystruct (b, c, d) VALUES ( ?, ?, ?)
It just support some Sqlite's sql types:
- i32| u32 | i64 | u64 => INTEGER
- f32 => REAL
- f64 => REAL
- String | str => TEXT
- bool => BOOLEAN
- NaiveDate => DATE
- NaiveDateTime => TIMESTAMP
- UuidByte=>BYTEA //UuidByte can't be used, unless you have defined one by your own.
- Hash=>BYTEA //Hash can't be used, unless you have defined one by your own.