mirror of
https://github.com/eliasrenman/r-database.git
synced 2026-03-16 20:46:08 +01:00
chore: formatting
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Index {
|
pub struct Index {
|
||||||
pk: u64,
|
pk: u64,
|
||||||
key: String,
|
key: String,
|
||||||
asd_sort: bool,
|
asd_sort: bool,
|
||||||
unique: bool,
|
unique: bool,
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -60,10 +60,9 @@ mod tests {
|
|||||||
let db = table.unwrap();
|
let db = table.unwrap();
|
||||||
let table = db.tables.get("Cats");
|
let table = db.tables.get("Cats");
|
||||||
if table.is_none() {
|
if table.is_none() {
|
||||||
panic!("Unable to find table");
|
panic!("Unable to find table");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let row = table.unwrap().find_by_pk(&1u64);
|
let row = table.unwrap().find_by_pk(&1u64);
|
||||||
assert_eq!(row.is_ok(), true);
|
assert_eq!(row.is_ok(), true);
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Row {
|
pub struct Row {
|
||||||
pub columns: HashMap<String, Value>
|
pub columns: HashMap<String, Value>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Row {
|
impl Row {
|
||||||
pub fn new(cols: HashMap<String, Value>) -> Row {
|
pub fn new(cols: HashMap<String, Value>) -> Row {
|
||||||
Row {columns: cols}
|
Row { columns: cols }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,50 +1,51 @@
|
|||||||
|
use super::{index::Index, row::Row};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{collections::HashMap, fs};
|
use std::{collections::HashMap, fs};
|
||||||
use super::{index::Index, row::Row};
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Table {
|
pub struct Table {
|
||||||
indexes: Vec<Index>,
|
indexes: Vec<Index>,
|
||||||
rows: HashMap<u64, Row>,
|
rows: HashMap<u64, Row>,
|
||||||
name: String,
|
name: String,
|
||||||
pk_key: String,
|
pk_key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Table {
|
impl Table {
|
||||||
|
pub fn new(name: &str, pk_key: &str) -> Table {
|
||||||
pub fn new(name: &str, pk_key: &str) -> Table {
|
Table {
|
||||||
Table { indexes: vec![], rows: HashMap::new(), name: name.to_string(), pk_key: pk_key.to_string() }
|
indexes: vec![],
|
||||||
}
|
rows: HashMap::new(),
|
||||||
|
name: name.to_string(),
|
||||||
pub fn create_index(&mut self, index: Index) {
|
pk_key: pk_key.to_string(),
|
||||||
// TODO: Add row indexes to the index itself before pushing to the table
|
}
|
||||||
self.indexes.push(index)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn find_by_pk(&self, value: &u64) -> Result<&Row, &'static str> {
|
|
||||||
let row = self.rows.get(value);
|
|
||||||
if row.is_none() {
|
|
||||||
return Err("Row not found!");
|
|
||||||
}
|
|
||||||
Ok(row.unwrap().clone())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert_row(&mut self, row: Row) {
|
|
||||||
let key_option = row.columns.get(&self.pk_key);
|
|
||||||
|
|
||||||
if key_option.is_none() {
|
|
||||||
panic!("Primary key not found on row to insert!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let key = key_option.unwrap().as_u64();
|
pub fn create_index(&mut self, index: Index) {
|
||||||
|
// TODO: Add row indexes to the index itself before pushing to the table
|
||||||
if key.is_none() {
|
self.indexes.push(index)
|
||||||
panic!("Primary key is not of type u64");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.rows.insert(key.unwrap(), row);
|
pub fn find_by_pk(&self, value: &u64) -> Result<&Row, &'static str> {
|
||||||
}
|
let row = self.rows.get(value);
|
||||||
|
if row.is_none() {
|
||||||
|
return Err("Row not found!");
|
||||||
|
}
|
||||||
|
Ok(row.unwrap().clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_row(&mut self, row: Row) {
|
||||||
|
let key_option = row.columns.get(&self.pk_key);
|
||||||
|
|
||||||
|
if key_option.is_none() {
|
||||||
|
panic!("Primary key not found on row to insert!");
|
||||||
|
}
|
||||||
|
|
||||||
|
let key = key_option.unwrap().as_u64();
|
||||||
|
|
||||||
|
if key.is_none() {
|
||||||
|
panic!("Primary key is not of type u64");
|
||||||
|
}
|
||||||
|
|
||||||
|
self.rows.insert(key.unwrap(), row);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
macro_rules! hashmapJson {
|
macro_rules! hashmapJson {
|
||||||
($( $key: expr => $val: expr ),*) => {{
|
($( $key: expr => $val: expr ),*) => {{
|
||||||
let mut map: ::std::collections::HashMap<String, ::serde_json::Value> = ::std::collections::HashMap::new();
|
let mut map: ::std::collections::HashMap<String, ::serde_json::Value> = ::std::collections::HashMap::new();
|
||||||
@@ -17,8 +16,7 @@ macro_rules! hashmap {
|
|||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
|
|
||||||
fn main() {
|
fn main() {}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use crate::database::{table::Table, row::Row};
|
use crate::database::{row::Row, table::Table};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_find_two_rows() {
|
fn should_find_two_rows() {
|
||||||
@@ -29,6 +29,4 @@ mod tests {
|
|||||||
let row = table.find_by_pk(&3u64);
|
let row = table.find_by_pk(&3u64);
|
||||||
assert_eq!(row.is_err(), true);
|
assert_eq!(row.is_err(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user