354 |
354 |
355 |
355 |
356 bool DB_recipe::save(Recipe *reci, QDialog *dialog) |
356 bool DB_recipe::save(Recipe *reci, QDialog *dialog) |
357 { |
357 { |
358 QSqlQuery query; |
358 QSqlQuery query; |
|
359 QString sql = ""; |
359 |
360 |
360 if (reci->record == -1) { |
361 if (reci->record == -1) { |
361 query.prepare("INSERT INTO recipes SET locked=:locked, st_name=:st_name, st_letter=:st_letter, " |
362 sql = "INSERT INTO recipes SET "; |
|
363 } else { |
|
364 sql = "UPDATE recipes SET "; |
|
365 } |
|
366 sql.append("locked=:locked, st_name=:st_name, st_letter=:st_letter, " |
362 "st_guide=:st_guide, st_category=:st_category, st_category_number=:st_catnr, st_type=:st_type, " |
367 "st_guide=:st_guide, st_category=:st_category, st_category_number=:st_catnr, st_type=:st_type, " |
363 "st_og_min=:st_og_min, st_og_max=:st_og_max, st_fg_min=:st_fg_min, st_fg_max=:st_fg_max, " |
368 "st_og_min=:st_og_min, st_og_max=:st_og_max, st_fg_min=:st_fg_min, st_fg_max=:st_fg_max, " |
364 "st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, " |
369 "st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, " |
365 "st_carb_min=:st_carb_min, st_carb_max=:st_carb_max, st_abv_min=:st_abv_min, st_abv_max=:st_abv_max, " |
370 "st_carb_min=:st_carb_min, st_carb_max=:st_carb_max, st_abv_min=:st_abv_min, st_abv_max=:st_abv_max, " |
366 "name=:name, notes=:notes, type=:type, batch_size=:batch_size, boil_size=:boil_size, " |
371 "name=:name, notes=:notes, type=:type, batch_size=:batch_size, boil_size=:boil_size, " |
381 "wg_total_alkalinity=:wg_total_alkalinity, wg_ph=:wg_ph, " |
386 "wg_total_alkalinity=:wg_total_alkalinity, wg_ph=:wg_ph, " |
382 "wb_calcium=:wb_calcium, wb_sulfate=:wb_sulfate, wb_chloride=:wb_chloride, wb_sodium=:wb_sodium, " |
387 "wb_calcium=:wb_calcium, wb_sulfate=:wb_sulfate, wb_chloride=:wb_chloride, wb_sodium=:wb_sodium, " |
383 "wb_magnesium=:wb_magnesium, wb_total_alkalinity=:wb_total_alkalinity, wb_ph=:wb_ph, " |
388 "wb_magnesium=:wb_magnesium, wb_total_alkalinity=:wb_total_alkalinity, wb_ph=:wb_ph, " |
384 "wa_acid_name=:wa_acid_name, wa_acid_perc=:wa_acid_perc, wa_base_name=:wa_base_name, " |
389 "wa_acid_name=:wa_acid_name, wa_acid_perc=:wa_acid_perc, wa_base_name=:wa_base_name, " |
385 "json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, " |
390 "json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, " |
386 "json_yeasts=:json_yeasts, json_mashs=:json_mashs, uuid=:uuid"); |
391 "json_yeasts=:json_yeasts, json_mashs=:json_mashs"); |
387 } else { |
392 if (reci->record == -1) { |
388 query.prepare("UPDATE recipes SET locked=:locked, st_name=:st_name, st_letter=:st_letter, " |
393 sql.append(", uuid=:uuid"); |
389 "st_guide=:st_guide, st_category=:st_category, st_category_number=:st_catnr, st_type=:st_type, " |
394 } else { |
390 "st_og_min=:st_og_min, st_og_max=:st_og_max, st_fg_min=:st_fg_min, st_fg_max=:st_fg_max, " |
395 sql.append(" WHERE record = :recno"); |
391 "st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, " |
396 } |
392 "st_carb_min=:st_carb_min, st_carb_max=:st_carb_max, st_abv_min=:st_abv_min, st_abv_max=:st_abv_max, " |
397 |
393 "name=:name, notes=:notes, type=:type, batch_size=:batch_size, boil_size=:boil_size, " |
398 query.prepare(sql); |
394 "boil_time=:boil_time, efficiency=:efficiency, est_og=:est_og, est_fg=:est_fg, est_abv=:est_abv, " |
|
395 "est_color=:est_color, color_method=:color_method, est_ibu=:est_ibu, ibu_method=:ibu_method, " |
|
396 "est_carb=:est_carb, sparge_temp=:sparge_temp, sparge_ph=:sparge_ph, " |
|
397 "sparge_volume=:sparge_volume, sparge_source=:sparge_source, sparge_acid_type=:sparge_acid_type, " |
|
398 "sparge_acid_perc=:sparge_acid_perc, sparge_acid_amount=:sparge_acid_amount, mash_ph=:mash_ph, " |
|
399 "mash_name=:mash_name, calc_acid=:calc_acid, " |
|
400 "w1_name=:w1_name, w1_amount=:w1_amount, w1_calcium=:w1_calcium, w1_sulfate=:w1_sulfate, " |
|
401 "w1_chloride=:w1_chloride, w1_sodium=:w1_sodium, w1_magnesium=:w1_magnesium, " |
|
402 "w1_total_alkalinity=:w1_total_alkalinity, w1_ph=:w1_ph, w1_cost=:w1_cost, " |
|
403 "w2_name=:w2_name, w2_amount=:w2_amount, w2_calcium=:w2_calcium, w2_sulfate=:w2_sulfate, " |
|
404 "w2_chloride=:w2_chloride, w2_sodium=:w2_sodium, w2_magnesium=:w2_magnesium, " |
|
405 "w2_total_alkalinity=:w2_total_alkalinity, w2_ph=:w2_ph, w2_cost=:w2_cost, " |
|
406 "wg_amount=:wg_amount, wg_calcium=:wg_calcium, wg_sulfate=:wg_sulfate, " |
|
407 "wg_chloride=:wg_chloride, wg_sodium=:wg_sodium, wg_magnesium=:wg_magnesium, " |
|
408 "wg_total_alkalinity=:wg_total_alkalinity, wg_ph=:wg_ph, " |
|
409 "wb_calcium=:wb_calcium, wb_sulfate=:wb_sulfate, wb_chloride=:wb_chloride, wb_sodium=:wb_sodium, " |
|
410 "wb_magnesium=:wb_magnesium, wb_total_alkalinity=:wb_total_alkalinity, wb_ph=:wb_ph, " |
|
411 "wa_acid_name=:wa_acid_name, wa_acid_perc=:wa_acid_perc, wa_base_name=:wa_base_name, " |
|
412 "json_fermentables=:json_fermentables, json_hops=:json_hops, json_miscs=:json_miscs, " |
|
413 "json_yeasts=:json_yeasts, json_mashs=:json_mashs WHERE record = :recno"); |
|
414 } |
|
415 query.bindValue(":locked", reci->locked ? 1:0); |
399 query.bindValue(":locked", reci->locked ? 1:0); |
416 query.bindValue(":st_name", reci->st_name); |
400 query.bindValue(":st_name", reci->st_name); |
417 query.bindValue(":st_letter", reci->st_letter); |
401 query.bindValue(":st_letter", reci->st_letter); |
418 query.bindValue(":st_guide", reci->st_guide); |
402 query.bindValue(":st_guide", reci->st_guide); |
419 query.bindValue(":st_category", reci->st_category); |
403 query.bindValue(":st_category", reci->st_category); |
645 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
629 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); |
646 } else { |
630 } else { |
647 query.bindValue(":recno", reci->record); |
631 query.bindValue(":recno", reci->record); |
648 } |
632 } |
649 query.exec(); |
633 query.exec(); |
650 qDebug() << query.lastQuery(); |
|
651 if (query.lastError().isValid()) { |
634 if (query.lastError().isValid()) { |
652 qWarning() << "EditRecipe" << query.lastError(); |
635 qWarning() << "saveRecipe" << query.lastError(); |
653 QMessageBox::warning(dialog, QObject::tr("Database error"), |
636 QMessageBox::warning(dialog, QObject::tr("Database error"), |
654 QObject::tr("MySQL error: %1\n%2\n%3") |
637 QObject::tr("MySQL error: %1\n%2\n%3") |
655 .arg(query.lastError().nativeErrorCode()) |
638 .arg(query.lastError().nativeErrorCode()) |
656 .arg(query.lastError().driverText()) |
639 .arg(query.lastError().driverText()) |
657 .arg(query.lastError().databaseText())); |
640 .arg(query.lastError().databaseText())); |
658 return false; |
641 return false; |
659 } else { |
642 } |
660 /* |
643 |
661 * If this was a new recipe, find out what record number we |
644 /* |
662 * have got and set it. So when the user saves this record |
645 * If this was a new recipe, find out what record number we |
663 * again, it will be updated instead of inserting a new copy. |
646 * have got and set it. So when the user saves this record |
664 */ |
647 * again, it will be updated instead of inserting a new copy. |
665 if (reci->record < 0) { |
648 */ |
666 QVariant id = query.lastInsertId(); |
649 if (reci->record < 0) { |
667 reci->record = id.toInt(); |
650 QVariant id = query.lastInsertId(); |
668 qInfo() << "saveRecipe Inserted record" << reci->record; |
651 reci->record = id.toInt(); |
669 } else { |
652 qInfo() << "saveRecipe Inserted record" << reci->record; |
670 qInfo() << "saveRecipe Updated record" << reci->record; |
653 } else { |
671 } |
654 qInfo() << "saveRecipe Updated record" << reci->record; |
672 } |
655 } |
673 return true; |
656 return true; |
674 } |
657 } |
675 |
658 |
676 |
659 |