|
| 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 | +} |
0 commit comments