# HG changeset patch # User Matti Hamalainen # Date 1513589280 -7200 # Node ID 131463be208bfbd4b9e642e174206a3d9102c6fa # Parent 70317bb39f05c1bb7da718b76d6bc900f1e9867a Split the custom SQL models code into sqlmodels.cpp diff -r 70317bb39f05 -r 131463be208b Makefile.gen --- a/Makefile.gen Tue Nov 21 12:31:58 2017 +0200 +++ b/Makefile.gen Mon Dec 18 11:28:00 2017 +0200 @@ -25,7 +25,7 @@ APP_SRC=src/ APP_IMG=img/ APP_BIN=$(BINPATH)Syntilista$(EXEEXT) -APP_OBJS=main.o printing.o resources.o moc_main.o +APP_OBJS=main.o sqlmodels.o printing.o resources.o moc_main.o APP_VERSION := $(shell cat VERSION) comma:= , APP_VERSION_COM := $(subst .,$(comma),$(APP_VERSION)) diff -r 70317bb39f05 -r 131463be208b src/main.cpp --- 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) : diff -r 70317bb39f05 -r 131463be208b src/main.h --- a/src/main.h Tue Nov 21 12:31:58 2017 +0200 +++ b/src/main.h Mon Dec 18 11:28:00 2017 +0200 @@ -80,6 +80,7 @@ // double slMoneyStrToValue(const QString &str); QString slMoneyValueToStr(double val); +QString slMoneyValueToStrSign(double val); QString slCleanupStr(const QString &str); const QDateTime slDateTimeToLocal(const QDateTime &val); const QString slDateTimeToStr(const QDateTime &val); diff -r 70317bb39f05 -r 131463be208b src/sqlmodels.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sqlmodels.cpp Mon Dec 18 11:28:00 2017 +0200 @@ -0,0 +1,170 @@ +// +// Syntilista - debt list/management database program +// Programmed and designed by Matti Hämäläinen +// (C) Copyright 2017 Tecnic Software productions (TNSP) +// +// Distributed under 3-clause BSD style license, refer to +// included file "COPYING" for exact terms. +// +#include "main.h" + + +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()); +}