Skip to content

Commit

Permalink
Merge pull request #536 from mikfire/bugs/copyinventory
Browse files Browse the repository at this point in the history
Closes #532 -- Problem with amounts in inventory
  • Loading branch information
mattiasmaahl authored Apr 8, 2021
2 parents 5fcb0be + 3c6f3a0 commit c84445a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 52 deletions.
12 changes: 8 additions & 4 deletions src/BtTreeModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,8 @@ void BtTreeModel::copySelected(QList< QPair<QModelIndex, QString> > 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
Expand All @@ -847,7 +848,8 @@ void BtTreeModel::copySelected(QList< QPair<QModelIndex, QString> > 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
Expand All @@ -856,7 +858,8 @@ void BtTreeModel::copySelected(QList< QPair<QModelIndex, QString> > 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
Expand All @@ -883,7 +886,8 @@ void BtTreeModel::copySelected(QList< QPair<QModelIndex, QString> > 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
Expand Down
92 changes: 48 additions & 44 deletions src/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() );
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c84445a

Please sign in to comment.