This commit is contained in:
Claveau Joshua 2020-07-01 18:13:25 +02:00
parent 6652401d07
commit 8be1e207b8
3 changed files with 106 additions and 93 deletions

View File

@ -207,6 +207,7 @@ TARGET = qelectrotech
# Ajustement des bibliotheques utilisees lors de l'edition des liens # Ajustement des bibliotheques utilisees lors de l'edition des liens
unix:QMAKE_LIBS_THREAD -= -lpthread unix:QMAKE_LIBS_THREAD -= -lpthread
unix|win32: LIBS += -lsqlite3
# Enable C++11 # Enable C++11
QMAKE_CXXFLAGS += -std=c++11 QMAKE_CXXFLAGS += -std=c++11

View File

@ -24,6 +24,8 @@
#include "diagramposition.h" #include "diagramposition.h"
#include <QSqlError> #include <QSqlError>
#include <QSqlDriver>
#include <sqlite3.h>
/** /**
* @brief projectDataBase::projectDataBase * @brief projectDataBase::projectDataBase
@ -38,13 +40,6 @@ projectDataBase::projectDataBase(QETProject *project, QObject *parent) :
createDataBase(); createDataBase();
} }
projectDataBase::projectDataBase(QETProject *project, const QString &connection_name, const QString &path, QObject *parent) :
QObject(parent),
m_project(project)
{
createDataBase(connection_name, path);
}
/** /**
* @brief projectDataBase::~projectDataBase * @brief projectDataBase::~projectDataBase
* Destructor * Destructor
@ -91,93 +86,81 @@ QSqlQuery projectDataBase::newQuery(const QString &query) {
* Create the data base * Create the data base
* @return : true if the data base was successfully created. * @return : true if the data base was successfully created.
*/ */
bool projectDataBase::createDataBase(const QString &connection_name, const QString &name) bool projectDataBase::createDataBase()
{ {
m_data_base = QSqlDatabase::addDatabase("QSQLITE", "qet_project_db_" + m_project->uuid().toString());
if(!m_data_base.open()) {
m_data_base.close();
return false;
}
QString connect_name = connection_name; m_data_base.exec("PRAGMA temp_store = MEMORY");
if (connect_name.isEmpty()) { m_data_base.exec("PRAGMA journal_mode = MEMORY");
connect_name = "qet_project_db_" + m_project->uuid().toString(); m_data_base.exec("PRAGMA synchronous = OFF");
QSqlQuery query_(m_data_base);
bool first_ = true;
//Create diagram table
QString diagram_table("CREATE TABLE diagram ("
"uuid VARCHAR(50) PRIMARY KEY NOT NULL,"
"pos INTEGER)");
if (!query_.exec(diagram_table)) {
qDebug() << "diagram_table query : "<< query_.lastError();
} }
if (m_data_base.connectionNames().contains(connect_name)) {
m_data_base = QSqlDatabase::database(connect_name); //Create the table element
QString element_table("CREATE TABLE element"
"( "
"uuid VARCHAR(50) PRIMARY KEY NOT NULL, "
"diagram_uuid VARCHAR(50) NOT NULL,"
"pos VARCHAR(6) NOT NULL,"
"type VARCHAR(50),"
"sub_type VARCHAR(50),"
"FOREIGN KEY (diagram_uuid) REFERENCES diagram (uuid)"
")");
if (!query_.exec(element_table)) {
qDebug() <<" element_table query : "<< query_.lastError();
} }
else
//Create the diagram info table
QString diagram_info_table("CREATE TABLE diagram_info (diagram_uuid VARCHAR(50) PRIMARY KEY NOT NULL, ");
first_ = true;
for (auto string : QETApp::diagramInfoKeys())
{ {
m_data_base = QSqlDatabase::addDatabase("QSQLITE", connect_name); if (first_) {
m_data_base.setDatabaseName(name); first_ = false;
if(!m_data_base.open()) { } else {
m_data_base.close(); diagram_info_table += ", ";
return false;
} }
diagram_info_table += string += string=="date" ? " DATE" : " VARCHAR(100)";
m_data_base.exec("PRAGMA temp_store = MEMORY");
m_data_base.exec("PRAGMA journal_mode = MEMORY");
m_data_base.exec("PRAGMA synchronous = OFF");
QSqlQuery query_(m_data_base);
bool first_ = true;
//Create diagram table
QString diagram_table("CREATE TABLE diagram ("
"uuid VARCHAR(50) PRIMARY KEY NOT NULL,"
"pos INTEGER)");
if (!query_.exec(diagram_table)) {
qDebug() << "diagram_table query : "<< query_.lastError();
}
//Create the table element
QString element_table("CREATE TABLE element"
"( "
"uuid VARCHAR(50) PRIMARY KEY NOT NULL, "
"diagram_uuid VARCHAR(50) NOT NULL,"
"pos VARCHAR(6) NOT NULL,"
"type VARCHAR(50),"
"sub_type VARCHAR(50),"
"FOREIGN KEY (diagram_uuid) REFERENCES diagram (uuid)"
")");
if (!query_.exec(element_table)) {
qDebug() <<" element_table query : "<< query_.lastError();
}
//Create the diagram info table
QString diagram_info_table("CREATE TABLE diagram_info (diagram_uuid VARCHAR(50) PRIMARY KEY NOT NULL, ");
first_ = true;
for (auto string : QETApp::diagramInfoKeys())
{
if (first_) {
first_ = false;
} else {
diagram_info_table += ", ";
}
diagram_info_table += string += string=="date" ? " DATE" : " VARCHAR(100)";
}
diagram_info_table += ", FOREIGN KEY (diagram_uuid) REFERENCES diagram (uuid))";
if (!query_.exec(diagram_info_table)) {
qDebug() << "diagram_info_table query : " << query_.lastError();
}
//Create the element info table
QString element_info_table("CREATE TABLE element_info(element_uuid VARCHAR(50) PRIMARY KEY NOT NULL,");
first_=true;
for (auto string : QETApp::elementInfoKeys())
{
if (first_) {
first_ = false;
} else {
element_info_table += ",";
}
element_info_table += string += " VARCHAR(100)";
}
element_info_table += ", FOREIGN KEY (element_uuid) REFERENCES element (uuid));";
if (!query_.exec(element_info_table)) {
qDebug() << " element_info_table query : " << query_.lastError();
}
createElementNomenclatureView();
createSummaryView();
} }
diagram_info_table += ", FOREIGN KEY (diagram_uuid) REFERENCES diagram (uuid))";
if (!query_.exec(diagram_info_table)) {
qDebug() << "diagram_info_table query : " << query_.lastError();
}
//Create the element info table
QString element_info_table("CREATE TABLE element_info(element_uuid VARCHAR(50) PRIMARY KEY NOT NULL,");
first_=true;
for (auto string : QETApp::elementInfoKeys())
{
if (first_) {
first_ = false;
} else {
element_info_table += ",";
}
element_info_table += string += " VARCHAR(100)";
}
element_info_table += ", FOREIGN KEY (element_uuid) REFERENCES element (uuid));";
if (!query_.exec(element_info_table)) {
qDebug() << " element_info_table query : " << query_.lastError();
}
createElementNomenclatureView();
createSummaryView();
updateDB(); updateDB();
return true; return true;
@ -432,8 +415,38 @@ void projectDataBase::exportDb(projectDataBase *db, QWidget *parent, const QStri
return; return;
} }
//Database is filled at creation, work is done.
QString connection_name("export_project_db_" + db->project()->uuid().toString()); QString connection_name("export_project_db_" + db->project()->uuid().toString());
projectDataBase file_db(db->project(), connection_name, path_);
if (true) //Enter in a scope only to nicely use QSqlDatabase::removeDatabase just after the end of the scope
{
auto file_db = QSqlDatabase::addDatabase("QSQLITE", connection_name);
file_db.setDatabaseName(path_);
if (!file_db.open()) {
return;
}
auto memory_db_handle = sqliteHandle(&db->m_data_base);
auto file_db_handle = sqliteHandle(&file_db);
auto sqlite_backup = sqlite3_backup_init(file_db_handle, "main", memory_db_handle, "main");
if (sqlite_backup)
{
sqlite3_backup_step(sqlite_backup, -1);
sqlite3_backup_finish(sqlite_backup);
}
file_db.close();
}
QSqlDatabase::removeDatabase(connection_name); QSqlDatabase::removeDatabase(connection_name);
} }
sqlite3 *projectDataBase::sqliteHandle(QSqlDatabase *db)
{
sqlite3 *handle = nullptr;
QVariant v = db->driver()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*") == 0) {
handle = *static_cast<sqlite3 **>(v.data());
}
return handle;
}

