view src/sqlmodels.cpp @ 255:55581d90c55d

Change API for slCheckAndReportSQLError().
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 10 Oct 2018 13:21:51 +0300
parents 43a5e09bb832
children 0f345852b3ed
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-2018 Tecnic Software productions (TNSP)
//
// Distributed under 3-clause BSD style license, refer to
// included file "COPYING" for exact terms.
//
#include "main.h"
#include "util.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(np, "SLPersonSQLModel::updatePerson()"))
        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(np, "SLPersonSQLModel::addPerson()"))
        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(del, "delete user"))
    {
        QSqlDatabase::database().rollback();
        return -1;
    }

    del.prepare(QStringLiteral("DELETE FROM transactions WHERE person=?"));
    del.addBindValue(id);
    del.exec();

    if (!slCheckAndReportSQLError(del, "delete user transactions"))
    {
        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());
}