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
//! # Crudly
//! Derivable crud helpers for sqlx. Write your `SELECT`, `INSERT`, `UPDATE`, and `DELETE` queries once and reuse them for all your entities.
//!
//! Provides the `#[derive(IntoRow)]` and `#[derive(Crudly)]` macros.
//!
//! ## Features
//!
//! - `derive`: (Default) Enables the `#[derive(Crudly)]` and `#[derive(IntoRow)]` macros
//! - `postgres`: Enables default crud implementations for database
//! - `mysql`: Enables default crud implementations for MySQL
//! - `sqlite`: Enables default crud implementations for sqlite
//! - `all-databases`: Enables default crud implementations all DBs above
//! - `json`: Enables support for the `#[sqlx(json)]` / `#[crudly(json)]` attribute using [serde](https://crates.io/crates/serde).
//!
//! ## Example
//!
//! ```rust
//! # #[allow(unused_variables)]
//! # use crudly::{Crudly, InsertReturningId};
//! # use sqlx::{SqlitePool, query};
//! #[derive(sqlx::FromRow, crudly::IntoRow, crudly::Crudly)]
//! struct User {
//! #[crudly(id)]
//! id: i64,
//! name: String,
//! }
//!
//! const CREATE_USERS_TABLE_SQL: &str =
//! "CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT NOT NULL);";
//!
//! #[tokio::main]
//! async fn main() {
//! let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
//! query(CREATE_USERS_TABLE_SQL).execute(&pool).await.unwrap();
//!
//! // --------------- Inserting a user ---------------
//!
//! let user = User {
//! id: 0,
//! name: "John Doe".to_string(),
//! };
//! let inserted_id = user.insert_returning_id(&pool).await.unwrap();
//! assert_eq!(inserted_id, 1);
//!
//! // --------------- Selecting all users ---------------
//!
//! let mut users: Vec<User> = User::select_all(&pool).await.unwrap();
//! assert_eq!(users.len(), 1);
//!
//! let mut jon = users.remove(0);
//!
//! jon.name = "Jane Doe".to_string();
//!
//! // --------------- Updating a user ---------------
//!
//! let updated = jon.update_by_id(&pool).await.unwrap();
//!
//! assert!(updated);
//!
//! // --------------- Getting a user by id ---------------
//!
//! let updated_user = User::select_by_id(&1, &pool).await.unwrap().unwrap();
//! assert_eq!(updated_user.name, "Jane Doe");
//!
//! // --------------- Deleting a user ---------------
//!
//! let deleted = User::delete_by_id(&1, &pool).await.unwrap();
//! assert!(deleted);
//!
//! let users: Vec<User> = User::select_all(&pool).await.unwrap();
//! assert!(users.is_empty());
//! }
//! ```
//!
//! ## The `IntoRow` trait
//!
//! IntoRow tries to be the counterpart to [sqlx::FromRow](https://docs.rs/sqlx/latest/sqlx/trait.FromRow.html).
//! It exposes the following two functions:
//! - `columns`: Returns the column names for the struct (respecting all sqlx attributes)
//! - `bind_arguments`: Binds the values of the struct to an SQLx [`sqlx::Arguments`] buffer
//!
//! **Deriving IntoRow**
//!
//! IntoRow understands the same attributes as `sqlx::FromRow` supports as of sqlx 0.8.6. So The code below will just work:
//! ```
//! # #[allow(unused_variables)]
//! # use crudly::{Crudly, IntoRow, HasColumns};
//! # use sqlx::{SqlitePool, query};
//! #[derive(crudly::IntoRow)]
//! #[sqlx(rename_all = "camelCase")]
//! struct User {
//! #[sqlx(rename = "userId")]
//! id: i64,
//! user_name: String,
//! #[sqlx(skip)]
//! skip_me: String,
//! }
//!
//! assert_eq!(User::columns(), vec!["userId", "userName"]);
//! ```
//! In case you only want to derive IntoRow and not FromRow all the attributes also have a #[crudly(rename = "name")] format.
//!
//! Crudly understands the `#[sqlx(try_from = ...))]` as `#[crudly(try_into = ...)]` for binding to arguments it will then use
//! the `TryInto` trait before decoding.
//!
//! **Note:** the field with the `#[crudly(id)]` attribute will never be included in the arguments generated by IntoRow,
//! because depending on context the id might or might not be desired.
//!
//! ## The `Crudly` trait
//!
//! Deriving Crudly enables the following methods: `select_all`, `select_by_id`, `update_by_id`,
//! `id_exists`, `delete_by_id` and either `insert_returning_id` or `insert_with_id` (see next section).
//!
//! ### IDs
//!
//! Currently crudly requires that every entity has a single id column that is expected to be unique (probably a PK).
//! A future version might introduce support for multiple id columns if theres a need for it.
//! The id must be marked with the `#[crudly(id)]` attribute.
//!
//! Further crudly distinguishes between two types of ids:
//!
//! - `DBAssignedId`: The id is assigned by the database using something like an AUTOINCREMENT or SERIAL column.
//! - `ExternallyAssignedId`: The id is assigned by the application using something like a uuid.
//!
//! By default crudly will assume that the id is assigned by the database. If you want to assign the id inside of
//! rust instead, use `#[crudly(external_ids)]` on the struct. See the
//! [external_ids example](https://docs.rs/crudly/latest/crudly/example.external_ids/index.html).
//!
//! ## Executors
//!
//! Crudly provides default implementations for performing the crud operations for sqlite, mysql and postgres.
//! However you might want to customize their behavior. Add some logging, return a custom error type or even add another Database.
//! For this purpose crudly provides the `CRUDExecutor` trait. By default crudly will use the `DefaultCRUDExecutor` struct for all crud operations.
//! you can overwrite this by using the `#[crudly(executor = ...)]` attribute. See the [executor example](https://docs.rs/crudly/latest/crudly/example.custom_executor/index.html) for more details.
//!
//! ```
//! # #[allow(unused_variables)]
//! # use crudly::{Crudly, IntoRow, HasColumns, DefaultCRUDExecutor};
//! # use sqlx::{SqlitePool, query};
//! #[derive(sqlx::FromRow, crudly::IntoRow, crudly::Crudly)]
//! #[crudly(executor = DefaultCRUDExecutor)] // <-- Use your custom executor here
//! struct User {
//! #[crudly(id)]
//! id: i64,
//! user_name: String,
//! }
//! ```
//!
//! ## MSRV
//! The minimum supported Rust version is 1.85.0 (the version that released edition 2024).
//!
//! ## Contributing
//! Contributions, bug reports and feature requests are always welcome! Please open an issue or a pull request on
//! [GitHub](https://github.com/TimoLehnertz/crudly).
pub use ;
pub use *;
pub use *;