diff --git a/src/BtTreeModel.cpp b/src/BtTreeModel.cpp index d71da82c8..92d194ea5 100644 --- a/src/BtTreeModel.cpp +++ b/src/BtTreeModel.cpp @@ -838,7 +838,8 @@ void BtTreeModel::copySelected(QList< QPair > toBeCopied) case BtTreeItem::FERMENTABLE: Fermentable *copyFerm, *oldFerm; oldFerm = fermentable(ndx); - copyFerm = Database::instance().newFermentable(oldFerm); // Create a deep copy. + // Create a deep copy with a new inventory row + copyFerm = Database::instance().newFermentable(oldFerm,true); if ( copyFerm ) copyFerm->setName(name); else @@ -847,7 +848,8 @@ void BtTreeModel::copySelected(QList< QPair > toBeCopied) case BtTreeItem::HOP: Hop *copyHop, *oldHop; oldHop = hop(ndx); - copyHop = Database::instance().newHop(oldHop); // Create a deep copy. + // Create a deep copy with a new inventory row + copyHop = Database::instance().newHop(oldHop,true); if ( copyHop ) copyHop->setName(name); else @@ -856,7 +858,8 @@ void BtTreeModel::copySelected(QList< QPair > toBeCopied) case BtTreeItem::MISC: Misc *copyMisc, *oldMisc; oldMisc = misc(ndx); - copyMisc = Database::instance().newMisc(oldMisc); // Create a deep copy. + // Create a deep copy with a new inventory row + copyMisc = Database::instance().newMisc(oldMisc,true); if ( copyMisc ) copyMisc->setName(name); else @@ -883,7 +886,8 @@ void BtTreeModel::copySelected(QList< QPair > toBeCopied) case BtTreeItem::YEAST: Yeast *copyYeast, *oldYeast; oldYeast = yeast(ndx); - copyYeast = Database::instance().newYeast(oldYeast); // Create a deep copy. + // Create a deep copy with a new inventory row + copyYeast = Database::instance().newYeast(oldYeast,true); if ( copyYeast ) copyYeast->setName(name); else diff --git a/src/database.cpp b/src/database.cpp index b1c38c33a..5cb7a74db 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -1479,35 +1479,33 @@ Equipment* Database::newEquipment(Equipment* other) return tmp; } -Fermentable* Database::newFermentable(Fermentable* other) +Fermentable* Database::newFermentable(Fermentable* other, bool add_inventory) { Fermentable* tmp; - bool transact = false; + add_inventory = add_inventory || other == nullptr; + sqlDatabase().transaction(); try { - // copies automatically get their inventory_id properly set - if (other) { + if (other != nullptr) { tmp = copy(other, &allFermentables); } else { - // new ingredients don't. this gets ugly fast, because we are now - // writing to two tables and need some transactional protection - sqlDatabase().transaction(); - transact = true; tmp = newNamedEntity(&allFermentables); + } + + if ( add_inventory ) { int invkey = newInventory( dbDefn->table(Brewtarget::FERMTABLE)); tmp->setInventoryId(invkey); } } catch (QString e) { qCritical() << QString("%1 %2").arg(Q_FUNC_INFO).arg(e); - if ( transact ) sqlDatabase().rollback(); - throw; + sqlDatabase().rollback(); + abort(); } - if ( transact ) { - sqlDatabase().commit(); - } + sqlDatabase().commit(); + if ( tmp ) { emit changed( metaProperty("fermentables"), QVariant() ); emit newFermentableSignal(tmp); @@ -1519,32 +1517,30 @@ Fermentable* Database::newFermentable(Fermentable* other) return tmp; } -Hop* Database::newHop(Hop* other) +Hop* Database::newHop(Hop* other, bool add_inventory ) { Hop* tmp; - bool transact = false; + add_inventory = add_inventory || other == nullptr; + sqlDatabase().transaction(); try { - if ( other ) { + if ( other != nullptr ) tmp = copy(other, &allHops); - } - else { - sqlDatabase().transaction(); - transact = true; + else tmp = newNamedEntity(&allHops); + + if ( add_inventory ) { int invkey = newInventory( dbDefn->table(Brewtarget::HOPTABLE)); tmp->setInventoryId(invkey); } } catch (QString e) { qCritical() << QString("%1 %2").arg(Q_FUNC_INFO).arg(e); - if ( transact ) sqlDatabase().rollback(); - throw; + sqlDatabase().rollback(); + abort(); } - if ( transact ) { - sqlDatabase().commit(); - } + sqlDatabase().commit(); if ( tmp ) { emit changed( metaProperty("hops"), QVariant() ); @@ -1743,32 +1739,32 @@ MashStep* Database::newMashStep(Mash* mash, bool connected) return tmp; } -Misc* Database::newMisc(Misc* other) +Misc* Database::newMisc(Misc* other, bool add_inventory) { Misc* tmp; - bool transact = false; + add_inventory = add_inventory || other == nullptr; + sqlDatabase().transaction(); try { - if ( other ) { + if ( other != nullptr ) { tmp = copy(other, &allMiscs); } else { - sqlDatabase().transaction(); - transact = true; tmp = newNamedEntity(&allMiscs); + } + + if ( add_inventory ) { int invkey = newInventory( dbDefn->table(Brewtarget::MISCTABLE)); tmp->setInventoryId(invkey); } } catch (QString e) { qCritical() << QString("%1 %2").arg(Q_FUNC_INFO).arg(e); - if ( transact ) sqlDatabase().rollback(); - throw; + sqlDatabase().rollback(); + abort(); } - if ( transact ) { - sqlDatabase().commit(); - } + sqlDatabase().commit(); if ( tmp ) { emit changed( metaProperty("miscs"), QVariant() ); @@ -1949,19 +1945,21 @@ Salt* Database::newSalt(Salt* other) return tmp; } -Yeast* Database::newYeast(Yeast* other) +Yeast* Database::newYeast(Yeast* other, bool add_inventory) { Yeast* tmp; - bool transact = false; + add_inventory = add_inventory || other == nullptr; + sqlDatabase().transaction(); try { - if (other) { + if (other != nullptr) { tmp = copy(other, &allYeasts); } else { - sqlDatabase().transaction(); - transact = true; tmp = newNamedEntity(&allYeasts); + } + + if (add_inventory) { int invkey = newInventory( dbDefn->table(Brewtarget::YEASTTABLE)); tmp->setInventoryId(invkey); } @@ -1972,11 +1970,17 @@ Yeast* Database::newYeast(Yeast* other) throw; } - if ( transact ) { - sqlDatabase().commit(); + sqlDatabase().commit(); + + if ( tmp ) { + emit changed( metaProperty("yeasts"), QVariant() ); + emit newYeastSignal(tmp); + } + else { + qCritical() << QString("%1 could not %2 yeast") + .arg(Q_FUNC_INFO) + .arg( other ? "copy" : "create"); } - emit changed( metaProperty("yeasts"), QVariant() ); - emit newYeastSignal(tmp); return tmp; } diff --git a/src/database.h b/src/database.h index b2b4dd508..d7b23c1a0 100644 --- a/src/database.h +++ b/src/database.h @@ -217,20 +217,20 @@ class Database : public QObject //! \returns a copy of the given note. BrewNote* newBrewNote(BrewNote* other, bool signal = true); Equipment* newEquipment(Equipment* other = nullptr); - Fermentable* newFermentable(Fermentable* other = nullptr); - Hop* newHop(Hop* other = nullptr); + Fermentable* newFermentable(Fermentable* other = nullptr, bool add_inventory = false); + Hop* newHop(Hop* other = nullptr, bool add_inventory = false); //! \returns a copy of the given recipe. Recipe* newRecipe(Recipe* other); /*! \returns a copy of the given mash. Displaces the mash currently in the * parent recipe unless \b displace is false. */ - Misc* newMisc(Misc* other = nullptr); + Misc* newMisc(Misc* other = nullptr, bool add_inventory = false); Style* newStyle(Style* other); Style* newStyle(QString name); Water* newWater(Water* other = nullptr); Salt* newSalt(Salt* other = nullptr); - Yeast* newYeast(Yeast* other = nullptr); + Yeast* newYeast(Yeast* other = nullptr, bool add_inventory = false); int insertElement(NamedEntity* ins); int insertEquipment(Equipment* ins);