Skip to content

Commit a40e8da

Browse files
authored
Merge pull request #15 v0.2.0
v0.2.0
2 parents 61fbb93 + dae43ad commit a40e8da

File tree

9 files changed

+93
-84
lines changed

9 files changed

+93
-84
lines changed

src/data/databases/database.rs

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

19+
use std::collections::HashMap;
20+
1921
use super::tables::table::Table;
2022

2123
#[derive(Debug)]
22-
pub struct Database {
23-
pub name: String,
24-
pub tables: Vec<Table>,
24+
pub struct Database<Value> {
25+
pub tables: HashMap<String, Table<Value>>,
2526
}
2627

27-
impl Database {
28-
pub fn create_database(name: String) -> Self {
28+
impl<Value> Database<Value> {
29+
pub fn create_database() -> Self {
2930
Self {
30-
name,
31-
tables: Vec::new(),
31+
tables: HashMap::new(),
3232
}
3333
}
3434

35-
pub fn add_table(&mut self, name: String) {
36-
self.tables.push(Table::create_table(name));
35+
pub fn add_table(&mut self, name: &str, table: Table<Value>) {
36+
self.tables.insert(name.to_string(), table);
3737
}
3838

39-
pub fn delete_table(&mut self, name: String) {
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-
}
39+
pub fn delete_table(&mut self, name: &str) {
40+
self.tables.remove(name);
4841
}
4942
}

src/data/databases/mod.rs

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

19-
pub(crate) mod database;
20-
pub(crate) mod tables;
19+
pub mod database;
20+
pub mod tables;

src/data/databases/tables/mod.rs

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

19-
pub(crate) mod table;
20-
pub(crate) mod rows;
19+
pub mod table;
20+
pub mod rows;

src/data/databases/tables/rows/mod.rs

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

19-
pub(crate) mod row;
19+
pub mod row;

src/data/databases/tables/rows/row.rs

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

19+
use std::collections::HashMap;
20+
1921
#[derive(Debug)]
20-
pub struct Row {
21-
pub primary_key: u32,
22-
pub columns: Vec<String>,
22+
pub struct Row<Value> {
23+
pub columns: HashMap<String, Value>,
2324
}
2425

25-
impl Row {
26-
pub fn create_row(primary_key: u32) -> Self {
26+
impl<Value> Row<Value> {
27+
pub fn create_row() -> Self {
2728
Self {
28-
primary_key,
29-
columns: Vec::new(),
29+
columns: HashMap::new(),
3030
}
3131
}
3232

33-
pub fn create_column(&mut self, name: String) {
34-
self.columns.push(name);
33+
pub fn create_column(&mut self, name: &str, value: Value) {
34+
self.columns.insert(name.to_string(), value);
3535
}
3636

37-
pub fn delete_column(&mut self, name: String) {
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-
}
37+
pub fn delete_column(&mut self, name: &str) {
38+
self.columns.remove(name);
4639
}
4740
}

src/data/databases/tables/table.rs

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

19+
use std::collections::HashMap;
1920
use crate::data::databases::tables::rows::row::Row;
2021

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

27-
impl Table {
28-
pub fn create_table(name: String) -> Self {
27+
impl<Value> Table<Value> {
28+
pub fn create_table() -> Self {
2929
Self {
30-
name,
31-
rows: Vec::new(),
30+
rows: HashMap::new(),
3231
}
3332
}
3433

35-
pub fn add_row(&mut self, primary_key: u32) {
36-
self.rows.push(Row::create_row(primary_key));
34+
pub fn add_row(&mut self, primary_key: u32, row: Row<Value>) {
35+
self.rows.insert(primary_key, row);
3736
}
3837

3938
pub fn delete_row(&mut self, primary_key: u32) {
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-
}
39+
self.rows.remove(&primary_key);
4840
}
4941
}

src/data/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub(crate) mod databases;
1+
pub mod databases;

src/lib.rs

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

19-
pub(crate) mod data;
19+
pub mod data;
20+
use std::collections::HashMap;
21+
2022
use crate::data::databases::database::Database;
2123

2224
#[derive(Debug)]
23-
pub struct RNSQL {
24-
pub databases: Vec<Database>,
25+
pub struct RNSQL<Value> {
26+
pub databases: HashMap<String, Database<Value>>,
2527
}
2628

27-
impl RNSQL {
29+
impl<Value> RNSQL<Value> {
2830
pub fn new() -> Self {
2931
Self {
30-
databases: Vec::new(),
32+
databases: HashMap::new(),
3133
}
3234
}
3335

34-
pub fn add_database(&mut self, name: String) {
35-
self.databases.push(Database::create_database(name));
36+
pub fn add_database(&mut self, name: &str, database: Database<Value>) {
37+
self.databases.insert(name.to_string(), database);
3638
}
3739

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-
}
40+
pub fn delete_database(&mut self, name: &str) {
41+
self.databases.remove(name);
4742
}
4843
}

src/main.rs

+45-9
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,78 @@
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+
1819
use rapid_naive_sql::RNSQL;
19-
20+
use rapid_naive_sql::data::databases::database::Database;
21+
use rapid_naive_sql::data::databases::tables::table::Table;
22+
use rapid_naive_sql::data::databases::tables::rows::row::Row;
23+
2024
fn main() {
25+
const DB_NAME: &str = "db1";
26+
const TB_NAME: &str = "tb1";
27+
const RW_NAME: u32 = 1;
28+
const CL_NAME: &str = "cl1";
29+
2130
let mut project = RNSQL::new();
2231

2332
println!("{:#?}\n\n", project);
2433

25-
project.add_database(String::from("db1"));
34+
let db1 = Database::create_database();
35+
project.add_database(DB_NAME, db1);
2636

2737
println!("{:#?}\n\n", project);
2838

29-
project.databases[0].add_table(String::from("tb1"));
39+
if let Some(database) = project.databases.get_mut(DB_NAME) {
40+
let tb1 = Table::create_table();
41+
database.add_table(TB_NAME, tb1);
42+
}
3043

3144
println!("{:#?}\n\n", project);
3245

33-
project.databases[0].tables[0].add_row(1);
46+
if let Some(database) = project.databases.get_mut(DB_NAME) {
47+
if let Some(table) = database.tables.get_mut(TB_NAME) {
48+
let rw1 = Row::create_row();
49+
table.add_row(RW_NAME, rw1);
50+
}
51+
}
3452

3553
println!("{:#?}\n\n", project);
3654

37-
project.databases[0].tables[0].rows[0].create_column(String::from("name"));
55+
if let Some(database) = project.databases.get_mut(DB_NAME) {
56+
if let Some(table) = database.tables.get_mut(TB_NAME) {
57+
if let Some(row) = table.rows.get_mut(&RW_NAME) {
58+
row.create_column(CL_NAME, String::from("hello"));
59+
}
60+
}
61+
}
3862

3963
println!("{:#?}\n\n", project);
4064

41-
project.databases[0].tables[0].rows[0].delete_column(String::from("name"));
65+
if let Some(database) = project.databases.get_mut(DB_NAME) {
66+
if let Some(table) = database.tables.get_mut(TB_NAME) {
67+
if let Some(row) = table.rows.get_mut(&RW_NAME) {
68+
row.delete_column(CL_NAME);
69+
}
70+
}
71+
}
4272

4373
println!("{:#?}\n\n", project);
4474

45-
project.databases[0].tables[0].delete_row(1);
75+
if let Some(database) = project.databases.get_mut(DB_NAME) {
76+
if let Some(table) = database.tables.get_mut(TB_NAME) {
77+
table.delete_row(RW_NAME);
78+
}
79+
}
4680

4781
println!("{:#?}\n\n", project);
4882

49-
project.databases[0].delete_table(String::from("tb1"));
83+
if let Some(database) = project.databases.get_mut(DB_NAME) {
84+
database.delete_table(TB_NAME);
85+
}
5086

5187
println!("{:#?}\n\n", project);
5288

53-
project.delete_database(String::from("db1"));
89+
project.delete_database(DB_NAME);
5490

5591
println!("{:#?}", project);
5692
}

0 commit comments

Comments
 (0)