Crate akita[][src]

Expand description

This create offers:

  • MySql database’s helper in pure rust;
  • A mini orm framework (Just MySQL)。

Features:

  • Other Database support, i.e. support SQLite, Oracle, MSSQL…;
  • support of original SQL;
  • support of named parameters for custom condition;

Installation

Put the desired version of the crate into the dependencies section of your Cargo.toml:

[dependencies]
akita = "*"

Annotions.

  • Table - to make Akita work with structs
  • column - to make struct field with own database.
  • name - work with column, make the table’s field name. default struct’ field name.
  • exist - ignore struct’s field with table. default true.

Support Field Types.

  • Option<T>
  • u8, u32, u64
  • i32, i64
  • usize
  • f32, f64
  • bool
  • str, String
  • NaiveDate, NaiveDateTime

Example

use akita::prelude::*;
 
 
/// Annotion Support: Table、Id、column
#[table(name="t_system_user")]
#[derive(Table, Clone)]
struct User{
    #[Id(name="id")]
    pub id: i32,
    #[column(name="username")]
    pub username: String,
    #[column]
    pub mobile: String,
    #[column]
    pub password: String,
}
 
// use r2d2 pool
let opts = Opts::from_url("mysql://root:127.0.0.1:3306/test").expect("database url is empty.");
let pool = Pool::builder().max_size(4).build(MysqlConnectionManager::new(OptsBuilder::from_opts(opts))).unwrap();
let mut conn = pool.get().unwrap();
 
/// build the wrapper.
let mut wrapper = UpdateWrapper::new()
    .like(true, "username", "ffff");
    .eq(true, "username", 12);
    .eq(true, "username", "3333");
    .in_(true, "username", vec![1,44,3]);
    .not_between(true, "username", 2, 8);
    .set(true, "username", 4);
 
let user = User{
    id: 2,
    username: "username".to_string(),
    mobile: "mobile".to_string(),
    password: "password".to_string()
};
 
// Transaction
conn.start_transaction(TxOpts::default()).map(|mut transaction| {
    match user.update( & mut wrapper, &mut ConnMut::TxMut(&mut transaction)) {
        Ok(res) => {}
        Err(err) => {
            println!("error : {:?}", err);
        }
    }
});

let mut pool = ConnMut::R2d2Polled(conn);
/// update by identify
match user.update_by_id(&mut conn) {
    Ok(res) => {}
    Err(err) => {
        println!("error : {:?}", err);
    }
}
 
/// delete by identify
match user.delete_by_id(&mut conn) {
    Ok(res) => {}
    Err(err) => {
        println!("error : {:?}", err);
    }
}
 
/// delete by condition
match user.delete:: < UpdateWrapper > ( & mut wrapper, &mut conn) {
    Ok(res) => {}
    Err(err) => {
        println!("error : {:?}", err);
    }
}
 
/// insert data
match user.insert(&mut conn) {
    Ok(res) => {}
    Err(err) => {
        println!("error : {:?}", err);
    }
}
 
/// find by identify
match user.find_by_id(&mut conn) {
    Ok(res) => {}
    Err(err) => {
        println!("error : {:?}", err);
    }
}
 
 
/// find one by condition
match user.find_one::<UpdateWrapper>(&mut wrapper, &mut conn) {
    Ok(res) => {}
    Err(err) => {
        println!("error : {:?}", err);
    }
}
 
/// find page by condition
match user.page::<UpdateWrapper>(1, 10,&mut wrapper, &mut conn) {
    Ok(res) => {}
    Err(err) => {
        println!("error : {:?}", err);
    }
}
 

API Documentation

Wrapper

 
let mut wrapper = UpdateWrapper::new();
wrapper.like(true, "column1", "ffff");
wrapper.eq(true, "column2", 12);
wrapper.eq(true, "column3", "3333");
wrapper.in_(true, "column4", vec![1,44,3]);
wrapper.not_between(true, "column5", 2, 8);
wrapper.set(true, "column1", 4);
match wrapper.get_target_sql("t_user") {
    Ok(sql) => {println!("ok:{}", sql);}
    Err(err) => {println!("err:{}", err);}
}
Update At 2021.07.13 10:21 
By Mr.Pan
 
 

Re-exports

pub use chrono;

Modules

Structs

Enums

Traits

Functions

创建连接池 database_url 连接地址 max_size 最大连接数量