From a682fc799b99653b67f5bbd8f1bed371bcbe48f1 Mon Sep 17 00:00:00 2001 From: Arno Date: Fri, 5 Oct 2012 12:18:14 +0200 Subject: Fixed adding and deleting children from MappingTreeModel Another fix to MappingTreeModel's new database layout. I think we're getting there... Insert the mappings into mapping_parents2 and add the MapParentId to the newly created index in the model. For now, the added date remains invalid. Make it possible (again?) to add root items to MappingTreeModel. For this I had to design a new QDialog with a checkbox. This one fixes another bug in SmTreeModel: Don't call parent() on a null pointer. Sometimes I'm getting random SIGBUS-Signals, but maybe that's because of the debug build of qt I'm using. Couldn't track it down yet... --- mappingtreemodel.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'mappingtreemodel.cpp') diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index 1babac1..eed686b 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -38,7 +38,7 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr mSelectChildQ = new QSqlQuery(mDb); mSelectChildQ->prepare("SELECT imapping_id, tmapping_name, tscreated FROM mappings WHERE tmapping_name = :name AND imapping_type = :type"); mAddParentQ = new QSqlQuery(mDb); - mAddParentQ->prepare("INSERT INTO mappings_parents (imapping_id, iparent_id) VALUES(:id, :parentid)"); + mAddParentQ->prepare("INSERT INTO mappings_parents2 (imapping_id, iparent_id) VALUES(:id, :parentid)"); mDeleteChildQ = new QSqlQuery(mDb); mDeleteChildQ->prepare("DELETE FROM mappings WHERE imapping_id = :id"); mUpdateParentQ = new QSqlQuery(mDb); @@ -301,18 +301,30 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent) 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(IdRole)); + // seems we're a child of root + QVariant ppId = parent.data(IdRole).isValid() ? parent.data(IdRole) : -1; + mAddParentQ->bindValue(":parentid", ppId); if(!mAddParentQ->exec()){ + mLastError = mAddParentQ->lastError(); + mDb.rollback(); return false; } //we're good now... db makes sure we're unique SmTreeItem *pItem = itemAt(parent); int where = lowerBound(pItem, name, Name); if(insertRows(where, 1, parent)){ + QSqlQuery pIdQuery = QSqlQuery("SELECT currval('mappings_parents_id__seq')", mDb); + int parentId = -1; + while(pIdQuery.next()){ + parentId = pIdQuery.value(0).toInt(); + } QModelIndex newIdx = index(where, 0, parent); setData(newIdx, name, NameRole); setData(newIdx, id, IdRole); + setData(newIdx, parentId, MapParentIdRole); + mDb.commit(); return true; } return false; -- cgit v1.2.3-70-g09d2