src/database/db_product.cpp

changeset 256
c136bd15b084
parent 248
1a7a5dffba58
child 262
6bab9440aeb9
equal deleted inserted replaced
255:f237db73d4c8 256:c136bd15b084
25 25
26 query.prepare("SELECT * FROM products WHERE record = :recno"); 26 query.prepare("SELECT * FROM products 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() << "loadProduct seek error record" << recno; 30 qWarning() << "loadProduct 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
269 const auto& f_json = query.value("json_fermentables").toString(); 269 const auto& f_json = query.value("json_fermentables").toString();
270 if (!f_json.trimmed().isEmpty()) { 270 if (!f_json.trimmed().isEmpty()) {
271 const auto& formattedJson = QString("%1").arg(f_json); 271 const auto& formattedJson = QString("%1").arg(f_json);
272 QJsonDocument fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 272 QJsonDocument fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
273 if (parseError.error != QJsonParseError::NoError) { 273 if (parseError.error != QJsonParseError::NoError) {
274 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 274 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
275 } else if (fermentables.isArray()) { 275 } else if (fermentables.isArray()) {
276 double percentcheck = 0; 276 double percentcheck = 0;
277 for (int i = 0; i < fermentables.array().size(); i++) { 277 for (int i = 0; i < fermentables.array().size(); i++) {
278 QJsonObject obj = fermentables.array().at(i).toObject(); 278 QJsonObject obj = fermentables.array().at(i).toObject();
279 //qDebug() << i << obj;
280 Fermentables f; 279 Fermentables f;
281 f.f_inventory = 0; 280 f.f_inventory = 0;
282 f.f_avail = false; 281 f.f_avail = false;
283 f.f_name = obj["f_name"].toString(); 282 f.f_name = obj["f_name"].toString();
284 f.f_origin = obj["f_origin"].toString(); 283 f.f_origin = obj["f_origin"].toString();
327 } 326 }
328 for (int i = 0; i < prod->fermentables.size(); i++) { 327 for (int i = 0; i < prod->fermentables.size(); i++) {
329 if (prod->fermentables.at(i).f_added < 4) 328 if (prod->fermentables.at(i).f_added < 4)
330 prod->fermentables[i].f_percentage = round((prod->fermentables.at(i).f_amount / total) * 10000.0) / 100.0; 329 prod->fermentables[i].f_percentage = round((prod->fermentables.at(i).f_amount / total) * 10000.0) / 100.0;
331 } 330 }
332 qDebug() << " fixed missing percentages"; 331 qInfo() << " fixed missing percentages";
333 // is_changed(); 332 // is_changed();
334 } 333 }
335 } 334 }
336 } else { 335 } else {
337 qDebug() << "empty fermentables"; 336 qDebug() << "empty fermentables";
340 const auto& h_json = query.value("json_hops").toString(); 339 const auto& h_json = query.value("json_hops").toString();
341 if (!h_json.trimmed().isEmpty()) { 340 if (!h_json.trimmed().isEmpty()) {
342 const auto& formattedJson = QString("%1").arg(h_json); 341 const auto& formattedJson = QString("%1").arg(h_json);
343 QJsonDocument hops = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 342 QJsonDocument hops = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
344 if (parseError.error != QJsonParseError::NoError) { 343 if (parseError.error != QJsonParseError::NoError) {
345 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset; 344 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset;
346 } else if (hops.isArray()) { 345 } else if (hops.isArray()) {
347 for (int i = 0; i < hops.array().size(); i++) { 346 for (int i = 0; i < hops.array().size(); i++) {
348 QJsonObject obj = hops.array().at(i).toObject(); 347 QJsonObject obj = hops.array().at(i).toObject();
349 //qDebug() << i << obj;
350 Hops h; 348 Hops h;
351 h.h_avail = false; 349 h.h_avail = false;
352 h.h_inventory = 0; 350 h.h_inventory = 0;
353 h.h_name = obj["h_name"].toString(); 351 h.h_name = obj["h_name"].toString();
354 h.h_origin = obj["h_origin"].toString(); 352 h.h_origin = obj["h_origin"].toString();
386 const auto& m_json = query.value("json_miscs").toString(); 384 const auto& m_json = query.value("json_miscs").toString();
387 if (!m_json.trimmed().isEmpty()) { 385 if (!m_json.trimmed().isEmpty()) {
388 const auto& formattedJson = QString("%1").arg(m_json); 386 const auto& formattedJson = QString("%1").arg(m_json);
389 QJsonDocument miscs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 387 QJsonDocument miscs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
390 if (parseError.error != QJsonParseError::NoError) { 388 if (parseError.error != QJsonParseError::NoError) {
391 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset; 389 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset;
392 } else if (miscs.isArray()) { 390 } else if (miscs.isArray()) {
393 for (int i = 0; i < miscs.array().size(); i++) { 391 for (int i = 0; i < miscs.array().size(); i++) {
394 QJsonObject obj = miscs.array().at(i).toObject(); 392 QJsonObject obj = miscs.array().at(i).toObject();
395 //qDebug() << i << obj;
396 Miscs m; 393 Miscs m;
397 m.m_avail = false; 394 m.m_avail = false;
398 m.m_inventory = 0; 395 m.m_inventory = 0;
399 m.m_name = obj["m_name"].toString(); 396 m.m_name = obj["m_name"].toString();
400 m.m_amount = obj["m_amount"].toDouble(); 397 m.m_amount = obj["m_amount"].toDouble();
422 const auto& y_json = query.value("json_yeasts").toString(); 419 const auto& y_json = query.value("json_yeasts").toString();
423 if (!y_json.trimmed().isEmpty()) { 420 if (!y_json.trimmed().isEmpty()) {
424 const auto& formattedJson = QString("%1").arg(y_json); 421 const auto& formattedJson = QString("%1").arg(y_json);
425 QJsonDocument yeasts = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 422 QJsonDocument yeasts = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
426 if (parseError.error != QJsonParseError::NoError) { 423 if (parseError.error != QJsonParseError::NoError) {
427 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 424 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
428 } else if (yeasts.isArray()) { 425 } else if (yeasts.isArray()) {
429 for (int i = 0; i < yeasts.array().size(); i++) { 426 for (int i = 0; i < yeasts.array().size(); i++) {
430 QJsonObject obj = yeasts.array().at(i).toObject(); 427 QJsonObject obj = yeasts.array().at(i).toObject();
431 //qDebug() << i << obj;
432 Yeasts y; 428 Yeasts y;
433 /* First some defaults for newer fields. */ 429 /* First some defaults for newer fields. */
434 y.y_tolerance = y.y_harvest_time = y.y_pitch_temperature = y.y_zymocide = 0; 430 y.y_tolerance = y.y_harvest_time = y.y_pitch_temperature = y.y_zymocide = 0;
435 y.y_avail = y.y_sta1 = y.y_bacteria = y.y_harvest_top = y.y_pofpos = false; 431 y.y_avail = y.y_sta1 = y.y_bacteria = y.y_harvest_top = y.y_pofpos = false;
436 y.y_gr_hl_lo = 50; 432 y.y_gr_hl_lo = 50;
506 const auto& ma_json = query.value("json_mashs").toString().trimmed(); 502 const auto& ma_json = query.value("json_mashs").toString().trimmed();
507 if (!ma_json.trimmed().isEmpty()) { 503 if (!ma_json.trimmed().isEmpty()) {
508 const auto& formattedJson = QString("%1").arg(ma_json); 504 const auto& formattedJson = QString("%1").arg(ma_json);
509 QJsonDocument mashs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); 505 QJsonDocument mashs = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
510 if (parseError.error != QJsonParseError::NoError) { 506 if (parseError.error != QJsonParseError::NoError) {
511 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 507 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
512 } else if (mashs.isArray()) { 508 } else if (mashs.isArray()) {
513 for (int i = 0; i < mashs.array().size(); i++) { 509 for (int i = 0; i < mashs.array().size(); i++) {
514 QJsonObject obj = mashs.array().at(i).toObject(); 510 QJsonObject obj = mashs.array().at(i).toObject();
515 qDebug() << i << obj;
516 Mashs m; 511 Mashs m;
517 m.step_name = obj["step_name"].toString(); 512 m.step_name = obj["step_name"].toString();
518 m.step_type = obj["step_type"].toInt(); 513 m.step_type = obj["step_type"].toInt();
519 m.step_volume = obj["step_volume"].toDouble(); 514 m.step_volume = obj["step_volume"].toDouble();
520 m.step_infuse_amount = obj["step_infuse_amount"].toDouble(); 515 m.step_infuse_amount = obj["step_infuse_amount"].toDouble();
1059 query.bindValue(":recno", prod->record); 1054 query.bindValue(":recno", prod->record);
1060 } 1055 }
1061 query.exec(); 1056 query.exec();
1062 qDebug() << query.lastQuery(); 1057 qDebug() << query.lastQuery();
1063 if (query.lastError().isValid()) { 1058 if (query.lastError().isValid()) {
1064 qDebug() << "saveProduct" << query.lastError(); 1059 qWarning() << "saveProduct" << query.lastError();
1065 QMessageBox::warning(dialog, QObject::tr("Database error"), 1060 QMessageBox::warning(dialog, QObject::tr("Database error"),
1066 QObject::tr("MySQL error: %1\n%2\n%3") 1061 QObject::tr("MySQL error: %1\n%2\n%3")
1067 .arg(query.lastError().nativeErrorCode()) 1062 .arg(query.lastError().nativeErrorCode())
1068 .arg(query.lastError().driverText()) 1063 .arg(query.lastError().driverText())
1069 .arg(query.lastError().databaseText())); 1064 .arg(query.lastError().databaseText()));
1075 * again, it will be updated instead of inserting a new copy. 1070 * again, it will be updated instead of inserting a new copy.
1076 */ 1071 */
1077 if (prod->record < 0) { 1072 if (prod->record < 0) {
1078 QVariant id = query.lastInsertId(); 1073 QVariant id = query.lastInsertId();
1079 prod->record = id.toInt(); 1074 prod->record = id.toInt();
1080 qDebug() << "saveProduct Inserted record" << prod->record; 1075 qInfo() << "saveProduct Inserted record" << prod->record;
1081 } else { 1076 } else {
1082 qDebug() << "saveProduct Updated record" << prod->record; 1077 qInfo() << "saveProduct Updated record" << prod->record;
1083 } 1078 }
1084 } 1079 }
1085 return true; 1080 return true;
1086 } 1081 }
1087 1082
1092 1087
1093 query.prepare("DELETE FROM products WHERE record = :recno"); 1088 query.prepare("DELETE FROM products WHERE record = :recno");
1094 query.bindValue(":recno", recno); 1089 query.bindValue(":recno", recno);
1095 query.exec(); 1090 query.exec();
1096 if (query.lastError().isValid()) { 1091 if (query.lastError().isValid()) {
1097 qDebug() << "deleteProduct" << query.lastError(); 1092 qWarning() << "deleteProduct" << query.lastError();
1098 QMessageBox::warning(dialog, QObject::tr("Database error"), 1093 QMessageBox::warning(dialog, QObject::tr("Database error"),
1099 QObject::tr("MySQL error: %1\n%2\n%3") 1094 QObject::tr("MySQL error: %1\n%2\n%3")
1100 .arg(query.lastError().nativeErrorCode()) 1095 .arg(query.lastError().nativeErrorCode())
1101 .arg(query.lastError().driverText()) 1096 .arg(query.lastError().driverText())
1102 .arg(query.lastError().databaseText())); 1097 .arg(query.lastError().databaseText()));
1103 return false; 1098 return false;
1104 } 1099 }
1105 1100
1106 qDebug() << "deleteProduct Deleted" << recno; 1101 qInfo() << "deleteProduct Deleted" << recno;
1107 return true; 1102 return true;
1108 } 1103 }
1109 1104
1110 1105

mercurial