src/EditRecipe.cpp

changeset 152
58e4ce7dd217
parent 151
b5b2483f3a3f
child 153
e71642121fb6
--- a/src/EditRecipe.cpp	Thu Apr 21 17:22:01 2022 +0200
+++ b/src/EditRecipe.cpp	Fri Apr 22 13:46:59 2022 +0200
@@ -24,7 +24,7 @@
 
 EditRecipe::EditRecipe(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditRecipe)
 {
-    QSqlQuery query, wquery;
+    QSqlQuery query, wquery, yquery;
 
     qDebug() << "EditRecipe record:" << id;
     recipe = new Recipe;
@@ -96,7 +96,11 @@
 	query.prepare("SELECT * FROM recipes WHERE record = :recno");
 	query.bindValue(":recno", id);
 	query.exec();
-	query.next();
+    	query.first();
+
+	QSqlRecord rec = query.record();
+	for (int i = 0; i < rec.count(); i++)
+            qDebug() << i << rec.fieldName(i) << query.value(i);
 
 	recipe->record = query.value(0).toInt();
 	recipe->uuid = query.value(1).toString();
@@ -299,7 +303,7 @@
 	    } else if (yeasts.isArray()) {
 		for (int i = 0; i < yeasts.array().size(); i++) {
 		    QJsonObject obj = yeasts.array().at(i).toObject();
-		    qDebug() << i << obj;
+		    //qDebug() << i << obj;
 		    Yeasts y;
 		    /* First some defaults for newer fields. */
 		    y.y_tolerance = y.y_harvest_time = y.y_pitch_temperature = y.y_zymocide = 0;
@@ -336,31 +340,31 @@
 		    y.y_sg_hi = obj["y_sg_hi"].toDouble();
 		    y.y_cost = obj["y_cost"].toDouble();
 
-		    if (y.y_tolerance == 0) {	// More and better tests?
+		    if (y.y_tolerance == 0 || y.y_cells == 0) {	// More and better tests?
 			/*
 			 * Possible data upgrade needed.
 			 */
-			query.prepare("SELECT tolerance,cells,sta1,bacteria,harvest_top,harvest_time,pitch_temperature,"
-				      "pofpos,zymocide,gr_hl_lo,sg_lo,gr_hl_hi,sg_hi "
-				      "FROM inventory_yeasts WHERE name=:name AND laboratory=:laboratory AND product_id=:product_id");
-			query.bindValue(":name", y.y_name);
-			query.bindValue(":laboratory", y.y_laboratory);
-			query.bindValue(":product_id", y.y_product_id);
-            		query.exec();
-            		if (query.first()) {
-			    y.y_tolerance = query.value(0).toDouble();
-			    y.y_cells = query.value(1).toDouble();
-			    y.y_sta1 = query.value(2).toInt() ? true:false;
-			    y.y_bacteria = query.value(3).toInt() ? true:false;
-			    y.y_harvest_top = query.value(4).toInt() ? true:false;
-			    y.y_harvest_time = query.value(5).toInt();
-			    y.y_pitch_temperature = query.value(6).toDouble();
-			    y.y_pofpos = query.value(7).toInt() ? true:false;
-			    y.y_zymocide = query.value(8).toInt();
-			    y.y_gr_hl_lo = query.value(9).toInt();
-			    y.y_sg_lo = query.value(10).toDouble();
-			    y.y_gr_hl_hi = query.value(11).toInt();
-			    y.y_sg_hi = query.value(12).toDouble();
+			yquery.prepare("SELECT tolerance,cells,sta1,bacteria,harvest_top,harvest_time,pitch_temperature,"
+				       "pofpos,zymocide,gr_hl_lo,sg_lo,gr_hl_hi,sg_hi "
+				       "FROM inventory_yeasts WHERE name=:name AND laboratory=:laboratory AND product_id=:product_id");
+			yquery.bindValue(":name", y.y_name);
+			yquery.bindValue(":laboratory", y.y_laboratory);
+			yquery.bindValue(":product_id", y.y_product_id);
+            		yquery.exec();
+            		if (yquery.first()) {
+			    y.y_tolerance = yquery.value(0).toDouble();
+			    y.y_cells = yquery.value(1).toDouble();
+			    y.y_sta1 = yquery.value(2).toInt() ? true:false;
+			    y.y_bacteria = yquery.value(3).toInt() ? true:false;
+			    y.y_harvest_top = yquery.value(4).toInt() ? true:false;
+			    y.y_harvest_time = yquery.value(5).toInt();
+			    y.y_pitch_temperature = yquery.value(6).toDouble();
+			    y.y_pofpos = yquery.value(7).toInt() ? true:false;
+			    y.y_zymocide = yquery.value(8).toInt();
+			    y.y_gr_hl_lo = yquery.value(9).toInt();
+			    y.y_sg_lo = yquery.value(10).toDouble();
+			    y.y_gr_hl_hi = yquery.value(11).toInt();
+			    y.y_sg_hi = yquery.value(12).toDouble();
 			} else {
 			    qDebug() << y.y_name << y.y_product_id << "not found for upgrade";
 			}
@@ -373,8 +377,7 @@
             qDebug() << "empty yeasts";
         }
 
-qDebug() << query.value(88).toString();
-	const auto& ma_json = query.value(88).toString();
+	const auto& ma_json = query.value("json_mashs").toString().trimmed();
         if (!ma_json.trimmed().isEmpty()) {
             const auto& formattedJson = QString("%1").arg(ma_json);
             QJsonDocument mashs = QJsonDocument::fromJson(formattedJson.toUtf8(),  &parseError);
@@ -1041,6 +1044,7 @@
 
 	if (recipe->mashs.size() == 0) {
             query.bindValue(":json_mashs", "[]");
+	    qDebug() << "Saved empty mashs";
         } else {
             QJsonArray array;
             for (int i = 0; i < recipe->mashs.size(); i++) {
@@ -1060,7 +1064,7 @@
             }
 	    QJsonDocument doc;
             doc.setArray(array);
-//            qDebug() << doc.toJson(QJsonDocument::Compact);
+            qDebug() << doc.toJson(QJsonDocument::Compact);
             query.bindValue(":json_mashs", doc.toJson(QJsonDocument::Compact));
         }
 

mercurial