Skip to content

Commit 2c7f62b

Browse files
[Rust - Task 2] Import UPDATE & usize instead of u32
1 parent 4c03890 commit 2c7f62b

5 files changed

Lines changed: 82 additions & 15 deletions

File tree

rust-method/src/queries/delete.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum IdType {
1212
}
1313

1414

15-
fn delete_from_inrolari_by_id(s: &mut Secretariat, id_type: IdType, id: u32) {
15+
fn delete_from_inrolari_by_id(s: &mut Secretariat, id_type: IdType, id: usize) {
1616
let mut idx = 0;
1717

1818
while idx < s.inrolari.len() {
@@ -31,33 +31,33 @@ fn delete_from_inrolari_by_id(s: &mut Secretariat, id_type: IdType, id: u32) {
3131

3232
/// Trait pentru elementele unei tabele
3333
trait TableEntry: Matchable {
34-
fn id(&self) -> u32;
34+
fn id(&self) -> usize;
3535

3636
/// returneaza ID-urile care trebuie sterse din Inrolari
37-
fn related_ids_to_delete(&self) -> Vec<(IdType, u32)> {
37+
fn related_ids_to_delete(&self) -> Vec<(IdType, usize)> {
3838
Vec::new() // default: nu sterge nimic
3939
}
4040
}
4141

4242

4343
impl TableEntry for Student {
44-
fn id(&self) -> u32 { self.id }
44+
fn id(&self) -> usize { self.id }
4545

46-
fn related_ids_to_delete(&self) -> Vec<(IdType, u32)> {
46+
fn related_ids_to_delete(&self) -> Vec<(IdType, usize)> {
4747
vec![(IdType::IdStudent, self.id)]
4848
}
4949
}
5050

5151
impl TableEntry for Materie {
52-
fn id(&self) -> u32 { self.id }
52+
fn id(&self) -> usize { self.id }
5353

54-
fn related_ids_to_delete(&self) -> Vec<(IdType, u32)> {
54+
fn related_ids_to_delete(&self) -> Vec<(IdType, usize)> {
5555
vec![(IdType::IdMaterie, self.id)]
5656
}
5757
}
5858

5959
impl TableEntry for Inrolare {
60-
fn id(&self) -> u32 { 0 }
60+
fn id(&self) -> usize { 0 }
6161
// default: nu sterge nimic
6262
}
6363

@@ -67,7 +67,7 @@ impl TableEntry for Inrolare {
6767
fn delete_from_table<T: TableEntry>(
6868
entries: &mut Vec<T>,
6969
conditii: &Vec<Conditie>,
70-
) -> Result<Vec<(IdType, u32)>, String> {
70+
) -> Result<Vec<(IdType, usize)>, String> {
7171
let mut idx = 0;
7272
let mut related_ids = Vec::new();
7373

rust-method/src/queries/update.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use crate::Secretariat;
2+
3+
use crate::queries::where_clause::*;
4+
use crate::queries::where_clause::parseaza_conditiile_where;
5+
6+
trait Updateable {
7+
fn update_table_field(&self, field: &str, value: &str) -> Result<(), String>;
8+
}
9+
10+
11+
12+
13+
/* Template:
14+
UPDATE <tabel> SET <camp> = <valoare> WHERE <conditie>;
15+
UPDATE <tabel> SET <camp> = <valoare> WHERE <cond1> AND <cond2>;
16+
*/
17+
pub fn update(s: &mut Secretariat, query: &str) -> Result<(), String> {
18+
// Cauta "UPDATE"
19+
let update_pos = query.find("UPDATE").ok_or("Lipseste 'UPDATE'")?;
20+
let mut ptr = &query[update_pos + "UPDATE".len()..];
21+
ptr = ptr.trim_start();
22+
23+
// Extrage <tabel>
24+
let set_pos = ptr.find("SET").ok_or("Lipseste 'SET'")?;
25+
let nume_tabela = ptr[..set_pos].trim();
26+
ptr = &ptr[set_pos + "SET".len()..];
27+
ptr = ptr.trim_start();
28+
29+
// Extrage "<camp> = <valoare>"
30+
let where_pos = ptr.find("WHERE").ok_or("Lipseste 'WHERE'")?;
31+
let camp_valoare = ptr[..where_pos].trim();
32+
ptr = &ptr[where_pos + "WHERE".len()..];
33+
ptr = ptr.trim_start();
34+
35+
// Imparte <camp> de <valoare>
36+
let eq_pos = camp_valoare.find('=').ok_or("Lipseste '=' in expresia de update")?;
37+
let camp = camp_valoare[..eq_pos].trim();
38+
let mut valoare = camp_valoare[eq_pos + 1..].trim().to_string();
39+
40+
// Sterge ghilimele daca exista
41+
valoare = valoare.trim_matches('"').trim_matches('\'').to_string();
42+
43+
// Extrage conditii (TOT ce este dupa WHERE)
44+
let mut str_conditii = ptr.trim().to_string();
45+
46+
// Sterge ';' de la final
47+
if str_conditii.ends_with(';') {
48+
str_conditii.pop();
49+
}
50+
51+
// Aici poti sa folosesti deja `nume_tabela`, `camp`, `valoare` si `str_conditii`
52+
// exemplu: parseaza conditii
53+
let conditii = parseaza_conditiile_where(&str_conditii)?;
54+
55+
println!("Tabela: {}", nume_tabela);
56+
println!("Camp: {}", camp);
57+
println!("Valoare: {}", valoare);
58+
println!("Conditii: {:?}", conditii);
59+
60+
Ok(())
61+
}

rust-method/src/structuri.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[derive(Debug, Clone, Default)]
22
pub struct Student {
3-
pub id: u32,
3+
pub id: usize,
44
pub nume: String,
55
pub an_studiu: u8,
66
pub statut: char,
@@ -9,15 +9,15 @@ pub struct Student {
99

1010
#[derive(Debug, Clone, Default)]
1111
pub struct Materie {
12-
pub id: u32,
12+
pub id: usize,
1313
pub nume: String,
1414
pub nume_titular: String
1515
}
1616

1717
#[derive(Debug, Clone, Default)]
1818
pub struct Inrolare {
19-
pub id_student: u32,
20-
pub id_materie: u32,
19+
pub id_student: usize,
20+
pub id_materie: usize,
2121
pub note: [f32; 3]
2222
}
2323

rust-method/src/task1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn parseaza_intrare_inrolare(linie: &str) -> Inrolare {
101101
}
102102

103103

104-
pub fn adauga_student(s: &mut Secretariat, id: u32, nume: &String, an_studiu: u8, statut: char, medie_generala: f32) {
104+
pub fn adauga_student(s: &mut Secretariat, id: usize, nume: &String, an_studiu: u8, statut: char, medie_generala: f32) {
105105
let student = Student {
106106
id: id,
107107
nume: nume.clone(),

rust-method/src/task2.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ mod queries {
66
pub mod where_clause;
77
pub mod select;
88
pub mod delete;
9+
pub mod update;
910
}
1011

1112
use crate::structuri::*;
1213
use crate::queries::select::select;
1314
use crate::queries::delete::delete;
15+
use crate::queries::update::update;
1416

1517

1618
use std::{env, process::exit};
1719
use crate::task1::citeste_secretariat;
18-
use std::io::{self};
20+
use std::io;
1921

2022

2123

@@ -61,6 +63,10 @@ fn main() {
6163
Err(message) => eprintln!("[EROARE] {:}", message)
6264
}
6365
} else if query.starts_with("UPDATE") {
66+
match update(&mut secretariat, query) {
67+
Ok(_) => (),
68+
Err(message) => eprintln!("[EROARE] {:}", message)
69+
}
6470
} else {
6571
eprintln!("[EROARE] Interogare invalida!");
6672
}

0 commit comments

Comments
 (0)