view src/sqlmodels.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
children 58af72da7f60
line wrap: on
line source

//
// Syntilista - debt list/management database program
// Programmed and designed by Matti Hämäläinen <ccr@tnsp.org>
// (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());
}