From 41386b3a40e7eed22588fa943e000de5e85d8187 Mon Sep 17 00:00:00 2001 From: git-user-cpp Date: Sun, 17 Mar 2024 12:03:31 +0200 Subject: [PATCH 1/2] v0.1.0-remake - implementing essential methods --- src/databases/database.rs | 19 ++++++++++++++++--- src/databases/tables/rows/row.rs | 19 ++++++++++++++++++- src/databases/tables/table.rs | 13 ++++++++++++- src/lib.rs | 13 ++++++++++++- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/databases/database.rs b/src/databases/database.rs index 818e421..85bcfb4 100644 --- a/src/databases/database.rs +++ b/src/databases/database.rs @@ -18,14 +18,27 @@ use super::tables::table::Table; -struct Database { +pub struct Database { + name: String, tables: Vec, } impl Database { - pub fn new(tables: Vec
) -> Self { + pub fn create_database(name: String, tables: Vec
) -> Self { Self { - tables + name, + tables, } } + + pub fn create_database_without_tables(name: String) -> Self { + Self { + name, + tables: Vec::new(), + } + } + + pub fn delete_table(&mut self, name: String) { + todo!() + } } \ No newline at end of file diff --git a/src/databases/tables/rows/row.rs b/src/databases/tables/rows/row.rs index 667570d..41e81db 100644 --- a/src/databases/tables/rows/row.rs +++ b/src/databases/tables/rows/row.rs @@ -17,13 +17,30 @@ */ pub struct Row { + primary_key: u32, columns: Vec, } impl Row { - pub fn new(id: u32, columns: Vec) -> Self { + pub fn create_row(primary_key: u32, columns: Vec) -> Self { Self { + primary_key, columns, } } + + pub fn create_row_without_columns(primary_key: u32) -> Self { + Self { + primary_key, + columns: Vec::new(), + } + } + + pub fn create_column(&mut self, primary_key: u32, name: String) { + todo!() + } + + pub fn delete_column(&mut self, name: String) { + todo!() + } } \ No newline at end of file diff --git a/src/databases/tables/table.rs b/src/databases/tables/table.rs index 8d99f47..98638b8 100644 --- a/src/databases/tables/table.rs +++ b/src/databases/tables/table.rs @@ -25,10 +25,21 @@ pub struct Table { } impl Table { - pub fn new(name: String, rows: HashMap) -> Self { + pub fn create_table(name: String, rows: HashMap) -> Self { Self { name, rows, } } + + pub fn create_table_without_rows(name: String) -> Self { + Self { + name, + rows: HashMap::new(), + } + } + + pub fn delete_row(&mut self, primary_key: u32) { + todo!() + } } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e11417d..55f9b48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,4 +16,15 @@ * along with this program. If not, see . */ -pub(crate) mod databases; \ No newline at end of file +pub(crate) mod databases; +use crate::databases::database::Database; + +struct RNSQL { + databases: Vec, +} + +impl RNSQL { + pub fn new() { + todo!() + } +} \ No newline at end of file From ff048fae11d7a3489634b8b260c3d330971ae30e Mon Sep 17 00:00:00 2001 From: git-user-cpp Date: Sat, 23 Mar 2024 11:43:23 +0200 Subject: [PATCH 2/2] implemented main controller functionality --- src/databases/database.rs | 25 ++++++++++++-------- src/databases/tables/rows/row.rs | 27 +++++++++++----------- src/databases/tables/table.rs | 26 ++++++++++++--------- src/lib.rs | 26 +++++++++++++++++---- src/main.rs | 39 ++++++++++++++++++++++++++++++-- 5 files changed, 103 insertions(+), 40 deletions(-) diff --git a/src/databases/database.rs b/src/databases/database.rs index 85bcfb4..e29673d 100644 --- a/src/databases/database.rs +++ b/src/databases/database.rs @@ -18,27 +18,32 @@ use super::tables::table::Table; +#[derive(Debug)] pub struct Database { - name: String, - tables: Vec
, + pub name: String, + pub tables: Vec
, } impl Database { - pub fn create_database(name: String, tables: Vec
) -> Self { + pub fn create_database(name: String) -> Self { Self { name, - tables, + tables: Vec::new(), } } - pub fn create_database_without_tables(name: String) -> Self { - Self { - name, - tables: Vec::new(), - } + pub fn add_table(&mut self, name: String) { + self.tables.push(Table::create_table(name)); } pub fn delete_table(&mut self, name: String) { - todo!() + let mut ind = 0; + while ind != self.tables.len() { + if self.tables[ind].name == name { + self.tables.remove(ind); + } else { + ind += 1; + } + } } } \ No newline at end of file diff --git a/src/databases/tables/rows/row.rs b/src/databases/tables/rows/row.rs index 41e81db..4d108ac 100644 --- a/src/databases/tables/rows/row.rs +++ b/src/databases/tables/rows/row.rs @@ -16,31 +16,32 @@ * along with this program. If not, see . */ +#[derive(Debug)] pub struct Row { - primary_key: u32, - columns: Vec, + pub primary_key: u32, + pub columns: Vec, } impl Row { - pub fn create_row(primary_key: u32, columns: Vec) -> Self { - Self { - primary_key, - columns, - } - } - - pub fn create_row_without_columns(primary_key: u32) -> Self { + pub fn create_row(primary_key: u32) -> Self { Self { primary_key, columns: Vec::new(), } } - pub fn create_column(&mut self, primary_key: u32, name: String) { - todo!() + pub fn create_column(&mut self, name: String) { + self.columns.push(name); } pub fn delete_column(&mut self, name: String) { - todo!() + let mut ind = 0; + while ind != self.columns.len() { + if self.columns[ind] == name { + self.columns.remove(ind); + } else { + ind += 1; + } + } } } \ No newline at end of file diff --git a/src/databases/tables/table.rs b/src/databases/tables/table.rs index 98638b8..b880186 100644 --- a/src/databases/tables/table.rs +++ b/src/databases/tables/table.rs @@ -16,30 +16,34 @@ * along with this program. If not, see . */ -use std::collections::HashMap; use crate::databases::tables::rows::row::Row; +#[derive(Debug)] pub struct Table { - name: String, - rows: HashMap, + pub name: String, + pub rows: Vec, } impl Table { - pub fn create_table(name: String, rows: HashMap) -> Self { + pub fn create_table(name: String) -> Self { Self { name, - rows, + rows: Vec::new(), } } - pub fn create_table_without_rows(name: String) -> Self { - Self { - name, - rows: HashMap::new(), - } + pub fn add_row(&mut self, primary_key: u32) { + self.rows.push(Row::create_row(primary_key)); } pub fn delete_row(&mut self, primary_key: u32) { - todo!() + let mut ind = 0; + while ind != self.rows.len() { + if self.rows[ind].primary_key == primary_key { + self.rows.remove(ind); + } else { + ind += 1; + } + } } } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 55f9b48..8aeb6e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,12 +19,30 @@ pub(crate) mod databases; use crate::databases::database::Database; -struct RNSQL { - databases: Vec, +#[derive(Debug)] +pub struct RNSQL { + pub databases: Vec, } impl RNSQL { - pub fn new() { - todo!() + pub fn new() -> Self { + Self { + databases: Vec::new(), + } + } + + pub fn add_database(&mut self, name: String) { + self.databases.push(Database::create_database(name)); + } + + pub fn delete_database(&mut self, name: String) { + let mut ind = 0; + while ind != self.databases.len() { + if self.databases[ind].name == name { + self.databases.remove(ind); + } else { + ind += 1; + } + } } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6f3b172..76ef0b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,42 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - +use rapid_naive_sql::RNSQL; + fn main() { - println!("database"); + let mut project = RNSQL::new(); + + println!("{:#?}\n\n", project); + + project.add_database(String::from("db1")); + + println!("{:#?}\n\n", project); + + project.databases[0].add_table(String::from("tb1")); + + println!("{:#?}\n\n", project); + + project.databases[0].tables[0].add_row(1); + + println!("{:#?}\n\n", project); + + project.databases[0].tables[0].rows[0].create_column(String::from("name")); + + println!("{:#?}\n\n", project); + + project.databases[0].tables[0].rows[0].delete_column(String::from("name")); + + println!("{:#?}\n\n", project); + + project.databases[0].tables[0].delete_row(1); + + println!("{:#?}\n\n", project); + + project.databases[0].delete_table(String::from("tb1")); + + println!("{:#?}\n\n", project); + + project.delete_database(String::from("db1")); + + println!("{:#?}", project); } \ No newline at end of file