dbactions.cpp 3.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/***
Pix  Copyright (C) 2018  Camilo Higuita
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

 This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
***/

#include "dbactions.h"

#ifdef STATIC_MAUIKIT
#include "fm.h"
#else
#include <MauiKit/fm.h>
#endif

DBActions::DBActions(QObject *parent) : DB(parent) {}

DBActions::~DBActions() {}

void DBActions::init()
{
    qDebug() << "Getting collectionDB info from: " << UNI::CollectionDBPath;

    qDebug()<< "Starting DBActions";
}

DBActions *DBActions::instance = nullptr;

DBActions *DBActions::getInstance()
{
    if(!instance)
    {
        instance = new DBActions();
        qDebug() << "getInstance(): First DBActions instance\n";
        instance->init();
        return instance;
    } else
    {
        qDebug()<< "getInstance(): previous DBActions instance\n";
        return instance;
    }
}

bool DBActions::execQuery(const QString &queryTxt)
{
    auto query = this->getQuery(queryTxt);
    return query.exec();
}

bool DBActions::insertContact(const FMH::MODEL &con)
{

    auto contact = FM::toMap(con);
    contact.insert(FMH::MODEL_NAME[FMH::MODEL_KEY::ADDDATE], QDateTime::currentDateTime().toString(Qt::TextDate));
    contact.insert(FMH::MODEL_NAME[FMH::MODEL_KEY::MODIFIED], QDateTime::currentDateTime().toString(Qt::TextDate));


    return this->insert(UNI::TABLEMAP[UNI::TABLE::CONTACTS], contact);
}


bool DBActions::removeContact(const QString &id)
{
    const auto queryTxt = QString("DELETE FROM contacts WHERE id =  \"%1\"").arg(id);
    return this->execQuery(queryTxt);
}

81 82 83 84 85 86 87 88
bool DBActions::updateContact(const FMH::MODEL &con)
{
    auto contact = con;
    contact.insert(FMH::MODEL_KEY::MODIFIED, QDateTime::currentDateTime().toString(Qt::TextDate));
    QVariantMap where =  {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], con[FMH::MODEL_KEY::ID]}};
    return this->update(UNI::TABLEMAP[UNI::TABLE::CONTACTS], con, where);
}

89 90
bool DBActions::favContact(const QString &id, const bool &fav )
{
91
    if(!this->checkExistance(UNI::TABLEMAP[UNI::TABLE::CONTACTS], FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id))
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        return false;

    const FMH::MODEL faved = {{FMH::MODEL_KEY::FAV, fav ? "1" : "0"}};
    return this->update(UNI::TABLEMAP[UNI::TABLE::CONTACTS], faved, QVariantMap({{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}}) );
}


bool DBActions::isFav(const QString &id)
{
    const auto data = this->getDBData(QString("select * from contacts where id = '%1'").arg(id));

    if (data.isEmpty()) return false;

    return data.first()[FMH::MODEL_KEY::FAV] == "1" ? true : false;
}


FMH::MODEL_LIST DBActions::getDBData(const QString &queryTxt)
{
    FMH::MODEL_LIST mapList;

    auto query = this->getQuery(queryTxt);

    if(query.exec())
    {
        while(query.next())
        {
            FMH::MODEL data;
            for(auto key : FMH::MODEL_NAME.keys())
                if(query.record().indexOf(FMH::MODEL_NAME[key]) > -1)
                    data.insert(key, query.value(FMH::MODEL_NAME[key]).toString());
            mapList<< data;
        }

    }else qDebug()<< query.lastError()<< query.lastQuery();

    return mapList;
}