diff options
-rw-r--r-- | mappingtreemodel.cpp | 99 | ||||
-rw-r--r-- | mappingtreemodel.h | 9 | ||||
-rw-r--r-- | mappingtreewidget.cpp | 28 |
3 files changed, 62 insertions, 74 deletions
diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index 29020f0..9650e77 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -17,22 +17,7 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr mDb = QSqlDatabase::database("treedb"); getMappingTypes(); - //prepare Queries mSParentsQ = "SELECT mapping_parents.imapping_parents_id, mapping_parents.idescription_id, mapping_description.tdescription_name, mapping_description.tscreated, mapping_parents.iparent_id FROM mapping_parents, mapping_description WHERE mapping_parents.iparent_id = :pid AND mapping_description.idescription_type = :type AND mapping_parents.idescription_id = mapping_description.idescription_id ORDER BY mapping_description.tdescription_name"; - mAddMappingTypeQ = new QSqlQuery(mDb); - mAddMappingTypeQ->prepare("INSERT INTO mappings_type (tmappings_type_name) VALUES(:value)"); - mDeleteMappingTypeQ = new QSqlQuery(mDb); - mDeleteMappingTypeQ->prepare("DELETE FROM mappings_type WHERE imappings_type_id = :id"); - mAddDescriptionQ = new QSqlQuery(mDb); - mAddDescriptionQ->prepare("INSERT INTO mapping_description (tdescription_name, idescription_type) VALUES(:name, :type)"); - mSelectDescriptionQ = new QSqlQuery(mDb); - mSelectDescriptionQ->prepare("SELECT idescription_id, tdescription_name, tscreated FROM mapping_description WHERE tdescription_name = :name AND idescription_type = :type"); - mUpdateParentQ = new QSqlQuery(mDb); - mUpdateParentQ->prepare("UPDATE mapping_parents SET iparent_id = :id where imapping_parents_id = :sid"); - mDeleteMappingParentQ = new QSqlQuery(mDb); - mDeleteMappingParentQ->prepare("DELETE FROM mapping_parents WHERE imapping_parents_id = :id"); - mDescriptionQ = new QSqlQuery(mDb); - mDescriptionQ->prepare("SELECT tdescription_name, idescription_id FROM mapping_description WHERE idescription_type = :type"); } QStringList MappingTreeModel::mappingTypeNames() const { @@ -184,43 +169,42 @@ bool MappingTreeModel::setData(const QModelIndex &idx, const QVariant &value, in bool MappingTreeModel::move(const QModelIndex &source, const QModelIndex &dest){ QVariant sourceId = source.data(MappingIdRole); QVariant destId = dest.data(MappingIdRole); - mDb.transaction(); - mUpdateParentQ->bindValue(":id", destId); - mUpdateParentQ->bindValue(":sid", sourceId); - if(mUpdateParentQ->exec()){ + QSqlQuery updateParentQ(mDb); + updateParentQ.prepare("UPDATE mapping_parents SET iparent_id = :id where imapping_parents_id = :sid"); + updateParentQ.bindValue(":id", destId); + updateParentQ.bindValue(":sid", sourceId); + if(updateParentQ.exec()){ // well, well, I tried to make this work with removeRows and insertRows, // but qt has a mind of its own there. So take the easy way out. Also, // it's way faster! - mDb.commit(); populate(); return true; } - mLastError = mUpdateParentQ->lastError(); - mDb.rollback(); + mLastError = updateParentQ.lastError(); return false; } bool MappingTreeModel::addMappingType(const QString &type){ - mAddMappingTypeQ->bindValue(":value", type); - mDb.transaction(); - if(mAddMappingTypeQ->exec()){ - mDb.commit(); + QSqlQuery addMappingTypeQ(mDb); + addMappingTypeQ.prepare("INSERT INTO mappings_type (tmappings_type_name) VALUES(:value)"); + addMappingTypeQ.bindValue(":value", type); + if(addMappingTypeQ.exec()){ getMappingTypes(); return true; } - mDb.rollback(); + mLastError = addMappingTypeQ.lastError(); return false; } bool MappingTreeModel::deleteMappingType(int typeId){ - mDeleteMappingTypeQ->bindValue(":id", typeId); - mDb.transaction(); - if(mDeleteMappingTypeQ->exec()){ - mDb.commit(); + QSqlQuery deleteMappingTypeQ(mDb); + deleteMappingTypeQ.prepare("DELETE FROM mappings_type WHERE imappings_type_id = :id"); + deleteMappingTypeQ.bindValue(":id", typeId); + if(deleteMappingTypeQ.exec()){ getMappingTypes(); return true; } - mDb.rollback(); + mLastError = deleteMappingTypeQ.lastError(); return false; } @@ -234,7 +218,7 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent) //check if we already have a mapping with this name int id = mMappings.value(name.toString(), -1); if(id == -1){ - id = addChild(name.toString(), mType); + id = addDescription(name.toString(), mType); } QSqlQuery addParentQ(mDb); addParentQ.prepare("INSERT INTO mapping_parents (idescription_id, iparent_id) VALUES(:id, :parentid)"); @@ -256,12 +240,14 @@ bool MappingTreeModel::deleteChild(const QModelIndex &idx){ if(item->childCount() > 0){ return false; } - mDeleteMappingParentQ->bindValue(":id", item->data(MappingId)); - if(mDeleteMappingParentQ->exec()){ + QSqlQuery deleteChildQ(mDb); + deleteChildQ.prepare("DELETE FROM mapping_parents WHERE imapping_parents_id = :id"); + deleteChildQ.bindValue(":id", item->data(MappingId)); + if(deleteChildQ.exec()){ removeRows(idx.row(), 1, idx.parent()); return true; } - mLastError = mDeleteMappingParentQ->lastError(); + mLastError = deleteChildQ.lastError(); return false; } @@ -316,32 +302,35 @@ void MappingTreeModel::setType(int type){ } void MappingTreeModel::getMappings(){ - mDescriptionQ->bindValue(":type", mType); - if(mDescriptionQ->exec()){ + QSqlQuery descriptionQ(mDb); + descriptionQ.prepare("SELECT tdescription_name, idescription_id FROM mapping_description WHERE idescription_type = :type"); + descriptionQ.bindValue(":type", mType); + if(descriptionQ.exec()){ mMappings.clear(); - while(mDescriptionQ->next()){ - mMappings.insert(mDescriptionQ->value(0).toString(), mDescriptionQ->value(1).toInt()); + while(descriptionQ.next()){ + mMappings.insert(descriptionQ.value(0).toString(), descriptionQ.value(1).toInt()); } } } -int MappingTreeModel::addChild(const QString &name, int type){ - mAddDescriptionQ->bindValue(":name", name); - mAddDescriptionQ->bindValue(":type", type); - if(mAddDescriptionQ->exec()){ - mSelectDescriptionQ->bindValue(":name", name); - mSelectDescriptionQ->bindValue(":type", type); - int c = 0; - mSelectDescriptionQ->exec(); - while(mSelectDescriptionQ->next()){ - ++c; - mMappings.insert(mSelectDescriptionQ->value(1).toString(), mSelectDescriptionQ->value(0).toInt()); - } - if(c > 1){ - qWarning("addChild yielded more than 1 result!"); +int MappingTreeModel::addDescription(const QString &name, int type){ + mDb.transaction(); + QSqlQuery addDescriptionQ(mDb); + addDescriptionQ.prepare("INSERT INTO mapping_description (tdescription_name, idescription_type) VALUES(:name, :type)"); + addDescriptionQ.bindValue(":name", name); + addDescriptionQ.bindValue(":type", type); + if(addDescriptionQ.exec()){ + int currval = -1; + QSqlQuery curval("SELECT currval('mapping_id__seq')", mDb); + while(curval.next()){ + currval = curval.value(0).toInt(); } + mMappings.insert(name, currval); + mDb.commit(); + return currval;; } - return mMappings.value(name); + mDb.rollback(); + return -1; } void MappingTreeModel::getMappingTypes(){ diff --git a/mappingtreemodel.h b/mappingtreemodel.h index 1fd2c82..c28967a 100644 --- a/mappingtreemodel.h +++ b/mappingtreemodel.h @@ -64,7 +64,7 @@ class MappingTreeModel : public SmTreeModel { QVariant name; }; void getMappings(); - int addChild(const QString &name, int type); + int addDescription(const QString &name, int type); void getMappingTypes(); void getChildrenRecursive(SmTreeItem *item); bool updateChild(SmTreeItem *item, const QVariant &value); @@ -74,13 +74,6 @@ class MappingTreeModel : public SmTreeModel { int lowerBound(SmTreeItem *item, const QVariant &value, int column = 0) const; QSqlDatabase mDb; QString mSParentsQ; - QSqlQuery *mAddMappingTypeQ; - QSqlQuery *mDeleteMappingTypeQ; - QSqlQuery *mAddDescriptionQ; - QSqlQuery *mSelectDescriptionQ; - QSqlQuery *mUpdateParentQ; - QSqlQuery *mDeleteMappingParentQ; - QSqlQuery *mDescriptionQ; QList<mappingType> mMappingTypes; QMap<QString, int> mMappings; const QString mForbidden; diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp index 8522f8b..1bb093f 100644 --- a/mappingtreewidget.cpp +++ b/mappingtreewidget.cpp @@ -6,20 +6,20 @@ */ #include <QSortFilterProxyModel> -#include <QtWidgets/QComboBox> -#include <QtWidgets/QPushButton> -#include <QtWidgets/QLineEdit> -#include <QtWidgets/QVBoxLayout> -#include <QtWidgets/QHBoxLayout> -#include <QtWidgets/QInputDialog> -#include <QtWidgets/QCheckBox> -#include <QtWidgets/QLabel> +#include <QComboBox> +#include <QPushButton> +#include <QLineEdit> +#include <QVBoxLayout> +#include <QHBoxLayout> +#include <QInputDialog> +#include <QCheckBox> +#include <QLabel> #include <QStringListModel> -#include <QtWidgets/QMessageBox> +#include <QMessageBox> #include <QSettings> #include <QHideEvent> -#include <QtWidgets/QAction> -#include <QtWidgets/QMenu> +#include <QAction> +#include <QMenu> #include <QSqlError> #include <QApplication> @@ -49,10 +49,14 @@ MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){ mTypeBox = new QComboBox; mTypeBox->setModel(mTypesModel); connect(mTypeBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(typeChanged(QString))); + /* Hide buttons, this doesn't really work, + * but keep them for future use :) */ mAddType = new QPushButton(tr("Add &type")); connect(mAddType, SIGNAL(clicked()), this, SLOT(addType())); + mAddType->setHidden(true); mDeleteType = new QPushButton(tr("Delete type")); connect(mDeleteType, SIGNAL(clicked()), this, SLOT(deleteType())); + mDeleteType->setHidden(true); QHBoxLayout *typesButtonLayout = new QHBoxLayout; typesButtonLayout->addWidget(mDeleteType); typesButtonLayout->addWidget(mAddType); @@ -149,6 +153,8 @@ void MappingTreeWidget::addType(){ if(idxOf != -1){ mTypeBox->setCurrentIndex(idxOf); } + }else{ + QMessageBox::critical(this, tr("Error"), tr("Failed to add type. Most likely it already exists!")); } } |