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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
//! Do dynamic-sql query through proc-macro
//!
//! It bases on [**sqlx**] crate (default feature), you can switch them by setting the features.
//! It uses [**Ramhorns**] the high performance template engine implementation of [**Mustache**]
//!
//! ## Example (Sqlx)
//!
//! ### main.rs
//! ```ignore
//! //...
//!
//! # #[tokio::main]
//! async fn main() {
//! let conn = connect_postgres_db().await;
//!
//! // fetch all
//! let dto = UserDto{ id: None, name: None, age: Some(15) };
//! let rst = fetch_all!(|&dto, &conn| -> User {
//! r#"SELECT * FROM test_user
//! WHERE 1 = 1
//! {{#name}}AND name = :name{{/name}}
//! {{#age}}AND age > :age{{/age}}
//! ORDER BY id"#
//! }).unwrap();
//! assert_eq!(
//! vec![
//! User { id: 2, name: Some("zhanglan".to_owned()), age: Some(21) },
//! User { id: 3, name: Some("zhangsan".to_owned()), age: Some(35) }
//! ],
//! rst
//! );
//!
//! let rst = fetch_one!(...).unwrap();
//!
//! let rst = fetch_scalar!(...).unwrap();
//!
//! let affected_rows_num = execute!(...).unwrap();
//!
//! let insert_id = insert!(...).unwrap();
//!
//! sql!('sql_fragment_1', "select * from table1");
//! let rst = fetch_one!(|...| sql_fragment_1 + "where age > 10").unwrap();
//!
//! let mut page_dto = ...;
//! let pagination = page!(|&mut page_dto, &conn| -> User).unwrap();
//! }
//! ```
//!
//! ## Example (sqlx)
//! Full example please see: [Dysql sqlx example](https://github.com/evanzp0/dysql-project/tests)
mod dy_sqlx;
use dy_sqlx::expand;
mod sql_expand;
use proc_macro::TokenStream;
use syn::{punctuated::Punctuated, parse_macro_input, Token};
use std::{collections::HashMap, sync::RwLock};
use quote::quote;
use dysql::{QueryType, get_dysql_config, STATIC_SQL_FRAGMENT_MAP, get_sql_fragment};
#[derive(Debug)]
struct SqlFragment {
name: String,
value: String,
}
impl syn::parse::Parse for SqlFragment {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let name= input.parse::<syn::LitStr>()?.value();
input.parse::<syn::Token!(,)>()?;
let value= input.parse::<syn::LitStr>()?.value();
Ok(Self { name, value })
}
}
#[allow(dead_code)]
#[derive(Debug)]
struct SqlClosure {
dto: Option<syn::Ident>,
is_dto_ref: bool,
is_dto_ref_mut: bool,
cot: syn::Ident, // database connection or transaction
is_cot_ref: bool,
is_cot_ref_mut: bool,
sql_name: Option<String>,
ret_type: Option<syn::Path>, // return type
dialect: syn::Ident,
body: String,
}
impl syn::parse::Parse for SqlClosure {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
// parse closure parameters
let mut is_dto_ref = false;
let mut is_dto_ref_mut = false;
//// parse dto
input.parse::<syn::Token!(|)>()?;
let dto = match input.parse::<syn::Ident>() {
Ok(i) => Some(i),
Err(e) => match input.parse::<syn::Token!(_)>() {
Ok(_) => None,
Err(_) => {
match input.parse::<syn::Token!(&)>() {
Ok(_) => {
is_dto_ref = true;
match input.parse::<syn::Token!(mut)>() {
Ok(_) => {
is_dto_ref = false;
is_dto_ref_mut = true;
Some(input.parse::<syn::Ident>()?)
},
Err(_) => Some(input.parse::<syn::Ident>()?),
}
},
Err(_) => return Err(e),
}
},
},
};
//// parse cot
let mut is_cot_ref = false;
let mut is_cot_ref_mut = false;
input.parse::<syn::Token!(,)>()?;
//////parse ref mut
let cot: syn::Ident = match input.parse::<syn::Token!(&)>() {
Ok(_) => {
is_cot_ref = true;
match input.parse::<syn::Token!(mut)>() {
Ok(_) => {
is_cot_ref = false;
is_cot_ref_mut = true;
input.parse()?
},
Err(_) => input.parse()?,
}
},
Err(_) => {
input.parse()?
},
};
// parse sql_name
let mut sql_name = None;
match input.parse::<syn::Token!(|)>() {
Ok(_) => (),
Err(_) => {
input.parse::<syn::Token!(,)>()?;
sql_name = match input.parse::<syn::LitStr>() {
Ok(s) => Some(s.value()),
Err(_) => None,
};
input.parse::<syn::Token!(|)>()?;
},
}
// parses token(->) first ,and then parses the tuple of return type and dialect
let dialect: syn::Ident;
let ret_type:Option<syn::Path>;
match input.parse::<syn::Token!(->)>() {
Ok(_) => {
// try to parse return type path: ret_type, or ( ... )
match input.parse::<syn::Path>() {
Ok(p) => {
ret_type = Some(p);
dialect = get_default_dialect(&input.span());
},
Err(_) => match parse_return_tuple(input) {
// try to parse return tuple : ( ret_type, dialect ) or ( ret_type, _ )
Ok(tp) => {
match tp.parse::<syn::Path>() {
Ok(p) => {
ret_type = Some(p);
tp.parse::<syn::Token!(,)>()?;
if let Err(_) = tp.parse::<syn::Token!(_)>() {
match tp.parse::<syn::Ident>() {
Ok(i) => dialect = i,
Err(_) => return Err(syn::Error::new(proc_macro2::Span::call_site(), "Need specify the dialect")),
}
} else {
dialect = get_default_dialect(&input.span());
}
},
// try to parse ( _, dialect )
Err(_) => match tp.parse::<syn::Token!(_)>() {
Ok(_) => {
ret_type = None;
tp.parse::<syn::Token!(,)>()?;
if let Err(_) = tp.parse::<syn::Token!(_)>() {
match tp.parse::<syn::Ident>() {
Ok(i) => dialect = i,
Err(_) => return Err(syn::Error::new(proc_macro2::Span::call_site(), "Need specify the dialect")),
}
} else {
dialect = get_default_dialect(&input.span());
}
},
Err(_) => return Err(syn::Error::new(proc_macro2::Span::call_site(), "Need specify the return type")),
},
}
},
Err(_) => return Err(syn::Error::new(proc_macro2::Span::call_site(), "Need specify the return type and dialect")),
},
}
},
Err(_) => {
ret_type = None;
dialect = get_default_dialect(&input.span());
},
};
// parse closure sql body
let body = parse_body(input)?;
let body: Vec<String> = body.split('\n').into_iter().map(|f| f.trim().to_owned()).collect();
let body = body.join(" ").to_owned();
let sc = SqlClosure { dto, is_dto_ref, is_dto_ref_mut, cot, is_cot_ref, is_cot_ref_mut, sql_name, ret_type, dialect, body };
// eprintln!("{:#?}", sc);
Ok(sc)
}
}
fn parse_body(input: &syn::parse::ParseBuffer) -> Result<String, syn::Error> {
let body_buf;
syn::braced!(body_buf in input);
let ts = body_buf.cursor().token_stream().into_iter();
let mut sql = String::new();
for it in ts {
match it {
proc_macro2::TokenTree::Group(_) => {
return Err(syn::Error::new(input.span(), "error not support group in sql".to_owned()));
},
proc_macro2::TokenTree::Ident(_) => {
let v: syn::Ident = body_buf.parse()?;
let sql_fragment = get_sql_fragment(&v.to_string());
if let Some(s) = sql_fragment {
sql.push_str(&s);
} else {
return Err(syn::Error::new(input.span(), "error not found sql identity".to_owned()));
}
},
proc_macro2::TokenTree::Punct(v) => {
if v.to_string() == "+" {
body_buf.parse::<Token!(+)>()?;
} else {
return Err(syn::Error::new(input.span(), "error only support '+' expr".to_owned()));
}
},
proc_macro2::TokenTree::Literal(_) => {
let rst: syn::LitStr = body_buf.parse()?;
sql.push_str(&rst.value());
},
};
}
Ok(sql)
}
pub(crate) fn gen_path(s: &str) -> syn::Path {
let seg = syn::PathSegment {
ident: syn::Ident::new(s, proc_macro2::Span::call_site()),
arguments: syn::PathArguments::None,
};
let mut punct: Punctuated<syn::PathSegment, syn::Token![::]> = Punctuated::new();
punct.push_value(seg);
let path = syn::Path{ leading_colon: None, segments: punct };
path
}
fn parse_return_tuple(input: syn::parse::ParseStream) -> syn::Result<syn::parse::ParseBuffer> {
let tuple_buf;
syn::parenthesized!(tuple_buf in input);
Ok(tuple_buf)
}
fn get_default_dialect(span: &proc_macro2::Span) -> syn::Ident {
match get_dysql_config().dialect {
dysql::SqlDialect::postgres => syn::Ident::new(&dysql::SqlDialect::postgres.to_string(), span.clone()),
dysql::SqlDialect::mysql => syn::Ident::new(&dysql::SqlDialect::mysql.to_string(), span.clone()),
dysql::SqlDialect::sqlite => syn::Ident::new(&dysql::SqlDialect::sqlite.to_string(), span.clone()),
}
}
///
/// fetch all datas that filtered by dto
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// let mut conn = connect_db().await;
///
/// let dto = UserDto {id: None, name: None, age: 13};
/// let rst = fetch_all!(|&dto, &conn| -> User {
/// r#"select * from test_user
/// where 1 = 1
/// {{#name}}and name = :name{{/name}}
/// {{#age}}and age > :age{{/age}}
/// order by id"#
/// }).unwrap();
///
/// assert_eq!(
/// vec![
/// User { id: 2, name: Some("zhanglan".to_owned()), age: Some(21) },
/// User { id: 3, name: Some("zhangsan".to_owned()), age: Some(35) },
/// ],
/// rst
/// );
/// ```
#[proc_macro]
pub fn fetch_all(input: TokenStream) -> TokenStream {
let st = syn::parse_macro_input!(input as SqlClosure);
if st.ret_type.is_none() { panic!("ret_type can't be null.") }
match expand(&st, QueryType::FetchAll) {
Ok(ret) => ret.into(),
Err(e) => e.into_compile_error().into(),
}
}
///
/// fetch one data that filtered by dto
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// let mut conn = connect_db().await;
///
/// let dto = UserDto {id: 2, name: None, age: None};
/// let rst = fetch_one!(|&dto, &conn| -> User {
/// r#"select * from test_user
/// where id = :id
/// order by id"#
/// }).unwrap();
///
/// assert_eq!(User { id: 2, name: Some("zhanglan".to_owned()), age: Some(21) }, rst);
/// ```
#[proc_macro]
pub fn fetch_one(input: TokenStream) -> TokenStream {
let st = syn::parse_macro_input!(input as SqlClosure);
if st.ret_type.is_none() { panic!("ret_type can't be null.") }
match expand(&st, QueryType::FetchOne) {
Ok(ret) => ret.into(),
Err(e) => e.into_compile_error().into(),
}
}
///
/// Fetch a scalar value from query
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// let mut conn = connect_db().await;
///
/// let rst = fetch_scalar!(|_, &conn| -> i64 {
/// r#"select count (*) from test_user"#
/// }).unwrap();
/// assert_eq!(3, rst);
/// ```
#[proc_macro]
pub fn fetch_scalar(input: TokenStream) -> TokenStream {
let st = syn::parse_macro_input!(input as SqlClosure);
if st.ret_type.is_none() { panic!("ret_type can't be null.") }
match expand(&st, QueryType::FetchScalar) {
Ok(ret) => ret.into(),
Err(e) => e.into_compile_error().into(),
}
}
///
/// Execute query
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// let mut tran = get_transaction().await.unwrap();
///
/// let dto = UserDto::new(Some(2), None, None);
/// let rst = execute!(|&dto, &mut tran| {
/// r#"delete from test_user where id = :id"#
/// }).unwrap();
/// assert_eq!(1, rst);
///
/// tran.rollback().await?;
/// ```
#[proc_macro]
pub fn execute(input: TokenStream) -> TokenStream {
let st = syn::parse_macro_input!(input as SqlClosure);
match expand(&st, QueryType::Execute) {
Ok(ret) => ret.into(),
Err(e) => e.into_compile_error().into(),
}
}
///
/// Insert data
/// **Note:** if you use this macro under **postgres** database, you should add "returning id" at the end of sql statement by yourself.
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// let mut tran = get_transaction().await.unwrap();
/// let dto = UserDto{ id: Some(4), name: Some("lisi".to_owned()), age: Some(50) };
/// let last_insert_id = insert!(|&dto, &mut tran| -> (_, mysql) {
/// r#"insert into test_user (id, name, age) values (4, 'aa', 1)"# // works for mysql and sqlite
/// // r#"insert into test_user (id, name, age) values (4, 'aa', 1) returning id"# // works for postgres
/// }).unwrap();
/// assert_eq!(4, last_insert_id);
///
/// tran.rollback().await?;
/// ```
#[proc_macro]
pub fn insert(input: TokenStream) -> TokenStream {
let st = syn::parse_macro_input!(input as SqlClosure);
match expand(&st, QueryType::Insert) {
Ok(ret) => ret.into(),
Err(e) => e.into_compile_error().into(),
}
}
///
/// Define a global sql fragment
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// sql!("select_sql", "select * from table1 ")
///
/// let last_insert_id = fetch_all!(|&dto, &mut tran| {
/// select_sql + "where age > 10 "
/// }).unwrap();
///
/// tran.rollback().await?;
/// ```
#[proc_macro]
pub fn sql(input: TokenStream) -> TokenStream {
let st = parse_macro_input!(input as SqlFragment);
let cache = STATIC_SQL_FRAGMENT_MAP.get_or_init(|| {
RwLock::new(HashMap::new())
});
cache.write().unwrap().insert(st.name, st.value.to_string());
quote!().into()
}
///
/// page query
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// let conn = connect_db().await;
/// let dto = UserDto::new(None, None, Some(13));
/// let mut pg_dto = PageDto::new(3, 10, &dto);
///
/// let rst = page!(|&mut pg_dto, &conn| -> User {
/// "select * from test_user
/// where 1 = 1
/// {{#data}}
/// {{#name}}and name = :data.name{{/name}}
/// {{#age}}and age > :data.age{{/age}}
/// {{/data}}
/// order by id"
/// }).unwrap();
///
/// assert_eq!(7, rst.total);
/// /// ```
#[proc_macro]
pub fn page(input: TokenStream) -> TokenStream {
let st = syn::parse_macro_input!(input as SqlClosure);
if st.ret_type.is_none() { panic!("ret_type can't be null.") }
match expand(&st, QueryType::Page) {
Ok(ret) => ret.into(),
Err(e) => e.into_compile_error().into(),
}
}