Mercurial > hg > syntilista
diff src/main.cpp @ 213:131463be208b
Split the custom SQL models code into sqlmodels.cpp
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 18 Dec 2017 11:28:00 +0200 |
parents | 70317bb39f05 |
children | 8b9d55fb8988 |
line wrap: on
line diff
--- a/src/main.cpp Tue Nov 21 12:31:58 2017 +0200 +++ b/src/main.cpp Mon Dec 18 11:28:00 2017 +0200 @@ -1508,170 +1508,6 @@ // -// Custom SQL models -// -SLPersonSQLModel::SLPersonSQLModel(QObject *parent) : QSqlQueryModel(parent) -{ -} - - -QVariant SLPersonSQLModel::data(const QModelIndex &index, int role) const -{ - QVariant value = QSqlQueryModel::data(index, role); - - if (value.isValid() && role == Qt::DisplayRole) - { - // Format some of the displayed values - switch (index.column()) - { - case 3: - return slMoneyValueToStr(value.toDouble()); - - case 4: - return slDateTimeToStr(value.toDateTime()); - } - } - - if (index.column() == 3 && role == Qt::ForegroundRole) - { - // Use fancy coloring for debt - double val = QSqlQueryModel::data(index, Qt::DisplayRole).toDouble(); - if (val < 0) - return QVariant::fromValue(QColor(Qt::red)); - else - return QVariant::fromValue(QColor(Qt::green)); - } - - return value; -} - - -int SLPersonSQLModel::updatePerson(const SLPersonInfo &info) -{ - QSqlQuery np; - - np.prepare(QStringLiteral("UPDATE people SET first_name=?,last_name=?,extra_info=?,updated=? WHERE id=?")); - np.addBindValue(info.firstName); - np.addBindValue(info.lastName); - np.addBindValue(info.extraInfo); - np.addBindValue(QDateTime::currentDateTimeUtc()); - np.addBindValue(info.id); - np.exec(); - - if (!slCheckAndReportSQLError("SLPersonSQLModel::updatePerson()", np.lastError())) - return -1; - - QSqlDatabase::database().commit(); - updateModel(); - return 0; -} - - -qint64 SLPersonSQLModel::addPerson(const SLPersonInfo &info) -{ - QSqlQuery np; - np.prepare(QStringLiteral("INSERT INTO people (first_name,last_name,extra_info,added,updated) VALUES (?,?,?,?,?)")); - np.addBindValue(info.firstName); - np.addBindValue(info.lastName); - np.addBindValue(info.extraInfo); - np.addBindValue(QDateTime::currentDateTimeUtc()); - np.addBindValue(QDateTime::currentDateTimeUtc()); - np.exec(); - - if (!slCheckAndReportSQLError("SLPersonSQLModel::addPerson()", np.lastError())) - return -1; - - QSqlDatabase::database().commit(); - - QVariant idp = np.lastInsertId(); - - updateModel(); - return idp.isValid() ? idp.toInt() : -2; -} - - -int SLPersonSQLModel::deletePerson(qint64 id) -{ - QSqlDatabase::database().transaction(); - QSqlQuery del; - - del.prepare(QStringLiteral("DELETE FROM people WHERE id=?")); - del.addBindValue(id); - del.exec(); - - if (!slCheckAndReportSQLError("delete user", del.lastError())) - { - QSqlDatabase::database().rollback(); - return -1; - } - - del.prepare(QStringLiteral("DELETE FROM transactions WHERE person=?")); - del.addBindValue(id); - del.exec(); - - if (!slCheckAndReportSQLError("delete user transactions", del.lastError())) - { - QSqlDatabase::database().rollback(); - return -2; - } - - QSqlDatabase::database().commit(); - updateModel(); - return 0; -} - - -void SLPersonSQLModel::updateModel() -{ - query().exec(); - emit dataChanged(index(0, 0), index(rowCount(), columnCount())); -} - - -SLTransactionSQLModel::SLTransactionSQLModel(QObject *parent) : QSqlQueryModel(parent) -{ -} - - -QVariant SLTransactionSQLModel::data(const QModelIndex &index, int role) const -{ - QVariant value = QSqlQueryModel::data(index, role); - - if (value.isValid() && role == Qt::DisplayRole) - { - // Format some of the displayed values - switch (index.column()) - { - case 1: - return slMoneyValueToStrSign(value.toDouble()); - - case 2: - return slDateTimeToStr(value.toDateTime()); - } - } - - if (index.column() == 1 && role == Qt::ForegroundRole) - { - // Use fancy coloring for debt - double val = QSqlQueryModel::data(index, Qt::DisplayRole).toDouble(); - if (val < 0) - return QVariant::fromValue(QColor(Qt::red)); - else - return QVariant::fromValue(QColor(Qt::green)); - } - - return value; -} - - -void SLTransactionSQLModel::updateModel() -{ - query().exec(); - emit dataChanged(QModelIndex(), QModelIndex()); -} - - -// // About window // AboutWindow::AboutWindow(QWidget *parent) :