|
- #include "bdd.h"
-
- class_bdd::class_bdd(const wxString& path_in, wxGrid* grid_consults)
- {
- this->path_in = path_in;
- if (wxFileExists(path_in + slash + "db.lck"))
- {
- wxMessageBox(_T("La base est actuellement en cours d'utilisation.\nSi vous êtes certains que ce n'est pas le cas,\n(comme par exemple après un crash)\nsupprimez le fichier db.lck situé dans le même répertoire"));
- exit(0);
- }
- this->grid_consults = grid_consults;
- wxString path = path_in + slash + "cosmos.db";
- if (!wxFileExists(path))
- CreateEmpty(path);
- else if ((rc=sqlite3_open_v2(_C(path), &db, SQLITE_OPEN_READWRITE, NULL)) != SQLITE_OK)
- {
- wxMessageBox(_T("Erreur lors de l'ouverture de la base de données"), "Erreur", wxICON_ERROR | wxOK);
- exit(0);
- }
- wxFile* file_lock = new wxFile(path_in + slash + "db.lck", wxFile::write);
- file_lock->Close();
- }
-
- class_bdd::~class_bdd()
- {
- sqlite3_close(db);
- wxRemoveFile(path_in + slash + "db.lck");
- }
-
- void class_bdd::CreateEmpty(const wxString& path)
- {
- rc = 0;
- rc += sqlite3_open_v2(_C(path), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
- rc += sqlite3_exec(db, "CREATE TABLE Consult(id INTEGER PRIMARY KEY ASC, n_dossier INTEGER, nom VARCHAR(50), prenom VARCHAR(50), responsable VARCHAR(100), consultant VARCHAR(50), theme TEXT, etablissement VARCHAR(100), travail VARCHAR(50), date_cs DATE, service VARCHAR(100));", NULL, NULL, NULL);
- rc += sqlite3_exec(db, "CREATE TABLE Result(id INTEGER PRIMARY KEY ASC, publi BOOLEAN DEFAULT 0, publi_CHU BOOLEAN DEFAULT 0, cs_associe BOOLEAN DEFAULT 0, nb_result INTEGER DEFAULT 0, id_result TEXT DEFAULT \"\");", NULL, NULL, NULL);
- rc += sqlite3_exec(db, "CREATE TABLE Publi(id INTEGER PRIMARY KEY, titre TEXT, auteurs TEXT, abstract TEXT, revue TEXT, lien VARCHAR(50), date_publi TEXT, affiliation TEXT);", NULL, NULL, NULL);
- rc += sqlite3_exec(db, "CREATE TABLE Correc(orig TEXT PRIMARY KEY, dest TEXT);", NULL, NULL, NULL);
- if (rc)
- {
- wxMessageBox(_T("Erreur lors de la création de la base de données"), "Erreur", wxICON_ERROR | wxOK);
- exit(0);
- }
- }
-
- void class_bdd::Importer(const wxString& filename)
- {
- rc = 0;
- int size = 0;
- int progress = 0; //indicateurs de progression
- wxUniChar c;
- wxString content;
- bool inquote = false; //guillemets
- bool is_first_line = true;
- wxString item[23];
-
- wxFFile* file_import = new wxFFile();
- file_import->Open(filename);
- #ifdef __WXMSW__
- file_import->ReadAll(&content, wxConvAuto(wxFONTENCODING_SYSTEM));
- #elif defined(__WXGTK__)
- file_import->ReadAll(&content);
- #endif
- size = content.length() + 1;
-
- sqlite3_exec(db, "SAVEPOINT before_import;", NULL, NULL, NULL);
- wxProgressDialog dialog_progress("Import", "Import de la base...", size, NULL, wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_SMOOTH | wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME | wxPD_CAN_ABORT);
-
- do
- {
- for (int i=0; i<22; i++) //boucle pour les 21 premiers items
- {
- item[i]="";
- do
- {
- c = content[progress];
- progress++;
- if (c == '"')
- {
- inquote = (!inquote) ? true : false;
- }
- if (c != '"')
- item[i] << c;
- }while (((c != ';') && (!inquote)) || inquote); //boucle pour un item, se finit par un ';' sauf entre guillemets
- item[i] = item[i].BeforeLast(';');
- }
- item[22]="";
- do
- {
- c = content[progress];
- progress++;
- if ((c != '\n') && (c != '\0'))
- item[22] << c;
- }while ((c != '\n') && (c != '\0')); //dernier item de la ligne, se finit si retour ou EOF
-
- if (!is_first_line)
- {
- //Insertion dans la table de consults
- wxString requete = "INSERT OR REPLACE INTO Consult (id, n_dossier, nom, prenom, responsable, consultant, theme, etablissement, travail, date_cs, service) VALUES (";
- requete << item[0] << ", "; //numéro travail -> id PRIMARY KEY
- if (item[1].IsSameAs("")) //n_dossier
- requete << "NULL, ";
- else
- requete << item[1] << ", ";
- requete << "\"" << item[3] << "\", " //Nom
- << "\"" << item[4] << "\", " //Prénom
- << "\"" << item[19] << "\", " //Responsable
- << "\"" << item[20] << "\", " //Consultant
- << "\"" << item[13] << "\", " //Thème
- << "\"" << item[7] << "\", " //Etablissement
- << "\"" << item[12] << "\", " //Type travail
- << "\"" << item[21] << "\", " //Date_cs
- << "\"" << item[6] << "\"" //Service
- << ");";
- rc += sqlite3_exec(db, _C(requete), NULL, NULL, NULL);
-
- //Insertion dans la table de résultats
- requete = "INSERT OR IGNORE INTO Result (id) VALUES (";
- requete << item[0]
- << ");";
- rc += sqlite3_exec(db, _C(requete), NULL, NULL, NULL);
- }
- is_first_line = false;
- //Annulation de l'import
- if (!dialog_progress.Update(progress))
- {
- wxMessageBox(_T("Aucune nouvelle donnée n'a été ajoutée"), "Attention !");
- sqlite3_exec(db, "ROLLBACK TO before_import;", NULL, NULL, NULL);
- break;
- }
- if (content[progress] == '\0')
- break;
-
- }while (c != '\0');
- file_import->Close();
- dialog_progress.Update(size);
- sqlite3_exec(db, "RELEASE before_import;", NULL, NULL, NULL);
- if (rc)
- wxMessageBox("Erreur lors de l'import", "Erreur", wxOK | wxICON_ERROR);
- GenerateGrid();
- }
-
- void class_bdd::Exporter(const wxString& filename)
- {
- wxProgressDialog dialog_progress("Export", "Export de la base...", grid_consults->GetNumberRows(), NULL, wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_SMOOTH | wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME);
- wxFFile* file_export = new wxFFile(filename, "w");
- wxString line;
- line = _T("Travail numéro;Dossier numéro;Nom demandeur;Prénom demandeur;Service libellé;Etablissement libellé;Type travail libellé;Thème;Nom responsable;Consultant responsable;Date dernier RDV eff;Publi;Publi CHU;Consultant associé;Date de publication 1;Revue 1;Lien 1;Date de publication 2;Revue 2;Lien 2\n");
- file_export->Write(line, wxConvLocal);
- for (int i=0; i < grid_consults->GetNumberRows(); i++)
- {
- wxString id = grid_consults->GetCellValue(i,13);
- line="";
- wxString requete = "SELECT service FROM Consult WHERE id=" + id + ";";
- sqlite3_prepare_v2(db, _C(requete), -1, &stmt, NULL);
- sqlite3_step(stmt);
- wxString service = sqlite3_column_text(stmt,0);
- line << grid_consults->GetCellValue(i,13) << ";"
- << grid_consults->GetCellValue(i,4) << ";"
- << "\"" << grid_consults->GetCellValue(i,5) << "\";"
- << "\"" << grid_consults->GetCellValue(i,6) << "\";"
- << "\"" << service << "\";"
- << "\"" << grid_consults->GetCellValue(i,10) << "\";"
- << "\"" << grid_consults->GetCellValue(i,11) << "\";"
- << "\"" << grid_consults->GetCellValue(i,9) << "\";"
- << "\"" << grid_consults->GetCellValue(i,7) << "\";"
- << "\"" << grid_consults->GetCellValue(i,8) << "\";"
- << grid_consults->GetCellValue(i,12) << ";"
- << grid_consults->GetCellValue(i,1) << ";"
- << grid_consults->GetCellValue(i,2) << ";"
- << grid_consults->GetCellValue(i,3) << ";";
- wxArrayString list_publi = GetIdResult(id);
- for (unsigned int j=0; j < list_publi.GetCount(); j++)
- {
- if (list_publi[j].Left(1) == '@')
- {
- requete = "SELECT date_publi,revue,lien FROM Publi WHERE id=" + list_publi[j].AfterLast('@') + ";";
- sqlite3_prepare_v2(db, _C(requete), -1, &stmt, NULL);
- sqlite3_step(stmt);
- line << sqlite3_column_text(stmt,0) << ";" << sqlite3_column_text(stmt,1) << ";" << sqlite3_column_text(stmt,2) << ";";
- }
- }
- line << "\n";
- file_export->Write(line, wxConvLocal);
- dialog_progress.Update(i);
- }
-
- file_export->Close();
- }
-
- void class_bdd::GenerateGrid()
- {
- wxBeginBusyCursor();
-
- int line_nb = 0;
-
- rc = 0;
- stmt = NULL;
- if (grid_consults->GetNumberRows() != 0)
- grid_consults->DeleteRows(0, grid_consults->GetNumberRows()); //reset de la grille
- rc = sqlite3_prepare_v2(db, "SELECT * FROM Consult;", -1, &stmt, NULL); //import depuis Consult
- if (sqlite3_step(stmt) == SQLITE_DONE) //Si la bdd est vide
- {
- wxEndBusyCursor();
- wxMessageBox(_T("La base de données est vide,\nveuillez importer des données !"));
- return;
- }
- else
- sqlite3_reset(stmt); //retour à la première ligne, celle-ci étant lue juste avant pour vérifier la non vacuité
-
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- grid_consults->AppendRows();
- grid_consults->SetCellValue(line_nb, 4, _itoW(sqlite3_column_int(stmt, 1))); //n_dossier
- for (int i=2; i<10; i++) //nom, prénom, responsable, consultant, theme, etablissement, travail, date
- {
- wxString orig = sqlite3_column_text(stmt, i);
- // corrections
- rc = sqlite3_prepare_v2(db, _C("SELECT dest FROM Correc WHERE orig=\"" + orig + "\";"), -1, &stmt_correc, NULL);
-
- if (sqlite3_step(stmt_correc) == SQLITE_DONE)
- grid_consults->SetCellValue(line_nb, i+3, orig);
- else
- grid_consults->SetCellValue(line_nb, i+3, sqlite3_column_text(stmt_correc, 0));
-
- sqlite3_finalize(stmt_correc);
- }
- // autocorrection prenoms
- wxString prenom = grid_consults->GetCellValue(line_nb, 6);
- if (prenom.IsSameAs("xxx", false) || prenom.IsSameAs("xx", false) || prenom.IsSameAs("zzz", false) || prenom.IsSameAs("zz", false) || prenom.IsSameAs("yy", false) || prenom.IsSameAs("xy", false))
- grid_consults->SetCellValue(line_nb, 6, "");
- // autocorrection responsables
- wxString responsable = grid_consults->GetCellValue(line_nb, 7);
- responsable.Replace("Mr. Le Pr. ", "", true);
- responsable.Replace("Mme Le Dr. ", "", true);
- responsable.Replace("Mme Le Pr. ", "", true);
- responsable.Replace("Mr. ", "", true);
- responsable.Replace("Dr. ", "", true);
- responsable.Replace("Dr ", "", true);
- responsable.Replace("Pr. ", "", true);
- responsable.Replace("Pr ", "", true);
- responsable.Replace("Mme ", "", true);
- responsable.Replace("Melle ", "", true);
- grid_consults->SetCellValue(line_nb, 7, responsable);
-
- grid_consults->SetCellValue(line_nb, 13, _itoW(sqlite3_column_int(stmt, 0))); //id, caché
- line_nb++;
- }
- sqlite3_finalize(stmt);
-
- line_nb = 0;
- sqlite3_prepare_v2(db, "SELECT * FROM Result;", -1, &stmt, NULL); //import depuis Result
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- grid_consults->SetCellValue(line_nb, 0, _itoW(sqlite3_column_int(stmt, 4))); //nb_results
- for (int i=1; i<4; i++)
- grid_consults->SetCellValue(line_nb, i, _itoW(sqlite3_column_int(stmt, i))); //publi, publi_CHU, cs_associe
- if (sqlite3_column_int(stmt, 1))
- for (int i=0; i<13; i++)
- grid_consults->SetCellBackgroundColour(line_nb, i, *wxGREEN); //coloration des consults trouvées
- line_nb++;
- }
- sqlite3_finalize(stmt);
-
- grid_consults->AutoSizeColumns(false);
- grid_consults->AutoSizeRows(false);
- grid_consults->HideCol(13);
-
- wxEndBusyCursor();
- }
-
- void class_bdd::SetResultPubli(int row, unsigned int bitfield)
- {
- wxString id = grid_consults->GetCellValue(row,13);
- wxString val[3] = {"0","0","0"};
-
- //Actualisation de la grille
- for (int i=1; i<= 3; i++)
- grid_consults->SetCellValue(row,i,"0");
-
- if (bitfield & 1<<1)
- {
- grid_consults->SetCellValue(row,1,"1");
- val[0] = "1";
- }
- if (bitfield & 1<<2)
- {
- grid_consults->SetCellValue(row,2,"1");
- val[1] = "1";
- }
- if (bitfield & 1<<3)
- {
- grid_consults->SetCellValue(row,3,"1");
- val[2] = "1";
- }
-
- for (int i=0; i<13; i++)
- {
- if (bitfield & 1<<1)
- grid_consults->SetCellBackgroundColour(row, i, *wxGREEN);
- else
- grid_consults->SetCellBackgroundColour(row, i, grid_consults->GetDefaultCellBackgroundColour());
- }
- grid_consults->ForceRefresh();
-
- rc = 0;
- wxString requete = "UPDATE Result SET";
- requete << " publi=" << val[0]
- << ", publi_CHU=" << val[1]
- << ", cs_associe=" << val[2]
- << " WHERE id="
- << id << ";";
- rc = sqlite3_exec(db, _C(requete), NULL, NULL, NULL);
- if (rc)
- wxMessageBox("Erreur lors de la mise à jour de la valeur", "Erreur", wxOK | wxICON_ERROR);
- }
-
- wxArrayString* class_bdd::GetListCorrecs()
- {
- wxArrayString* out = new wxArrayString[2];
-
- sqlite3_prepare_v2(db, "SELECT * FROM Correc;", -1, &stmt, NULL);
- while (sqlite3_step(stmt) == SQLITE_ROW)
- {
- out[0].Add(sqlite3_column_text(stmt,0));
- out[1].Add(sqlite3_column_text(stmt,1));
- }
- sqlite3_finalize(stmt);
-
- return out;
- }
-
- void class_bdd::AddCorrec(const wxString& id, int col, const wxString& dest)
- {
- rc = 0;
- wxString requete = "SELECT * FROM Consult WHERE id=" + id + ";";
- sqlite3_prepare_v2(db, _C(requete), -1, &stmt, NULL);
- sqlite3_step(stmt);
- wxString orig = sqlite3_column_text(stmt, col-3);
- sqlite3_finalize(stmt);
- SetCorrec(orig, dest);
- }
-
- void class_bdd::SetCorrec(const wxString& orig, const wxString& dest)
- {
- wxString requete = "INSERT OR REPLACE INTO Correc VALUES (\"" + orig + "\",\"" + dest + "\");";
- sqlite3_exec(db, _C(requete), NULL, NULL, NULL);
-
- // Mise à jour de la grille
- for (int i=0; i<grid_consults->GetNumberRows(); i++)
- {
- requete = "SELECT * FROM Consult WHERE id=" + grid_consults->GetCellValue(i, 13) + ";";
- sqlite3_prepare_v2(db, _C(requete), -1, &stmt, NULL);
- sqlite3_step(stmt);
- for (int j=5; j<11; j++)
- {
- wxString table_orig = sqlite3_column_text(stmt, j - 3);
- if (table_orig == orig)
- grid_consults->SetCellValue(i, j, dest);
- }
- sqlite3_finalize(stmt);
- }
-
-
- // Suppression d'une correction si on revient à l'original
- if (dest == orig)
- DelCorrec(orig);
- }
-
- void class_bdd::DelCorrec(const wxString& orig)
- {
- sqlite3_exec(db, _C("DELETE FROM Correc WHERE orig=\"" + orig + "\";"), NULL, NULL, NULL);
- }
-
- int class_bdd::GetNbResults(const wxString& id)
- {
- sqlite3_prepare_v2(db, _C("SELECT nb_result FROM Result WHERE id=" + id + ";") , -1, &stmt, NULL);
- sqlite3_step(stmt);
- int out = sqlite3_column_int(stmt, 0);
- sqlite3_finalize(stmt);
-
- return out;
- }
-
- void class_bdd::SetNbResults(const wxString& id, int nb_results)
- {
- sqlite3_exec(db, _C("UPDATE Result SET nb_result=" + _itoW(nb_results) + " WHERE id=" + id + ";"), NULL, NULL, NULL);
- }
-
- wxArrayString class_bdd::GetIdResult(const wxString& id, bool stripped)
- {
- sqlite3_prepare_v2(db, _C("SELECT id_result FROM Result WHERE id=" + id + ";"), -1, &stmt, NULL);
- sqlite3_step(stmt);
- wxString list_id = sqlite3_column_text(stmt, 0);
- sqlite3_finalize(stmt);
-
- wxArrayString out;
- while (list_id.Length() > 0)
- {
- if (stripped)
- out.Add(list_id.BeforeFirst(',').AfterLast('!'));
- else
- out.Add(list_id.BeforeFirst(','));
- list_id = list_id.AfterFirst(',');
- }
-
- return out;
- }
-
- void class_bdd::SetIdResult(const wxString& id, wxArrayString id_results)
- {
- wxString list_id = "";
- int count = 0;
- for (unsigned int i=0; i<id_results.GetCount(); i++)
- {
- list_id += id_results[i] + ",";
- if (id_results[i].Left(1) != '!')
- count++;
- }
- list_id = list_id.BeforeLast(',');
- sqlite3_exec(db, _C("UPDATE Result SET id_result=\"" + list_id + "\" WHERE id=" + id + ";"), NULL, NULL, NULL);
- SetNbResults(id, count);
- }
-
- void class_bdd::AddIdResult(const wxString& id, const wxString& id_result)
- {
- sqlite3_prepare_v2(db, _C("SELECT id_result FROM Result WHERE id=" + id + ";"), -1, &stmt, NULL);
- sqlite3_step(stmt);
- wxString list_id = sqlite3_column_text(stmt, 0);
- sqlite3_finalize(stmt);
- if (list_id.Length() > 0)
- list_id = id_result + "," + list_id;
- else
- list_id = id_result;
-
- sqlite3_exec(db, _C("UPDATE Result SET id_result=\"" + list_id + "\" WHERE id=" + id + ";"), NULL, NULL, NULL);
- }
-
- bool class_bdd::IsPubli(const wxString& id)
- {
- sqlite3_prepare_v2(db, _C("SELECT * FROM Publi WHERE id=" + id + ";"), -1, &stmt, NULL);
- bool out = (sqlite3_step(stmt) == SQLITE_ROW);
- sqlite3_finalize(stmt);
- return out;
- }
-
- void class_bdd::AddPubli(wxString id, wxString title, wxString authors, wxString abstract, wxString journal, wxString link, wxString date, wxString affiliation)
- {
- title.Replace("\"", "_");
- abstract.Replace("\"", "_");
- wxString requete = "INSERT OR REPLACE INTO Publi (id, titre, auteurs, abstract, revue, lien, date_publi, affiliation) VALUES (";
- requete << id << ", "
- << "\"" << title << "\", "
- << "\"" << authors << "\", "
- << "\"" << abstract << "\", "
- << "\"" << journal << "\", "
- << "\"" << link << "\", "
- << "\"" << date << "\", "
- << "\"" << affiliation << "\");";
- rc = sqlite3_exec(db, _C(requete), NULL, NULL, NULL);
- if (rc)
- wxMessageBox(_itoW(rc) + "\n" + requete);
- }
-
- wxArrayString class_bdd::GetPublis(wxString id)
- {
- wxArrayString out;
- sqlite3_prepare_v2(db, _C("SELECT * FROM Publi WHERE id=" + id + ";"), -1, &stmt, NULL);
- sqlite3_step(stmt);
- out.Add(wxString::FromUTF8((const char*)sqlite3_column_text(stmt, 1)));
- out.Add(wxString::FromUTF8((const char*)sqlite3_column_text(stmt, 2)));
- out.Add(wxString::FromUTF8((const char*)sqlite3_column_text(stmt, 3)));
- out.Add(wxString::FromUTF8((const char*)sqlite3_column_text(stmt, 4)));
- out.Add(wxString::FromUTF8((const char*)sqlite3_column_text(stmt, 5)));
- out.Add(wxString::FromUTF8((const char*)sqlite3_column_text(stmt, 6)));
- out.Add(wxString::FromUTF8((const char*)sqlite3_column_text(stmt, 7)));
-
- return out;
- }
|