src/database/db_recipe.cpp

changeset 262
6bab9440aeb9
parent 256
c136bd15b084
child 280
efc213beb605
--- a/src/database/db_recipe.cpp	Mon Jun 06 19:35:39 2022 +0200
+++ b/src/database/db_recipe.cpp	Mon Jun 06 20:19:27 2022 +0200
@@ -356,9 +356,14 @@
 bool DB_recipe::save(Recipe *reci, QDialog *dialog)
 {
     QSqlQuery query;
+    QString sql = "";
 
     if (reci->record == -1) {
-	query.prepare("INSERT INTO recipes SET locked=:locked, st_name=:st_name, st_letter=:st_letter, "
+	sql = "INSERT INTO recipes SET ";
+    } else {
+	sql = "UPDATE recipes SET ";
+    }
+    sql.append("locked=:locked, st_name=:st_name, st_letter=:st_letter, "
 		"st_guide=:st_guide, st_category=:st_category, st_category_number=:st_catnr, st_type=:st_type, "
 		"st_og_min=:st_og_min, st_og_max=:st_og_max, st_fg_min=:st_fg_min, st_fg_max=:st_fg_max, "
 		"st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, "
@@ -383,35 +388,14 @@
 		"wb_magnesium=:wb_magnesium, wb_total_alkalinity=:wb_total_alkalinity, wb_ph=:wb_ph, "
 		"wa_acid_name=:wa_acid_name, wa_acid_perc=:wa_acid_perc, wa_base_name=:wa_base_name, "
 		"json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, "
-		"json_yeasts=:json_yeasts, json_mashs=:json_mashs, uuid=:uuid");
+		"json_yeasts=:json_yeasts, json_mashs=:json_mashs");
+    if (reci->record == -1) {
+	sql.append(", uuid=:uuid");
     } else {
-	query.prepare("UPDATE recipes SET locked=:locked, st_name=:st_name, st_letter=:st_letter, "
-		"st_guide=:st_guide, st_category=:st_category, st_category_number=:st_catnr, st_type=:st_type, "
-                "st_og_min=:st_og_min, st_og_max=:st_og_max, st_fg_min=:st_fg_min, st_fg_max=:st_fg_max, "
-                "st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, "
-                "st_carb_min=:st_carb_min, st_carb_max=:st_carb_max, st_abv_min=:st_abv_min, st_abv_max=:st_abv_max, "
-                "name=:name, notes=:notes, type=:type, batch_size=:batch_size, boil_size=:boil_size, "
-		"boil_time=:boil_time, efficiency=:efficiency, est_og=:est_og, est_fg=:est_fg, est_abv=:est_abv, "
-                "est_color=:est_color, color_method=:color_method, est_ibu=:est_ibu, ibu_method=:ibu_method, "
-                "est_carb=:est_carb, sparge_temp=:sparge_temp, sparge_ph=:sparge_ph, "
-		"sparge_volume=:sparge_volume, sparge_source=:sparge_source, sparge_acid_type=:sparge_acid_type, "
-                "sparge_acid_perc=:sparge_acid_perc, sparge_acid_amount=:sparge_acid_amount, mash_ph=:mash_ph, "
-                "mash_name=:mash_name, calc_acid=:calc_acid, "
-                "w1_name=:w1_name, w1_amount=:w1_amount, w1_calcium=:w1_calcium, w1_sulfate=:w1_sulfate, "
-                "w1_chloride=:w1_chloride, w1_sodium=:w1_sodium, w1_magnesium=:w1_magnesium, "
-                "w1_total_alkalinity=:w1_total_alkalinity, w1_ph=:w1_ph, w1_cost=:w1_cost, "
-                "w2_name=:w2_name, w2_amount=:w2_amount, w2_calcium=:w2_calcium, w2_sulfate=:w2_sulfate, "
-                "w2_chloride=:w2_chloride, w2_sodium=:w2_sodium, w2_magnesium=:w2_magnesium, "
-                "w2_total_alkalinity=:w2_total_alkalinity, w2_ph=:w2_ph, w2_cost=:w2_cost, "
-                "wg_amount=:wg_amount, wg_calcium=:wg_calcium, wg_sulfate=:wg_sulfate, "
-                "wg_chloride=:wg_chloride, wg_sodium=:wg_sodium, wg_magnesium=:wg_magnesium, "
-                "wg_total_alkalinity=:wg_total_alkalinity, wg_ph=:wg_ph, "
-                "wb_calcium=:wb_calcium, wb_sulfate=:wb_sulfate, wb_chloride=:wb_chloride, wb_sodium=:wb_sodium, "
-                "wb_magnesium=:wb_magnesium, wb_total_alkalinity=:wb_total_alkalinity, wb_ph=:wb_ph, "
-                "wa_acid_name=:wa_acid_name, wa_acid_perc=:wa_acid_perc, wa_base_name=:wa_base_name, "
-		"json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, "
-                "json_yeasts=:json_yeasts, json_mashs=:json_mashs WHERE record = :recno");
+	sql.append(" WHERE record = :recno");
     }
+
+    query.prepare(sql);
     query.bindValue(":locked", reci->locked ? 1:0);
     query.bindValue(":st_name", reci->st_name);
     query.bindValue(":st_letter", reci->st_letter);
@@ -647,28 +631,27 @@
 	query.bindValue(":recno", reci->record);
     }
     query.exec();
-    qDebug() << query.lastQuery();
     if (query.lastError().isValid()) {
-	qWarning() << "EditRecipe" << query.lastError();
+	qWarning() << "saveRecipe" << query.lastError();
 	QMessageBox::warning(dialog, QObject::tr("Database error"),
                         QObject::tr("MySQL error: %1\n%2\n%3")
                         .arg(query.lastError().nativeErrorCode())
                         .arg(query.lastError().driverText())
                         .arg(query.lastError().databaseText()));
 	return false;
+    }
+
+    /*
+     * If this was a new recipe, find out what record number we
+     * have got and set it. So when the user saves this record
+     * again, it will be updated instead of inserting a new copy.
+     */
+    if (reci->record < 0) {
+	QVariant id = query.lastInsertId();
+	reci->record = id.toInt();
+	qInfo() << "saveRecipe Inserted record" << reci->record;
     } else {
-	/*
-	 * If this was a new recipe, find out what record number we
-	 * have got and set it. So when the user saves this record
-	 * again, it will be updated instead of inserting a new copy.
-	 */
-	if (reci->record < 0) {
-	    QVariant id = query.lastInsertId();
-	    reci->record = id.toInt();
-	    qInfo() << "saveRecipe Inserted record" << reci->record;
-	} else {
-	    qInfo() << "saveRecipe Updated record" << reci->record;
-	}
+	qInfo() << "saveRecipe Updated record" << reci->record;
     }
     return true;
 }

mercurial