Mercurial > hg > syntilista
comparison main.cpp @ 13:ca5ce74c0563
Refactoring.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 30 Mar 2017 16:54:11 +0300 |
parents | 07db1a0bbdc7 |
children | 8a6a90ce0e0f |
comparison
equal
deleted
inserted
replaced
12:07db1a0bbdc7 | 13:ca5ce74c0563 |
---|---|
322 { | 322 { |
323 close(); | 323 close(); |
324 } | 324 } |
325 | 325 |
326 | 326 |
327 int SyntilistaMainWindow::deletePerson(qint64 id) | |
328 { | |
329 QSqlDatabase::database().transaction(); | |
330 | |
331 QSqlQuery del; | |
332 del.prepare("DELETE FROM people WHERE id=?"); | |
333 del.addBindValue(id); | |
334 del.exec(); | |
335 if (!checkAndReportSQLError("delete user", del.lastError())) | |
336 { | |
337 QSqlDatabase::database().rollback(); | |
338 return -1; | |
339 } | |
340 | |
341 del.prepare("DELETE FROM transactions WHERE person=?"); | |
342 del.addBindValue(id); | |
343 del.exec(); | |
344 | |
345 if (!checkAndReportSQLError("delete user transactions", del.lastError())) | |
346 { | |
347 QSqlDatabase::database().rollback(); | |
348 return -2; | |
349 } | |
350 | |
351 QSqlDatabase::database().commit(); | |
352 return 0; | |
353 } | |
354 | |
355 | |
356 void SyntilistaMainWindow::on_button_DeletePerson_clicked() | 327 void SyntilistaMainWindow::on_button_DeletePerson_clicked() |
357 { | 328 { |
358 if (currPerson.id <= 0) | 329 if (currPerson.id <= 0) |
359 { | 330 { |
360 statusMsg(tr("Ei valittua henkilöä!")); | 331 statusMsg(tr("Ei valittua henkilöä!")); |
376 arg(info.lastName).arg(info.firstName).arg(info.id), | 347 arg(info.lastName).arg(info.firstName).arg(info.id), |
377 QMessageBox::Yes | QMessageBox::No); | 348 QMessageBox::Yes | QMessageBox::No); |
378 | 349 |
379 if (ret == QMessageBox::Yes) | 350 if (ret == QMessageBox::Yes) |
380 { | 351 { |
381 int rv = deletePerson(info.id); | 352 int rv = model_People->deletePerson(info.id); |
382 if (rv != 0) | 353 if (rv != 0) |
383 { | 354 { |
384 errorMsg(tr("SQL-tietokantavirhe"), | 355 errorMsg(tr("SQL-tietokantavirhe"), |
385 tr("Henkilön tietoja poistettaessa tapahtui virhe #%1."). | 356 tr("Henkilön tietoja poistettaessa tapahtui virhe #%1."). |
386 arg(rv)); | 357 arg(rv)); |
865 | 836 |
866 return value; | 837 return value; |
867 } | 838 } |
868 | 839 |
869 | 840 |
870 void PersonSQLModel::updatePerson(const PersonInfo &info) | 841 int PersonSQLModel::updatePerson(const PersonInfo &info) |
871 { | 842 { |
872 QSqlQuery np; | 843 QSqlQuery np; |
873 | 844 |
874 np.prepare("UPDATE people SET first_name=?,last_name=?,extra_info=?,updated=? WHERE id=?"); | 845 np.prepare("UPDATE people SET first_name=?,last_name=?,extra_info=?,updated=? WHERE id=?"); |
875 np.addBindValue(info.firstName); | 846 np.addBindValue(info.firstName); |
877 np.addBindValue(info.extraInfo); | 848 np.addBindValue(info.extraInfo); |
878 np.addBindValue(QDateTime::currentDateTimeUtc()); | 849 np.addBindValue(QDateTime::currentDateTimeUtc()); |
879 np.addBindValue(info.id); | 850 np.addBindValue(info.id); |
880 np.exec(); | 851 np.exec(); |
881 | 852 |
882 checkAndReportSQLError("PersonSQLModel::updatePerson()", np.lastError()); | 853 if (!checkAndReportSQLError("PersonSQLModel::updatePerson()", np.lastError())) |
854 { | |
855 return -1; | |
856 } | |
857 | |
883 QSqlDatabase::database().commit(); | 858 QSqlDatabase::database().commit(); |
884 | 859 |
885 updateModel(); | 860 updateModel(); |
886 } | 861 return 0; |
887 | 862 } |
888 | 863 |
889 void PersonSQLModel::addPerson(const PersonInfo &info) | 864 |
865 int PersonSQLModel::addPerson(const PersonInfo &info) | |
890 { | 866 { |
891 // beginInsertRows(QModelIndex(), rowCount(), rowCount()); | 867 // beginInsertRows(QModelIndex(), rowCount(), rowCount()); |
892 | 868 |
893 QSqlQuery np; | 869 QSqlQuery np; |
894 np.prepare("INSERT INTO people (first_name,last_name,extra_info,added,updated) VALUES (?,?,?,?,?)"); | 870 np.prepare("INSERT INTO people (first_name,last_name,extra_info,added,updated) VALUES (?,?,?,?,?)"); |
897 np.addBindValue(info.extraInfo); | 873 np.addBindValue(info.extraInfo); |
898 np.addBindValue(QDateTime::currentDateTimeUtc()); | 874 np.addBindValue(QDateTime::currentDateTimeUtc()); |
899 np.addBindValue(QDateTime::currentDateTimeUtc()); | 875 np.addBindValue(QDateTime::currentDateTimeUtc()); |
900 np.exec(); | 876 np.exec(); |
901 | 877 |
902 checkAndReportSQLError("PersonSQLModel::addPerson()", np.lastError()); | 878 if (!checkAndReportSQLError("PersonSQLModel::addPerson()", np.lastError())) |
879 { | |
880 return -1; | |
881 } | |
882 | |
903 QSqlDatabase::database().commit(); | 883 QSqlDatabase::database().commit(); |
904 | 884 |
905 // endInsertRows(); | 885 // endInsertRows(); |
906 updateModel(); | 886 updateModel(); |
887 return 0; | |
888 } | |
889 | |
890 | |
891 int PersonSQLModel::deletePerson(qint64 id) | |
892 { | |
893 QSqlDatabase::database().transaction(); | |
894 QSqlQuery del; | |
895 | |
896 del.prepare("DELETE FROM people WHERE id=?"); | |
897 del.addBindValue(id); | |
898 del.exec(); | |
899 | |
900 if (!checkAndReportSQLError("delete user", del.lastError())) | |
901 { | |
902 QSqlDatabase::database().rollback(); | |
903 return -1; | |
904 } | |
905 | |
906 del.prepare("DELETE FROM transactions WHERE person=?"); | |
907 del.addBindValue(id); | |
908 del.exec(); | |
909 | |
910 if (!checkAndReportSQLError("delete user transactions", del.lastError())) | |
911 { | |
912 QSqlDatabase::database().rollback(); | |
913 return -2; | |
914 } | |
915 | |
916 QSqlDatabase::database().commit(); | |
917 updateModel(); | |
918 return 0; | |
907 } | 919 } |
908 | 920 |
909 | 921 |
910 void PersonSQLModel::updateModel() | 922 void PersonSQLModel::updateModel() |
911 { | 923 { |