Improve ProjectDataBase use

This commit is contained in:
Claveau Joshua 2020-07-01 18:31:19 +02:00
parent 6652401d07
commit c86f9ccc3a
7 changed files with 159 additions and 57 deletions

View File

@ -86,6 +86,69 @@ QSqlQuery projectDataBase::newQuery(const QString &query) {
return QSqlQuery(query, m_data_base);
}
/**
* @brief projectDataBase::addElement
* @param element
*/
void projectDataBase::addElement(Element *element)
{
m_insert_elements_query.bindValue(":uuid", element->uuid().toString());
m_insert_elements_query.bindValue(":diagram_uuid", element->diagram()->uuid().toString());
m_insert_elements_query.bindValue(":pos", element->diagram()->convertPosition(element->scenePos()).toString());
m_insert_elements_query.bindValue(":type", element->linkTypeToString());
m_insert_elements_query.bindValue(":sub_type", element->kindInformations()["type"].toString());
if (!m_insert_elements_query.exec()) {
qDebug() << "projectDataBase::addElement insert element error : " << m_insert_elements_query.lastError();
}
m_insert_element_info_query.bindValue(":uuid", element->uuid().toString());
auto hash = elementInfoToString(element);
for (auto key : hash.keys())
{
QString value = hash.value(key);
QString bind = key.prepend(":");
m_insert_element_info_query.bindValue(bind, value);
}
if (!m_insert_element_info_query.exec()) {
qDebug() << "projectDataBase::addElement insert element info error : " << m_insert_element_info_query.lastError();
} else {
emit dataBaseUpdated();
}
}
/**
* @brief projectDataBase::removeElement
* @param element
*/
void projectDataBase::removeElement(Element *element)
{
m_remove_element_query.bindValue(":uuid", element->uuid().toString());
if(!m_remove_element_query.exec()) {
qDebug() << "projectDataBase::removeElement remove error : " << m_remove_element_query.lastError();
} else {
emit dataBaseUpdated();
}
}
/**
* @brief projectDataBase::elementInfoChanged
* @param element
*/
void projectDataBase::elementInfoChanged(Element *element)
{
auto hash = elementInfoToString(element);
for (auto str : QETApp::elementInfoKeys()) {
m_update_element_query.bindValue(":" + str, hash.value(str));
}
m_update_element_query.bindValue(":uuid", element->uuid().toString());
if (!m_update_element_query.exec()) {
qDebug() << "projectDataBase::elementInfoChanged update error : " << m_update_element_query.lastError();
} else {
emit dataBaseUpdated();
}
}
/**
* @brief projectDataBase::createDataBase
* Create the data base
@ -113,6 +176,7 @@ bool projectDataBase::createDataBase(const QString &connection_name, const QStri
m_data_base.exec("PRAGMA temp_store = MEMORY");
m_data_base.exec("PRAGMA journal_mode = MEMORY");
m_data_base.exec("PRAGMA synchronous = OFF");
m_data_base.exec("PRAGMA foreign_keys = ON");
QSqlQuery query_(m_data_base);
bool first_ = true;
@ -133,7 +197,7 @@ bool projectDataBase::createDataBase(const QString &connection_name, const QStri
"pos VARCHAR(6) NOT NULL,"
"type VARCHAR(50),"
"sub_type VARCHAR(50),"
"FOREIGN KEY (diagram_uuid) REFERENCES diagram (uuid)"
"FOREIGN KEY (diagram_uuid) REFERENCES diagram (uuid) ON DELETE CASCADE"
")");
if (!query_.exec(element_table)) {
qDebug() <<" element_table query : "<< query_.lastError();
@ -151,7 +215,7 @@ bool projectDataBase::createDataBase(const QString &connection_name, const QStri
}
diagram_info_table += string += string=="date" ? " DATE" : " VARCHAR(100)";
}
diagram_info_table += ", FOREIGN KEY (diagram_uuid) REFERENCES diagram (uuid))";
diagram_info_table += ", FOREIGN KEY (diagram_uuid) REFERENCES diagram (uuid) ON DELETE CASCADE)";
if (!query_.exec(diagram_info_table)) {
qDebug() << "diagram_info_table query : " << query_.lastError();
}
@ -169,7 +233,7 @@ bool projectDataBase::createDataBase(const QString &connection_name, const QStri
element_info_table += string += " VARCHAR(100)";
}
element_info_table += ", FOREIGN KEY (element_uuid) REFERENCES element (uuid));";
element_info_table += ", FOREIGN KEY (element_uuid) REFERENCES element (uuid) ON DELETE CASCADE);";
if (!query_.exec(element_info_table)) {
qDebug() << " element_info_table query : " << query_.lastError();
@ -179,6 +243,7 @@ bool projectDataBase::createDataBase(const QString &connection_name, const QStri
createSummaryView();
}
prepareQuery();
updateDB();
return true;
}
@ -269,9 +334,6 @@ void projectDataBase::populateElementTable()
QSqlQuery query_(m_data_base);
query_.exec("DELETE FROM element");
QString insert_("INSERT INTO element (uuid, diagram_uuid, pos, type, sub_type) VALUES (:uuid, :diagram_uuid, :pos, :type, :sub_type)");
query_.prepare(insert_);
for (auto diagram : m_project->diagrams())
{
ElementProvider ep(diagram);
@ -279,13 +341,13 @@ void projectDataBase::populateElementTable()
//Insert all value into the database
for (auto elmt : elements_list)
{
query_.bindValue(":uuid", elmt->uuid().toString());
query_.bindValue(":diagram_uuid", diagram->uuid().toString());
query_.bindValue(":pos", diagram->convertPosition(elmt->scenePos()).toString());
query_.bindValue(":type", elmt->linkTypeToString());
query_.bindValue(":sub_type", elmt->kindInformations()["type"].toString());
if (!query_.exec()) {
qDebug() << "projectDataBase::populateElementTable insert error : " << query_.lastError();
m_insert_elements_query.bindValue(":uuid", elmt->uuid().toString());
m_insert_elements_query.bindValue(":diagram_uuid", diagram->uuid().toString());
m_insert_elements_query.bindValue(":pos", diagram->convertPosition(elmt->scenePos()).toString());
m_insert_elements_query.bindValue(":type", elmt->linkTypeToString());
m_insert_elements_query.bindValue(":sub_type", elmt->kindInformations()["type"].toString());
if (!m_insert_elements_query.exec()) {
qDebug() << "projectDataBase::populateElementTable insert error : " << m_insert_elements_query.lastError();
}
}
}
@ -300,20 +362,6 @@ void projectDataBase::populateElementInfoTable()
QSqlQuery query(m_data_base);
query.exec("DELETE FROM element_info");
//Prepare the query used for insert new record
QStringList bind_values;
for (auto key : QETApp::elementInfoKeys()) {
bind_values << key.prepend(":");
}
QString insert("INSERT INTO element_info (element_uuid," +
QETApp::elementInfoKeys().join(", ") +
") VALUES (:uuid," +
bind_values.join(", ") +
")");
query.prepare(insert);
for (auto *diagram : m_project->diagrams())
{
ElementProvider ep(diagram);
@ -322,17 +370,17 @@ void projectDataBase::populateElementInfoTable()
//Insert all value into the database
for (auto elmt : elements_list)
{
query.bindValue(":uuid", elmt->uuid().toString());
m_insert_element_info_query.bindValue(":uuid", elmt->uuid().toString());
auto hash = elementInfoToString(elmt);
for (auto key : hash.keys())
{
QString value = hash.value(key);
QString bind = key.prepend(":");
query.bindValue(bind, value);
m_insert_element_info_query.bindValue(bind, value);
}
if (!query.exec()) {
qDebug() << "projectDataBase::populateElementInfoTable insert error : " << query.lastError();
if (!m_insert_element_info_query.exec()) {
qDebug() << "projectDataBase::populateElementInfoTable insert error : " << m_insert_element_info_query.lastError();
}
}
}
@ -378,6 +426,43 @@ void projectDataBase::populateDiagramInfoTable()
}
}
void projectDataBase::prepareQuery()
{
//INSERT ELEMENT
QString insert_element_query("INSERT INTO element (uuid, diagram_uuid, pos, type, sub_type) VALUES (:uuid, :diagram_uuid, :pos, :type, :sub_type)");
m_insert_elements_query = QSqlQuery(m_data_base);
m_insert_elements_query.prepare(insert_element_query);
//INSERT ELEMENT INFO
QStringList bind_values;
for (auto key : QETApp::elementInfoKeys()) {
bind_values << key.prepend(":");
}
QString insert_element_info("INSERT INTO element_info (element_uuid," +
QETApp::elementInfoKeys().join(", ") +
") VALUES (:uuid," +
bind_values.join(", ") +
")");
m_insert_element_info_query = QSqlQuery(m_data_base);
m_insert_element_info_query.prepare(insert_element_info);
//REMOVE ELEMENT
QString remove_element("DELETE FROM element WHERE uuid=:uuid");
m_remove_element_query = QSqlQuery(m_data_base);
m_remove_element_query.prepare(remove_element);
//UPDATE ELEMENT INFO
QString update_str("UPDATE element_info SET ");
for (auto string : QETApp::elementInfoKeys()) {
update_str.append(string + " = :" + string + ", ");
}
update_str.remove(update_str.length()-2, 2); //Remove the last ", "
update_str.append(" WHERE element_uuid = :uuid");
m_update_element_query = QSqlQuery(m_data_base);
m_update_element_query.prepare(update_str);
}
/**
* @brief projectDataBase::elementInfoToString
* @param elmt

View File

@ -48,6 +48,9 @@ class projectDataBase : public QObject
void updateDB();
QETProject *project() const;
QSqlQuery newQuery(const QString &query = QString());
void addElement(Element *element);
void removeElement(Element *element);
void elementInfoChanged(Element *element);
signals:
void dataBaseUpdated();
@ -60,12 +63,16 @@ class projectDataBase : public QObject
void populateElementTable();
void populateElementInfoTable();
void populateDiagramInfoTable();
void prepareQuery();
static QHash<QString, QString> elementInfoToString(Element *elmt);
private:
QPointer<QETProject> m_project;
QSqlDatabase m_data_base;
QSqlQuery m_insert_elements_query;
QSqlQuery m_insert_elements_query,
m_insert_element_info_query,
m_remove_element_query,
m_update_element_query;
public:
static void exportDb(projectDataBase *db, QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString());

View File

@ -1221,6 +1221,11 @@ void Diagram::addItem(QGraphicsItem *item)
switch (item->type())
{
case Element::Type:
{
m_project->dataBase()->addElement(static_cast<Element *>(item));
break;
}
case Conductor::Type:
{
Conductor *conductor = static_cast<Conductor *>(item);
@ -1235,7 +1240,7 @@ void Diagram::addItem(QGraphicsItem *item)
/**
* @brief Diagram::removeItem
* Réimplemented from QGraphicsScene::removeItem(QGraphicsItem *item)
* Reimplemented from QGraphicsScene::removeItem(QGraphicsItem *item)
* Do some specific operation if item need it (for exemple an element)
* @param item
*/
@ -1247,8 +1252,9 @@ void Diagram::removeItem(QGraphicsItem *item)
{
case Element::Type:
{
Element *elmt = static_cast<Element*>(item);
auto elmt = static_cast<Element*>(item);
elmt->unlinkAllElements();
m_project->dataBase()->removeElement(elmt);
break;
}
case Conductor::Type:

View File

@ -59,8 +59,12 @@ DiagramEventAddElement::DiagramEventAddElement(ElementsLocation &location, Diagr
*/
DiagramEventAddElement::~DiagramEventAddElement()
{
if (m_element) delete m_element;
foreach(QGraphicsView *view, m_diagram->views())
if (m_element)
{
m_diagram->removeItem(m_element);
m_element->deleteLater();
}
for (auto view : m_diagram->views())
view -> setContextMenuPolicy(Qt::DefaultContextMenu);
}
@ -101,7 +105,8 @@ void DiagramEventAddElement::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::RightButton)
{
delete m_element;
m_diagram->removeItem(m_element);
m_element->deleteLater();
m_element = nullptr;
m_running = false;
emit finish();
@ -125,7 +130,8 @@ void DiagramEventAddElement::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *eve
{
if (m_element && (event -> button() == Qt::LeftButton))
{
delete m_element;
m_diagram->removeItem(m_element);
m_element->deleteLater();
m_element = nullptr;
m_running = false;
emit finish();

View File

@ -72,10 +72,8 @@ void QetGraphicsTableFactory::create(Diagram *diagram, AddTableDialog *dialog)
QetGraphicsTableItem::adjustTableToFolio(table_);
}
//Add new table if needed and option checked
dialog->addNewTableToNewDiagram();
qDebug() << "model " << table_->model();
table_->model()->rowCount();
table_->displayNRow();
if (dialog->addNewTableToNewDiagram() && table_->model()->rowCount() > table_->displayNRow())

View File

@ -356,23 +356,23 @@ void ProjectDBModel::setHeaderString()
void ProjectDBModel::fillValue()
{
m_record.clear();
m_record.clear();
auto query_ = m_project->dataBase()->newQuery(m_query);
if (!query_.exec()) {
qDebug() << "Query error : " << query_.lastError();
}
auto query_ = m_project->dataBase()->newQuery(m_query);
if (!query_.exec()) {
qDebug() << "Query error : " << query_.lastError();
}
while (query_.next())
{
QStringList record_;
auto i=0;
while (query_.value(i).isValid())
{
record_ << query_.value(i).toString();
++i;
}
m_record << record_;
}
while (query_.next())
{
QStringList record_;
auto i=0;
while (query_.value(i).isValid())
{
record_ << query_.value(i).toString();
++i;
}
m_record << record_;
}
}

View File

@ -64,6 +64,6 @@ void ChangeElementInformationCommand::redo() {
void ChangeElementInformationCommand::updateProjectDB()
{
if(m_element->diagram()) {
m_element->diagram()->project()->dataBase()->updateDB();
m_element->diagram()->project()->dataBase()->elementInfoChanged(m_element);
}
}