Skip to content

Commit 5a4d262

Browse files
[Rust - Task 2] Implemented DELETE clause
1 parent ac51256 commit 5a4d262

6 files changed

Lines changed: 117 additions & 17 deletions

File tree

rust-method/src/queries/delete.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use crate::Secretariat;
2+
3+
4+
use crate::queries::where_clause::*;
5+
6+
/*
7+
TODO: daca stergerea se fac din tabelele Studenti/Materii,
8+
sterge intrarile asociate din Inrolari
9+
*/
10+
11+
fn delete_from_table<T: Matchable>(
12+
entries: &mut Vec<T>,
13+
conditii: &Vec<Conditie>
14+
) -> Result<(), String>
15+
{
16+
let mut idx: usize = 0;
17+
while idx < entries.len() {
18+
match match_on_all_conditii(&entries[idx], conditii)? {
19+
true => {
20+
entries.remove(idx);
21+
}
22+
false =>
23+
idx += 1
24+
}
25+
26+
}
27+
28+
Ok(())
29+
}
30+
31+
/* Template
32+
DELETE FROM <tabel> WHERE <conditie>;
33+
DELETE FROM <tabel> WHERE <conditie1> AND <conditie2>;
34+
*/
35+
pub fn delete(s: &mut Secretariat, query: &str) -> Result<(), String> {
36+
// Gaseste "DELETE" urmat de cel putin un spatiu si "FROM"
37+
let delete_from_pos = query.find("DELETE")
38+
.ok_or_else(|| "DELETE nu a fost gasit".to_string())?;
39+
40+
// Avanseaza peste "DELETE"
41+
let mut ptr = &query[delete_from_pos + "DELETE".len()..];
42+
43+
// Trebuie sa existe cel putin un spatiu
44+
if !ptr.starts_with(char::is_whitespace) {
45+
return Err("trebuie cel putin un spatiu intre DELETE si FROM".to_string());
46+
}
47+
48+
// Sare peste toate spatiile
49+
ptr = ptr.trim_start();
50+
51+
// Verifica ca urmeaza "FROM"
52+
if !ptr.starts_with("FROM") {
53+
return Err("DELETE trebuie urmat de FROM".to_string());
54+
}
55+
56+
// Avanseaza peste "FROM"
57+
ptr = &ptr["FROM".len()..];
58+
ptr = ptr.trim_start();
59+
60+
// Gaseste WHERE daca exista
61+
let where_pos = ptr.find("WHERE");
62+
63+
// Extrage numele tabelei
64+
let nume_tabela = if let Some(pos) = where_pos {
65+
ptr[..pos].trim()
66+
} else {
67+
// pana la ';' sau sfarsitul stringului
68+
if let Some(end) = ptr.find(';') {
69+
ptr[..end].trim()
70+
} else {
71+
ptr.trim()
72+
}
73+
};
74+
75+
// Extrage conditiile daca exista
76+
let str_conditii = if let Some(pos) = where_pos {
77+
let mut cond_ptr = &ptr[pos + "WHERE".len()..];
78+
cond_ptr = cond_ptr.trim_start();
79+
if let Some(end) = cond_ptr.find(';') {
80+
cond_ptr[..end].trim()
81+
} else {
82+
cond_ptr.trim()
83+
}
84+
} else {
85+
""
86+
};
87+
88+
89+
// Aici poti implementa logica de stergere din Secretariat
90+
let conditii: Vec<Conditie> = parseaza_conditiile_where(str_conditii)?;
91+
92+
match nume_tabela {
93+
"studenti" => delete_from_table(&mut s.studenti, &conditii),
94+
"materii" => delete_from_table(&mut s.materii, &conditii),
95+
"inrolari" => delete_from_table(&mut s.inrolari, &conditii),
96+
_ => Err(format!(
97+
"Tabela {:?} nu exista in baza de date a facultati!",
98+
nume_tabela
99+
))
100+
}
101+
}

rust-method/src/queries/select.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub fn select(s: &Secretariat, query: &str) -> Result<(), String> {
132132
}
133133
}
134134

135-
let mut campuri: Vec<&str>;
135+
let campuri: Vec<&str>;
136136
if is_select_all {
137137
campuri = match nume_tabela {
138138
"studenti" => vec!["id", "nume", "an_studiu", "statut", "medie_generala"],
@@ -150,11 +150,7 @@ pub fn select(s: &Secretariat, query: &str) -> Result<(), String> {
150150
.collect();
151151
}
152152

153-
let conditii: Vec<Conditie> = if let Ok(array) = parseaza_conditiile_where(str_conditii) {
154-
array
155-
} else {
156-
return Err("Eroare la parsarea conditiilor!".to_string());
157-
};
153+
let conditii: Vec<Conditie> = parseaza_conditiile_where(str_conditii)?;
158154

159155
match nume_tabela {
160156
"studenti" => select_from_table(&s.studenti, &campuri, &conditii),
@@ -163,6 +159,6 @@ pub fn select(s: &Secretariat, query: &str) -> Result<(), String> {
163159
_ => Err(format!(
164160
"Tabela {:?} nu exista in baza de date a facultati!",
165161
nume_tabela
166-
)),
162+
))
167163
}
168164
}

rust-method/src/queries/where_clause.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn match_num_on_conditie<T>(num: T, cond: &Conditie) -> Result<bool, String>
8484
where
8585
T: FromStr + PartialEq + PartialOrd + std::fmt::Debug,
8686
{
87-
let mut valoare: T;
87+
let valoare: T;
8888
if let Ok(num) = cond.valoare.parse() {
8989
valoare = num;
9090
} else {

rust-method/src/task1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub fn calculeaza_medii_generale(s: &mut Secretariat) -> () {
155155
idx_inrolare += 1;
156156
}
157157

158-
student.medie_generala = suma_notelor / (idx_inrolare as f32);
158+
student.medie_generala = suma_notelor / (nr_materii as f32);
159159
}
160160
}
161161

rust-method/src/task2.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ mod task1;
33
mod task3;
44

55
mod queries {
6-
pub mod select;
76
pub mod where_clause;
7+
pub mod select;
8+
pub mod delete;
89
}
910

1011
use crate::structuri::*;
1112
use crate::queries::select::select;
12-
use crate::queries::where_clause::*;
13+
use crate::queries::delete::delete;
1314

1415

15-
use std::fmt::format;
16-
use std::{env, process::exit, vec};
16+
use std::{env, process::exit};
1717
use crate::task1::citeste_secretariat;
18-
use std::io::{self, BufRead};
19-
20-
use std::str::FromStr;
18+
use std::io::{self};
2119

2220

2321

@@ -58,6 +56,10 @@ fn main() {
5856
Err(message) => eprintln!("[EROARE] {:}", message)
5957
}
6058
} else if query.starts_with("DELETE") {
59+
match delete(&mut secretariat, query) {
60+
Ok(_) => (),
61+
Err(message) => eprintln!("[EROARE] {:}", message)
62+
}
6163
} else if query.starts_with("UPDATE") {
6264
} else {
6365
eprintln!("[EROARE] Interogare invalida!");

rust-method/src/task3.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::structuri::*;
22

3-
pub fn cripteaza_studenti(secretariat: &mut Secretariat, key: &String, iv: &String, cale_output: &String) {
3+
4+
pub fn cripteaza_studenti(_secretariat: &mut Secretariat, _key: &str, _iv: &str, _cale_output: &str) {
45
todo!("");
56
}

0 commit comments

Comments
 (0)