View File

@ -26,6 +26,7 @@
class Element; class Element;
class QETProject; class QETProject;
class sqlite3;
/** /**
* @brief The projectDataBase class * @brief The projectDataBase class
@ -40,9 +41,6 @@ class projectDataBase : public QObject
public: public:
projectDataBase(QETProject *project, QObject *parent = nullptr); projectDataBase(QETProject *project, QObject *parent = nullptr);
private:
projectDataBase(QETProject *project, const QString &connection_name, const QString &path, QObject *parent = nullptr);
public:
virtual ~projectDataBase() override; virtual ~projectDataBase() override;
void updateDB(); void updateDB();
@ -53,7 +51,7 @@ class projectDataBase : public QObject
void dataBaseUpdated(); void dataBaseUpdated();
private: private:
bool createDataBase(const QString &connection_name= QString(), const QString &name = QString()); bool createDataBase();
void createElementNomenclatureView(); void createElementNomenclatureView();
void createSummaryView(); void createSummaryView();
void populateDiagramTable(); void populateDiagramTable();
@ -61,6 +59,7 @@ class projectDataBase : public QObject
void populateElementInfoTable(); void populateElementInfoTable();
void populateDiagramInfoTable(); void populateDiagramInfoTable();
static QHash<QString, QString> elementInfoToString(Element *elmt); static QHash<QString, QString> elementInfoToString(Element *elmt);
static sqlite3 *sqliteHandle(QSqlDatabase *db);
private: private:
QPointer<QETProject> m_project; QPointer<QETProject> m_project;