Ajouter db.cpp
This commit is contained in:
143
db.cpp
Normal file
143
db.cpp
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
#include "db.h"
|
||||||
|
#include <sqlite3.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
sqlite3* db = nullptr;
|
||||||
|
|
||||||
|
// --- Connexion / fermeture ---
|
||||||
|
bool openDB(const std::string& filename) {
|
||||||
|
int rc = sqlite3_open(filename.c_str(), &db);
|
||||||
|
if(rc) {
|
||||||
|
std::cerr << "Impossible d'ouvrir la base : " << sqlite3_errmsg(db) << "\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void closeDB() {
|
||||||
|
if(db) sqlite3_close(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Authentification ---
|
||||||
|
int loginUser(const std::string& login, const std::string& password) {
|
||||||
|
sqlite3_stmt* stmt;
|
||||||
|
std::string sql = "SELECT id, role FROM users WHERE login=? AND password=?";
|
||||||
|
sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
|
||||||
|
sqlite3_bind_text(stmt, 1, login.c_str(), -1, SQLITE_STATIC);
|
||||||
|
sqlite3_bind_text(stmt, 2, password.c_str(), -1, SQLITE_STATIC);
|
||||||
|
|
||||||
|
int role = -1;
|
||||||
|
int rc = sqlite3_step(stmt);
|
||||||
|
if(rc == SQLITE_ROW) {
|
||||||
|
role = sqlite3_column_int(stmt, 1); // 0=ADMIN, 1=PROF, 2=STUDENT
|
||||||
|
}
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Étudiants ---
|
||||||
|
std::vector<Student> getAllStudents() {
|
||||||
|
std::vector<Student> list;
|
||||||
|
sqlite3_stmt* stmt;
|
||||||
|
std::string sql = "SELECT s.id, s.nom, n.note1, n.note2, n.note3 FROM students s JOIN notes n ON s.id=n.student_id";
|
||||||
|
sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
|
||||||
|
|
||||||
|
while(sqlite3_step(stmt) == SQLITE_ROW) {
|
||||||
|
Student s;
|
||||||
|
s.id = sqlite3_column_int(stmt, 0);
|
||||||
|
s.nom = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||||
|
s.notes[0] = sqlite3_column_int(stmt, 2);
|
||||||
|
s.notes[1] = sqlite3_column_int(stmt, 3);
|
||||||
|
s.notes[2] = sqlite3_column_int(stmt, 4);
|
||||||
|
list.push_back(s);
|
||||||
|
}
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
Student getStudentByLogin(const std::string& login) {
|
||||||
|
Student s;
|
||||||
|
sqlite3_stmt* stmt;
|
||||||
|
std::string sql = "SELECT s.id, s.nom, n.note1, n.note2, n.note3 FROM students s JOIN notes n ON s.id=n.student_id WHERE s.nom=?";
|
||||||
|
sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
|
||||||
|
sqlite3_bind_text(stmt, 1, login.c_str(), -1, SQLITE_STATIC);
|
||||||
|
|
||||||
|
if(sqlite3_step(stmt) == SQLITE_ROW){
|
||||||
|
s.id = sqlite3_column_int(stmt, 0);
|
||||||
|
s.nom = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||||
|
s.notes[0] = sqlite3_column_int(stmt, 2);
|
||||||
|
s.notes[1] = sqlite3_column_int(stmt, 3);
|
||||||
|
s.notes[2] = sqlite3_column_int(stmt, 4);
|
||||||
|
}
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Modifier une note ---
|
||||||
|
bool updateNote(int studentId, int noteIndex, int newNote) {
|
||||||
|
std::string col = "note" + std::to_string(noteIndex+1);
|
||||||
|
std::string sql = "UPDATE notes SET " + col + "=? WHERE student_id=?";
|
||||||
|
sqlite3_stmt* stmt;
|
||||||
|
sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
|
||||||
|
sqlite3_bind_int(stmt, 1, newNote);
|
||||||
|
sqlite3_bind_int(stmt, 2, studentId);
|
||||||
|
|
||||||
|
int rc = sqlite3_step(stmt);
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
return rc == SQLITE_DONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Export / Import ---
|
||||||
|
bool exportData(const std::string& filename, const std::string& role, const std::string& login) {
|
||||||
|
std::ofstream file(filename);
|
||||||
|
if(!file.is_open()) return false;
|
||||||
|
|
||||||
|
std::vector<Student> list;
|
||||||
|
if(role == "ADMIN" || role == "PROF") list = getAllStudents();
|
||||||
|
else list.push_back(getStudentByLogin(login));
|
||||||
|
|
||||||
|
for(auto& s : list){
|
||||||
|
file << s.nom << " " << s.notes[0] << " " << s.notes[1] << " " << s.notes[2] << "\n";
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool importData(const std::string& filename, const std::string& role) {
|
||||||
|
if(role == "STUDENT") return false; // pas autorisé
|
||||||
|
|
||||||
|
std::ifstream file(filename);
|
||||||
|
if(!file.is_open()) return false;
|
||||||
|
|
||||||
|
std::string nom;
|
||||||
|
int n1,n2,n3;
|
||||||
|
while(file >> nom >> n1 >> n2 >> n3){
|
||||||
|
if(role == "ADMIN"){
|
||||||
|
// chercher étudiant
|
||||||
|
sqlite3_stmt* stmt;
|
||||||
|
std::string sql = "UPDATE notes SET note1=?, note2=?, note3=? WHERE student_id=(SELECT id FROM students WHERE nom=?)";
|
||||||
|
sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
|
||||||
|
sqlite3_bind_int(stmt, 1, n1);
|
||||||
|
sqlite3_bind_int(stmt, 2, n2);
|
||||||
|
sqlite3_bind_int(stmt, 3, n3);
|
||||||
|
sqlite3_bind_text(stmt, 4, nom.c_str(), -1, SQLITE_STATIC);
|
||||||
|
sqlite3_step(stmt);
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
} else if(role == "PROF"){
|
||||||
|
// modification notes seulement
|
||||||
|
sqlite3_stmt* stmt;
|
||||||
|
std::string sql = "UPDATE notes SET note1=?, note2=?, note3=? WHERE student_id=(SELECT id FROM students WHERE nom=?)";
|
||||||
|
sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
|
||||||
|
sqlite3_bind_int(stmt, 1, n1);
|
||||||
|
sqlite3_bind_int(stmt, 2, n2);
|
||||||
|
sqlite3_bind_int(stmt, 3, n3);
|
||||||
|
sqlite3_bind_text(stmt, 4, nom.c_str(), -1, SQLITE_STATIC);
|
||||||
|
sqlite3_step(stmt);
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user