rust实现一个mysql驱动,rustorm: rustorm 是 Rust 语言的一个 ORM 框架,该框架目前只支持 PostgreSQL 数据库,还在进一步开发中...

rustorm.svg?branch=master

rustorm

Rustorm

badge.svg?label=financial+contributorsrustorm.svg

rustorm.svg?branch=master

license-MIT-blue.svg

Rustorm is an SQL-centered ORM with focus on ease of use on conversion of database types to

their appropriate rust type.

Selecting records

use rustorm::{

DbError,

FromDao,

Pool,

ToColumnNames,

ToTableName,

};

#[derive(Debug, FromDao, ToColumnNames, ToTableName)]

struct Actor {

actor_id: i32,

first_name: String,

}

#[cfg(any(feature="with-postgres", feature = "with-sqlite"))]

fn main() {

let mut pool = Pool::new();

#[cfg(feature = "with-sqlite")]

let db_url = "sqlite://sakila.db";

#[cfg(feature = "with-postgres")]

let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";

let em = pool.em(db_url).unwrap();

let sql = "SELECT * FROM actor LIMIT 10";

let actors: Result, DbError> =

em.execute_sql_with_return(sql, &[]);

println!("Actor: {:#?}", actors);

let actors = actors.unwrap();

assert_eq!(actors.len(), 10);

for actor in actors {

println!("actor: {:?}", actor);

}

}

#[cfg(feature="with-mysql")]

fn main() {

println!("see examples for mysql usage, mysql has a little difference in the api");

}

Inserting and displaying the inserted records

use chrono::{

offset::Utc,

DateTime,

NaiveDate,

};

use rustorm::{

DbError,

FromDao,

Pool,

TableName,

ToColumnNames,

ToDao,

ToTableName,

};

#[cfg(any(feature="with-postgres", feature = "with-sqlite"))]

fn main() {

mod for_insert {

use super::*;

#[derive(Debug, PartialEq, ToDao, ToColumnNames, ToTableName)]

pub struct Actor {

pub first_name: String,

pub last_name: String,

}

}

mod for_retrieve {

use super::*;

#[derive(Debug, FromDao, ToColumnNames, ToTableName)]

pub struct Actor {

pub actor_id: i32,

pub first_name: String,

pub last_name: String,

pub last_update: DateTime,

}

}

let mut pool = Pool::new();

#[cfg(feature = "with-sqlite")]

let db_url = "sqlite://sakila.db";

#[cfg(feature = "with-postgres")]

let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";

let em = pool.em(db_url).unwrap();

let tom_cruise = for_insert::Actor {

first_name: "TOM".into(),

last_name: "CRUISE".to_string(),

};

let tom_hanks = for_insert::Actor {

first_name: "TOM".into(),

last_name: "HANKS".to_string(),

};

let actors: Result, DbError> =

em.insert(&[&tom_cruise, &tom_hanks]);

println!("Actor: {:#?}", actors);

assert!(actors.is_ok());

let actors = actors.unwrap();

let today = Utc::now().date();

assert_eq!(tom_cruise.first_name, actors[0].first_name);

assert_eq!(tom_cruise.last_name, actors[0].last_name);

assert_eq!(today, actors[0].last_update.date());

assert_eq!(tom_hanks.first_name, actors[1].first_name);

assert_eq!(tom_hanks.last_name, actors[1].last_name);

assert_eq!(today, actors[1].last_update.date());

}

#[cfg(feature="with-mysql")]

fn main() {

println!("see examples for mysql usage, mysql has a little difference in the api");

}

Rustorm is wholly used by diwata

License: MIT

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

contributors.svg?width=890&button=false

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

individuals.svg?width=890

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

avatar.svg

8dec88635ab05d7781f4b27d3a36ba59.png

780e91fb46f1609576336e116c8c35b9.png

d53ec0f03f2270baa9e6317dcdfbc7cb.png

5fe1839cf4291af5db2449e47324bc97.png

0a7160f8130001fbdbe867011e724046.png

b1bae584fd07948f8b13d0ff15ece06c.png

327786d9c2f3077557b72921bab0f4cb.png

9ae138d2416497ba889359fbfad583f2.png

1eec1ae772dca36d9a5292e99feb809b.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值