Crate dysql

source ·
Expand description

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-ext] the high performance template engine implementation of [Mustache]

§Example (Sqlx)

§main.rs

//...
 
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

Modules§

  • Utilities dealing with writing the bits of a template or data to the output and escaping special HTML characters.
  • This module contains helper traits that are used internally to manage sequences of types implementing the Content trait, allowing us to statically manage parent section lookups without doing extra work on runtime.

Macros§

  • Execute query
  • fetch all datas that filtered by dto
  • fetch one data that filtered by dto
  • Fetch a scalar value from query
  • Insert data Note: if you use this macro under postgres database, you should add “returning id” at the end of sql statement by yourself.
  • page query
  • Define a global sql fragment

Structs§

Enums§

Statics§

Traits§

  • Trait allowing the rendering to quickly access data stored in the type that implements it. You needn’t worry about implementing it, in virtually all cases the #[derive(Content)] attribute above your types should be sufficient.

Functions§

Type Aliases§

Derive Macros§