diff options
-rw-r--r-- | mappingtreemodel.cpp | 91 | ||||
-rw-r--r-- | mappingtreemodel.h | 5 | ||||
-rw-r--r-- | newpicsdialog.cpp | 47 | ||||
-rw-r--r-- | newpicsdialog.h | 4 |
4 files changed, 37 insertions, 110 deletions
diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index 650a21d..29020f0 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -15,16 +15,10 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mForbidden("/"), mType(-1) { //init database mDb = QSqlDatabase::database("treedb"); - mTypesQ = new QSqlQuery(mDb); - mTypesQ->prepare("SELECT imappings_type_id, tmappings_type_name FROM mappings_type"); 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"; - mUpdateTypeQ = new QSqlQuery(mDb); - mUpdateTypeQ->prepare("UPDATE mappings_type SET tmappings_type_name = :value WHERE imappings_type_id = :id"); - mUpdateChildQ = new QSqlQuery(mDb); - mUpdateChildQ->prepare("UPDATE mapping_description SET tdescription_name = :value WHERE idescription_id = :id"); mAddMappingTypeQ = new QSqlQuery(mDb); mAddMappingTypeQ->prepare("INSERT INTO mappings_type (tmappings_type_name) VALUES(:value)"); mDeleteMappingTypeQ = new QSqlQuery(mDb); @@ -33,8 +27,6 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr 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"); - mAddParentQ = new QSqlQuery(mDb); - mAddParentQ->prepare("INSERT INTO mapping_parents (idescription_id, iparent_id) VALUES(:id, :parentid)"); mUpdateParentQ = new QSqlQuery(mDb); mUpdateParentQ->prepare("UPDATE mapping_parents SET iparent_id = :id where imapping_parents_id = :sid"); mDeleteMappingParentQ = new QSqlQuery(mDb); @@ -169,19 +161,7 @@ bool MappingTreeModel::setData(const QModelIndex &idx, const QVariant &value, in return false; } } - if(item == root()){ - if(updateType(item, value)){ - emit dataChanged(idx, idx); - return true; - } - }else{ - if(updateChild(item, value)){ - QModelIndex cstart = index(idx.row(), 0, idx.parent()); - QModelIndex cend = index(idx.row(), NumFields - 1, idx.parent()); - emit dataChanged(cstart, cend); - return true; - } - } + updateChild(item, value); } if(role == NameRole){ if(value.toString().contains(mForbidden)){ @@ -256,17 +236,15 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent) if(id == -1){ id = addChild(name.toString(), mType); } - //now check if we already have a mapping with the same parent - mDb.transaction(); - mAddParentQ->bindValue(":id", id); - mAddParentQ->bindValue(":parentid", parent.data(MappingIdRole)); - if(mAddParentQ->exec()){ - mDb.commit(); + QSqlQuery addParentQ(mDb); + addParentQ.prepare("INSERT INTO mapping_parents (idescription_id, iparent_id) VALUES(:id, :parentid)"); + addParentQ.bindValue(":id", id); + addParentQ.bindValue(":parentid", parent.data(MappingIdRole)); + if(addParentQ.exec()){ populate(); return true; } - mLastError = mAddParentQ->lastError(); - mDb.rollback(); + mLastError = addParentQ.lastError(); return false; } @@ -367,13 +345,15 @@ int MappingTreeModel::addChild(const QString &name, int type){ } void MappingTreeModel::getMappingTypes(){ - bool qRes = mTypesQ->exec(); + QSqlQuery typesQ(mDb); + typesQ.prepare("SELECT imappings_type_id, tmappings_type_name FROM mappings_type"); + bool qRes = typesQ.exec(); if(qRes){ mMappingTypes.clear(); - while(mTypesQ->next()){ + while(typesQ.next()){ mappingType t; - t.id = mTypesQ->value(0); - t.name = mTypesQ->value(1); + t.id = typesQ.value(0); + t.name = typesQ.value(1); mMappingTypes << t; } emit mappingTypesChanged(); @@ -396,45 +376,14 @@ void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){ } } -bool MappingTreeModel::updateType(SmTreeItem *item, const QVariant &value){ - mDb.transaction(); - mUpdateTypeQ->bindValue(":id", item->data(DescId)); - mUpdateTypeQ->bindValue(":value", value); - if(mUpdateTypeQ->exec()){ - mDb.commit(); - item->setData(Name, value); - return true; - } - mDb.rollback(); - return false; -} - bool MappingTreeModel::updateChild(SmTreeItem *item, const QVariant &value){ - QSqlQuery findValue(mDb); - QVariant descRes; - findValue.prepare("SELECT idescription_id FROM mapping_description WHERE tdescription_name = :name"); - findValue.bindValue(":name", value); - findValue.exec(); - while(findValue.next()){ - descRes = findValue.value(0); - } - if(!descRes.isValid()){ - mUpdateChildQ->bindValue(":id", item->data(DescId)); - mUpdateChildQ->bindValue(":value", value); - if(mUpdateChildQ->exec()){ - item->setData(Name, value); - return true; - } - }else{ - QSqlQuery updateChild(mDb); - updateChild.prepare("UPDATE mapping_parents SET idescription_id = :did WHERE imapping_parents_id = :id"); - updateChild.bindValue(":did", descRes); - updateChild.bindValue(":id", item->data(MappingId)); - if(updateChild.exec()){ - item->setData(Name, value); - item->setData(DescId, descRes); - return true; - } + QSqlQuery updateMapping(mDb); + updateMapping.prepare("UPDATE mapping_description SET tdescription_name = :new WHERE idescription_id = :id"); + updateMapping.bindValue(":id", item->data(DescId)); + updateMapping.bindValue(":new", value); + if(updateMapping.exec()){ + populate(); + return true; } return false; } diff --git a/mappingtreemodel.h b/mappingtreemodel.h index 4665e07..1fd2c82 100644 --- a/mappingtreemodel.h +++ b/mappingtreemodel.h @@ -67,22 +67,17 @@ class MappingTreeModel : public SmTreeModel { int addChild(const QString &name, int type); void getMappingTypes(); void getChildrenRecursive(SmTreeItem *item); - bool updateType(SmTreeItem *item, const QVariant &value); bool updateChild(SmTreeItem *item, const QVariant &value); QStringList getPathsRecursive(SmTreeItem *parent) const; QList<QVariant> getChildListRecursive(SmTreeItem *item, int column) const; QString basePath(SmTreeItem *item) const; int lowerBound(SmTreeItem *item, const QVariant &value, int column = 0) const; QSqlDatabase mDb; - QSqlQuery *mTypesQ; QString mSParentsQ; - QSqlQuery *mUpdateTypeQ; - QSqlQuery *mUpdateChildQ; QSqlQuery *mAddMappingTypeQ; QSqlQuery *mDeleteMappingTypeQ; QSqlQuery *mAddDescriptionQ; QSqlQuery *mSelectDescriptionQ; - QSqlQuery *mAddParentQ; QSqlQuery *mUpdateParentQ; QSqlQuery *mDeleteMappingParentQ; QSqlQuery *mDescriptionQ; diff --git a/newpicsdialog.cpp b/newpicsdialog.cpp index 31e14c4..5f4009b 100644 --- a/newpicsdialog.cpp +++ b/newpicsdialog.cpp @@ -24,14 +24,7 @@ NewPicsDialog::NewPicsDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(paren } void NewPicsDialog::setupDlg(){ - //setup database mDb = QSqlDatabase::database("treedb"); - mAddFileQ = new QSqlQuery(mDb); - mAddFileQ->prepare("INSERT INTO pics (tfilename, cmd5sum, isize, tformat) VALUES(:fn, :md5, :size, :format)"); - mAddMappingQ = new QSqlQuery(mDb); - mAddMappingQ->prepare("INSERT INTO pics_mappings(ipics_id, imappings_parents_id) VALUES(:picid, :mapid)"); - - //files widget mFilesWidget = new QWidget; mFilesV = new SmTreeView; mFilesProxy = new QSortFilterProxyModel(this); @@ -134,36 +127,28 @@ void NewPicsDialog::accept(){ if(files.isEmpty()){ return QDialog::accept(); } - QList<QVariant> parentIds = mMappingEditWidget->model()->columnValues(MappingTreeResultModel::MappingId); - mDb.transaction(); + QList<QVariant> mappingParentsIds = mMappingEditWidget->model()->columnValues(MappingTreeResultModel::MappingId); + QSqlQuery addFileQ(mDb); + addFileQ.prepare("INSERT INTO pics (tfilename, cmd5sum, isize, tformat) VALUES(:fn, :md5, :size, :format)"); + QSqlQuery addMappingQ(mDb); + addMappingQ.prepare("INSERT INTO pics_mappings(ipics_id, imappings_parents_id) VALUES((SELECT ipicsid FROM pics where cmd5sum = :md5), :mapid)"); foreach(FileData d, files){ - mAddFileQ->bindValue(":fn", d.fileName); - mAddFileQ->bindValue(":md5", d.md5sum); - mAddFileQ->bindValue(":size", d.size); - mAddFileQ->bindValue(":format", d.mimeType); - if(mAddFileQ->exec()){ - QSqlQuery curPicIdQ("SELECT currval('pics_ipicsid__seq')", mDb); - QVariant picId; - while(curPicIdQ.next()){ - picId = curPicIdQ.value(0); - } - if(!picId.isValid()){ - mDb.rollback(); - return; - } - foreach(QVariant myId, parentIds){ - mAddMappingQ->bindValue(":picid", picId); - mAddMappingQ->bindValue(":mapid", myId); - if(!mAddMappingQ->exec()){ - mDb.rollback(); + addFileQ.bindValue(":fn", d.fileName); + addFileQ.bindValue(":md5", d.md5sum); + addFileQ.bindValue(":size", d.size); + addFileQ.bindValue(":format", d.mimeType); + if(addFileQ.exec()){ + foreach(QVariant mpid, mappingParentsIds){ + addMappingQ.bindValue(":mapid", mpid); + addMappingQ.bindValue(":md5", d.md5sum); + if(!addMappingQ.exec()){ return; } } + Helper::moveToArchive(d.fullPath, d.md5sum); + mFilesModel->clear(); } - Helper::moveToArchive(d.fullPath, d.md5sum); } - mDb.commit(); - mFilesModel->clear(); return QDialog::accept(); } diff --git a/newpicsdialog.h b/newpicsdialog.h index 67068df..3f8a87b 100644 --- a/newpicsdialog.h +++ b/newpicsdialog.h @@ -8,7 +8,7 @@ #ifndef NEWPICSDIALOG_H #define NEWPICSDIALOG_H -#include <QtWidgets/QDialog> +#include <QDialog> #include <QSqlDatabase> #include "smtreemodel.h" @@ -48,8 +48,6 @@ class NewPicsDialog : public QDialog { QPushButton *mOk; QPushButton *mCancel; QSqlDatabase mDb; - QSqlQuery *mAddFileQ; - QSqlQuery *mAddMappingQ; }; class NewPicFilesModel : public SmTreeModel { |