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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use proc_macro2::TokenStream;
use quote::quote;
use crate::utils::macro_tokens::MacroTokens;
/// Generates the TokenStream for the __update() CRUD operation
pub fn generate_update_tokens(macro_data: &MacroTokens, table_schema_data: &String) -> TokenStream {
let ty = macro_data.ty;
let update_columns = macro_data.get_column_names_pk_parsed();
// Retrieves the fields of the Struct
let fields = macro_data.get_struct_fields();
let mut vec_columns_values: Vec<String> = Vec::new();
for (i, column_name) in update_columns.iter().enumerate() {
let column_equal_value = format!("{} = ${}", column_name.to_owned(), i + 2);
vec_columns_values.push(column_equal_value)
}
let str_columns_values = vec_columns_values.join(", ");
let update_values = fields.iter().map(|ident| {
quote! { &self.#ident }
});
let update_values_cloned = update_values.clone();
if let Some(primary_key) = macro_data.get_primary_key_annotation() {
let pk_index = macro_data
.get_pk_index()
.expect("Update method failed to retrieve the index of the primary key");
quote! {
/// Updates a database record that matches
/// the current instance of a T type, returning a result
/// indicating a possible failure querying the database.
async fn update(&self) -> Result<(), Box<dyn std::error::Error + Sync + std::marker::Send>> {
let stmt = format!(
"UPDATE {} SET {} WHERE {} = ${:?}",
#table_schema_data, #str_columns_values, #primary_key, #pk_index + 1
);
let update_values: &[&dyn canyon_sql::crud::bounds::QueryParameter<'_>] = &[#(#update_values),*];
<#ty as canyon_sql::crud::Transaction<#ty>>::query(
stmt, update_values, ""
).await?;
Ok(())
}
/// Updates a database record that matches
/// the current instance of a T type, returning a result
/// indicating a possible failure querying the database with the
/// specified datasource
async fn update_datasource<'a>(&self, datasource_name: &'a str)
-> Result<(), Box<dyn std::error::Error + Sync + std::marker::Send>>
{
let stmt = format!(
"UPDATE {} SET {} WHERE {} = ${:?}",
#table_schema_data, #str_columns_values, #primary_key, #pk_index + 1
);
let update_values: &[&dyn canyon_sql::crud::bounds::QueryParameter<'_>] = &[#(#update_values_cloned),*];
<#ty as canyon_sql::crud::Transaction<#ty>>::query(
stmt, update_values, datasource_name
).await?;
Ok(())
}
}
} else {
// If there's no primary key, update method over self won't be available.
// Use instead the update associated function of the querybuilder
// TODO Returning an error should be a provisional way of doing this
quote! {
async fn update(&self)
-> Result<(), Box<dyn std::error::Error + Sync + std::marker::Send>>
{
Err(
std::io::Error::new(
std::io::ErrorKind::Unsupported,
"You can't use the 'update' method on a \
CanyonEntity that does not have a #[primary_key] annotation. \
If you need to perform an specific search, use the Querybuilder instead."
).into_inner().unwrap()
)
}
async fn update_datasource<'a>(&self, datasource_name: &'a str)
-> Result<(), Box<dyn std::error::Error + Sync + std::marker::Send>>
{
Err(
std::io::Error::new(
std::io::ErrorKind::Unsupported,
"You can't use the 'update_datasource' method on a \
CanyonEntity that does not have a #[primary_key] annotation. \
If you need to perform an specific search, use the Querybuilder instead."
).into_inner().unwrap()
)
}
}
}
}
/// Generates the TokenStream for the __update() CRUD operation
/// being the query generated with the [`QueryBuilder`]
pub fn generate_update_query_tokens(
macro_data: &MacroTokens,
table_schema_data: &String,
) -> TokenStream {
let ty = macro_data.ty;
quote! {
/// Generates a [`canyon_sql::query::UpdateQueryBuilder`]
/// that allows you to customize the query by adding parameters and constrains dynamically.
///
/// It performs an `UPDATE table_name`, where `table_name` it's the name of your
/// entity but converted to the corresponding database convention,
/// unless concrete values are set on the available parameters of the
/// `canyon_macro(table_name = "table_name", schema = "schema")`
fn update_query<'a>() -> canyon_sql::query::UpdateQueryBuilder<'a, #ty> {
canyon_sql::query::UpdateQueryBuilder::new(#table_schema_data, "")
}
/// Generates a [`canyon_sql::query::UpdateQueryBuilder`]
/// that allows you to customize the query by adding parameters and constrains dynamically.
///
/// It performs an `UPDATE table_name`, where `table_name` it's the name of your
/// entity but converted to the corresponding database convention,
/// unless concrete values are set on the available parameters of the
/// `canyon_macro(table_name = "table_name", schema = "schema")`
///
/// The query it's made against the database with the configured datasource
/// described in the configuration file, and selected with the [`&str`]
/// passed as parameter.
fn update_query_datasource<'a>(datasource_name: &'a str) -> canyon_sql::query::UpdateQueryBuilder<'a, #ty> {
canyon_sql::query::UpdateQueryBuilder::new(#table_schema_data, datasource_name)
}
}
}