Derive Macro sql_tool_kit::GenValues
source · #[derive(GenValues)]
{
// Attributes available to this derive:
#[value]
#[config]
}
Expand description
生成针对特定结构体的 ValuesAttributeMacro 实现。
此函数解析标记在结构体字段上的 #[derive(GenValues)] 属性宏,
并生成一个实现 ValuesAttributeMacro trait 的代码块。
它处理每个字段的 ignore 和 index 指令,以生成相应的字段列表。
结构上的 #[config(database = "postgres", index = 1)] 用于设置使用的数据库类型,对于部分有索引的数据库如 postgres 可以使用index设置初始值
目前支持的数据库有:“postgres”, “mariadb”, “mysql”, “sqlite”, “mssql”
宏参数:
#[config(...)]: 设置全局配置。database- 指定生成的数据库类型,目前支持postgresqlmysqlmariadbsqlitemssqlindex- 指定开始的序列,仅postgresqlmssql上有效
#[value(...)] 接受的参数:
ignore- 忽略该字段index- 设置当前值的index,当设置了这个参数后,全局的 index 不会加一value- 直接替换当前的${index},当设置了这个参数后,全局的 index 不会加一- 例如:
#[value(value = "true")]=> [“$1”, “true”,…]
- 例如:
§返回值
返回一个 TokenStream,它包含了生成的 FieldsAttributeMacro trait 实现。
§示例
ⓘ
use sql_tool_core::ValuesAttributeMacro;
#[derive(GenValues)]
#[config(database = "postgres")]
struct PostgresStruct {
field1: i32,
#[value(ignore)]
field2: i32,
#[value(index = 4)]
field3: i32,
}
PostgresStruct::generate_values_clause(); // 输出:["$1", "$4"]
MysqlStruct::last_param_index(); // 2
#[derive(GenValues)]
#[config(database = "mysql")]
struct MysqlStruct {
field1: i32,
#[value(ignore)]
field2: i32,
#[value(index = 4)] // mysql 并不需要占位符,所以不需要 `index`
field3: i32,
}
MysqlStruct::generate_values_clause(); // 输出:["?", "?"]
PostgresStruct::last_param_index(); // 2
// 设置开始的索引
#[derive(GenValues)]
#[config(database = "postgres", index = 5)]
struct PostgresSetIndexStruct {
field1: i32,
field2: i32,
field3: i32,
}
PostgresSetIndexStruct::generate_values_clause(); // 输出["$5", "$6", "$7"]
PostgresSetIndexStruct::last_param_index(); // 7此函数将为 MyStruct 生成相应的 FieldsAttributeMacro 实现。