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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! CRUD Operations - Create, Read, Update, Delete operations for models
//!
//! Implements the core database operations with proper parameter binding,
//! timestamp management, soft delete support, and error handling.
use chrono::Utc;
use serde_json::Value;
use sqlx::{Pool, Postgres};
use crate::error::{ModelError, ModelResult};
use crate::model::core_trait::Model;
/// Trait providing CRUD operations for models
#[allow(async_fn_in_trait)]
pub trait CrudOperations: Model {
// <<<ELIF:BEGIN agent-editable:model_crud_operations>>>
/// Find a model by its primary key
async fn find(pool: &Pool<Postgres>, id: Self::PrimaryKey) -> ModelResult<Option<Self>>
where
Self: Sized,
{
let sql = format!(
"SELECT * FROM {} WHERE {} = $1",
Self::table_name(),
Self::primary_key_name()
);
let row = sqlx::query(&sql)
.bind(id.to_string())
.fetch_optional(pool)
.await
.map_err(|e| {
ModelError::Database(format!("Failed to find {}: {}", Self::table_name(), e))
})?;
match row {
Some(row) => {
let model = Self::from_row(&row)?;
Ok(Some(model))
}
None => Ok(None),
}
}
/// Find a model by its primary key or return an error if not found
async fn find_or_fail(pool: &Pool<Postgres>, id: Self::PrimaryKey) -> ModelResult<Self>
where
Self: Sized,
{
Self::find(pool, id.clone())
.await?
.ok_or_else(|| ModelError::NotFound(format!("{}({})", Self::table_name(), id)))
}
/// Create a new model instance in the database with field-based insertion
async fn create(pool: &Pool<Postgres>, mut model: Self) -> ModelResult<Self>
where
Self: Sized,
{
// Set timestamps if enabled
if Self::uses_timestamps() {
let now = Utc::now();
model.set_created_at(now);
model.set_updated_at(now);
}
// Get field-value pairs from the model
let fields = model.to_fields();
if fields.is_empty() {
// Fallback to DEFAULT VALUES if no fields
let insert_sql = format!(
"INSERT INTO {} DEFAULT VALUES RETURNING *",
Self::table_name()
);
let row = sqlx::query(&insert_sql)
.fetch_one(pool)
.await
.map_err(|e| {
ModelError::Database(format!("Failed to create {}: {}", Self::table_name(), e))
})?;
return Self::from_row(&row);
}
// Build dynamic INSERT query with actual field values
let field_names: Vec<String> = fields.keys().cloned().collect();
let field_placeholders: Vec<String> =
(1..=field_names.len()).map(|i| format!("${}", i)).collect();
let insert_sql = format!(
"INSERT INTO {} ({}) VALUES ({}) RETURNING *",
Self::table_name(),
field_names.join(", "),
field_placeholders.join(", ")
);
let mut query = sqlx::query(&insert_sql);
// Bind values in the same order as field_names
for field_name in &field_names {
if let Some(value) = fields.get(field_name) {
query = Self::bind_json_value(query, value)?;
}
}
let row = query.fetch_one(pool).await.map_err(|e| {
ModelError::Database(format!("Failed to create {}: {}", Self::table_name(), e))
})?;
Self::from_row(&row)
}
/// Update this model instance in the database with field-based updates
async fn update(&mut self, pool: &Pool<Postgres>) -> ModelResult<()> {
if let Some(pk) = self.primary_key() {
// Set updated_at timestamp if enabled
if Self::uses_timestamps() {
self.set_updated_at(Utc::now());
}
// Get field-value pairs from the model
let fields = self.to_fields();
if fields.is_empty() {
// Fallback to just updating timestamp
let update_sql = format!(
"UPDATE {} SET updated_at = NOW() WHERE {} = $1",
Self::table_name(),
Self::primary_key_name()
);
sqlx::query(&update_sql)
.bind(pk.to_string())
.execute(pool)
.await
.map_err(|e| {
ModelError::Database(format!(
"Failed to update {}: {}",
Self::table_name(),
e
))
})?;
return Ok(());
}
// Build dynamic UPDATE query with actual field values
// Filter out primary key from updates
let pk_name = Self::primary_key_name();
let update_fields: Vec<String> = fields
.keys()
.filter(|&field| field != pk_name)
.enumerate()
.map(|(i, field)| format!("{} = ${}", field, i + 1))
.collect();
if update_fields.is_empty() {
// No fields to update
return Ok(());
}
let update_sql = format!(
"UPDATE {} SET {} WHERE {} = ${}",
Self::table_name(),
update_fields.join(", "),
pk_name,
update_fields.len() + 1
);
let mut query = sqlx::query(&update_sql);
// Bind update values (excluding primary key)
for field_name in fields.keys() {
if field_name != pk_name {
if let Some(value) = fields.get(field_name) {
query = Self::bind_json_value(query, value)?;
}
}
}
// Bind primary key for WHERE clause
query = query.bind(pk.to_string());
query.execute(pool).await.map_err(|e| {
ModelError::Database(format!("Failed to update {}: {}", Self::table_name(), e))
})?;
Ok(())
} else {
Err(ModelError::MissingPrimaryKey)
}
}
/// Delete this model instance from the database
async fn delete(self, pool: &Pool<Postgres>) -> ModelResult<()> {
if let Some(pk) = self.primary_key() {
if Self::uses_soft_deletes() {
// Soft delete - just set deleted_at timestamp
let soft_delete_sql = format!(
"UPDATE {} SET deleted_at = NOW() WHERE {} = $1",
Self::table_name(),
Self::primary_key_name()
);
sqlx::query(&soft_delete_sql)
.bind(pk.to_string())
.execute(pool)
.await
.map_err(|e| {
ModelError::Database(format!(
"Failed to soft delete {}: {}",
Self::table_name(),
e
))
})?;
} else {
// Hard delete - remove from database
let delete_sql = format!(
"DELETE FROM {} WHERE {} = $1",
Self::table_name(),
Self::primary_key_name()
);
sqlx::query(&delete_sql)
.bind(pk.to_string())
.execute(pool)
.await
.map_err(|e| {
ModelError::Database(format!(
"Failed to delete {}: {}",
Self::table_name(),
e
))
})?;
}
Ok(())
} else {
Err(ModelError::MissingPrimaryKey)
}
}
/// Helper method to bind JSON values to SQL queries
fn bind_json_value<'a>(
query: sqlx::query::Query<'a, Postgres, sqlx::postgres::PgArguments>,
value: &Value,
) -> ModelResult<sqlx::query::Query<'a, Postgres, sqlx::postgres::PgArguments>> {
match value {
Value::Null => Ok(query.bind(None::<String>)),
Value::Bool(b) => Ok(query.bind(*b)),
Value::Number(n) => {
if let Some(i) = n.as_i64() {
Ok(query.bind(i))
} else if let Some(f) = n.as_f64() {
Ok(query.bind(f))
} else {
Ok(query.bind(n.to_string()))
}
}
Value::String(s) => Ok(query.bind(s.clone())),
Value::Array(_) | Value::Object(_) => {
// For complex JSON types, bind as JSONB
Ok(query.bind(sqlx::types::Json(value.clone())))
}
}
}
// <<<ELIF:END agent-editable:model_crud_operations>>>
}
// Implement CrudOperations for all types that implement Model
impl<T: Model> CrudOperations for T {}