Skip to content

Commit 88e46db

Browse files
authored
Merge pull request #13 implemented main controller functionality
implemented main controller functionality
2 parents f8d65e5 + ff048fa commit 88e46db

File tree

5 files changed

+103
-40
lines changed

5 files changed

+103
-40
lines changed

src/databases/database.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,32 @@
1818

1919
use super::tables::table::Table;
2020

21+
#[derive(Debug)]
2122
pub struct Database {
22-
name: String,
23-
tables: Vec<Table>,
23+
pub name: String,
24+
pub tables: Vec<Table>,
2425
}
2526

2627
impl Database {
27-
pub fn create_database(name: String, tables: Vec<Table>) -> Self {
28+
pub fn create_database(name: String) -> Self {
2829
Self {
2930
name,
30-
tables,
31+
tables: Vec::new(),
3132
}
3233
}
3334

34-
pub fn create_database_without_tables(name: String) -> Self {
35-
Self {
36-
name,
37-
tables: Vec::new(),
38-
}
35+
pub fn add_table(&mut self, name: String) {
36+
self.tables.push(Table::create_table(name));
3937
}
4038

4139
pub fn delete_table(&mut self, name: String) {
42-
todo!()
40+
let mut ind = 0;
41+
while ind != self.tables.len() {
42+
if self.tables[ind].name == name {
43+
self.tables.remove(ind);
44+
} else {
45+
ind += 1;
46+
}
47+
}
4348
}
4449
}

src/databases/tables/rows/row.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,32 @@
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19+
#[derive(Debug)]
1920
pub struct Row {
20-
primary_key: u32,
21-
columns: Vec<String>,
21+
pub primary_key: u32,
22+
pub columns: Vec<String>,
2223
}
2324

2425
impl Row {
25-
pub fn create_row(primary_key: u32, columns: Vec<String>) -> Self {
26-
Self {
27-
primary_key,
28-
columns,
29-
}
30-
}
31-
32-
pub fn create_row_without_columns(primary_key: u32) -> Self {
26+
pub fn create_row(primary_key: u32) -> Self {
3327
Self {
3428
primary_key,
3529
columns: Vec::new(),
3630
}
3731
}
3832

39-
pub fn create_column(&mut self, primary_key: u32, name: String) {
40-
todo!()
33+
pub fn create_column(&mut self, name: String) {
34+
self.columns.push(name);
4135
}
4236

4337
pub fn delete_column(&mut self, name: String) {
44-
todo!()
38+
let mut ind = 0;
39+
while ind != self.columns.len() {
40+
if self.columns[ind] == name {
41+
self.columns.remove(ind);
42+
} else {
43+
ind += 1;
44+
}
45+
}
4546
}
4647
}

src/databases/tables/table.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,34 @@
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19-
use std::collections::HashMap;
2019
use crate::databases::tables::rows::row::Row;
2120

21+
#[derive(Debug)]
2222
pub struct Table {
23-
name: String,
24-
rows: HashMap<u32, Row>,
23+
pub name: String,
24+
pub rows: Vec<Row>,
2525
}
2626

2727
impl Table {
28-
pub fn create_table(name: String, rows: HashMap<u32, Row>) -> Self {
28+
pub fn create_table(name: String) -> Self {
2929
Self {
3030
name,
31-
rows,
31+
rows: Vec::new(),
3232
}
3333
}
3434

35-
pub fn create_table_without_rows(name: String) -> Self {
36-
Self {
37-
name,
38-
rows: HashMap::new(),
39-
}
35+
pub fn add_row(&mut self, primary_key: u32) {
36+
self.rows.push(Row::create_row(primary_key));
4037
}
4138

4239
pub fn delete_row(&mut self, primary_key: u32) {
43-
todo!()
40+
let mut ind = 0;
41+
while ind != self.rows.len() {
42+
if self.rows[ind].primary_key == primary_key {
43+
self.rows.remove(ind);
44+
} else {
45+
ind += 1;
46+
}
47+
}
4448
}
4549
}

src/lib.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,30 @@
1919
pub(crate) mod databases;
2020
use crate::databases::database::Database;
2121

22-
struct RNSQL {
23-
databases: Vec<Database>,
22+
#[derive(Debug)]
23+
pub struct RNSQL {
24+
pub databases: Vec<Database>,
2425
}
2526

2627
impl RNSQL {
27-
pub fn new() {
28-
todo!()
28+
pub fn new() -> Self {
29+
Self {
30+
databases: Vec::new(),
31+
}
32+
}
33+
34+
pub fn add_database(&mut self, name: String) {
35+
self.databases.push(Database::create_database(name));
36+
}
37+
38+
pub fn delete_database(&mut self, name: String) {
39+
let mut ind = 0;
40+
while ind != self.databases.len() {
41+
if self.databases[ind].name == name {
42+
self.databases.remove(ind);
43+
} else {
44+
ind += 1;
45+
}
46+
}
2947
}
3048
}

src/main.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,42 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
18-
18+
use rapid_naive_sql::RNSQL;
19+
1920
fn main() {
20-
println!("database");
21+
let mut project = RNSQL::new();
22+
23+
println!("{:#?}\n\n", project);
24+
25+
project.add_database(String::from("db1"));
26+
27+
println!("{:#?}\n\n", project);
28+
29+
project.databases[0].add_table(String::from("tb1"));
30+
31+
println!("{:#?}\n\n", project);
32+
33+
project.databases[0].tables[0].add_row(1);
34+
35+
println!("{:#?}\n\n", project);
36+
37+
project.databases[0].tables[0].rows[0].create_column(String::from("name"));
38+
39+
println!("{:#?}\n\n", project);
40+
41+
project.databases[0].tables[0].rows[0].delete_column(String::from("name"));
42+
43+
println!("{:#?}\n\n", project);
44+
45+
project.databases[0].tables[0].delete_row(1);
46+
47+
println!("{:#?}\n\n", project);
48+
49+
project.databases[0].delete_table(String::from("tb1"));
50+
51+
println!("{:#?}\n\n", project);
52+
53+
project.delete_database(String::from("db1"));
54+
55+
println!("{:#?}", project);
2156
}

0 commit comments

Comments
 (0)