mirror of
https://github.com/eliasrenman/r-database.git
synced 2026-03-16 20:46:08 +01:00
chore: wip relations
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
use std::{borrow::BorrowMut, cell::RefCell, ops::DerefMut};
|
|
||||||
|
|
||||||
use super::{row::Row, table::Table, Database};
|
use super::{row::Row, table::Table, Database};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
pub struct Relation {
|
pub struct Relation {
|
||||||
pub table_name: String,
|
pub table_name: String,
|
||||||
variation: String,
|
variation: String,
|
||||||
@@ -46,18 +44,24 @@ impl OneToOne {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, table_name: String, database: &mut Database) -> Result<Row, String> {
|
pub fn get(&self, table_name: String, database: &mut Database) -> Result<Row, String> {
|
||||||
let relation: Relation = {
|
let relation_result: Result<&Relation, String> = {
|
||||||
// Get table
|
// Get table
|
||||||
let table: &mut Table = database.get_table(table_name).unwrap();
|
let table: &mut Table = match database.get_table(table_name) {
|
||||||
|
Ok(relation) => relation,
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
};
|
||||||
|
|
||||||
// Fetch the Relation
|
// Fetch the Relation
|
||||||
table.get_relation(&self.relation_name).unwrap().clone()
|
table.get_relation(&self.relation_name)
|
||||||
};
|
};
|
||||||
// Fetch row from foreign table
|
|
||||||
let foreign_row = relation.get_foreign_row(database, self.foreign_id)?;
|
|
||||||
// Now you can continue using 'relation' or anything else
|
|
||||||
|
|
||||||
Ok(foreign_row)
|
let relation: Relation = match relation_result {
|
||||||
|
Ok(relation) => relation.clone(),
|
||||||
|
Err(error) => return Err(error),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fetch row from foreign table
|
||||||
|
relation.get_foreign_row(database, self.foreign_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_value(value: Value) -> OneToOne {
|
pub fn from_value(value: Value) -> OneToOne {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ mod relation_test {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_fail_no_relation_found() {
|
fn should_succed_relationship_found() {
|
||||||
let cat_relations =
|
let cat_relations =
|
||||||
hashmap!["cat_food" => Relation::new("Food".to_string(), "one_to_one".to_string())];
|
hashmap!["cat_food" => Relation::new("Food".to_string(), "one_to_one".to_string())];
|
||||||
let cat_table: Table = Table::new("Cats", "id", None, Some(cat_relations));
|
let cat_table: Table = Table::new("Cats", "id", None, Some(cat_relations));
|
||||||
@@ -42,11 +42,85 @@ mod relation_test {
|
|||||||
foreign_row_unwrapped.get("id").unwrap().as_u64().unwrap(),
|
foreign_row_unwrapped.get("id").unwrap().as_u64().unwrap(),
|
||||||
123u64
|
123u64
|
||||||
);
|
);
|
||||||
|
print!(
|
||||||
|
"Printing foreign row {}\n",
|
||||||
|
serde_json::to_string(&foreign_row_unwrapped).unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_fail_no_relation_found() {
|
||||||
|
let cat_table: Table = Table::new("Cats", "id", None, None);
|
||||||
|
|
||||||
|
let food_table: Table = Table::new("Food", "id", None, None);
|
||||||
|
|
||||||
|
let mut database: Database = Database::new(vec![cat_table, food_table]);
|
||||||
|
|
||||||
|
_ = database
|
||||||
|
.get_table("Food".to_string())
|
||||||
|
.unwrap()
|
||||||
|
.insert_row(row!["id" => 123, "name" => "Dry Feed"]);
|
||||||
|
let _ = database.get_table("Cats".to_string()).unwrap().insert_row(row!["id" => 1, "name" => "Ozzy", "breed" => "mixed", "food" => OneToOne::new(123u64, "cat_food".to_string())]);
|
||||||
|
|
||||||
|
let cat_table = database.get_table("Cats".to_string()).unwrap();
|
||||||
|
let cat_1 = cat_table.find_by_pk(1u64).unwrap();
|
||||||
|
|
||||||
|
let cat_1_food = cat_1.get("food").unwrap();
|
||||||
|
|
||||||
|
let relation = OneToOne::from_value(cat_1_food.to_owned());
|
||||||
|
assert_eq!(relation.get_id(), 123u64);
|
||||||
|
|
||||||
|
let foreign_row = relation.get("Food".to_string(), database.borrow_mut());
|
||||||
|
|
||||||
|
assert_eq!(foreign_row.is_err(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_fail_no_table_found() {
|
||||||
|
let cat_table: Table = Table::new("Cats", "id", None, None);
|
||||||
|
|
||||||
|
let mut database: Database = Database::new(vec![cat_table]);
|
||||||
|
|
||||||
|
let _ = database.get_table("Cats".to_string()).unwrap().insert_row(row!["id" => 1, "name" => "Ozzy", "breed" => "mixed", "food" => OneToOne::new(123u64, "cat_food".to_string())]);
|
||||||
|
|
||||||
|
let cat_table = database.get_table("Cats".to_string()).unwrap();
|
||||||
|
let cat_1 = cat_table.find_by_pk(1u64).unwrap();
|
||||||
|
|
||||||
|
let cat_1_food = cat_1.get("food").unwrap();
|
||||||
|
|
||||||
|
let relation = OneToOne::from_value(cat_1_food.to_owned());
|
||||||
|
assert_eq!(relation.get_id(), 123u64);
|
||||||
|
|
||||||
|
let foreign_row = relation.get("Food".to_string(), database.borrow_mut());
|
||||||
|
print!(
|
||||||
|
"Printing foreign row error {}\n",
|
||||||
|
&foreign_row.unwrap_err().as_str()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn should_fail_no_table_found() {}
|
fn should_fail_no_row_found() {
|
||||||
#[test]
|
let cat_table: Table = Table::new("Cats", "id", None, None);
|
||||||
fn should_fail_no_row_found() {}
|
|
||||||
|
let food_table: Table = Table::new("Food", "id", None, None);
|
||||||
|
|
||||||
|
let mut database: Database = Database::new(vec![cat_table, food_table]);
|
||||||
|
|
||||||
|
let _ = database.get_table("Cats".to_string()).unwrap().insert_row(row!["id" => 1, "name" => "Ozzy", "breed" => "mixed", "food" => OneToOne::new(123u64, "cat_food".to_string())]);
|
||||||
|
|
||||||
|
let cat_table = database.get_table("Cats".to_string()).unwrap();
|
||||||
|
let cat_1 = cat_table.find_by_pk(1u64).unwrap();
|
||||||
|
|
||||||
|
let cat_1_food = cat_1.get("food").unwrap();
|
||||||
|
|
||||||
|
let relation = OneToOne::from_value(cat_1_food.to_owned());
|
||||||
|
assert_eq!(relation.get_id(), 123u64);
|
||||||
|
|
||||||
|
let foreign_row = relation.get("Cats".to_string(), database.borrow_mut());
|
||||||
|
print!(
|
||||||
|
"Printing foreign row error {}\n",
|
||||||
|
&foreign_row.unwrap_err().as_str()
|
||||||
|
);
|
||||||
|
}
|
||||||
// Fail without table found, invalid relation
|
// Fail without table found, invalid relation
|
||||||
// Fail row and table found but not row
|
// Fail row and table found but not row
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user