Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreCharles committed May 10, 2017
2 parents fdd6712 + da795f1 commit 5369129
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 39 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# QtDB
# QtDB

![alt text](https://github.com/Corailla20/QtDB/blob/master/data/img/uml.PNG)
Binary file added data/img/uml.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 24 additions & 25 deletions src/Model/DBInteractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,42 @@ const string DBInteractor::QSQL_DATABASE = "QSQLITE";
* Methode for get or create a database instance access
* Return a database instance
*/
DBInteractor* DBInteractor::getInstance()
DBInteractor* DBInteractor::getInstance()
{
if(!instanceFlag)
{
db_instance = new DBInteractor();
instanceFlag = true;
return db_instance;
}
else
{
return db_instance;
}
if(!instanceFlag)
{
db_instance = new DBInteractor();
instanceFlag = true;
return db_instance;
}
else
{
return db_instance;
}
}

/**
* Methode to create and instantiate a database interaction singleton
*/
DBInteractor::DBInteractor()
{
cout<<"Creation of the database instance"<<endl;
cout<<"Creation of the database instance"<<endl;
db = QSqlDatabase::addDatabase(QSQL_DATABASE.c_str());
db.setHostName(HOST_NAME.c_str());
db.setDatabaseName(DATABASE_NAME.c_str());
db.setUserName(USER_NAME.c_str());
db.setPassword(PASSWORD.c_str());
error = db.open();
error = db.open();

QSqlQuery query(db);
QSqlQuery query(db);

if (!query.exec("PRAGMA foreign_keys = ON")){
std::cout << "Impossible d'activer les clés étrangères" << std::endl;
instanceFlag = false;
}
if(instanceFlag == false){
std::cout << "Errors" << std::endl;
}
if (!query.exec("PRAGMA foreign_keys = ON")){
std::cout << "Impossible d'activer les clés étrangères" << std::endl;
instanceFlag = false;
}
if(instanceFlag == false){
std::cout << "Errors" << std::endl;
}
}

/**
Expand Down Expand Up @@ -126,7 +126,6 @@ void DBInteractor::PrepareAndExecuteQueryInsert(string tableName, map<string, st
}
}
insertSQLBuilder << ")";

cout << insertSQLBuilder.str() << endl;
//return insertSQLBuilder.toString();
}
Expand Down Expand Up @@ -186,6 +185,7 @@ void DBInteractor::PrepareAndExecuteQueryUpdate(string tableName, map<string, st
* @param columnValueMappingForCondition List of Column & Value pair for WHERE clause.
* @return Final generated DELETE SQL Statement.
*/

void DBInteractor::PrepareAndExecuteQueryDelete(string tableName, map<string, string> columnValueMappingForCondition)
{
std::stringstream deleteSQLBuilder;
Expand All @@ -201,7 +201,6 @@ void DBInteractor::PrepareAndExecuteQueryDelete(string tableName, map<string, st
//return deleteSQLBuilder.toString();
}


/**
* Methode for execute SQL query String
* It is a generic function.
Expand All @@ -215,7 +214,7 @@ void DBInteractor::ExecuteQuery(string sqlQueryString)
std::cout << "Une erreur s'est produite" << std::endl;
}

while (query.next()) {
while (query.next()) {
QString test = query.value(0).toString();
std::cout << "Il y a " << test.toStdString() << std::endl;
}
Expand All @@ -226,7 +225,7 @@ void DBInteractor::ExecuteQuery(string sqlQueryString)
* It is a function to execute each line of a sql document into the databse
* @param pathFileName the path of the file
*/
void DBInteractor::InsertDefaultData(string pathFileName)
void DBInteractor::InsertDefaultData(string pathFileName)
{
ifstream file(pathFileName, ios::in);
if(file) {
Expand Down
74 changes: 73 additions & 1 deletion src/View/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Model/DBInteractor.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <qvariant.h>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
Expand All @@ -13,9 +20,74 @@ MainWindow::~MainWindow()
delete ui;
}

void MainWindow::on_pushButton_clicked()
void MainWindow::initialization()
{
ui->CB_Pathologie->addItem("");
ui->CB_Modalite->addItem("");
ui->CB_Patient->addItem("");
ui->CB_Region->addItem("");
}

void MainWindow::getDataFromDB()
{
QSqlDatabase db;

const string HOST_NAME = "localhost";
const string DATABASE_NAME = "tech_pax";
const string USER_NAME = "techmed";
const string PASSWORD = "master";
const string QSQL_DATABASE = "QSQLITE";

db = QSqlDatabase::addDatabase(QSQL_DATABASE.c_str());
db.setHostName(HOST_NAME.c_str());
db.setDatabaseName(DATABASE_NAME.c_str());
db.setUserName(USER_NAME.c_str());
db.setPassword(PASSWORD.c_str());
bool ok = db.open();

QSqlQuery query("SELECT nom FROM patient", db);

while (query.next()) {
QString name = query.value(0).toString();
ui->CB_Patient->addItem(name);
}
}

void MainWindow::on_Button_Rechercher_clicked()
{
string patient_text = ui->CB_Patient->currentText();
string modalite_text = ui->CB_Modalite->currentText();
string pathologie_text = ui->CB_Pathologie->currentText();
string region_text = ui->CB_Region->currentText();

cout << "Patient : " << patient_text << endl;
cout << "Modalite : " << modalite_text << endl;
cout << "Pathologie : " << pathologie_text << endl;
cout << "Region : " << region_text << endl;

map<string, string> columnValueMappingForCondition;

if(patient_text != "")
{
columnValueMappingForCondition.put("patient.nom", patient_text);
}
if(modalite_text != "")
{
columnValueMappingForCondition.put("type_acquisition.modalite", modalite_text);
}
if(pathologie_text != "")
{
//columnValueMappingForCondition.put("Pathologie.nom", pathologie_text);
}
if(region_text != "")
{
columnValueMappingForCondition.put("region_anatomique.nom", region_text);
}

columnValueMappingForCondition.put("patient.patient_id", "etude.patient_id");
columnValueMappingForCondition.put("region.region_id", "etude.region_id");
columnValueMappingForCondition.put("serie.type_acquisition_id", "type_acquisition.type_acquisition_id");
columnValueMappingForCondition.put("serie.etude_id", "etude.etude_id");
// SELECT
//DBInteractor::getInstance()->PrepareAndExecuteQuerySelect("patient", columnValueMappingForCondition);
}
4 changes: 3 additions & 1 deletion src/View/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class MainWindow : public QMainWindow
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void getDataFromDB();
void initialization();

private slots:
void on_pushButton_clicked();


private:
Ui::MainWindow *ui;
Expand Down
4 changes: 2 additions & 2 deletions src/View/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<x>0</x>
<y>0</y>
<width>546</width>
<height>456</height>
<height>244</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>Gestionnaire de base de données</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
Expand Down
25 changes: 21 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@
#include <string>
#include <map>
#include "Model/DBInteractor.h"
#include <QApplication>
#include "View/mainwindow.h"

using namespace std;

int main()
int main(int argc, char** argv)
{


// Affichage de la fenetre
QApplication app(argc, argv);

MainWindow mywindow;

// Remplissage des combobox
mywindow.initialization();
mywindow.getDataFromDB();

mywindow.show();

return app.exec();

/*
//1) INSERT DATA
//DBInteractor::getInstance()->InsertDefaultData("../data/data.sql");
Expand All @@ -26,8 +42,6 @@ int main()
DBInteractor::getInstance()->PrepareAndExecuteQueryUpdate("TEST", columnValueMappingForSet, columnValueMappingForCondition);
*/

// EXAMPLE UPDATE
map<string, string> columnValueMappingForSet;
Expand All @@ -41,4 +55,7 @@ int main()
DBInteractor::getInstance()->PrepareAndExecuteQueryUpdate("TEST", columnValueMappingForSet, columnValueMappingForCondition);
*/


}
Loading

0 comments on commit 5369129

Please sign in to comment.