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
/*
* @Author: plucky
* @Date: 2022-10-21 16:53:21
* @LastEditTime: 2023-10-27 21:29:46
* @Description:
*/
//! Derive macro for sqlx to implement Create, Read, Update, and Delete (CRUD) methods.
//! # Use
//! adding the following to your project's Cargo.toml:
//! ```toml
//! [dependencies]
//! co-orm = { virsion = "0.2", features = ["mysql"] }
//! sqlx = { version = "0.7", features = ["mysql"] }
//! ```
//! features: mysql, postgres, sqlite, mssql
//!
//! # Examples
//! ```
//! use co_orm::{Crud, FromRow};
//!
//! #[derive(Debug, Crud, FromRow)]
//! #[orm_rename = "users"] // rename table name
//! pub struct User {
//! #[orm_seq] // sequence field, insert will ignore this field
//! //#[orm_pk] // default first field is primary key
//! pub id: u64,
//!
//! #[orm_by] // generate query_by_field,update_by_field,delete_by_field
//! // #[orm_rename = "name"] // rename field name
//! pub name: String,
//! #[orm_update] // generate method update_xxx.
//! pub password: String,
//!
//! #[orm_ignore] // ignore field
//! pub addr: String,
//! }
//!
//! // use crud
//! let u = User::get(&pool, 1).await.unwrap();
//! println!("get {:?}", u);
//! let u = User::get_by(&pool, "where id=1").await.unwrap();
//! println!("get_by {:?}", u);
//! ```
pub use co_orm_macros::Crud;
pub use co_orm_macros::FromRow;
/// sqlx::query_as的调用
/// ``` no_run
/// query_as!(User, "select * from users where name = ?", name).fetch_one(&pool).await
/// ```
#[macro_export]
macro_rules! query_as (
($out_struct:path, $query:expr) => ( {
sqlx::query_as::<_, $out_struct>($query)
});
($out_struct:path, $query:expr, $($args:expr),*) => ( {
sqlx::query_as::<_, $out_struct>($query)
$(.bind($args))*
})
);
/// sqlx::query的调用
/// ``` no_run
/// query!("insert into users (name, password) values (?,?)", name, password).execute(&pool).await
/// ```
#[macro_export]
macro_rules! query (
($query:expr) => ( {
sqlx::query($query)
});
($query:expr, $($args:expr),*) => ( {
sqlx::query($query)
$(.bind($args))*
})
);