Skip to content
Merged
20 changes: 20 additions & 0 deletions crates/dao/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ impl Dao {
}
}

pub fn get_opt<'a, T>(&'a self, s: &str) -> Result<Option<T>, DaoError>
where
T: FromValue,
{
let value: Option<&'a Value> = self.0.get(s);
match value {
Some(v) => {
match v {
Value::Nil => Ok(None),
_ => {
Ok(Some(
FromValue::from_value(v).map_err(DaoError::ConvertError)?,
))
}
}
}
None => Ok(None),
}
}

pub fn get_value(&self, s: &str) -> Option<&Value> { self.0.get(s) }

pub fn remove(&mut self, s: &str) -> Option<Value> { self.0.remove(s) }
Expand Down
2 changes: 1 addition & 1 deletion examples/insert_to_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() {

let db_url = "sqlite:///tmp/sqlite.db";
let mut pool = Pool::new();
let em = pool.em(db_url).unwrap();
let mut em = pool.em(db_url).unwrap();
let ret = em.db().execute_sql_with_return(create_sql, &[]);
println!("ret: {:?}", ret);
assert!(ret.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion examples/insert_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {

let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
let mut pool = Pool::new();
let em = pool.em(db_url).unwrap();
let mut em = pool.em(db_url).unwrap();
let tom_cruise = for_insert::Actor {
first_name: "TOM".into(),
last_name: "CRUISE".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion examples/insert_usage_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {
let db_url = "mysql://root:r00tpwdh3r3@localhost/sakila";
let mut pool = Pool::new();
pool.ensure(db_url);
let mut em = pool.em_mut(db_url).expect("Can not connect");
let mut em = pool.em(db_url).expect("Can not connect");
let tom_cruise = for_insert::Actor {
first_name: "TOM".into(),
last_name: "CRUISE".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion examples/select_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Actor {
fn main() {
let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
let mut pool = Pool::new();
let em = pool.em(db_url).unwrap();
let mut em = pool.em(db_url).unwrap();
let sql = "SELECT * FROM actor LIMIT 10";
let actors: Result<Vec<Actor>, DbError> = em.execute_sql_with_return(sql, &[]);
println!("Actor: {:#?}", actors);
Expand Down
3 changes: 1 addition & 2 deletions examples/select_usage_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ struct Actor {
fn main() {
let db_url = "mysql://root:r00tpwdh3r3@localhost/sakila";
let mut pool = Pool::new();
pool.ensure(db_url);
let mut em = pool
.em_mut(db_url)
.em(db_url)
.expect("Should be able to get a connection here..");
let sql = "SELECT * FROM actor LIMIT 10";
let actors: Result<Vec<Actor>, DbError> = em.execute_sql_with_return(sql, &[]);
Expand Down
27 changes: 27 additions & 0 deletions examples/select_usage_sqlite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use rustorm::{
DbError,
FromDao,
Pool,
ToColumnNames,
ToTableName,
};

#[derive(Debug, FromDao, ToColumnNames, ToTableName)]
struct Actor {
actor_id: i32,
first_name: String,
}

fn main() {
let db_url = "sqlite://sakila.db";
let mut pool = Pool::new();
let mut em = pool.em(db_url).unwrap();
let sql = "SELECT * FROM actor LIMIT 10";
let actors: Result<Vec<Actor>, 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);
}
}
2 changes: 1 addition & 1 deletion examples/simple_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustorm::{
fn main() {
let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
let mut pool = Pool::new();
let dm = pool.dm(db_url).unwrap();
let mut dm = pool.dm(db_url).unwrap();
let sql = "SELECT * FROM actor LIMIT 10";
let actors: Result<Rows, DbError> = dm.execute_sql_with_return(sql, &[]);
println!("Actor: {:#?}", actors);
Expand Down
2 changes: 1 addition & 1 deletion examples/update_usage_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
let mut pool = Pool::new();
pool.ensure(db_url);
let mut em = pool
.em_mut(db_url)
.em(db_url)
.expect("Should be able to get a connection here..");
let sql = "UPDATE actor SET last_name = ? WHERE first_name = ?".to_string();
let rows: Result<Rows, DbError> = em
Expand Down
14 changes: 9 additions & 5 deletions src/dao_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
platform::DBPlatform,
DBPlatform,
Dao,
DataError,
DbError,
Expand All @@ -10,13 +10,17 @@ use crate::{
pub struct DaoManager(pub DBPlatform);

impl DaoManager {
pub fn execute_sql_with_return(&self, sql: &str, params: &[&Value]) -> Result<Rows, DbError> {
pub fn execute_sql_with_return(
&mut self,
sql: &str,
params: &[&Value],
) -> Result<Rows, DbError> {
let rows = self.0.execute_sql_with_return(sql, params)?;
Ok(rows)
}

pub fn execute_sql_with_records_return(
&self,
&mut self,
sql: &str,
params: &[&Value],
) -> Result<Vec<Dao>, DbError> {
Expand All @@ -26,7 +30,7 @@ impl DaoManager {
}

pub fn execute_sql_with_one_return(
&self,
&mut self,
sql: &str,
params: &[&Value],
) -> Result<Dao, DbError> {
Expand All @@ -44,7 +48,7 @@ impl DaoManager {
}

pub fn execute_sql_with_maybe_one_return(
&self,
&mut self,
sql: &str,
params: &[&Value],
) -> Result<Option<Dao>, DbError> {
Expand Down
25 changes: 13 additions & 12 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@ use crate::{
Role,
User,
},
EntityManager,
DbError,
Rows,
Table,
TableName,
Value,
};
use rustorm_codegen::FromDao;
use serde::Serialize;

use crate::DbError;
use rustorm_codegen::FromDao;

/// The current database name and its comment
#[derive(Serialize, FromDao)]
pub struct DatabaseName {
name: String,
description: Option<String>,
pub(crate) name: String,
pub(crate) description: Option<String>,
}



pub trait Database {
fn execute_sql_with_return(&self, sql: &str, param: &[&Value]) -> Result<Rows, DbError>;
fn execute_sql_with_return(&mut self, sql: &str, param: &[&Value]) -> Result<Rows, DbError>;

fn get_table(&self, em: &EntityManager, table_name: &TableName) -> Result<Table, DbError>;
fn get_table(&mut self, table_name: &TableName) -> Result<Table, DbError>;

fn get_all_tables(&self, em: &EntityManager) -> Result<Vec<Table>, DbError>;
fn get_all_tables(&mut self) -> Result<Vec<Table>, DbError>;

fn get_grouped_tables(&self, em: &EntityManager) -> Result<Vec<SchemaContent>, DbError>;
fn get_grouped_tables(&mut self) -> Result<Vec<SchemaContent>, DbError>;

fn get_users(&self, em: &EntityManager) -> Result<Vec<User>, DbError>;
fn get_users(&mut self) -> Result<Vec<User>, DbError>;

fn get_roles(&self, em: &EntityManager, username: &str) -> Result<Vec<Role>, DbError>;
fn get_roles(&mut self, username: &str) -> Result<Vec<Role>, DbError>;

fn get_database_name(&self, em: &EntityManager) -> Result<Option<DatabaseName>, DbError>;
fn get_database_name(&mut self) -> Result<Option<DatabaseName>, DbError>;
}
17 changes: 0 additions & 17 deletions src/database_mut.rs

This file was deleted.

Loading