src/database/db_recipe.cpp

changeset 256
c136bd15b084
parent 249
3c28dc8dd51d
child 262
6bab9440aeb9
equal deleted inserted replaced
255:f237db73d4c8 256:c136bd15b084
25 25
26 query.prepare("SELECT * FROM recipes WHERE record = :recno"); 26 query.prepare("SELECT * FROM recipes WHERE record = :recno");
27 query.bindValue(":recno", recno); 27 query.bindValue(":recno", recno);
28 query.exec(); 28 query.exec();
29 if (! query.first()) { 29 if (! query.first()) {
30 qDebug() << "loadRecipe seek error record" << recno; 30 qWarning() << "loadRecipe seek error record" << recno;
31 QMessageBox::warning(dialog, QObject::tr("Database error"), QObject::tr("MySQL error: record %1 not found").arg(recno)); 31 QMessageBox::warning(dialog, QObject::tr("Database error"), QObject::tr("MySQL error: record %1 not found").arg(recno));
32 dialog->done(QDialog::Rejected); // At this stage, this doesn't work because the dialog is not yet visible. 32 dialog->done(QDialog::Rejected); // At this stage, this doesn't work because the dialog is not yet visible.
33 return false; 33 return false;
34 } 34 }
35 35
130 const auto& f_json = query.value("json_fermentables").toString(); 130 const auto& f_json = query.value("json_fermentables").toString();
131 if (!f_json.trimmed().isEmpty()) { 131 if (!f_json.trimmed().isEmpty()) {
132 const auto& formattedJson = QString("%1").arg(f_json); 132 const auto& formattedJson = QString("%1").arg(f_json);
133 QJsonDocument fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 133 QJsonDocument fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
134 if (parseError.error != QJsonParseError::NoError) { 134 if (parseError.error != QJsonParseError::NoError) {
135 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 135 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
136 } else if (fermentables.isArray()) { 136 } else if (fermentables.isArray()) {
137 double percentcheck = 0; 137 double percentcheck = 0;
138 for (int i = 0; i < fermentables.array().size(); i++) { 138 for (int i = 0; i < fermentables.array().size(); i++) {
139 QJsonObject obj = fermentables.array().at(i).toObject(); 139 QJsonObject obj = fermentables.array().at(i).toObject();
140 Fermentables f; 140 Fermentables f;
174 } 174 }
175 for (int i = 0; i < reci->fermentables.size(); i++) { 175 for (int i = 0; i < reci->fermentables.size(); i++) {
176 if (reci->fermentables.at(i).f_added < 4) 176 if (reci->fermentables.at(i).f_added < 4)
177 reci->fermentables[i].f_percentage = round((reci->fermentables.at(i).f_amount / total) * 10000.0) / 100.0; 177 reci->fermentables[i].f_percentage = round((reci->fermentables.at(i).f_amount / total) * 10000.0) / 100.0;
178 } 178 }
179 qDebug() << " fixed missing percentages"; 179 qInfo() << "fixed missing percentages";
180 } 180 }
181 } 181 }
182 } else { 182 } else {
183 qDebug() << "empty fermentables"; 183 qDebug() << "empty fermentables";
184 } 184 }
186 const auto& h_json = query.value("json_hops").toString(); 186 const auto& h_json = query.value("json_hops").toString();
187 if (!h_json.trimmed().isEmpty()) { 187 if (!h_json.trimmed().isEmpty()) {
188 const auto& formattedJson = QString("%1").arg(h_json); 188 const auto& formattedJson = QString("%1").arg(h_json);
189 QJsonDocument hops = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 189 QJsonDocument hops = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
190 if (parseError.error != QJsonParseError::NoError) { 190 if (parseError.error != QJsonParseError::NoError) {
191 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset; 191 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset;
192 } else if (hops.isArray()) { 192 } else if (hops.isArray()) {
193 for (int i = 0; i < hops.array().size(); i++) { 193 for (int i = 0; i < hops.array().size(); i++) {
194 QJsonObject obj = hops.array().at(i).toObject(); 194 QJsonObject obj = hops.array().at(i).toObject();
195 Hops h; 195 Hops h;
196 h.h_name = obj["h_name"].toString(); 196 h.h_name = obj["h_name"].toString();
219 const auto& m_json = query.value("json_miscs").toString(); 219 const auto& m_json = query.value("json_miscs").toString();
220 if (!m_json.trimmed().isEmpty()) { 220 if (!m_json.trimmed().isEmpty()) {
221 const auto& formattedJson = QString("%1").arg(m_json); 221 const auto& formattedJson = QString("%1").arg(m_json);
222 QJsonDocument miscs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 222 QJsonDocument miscs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
223 if (parseError.error != QJsonParseError::NoError) { 223 if (parseError.error != QJsonParseError::NoError) {
224 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset; 224 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset;
225 } else if (miscs.isArray()) { 225 } else if (miscs.isArray()) {
226 for (int i = 0; i < miscs.array().size(); i++) { 226 for (int i = 0; i < miscs.array().size(); i++) {
227 QJsonObject obj = miscs.array().at(i).toObject(); 227 QJsonObject obj = miscs.array().at(i).toObject();
228 Miscs m; 228 Miscs m;
229 m.m_name = obj["m_name"].toString(); 229 m.m_name = obj["m_name"].toString();
243 const auto& y_json = query.value("json_yeasts").toString(); 243 const auto& y_json = query.value("json_yeasts").toString();
244 if (!y_json.trimmed().isEmpty()) { 244 if (!y_json.trimmed().isEmpty()) {
245 const auto& formattedJson = QString("%1").arg(y_json); 245 const auto& formattedJson = QString("%1").arg(y_json);
246 QJsonDocument yeasts = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 246 QJsonDocument yeasts = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
247 if (parseError.error != QJsonParseError::NoError) { 247 if (parseError.error != QJsonParseError::NoError) {
248 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 248 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
249 } else if (yeasts.isArray()) { 249 } else if (yeasts.isArray()) {
250 for (int i = 0; i < yeasts.array().size(); i++) { 250 for (int i = 0; i < yeasts.array().size(); i++) {
251 QJsonObject obj = yeasts.array().at(i).toObject(); 251 QJsonObject obj = yeasts.array().at(i).toObject();
252 Yeasts y; 252 Yeasts y;
253 /* First some defaults for newer fields. */ 253 /* First some defaults for newer fields. */
324 const auto& ma_json = query.value("json_mashs").toString().trimmed(); 324 const auto& ma_json = query.value("json_mashs").toString().trimmed();
325 if (!ma_json.trimmed().isEmpty()) { 325 if (!ma_json.trimmed().isEmpty()) {
326 const auto& formattedJson = QString("%1").arg(ma_json); 326 const auto& formattedJson = QString("%1").arg(ma_json);
327 QJsonDocument mashs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 327 QJsonDocument mashs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
328 if (parseError.error != QJsonParseError::NoError) { 328 if (parseError.error != QJsonParseError::NoError) {
329 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 329 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
330 } else if (mashs.isArray()) { 330 } else if (mashs.isArray()) {
331 for (int i = 0; i < mashs.array().size(); i++) { 331 for (int i = 0; i < mashs.array().size(); i++) {
332 QJsonObject obj = mashs.array().at(i).toObject(); 332 QJsonObject obj = mashs.array().at(i).toObject();
333 Mashs m; 333 Mashs m;
334 m.step_name = obj["step_name"].toString(); 334 m.step_name = obj["step_name"].toString();
346 } 346 }
347 } else { 347 } else {
348 qDebug() << "empty mashs"; 348 qDebug() << "empty mashs";
349 } 349 }
350 350
351 qDebug() << "loadRecipe" << reci->record << "done"; 351 qInfo() << "loadRecipe" << reci->record << "done";
352 return true; 352 return true;
353 } 353 }
354 354
355 355
356 bool DB_recipe::save(Recipe *reci, QDialog *dialog) 356 bool DB_recipe::save(Recipe *reci, QDialog *dialog)
647 query.bindValue(":recno", reci->record); 647 query.bindValue(":recno", reci->record);
648 } 648 }
649 query.exec(); 649 query.exec();
650 qDebug() << query.lastQuery(); 650 qDebug() << query.lastQuery();
651 if (query.lastError().isValid()) { 651 if (query.lastError().isValid()) {
652 qDebug() << "EditRecipe" << query.lastError(); 652 qWarning() << "EditRecipe" << query.lastError();
653 QMessageBox::warning(dialog, QObject::tr("Database error"), 653 QMessageBox::warning(dialog, QObject::tr("Database error"),
654 QObject::tr("MySQL error: %1\n%2\n%3") 654 QObject::tr("MySQL error: %1\n%2\n%3")
655 .arg(query.lastError().nativeErrorCode()) 655 .arg(query.lastError().nativeErrorCode())
656 .arg(query.lastError().driverText()) 656 .arg(query.lastError().driverText())
657 .arg(query.lastError().databaseText())); 657 .arg(query.lastError().databaseText()));
663 * again, it will be updated instead of inserting a new copy. 663 * again, it will be updated instead of inserting a new copy.
664 */ 664 */
665 if (reci->record < 0) { 665 if (reci->record < 0) {
666 QVariant id = query.lastInsertId(); 666 QVariant id = query.lastInsertId();
667 reci->record = id.toInt(); 667 reci->record = id.toInt();
668 qDebug() << "saveRecipe Inserted record" << reci->record; 668 qInfo() << "saveRecipe Inserted record" << reci->record;
669 } else { 669 } else {
670 qDebug() << "saveRecipe Updated record" << reci->record; 670 qInfo() << "saveRecipe Updated record" << reci->record;
671 } 671 }
672 } 672 }
673 return true; 673 return true;
674 } 674 }
675 675
680 680
681 query.prepare("DELETE FROM recipes WHERE record = :recno"); 681 query.prepare("DELETE FROM recipes WHERE record = :recno");
682 query.bindValue(":recno", recno); 682 query.bindValue(":recno", recno);
683 query.exec(); 683 query.exec();
684 if (query.lastError().isValid()) { 684 if (query.lastError().isValid()) {
685 qDebug() << "deleteRecipe" << query.lastError(); 685 qWarning() << "deleteRecipe" << query.lastError();
686 QMessageBox::warning(dialog, QObject::tr("Database error"), 686 QMessageBox::warning(dialog, QObject::tr("Database error"),
687 QObject::tr("MySQL error: %1\n%2\n%3") 687 QObject::tr("MySQL error: %1\n%2\n%3")
688 .arg(query.lastError().nativeErrorCode()) 688 .arg(query.lastError().nativeErrorCode())
689 .arg(query.lastError().driverText()) 689 .arg(query.lastError().driverText())
690 .arg(query.lastError().databaseText())); 690 .arg(query.lastError().databaseText()));
691 return false; 691 return false;
692 } 692 }
693 693
694 qDebug() << "deleteRecipe Deleted" << recno; 694 qInfo() << "deleteRecipe Deleted" << recno;
695 return true; 695 return true;
696 } 696 }
697 697

mercurial