summaryrefslogtreecommitdiffstats
path: root/mappingtreemodel.cpp
diff options
context:
space:
mode:
authorArno <am@disconnect.de>2013-08-21 16:59:04 +0200
committerArno <am@disconnect.de>2013-08-21 16:59:04 +0200
commitd9ed1e53b7ea0c994972f4628a4dbb431fdf9fde (patch)
tree609df5caae9dd9cb6d29c228f3d408053297e69c /mappingtreemodel.cpp
parent0f3219e77f65a4c3d67f34034e67775a32d2aacb (diff)
downloadSheMov-d9ed1e53b7ea0c994972f4628a4dbb431fdf9fde.tar.gz
SheMov-d9ed1e53b7ea0c994972f4628a4dbb431fdf9fde.tar.bz2
SheMov-d9ed1e53b7ea0c994972f4628a4dbb431fdf9fde.zip
Fix MappingTreeModel for good
* Again: remove the transaction madness besides almost everywhere leaving one instance where it is actually useful. * Hide the buttons to add and delete mapping types. Haven't thought that through complete. What should be deleted on cascade? Do we really want/
Diffstat (limited to 'mappingtreemodel.cpp')
-rw-r--r--mappingtreemodel.cpp99
1 files changed, 44 insertions, 55 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(){