summaryrefslogtreecommitdiffstats
path: root/mappingtreemodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mappingtreemodel.cpp')
-rw-r--r--mappingtreemodel.cpp91
1 files changed, 20 insertions, 71 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;
}