2010-07-10 16:34:30 +02:00
|
|
|
/*
|
2016-10-08 20:05:01 +02:00
|
|
|
Copyright 2010-2016 Grégory Soutadé
|
2010-07-10 16:34:30 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
This file is part of KissCount.
|
2010-07-10 16:34:30 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
KissCount 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.
|
2010-07-10 16:34:30 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
KissCount 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.
|
2010-07-10 16:34:30 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
|
2010-07-10 16:34:30 +02:00
|
|
|
*/
|
|
|
|
|
2010-05-14 15:04:01 +02:00
|
|
|
#ifndef DATABASE_H
|
|
|
|
#define DATABASE_H
|
|
|
|
|
|
|
|
#include <list>
|
2011-08-25 17:45:41 +02:00
|
|
|
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
#include <QDate>
|
2012-03-20 20:57:45 +01:00
|
|
|
#include <QDir>
|
2010-05-14 15:04:01 +02:00
|
|
|
|
2011-08-20 14:02:47 +02:00
|
|
|
#include <controller/KissCount.hpp>
|
|
|
|
#include "model.hpp"
|
2010-05-15 11:21:42 +02:00
|
|
|
|
2012-03-11 18:02:54 +01:00
|
|
|
#ifdef WIN32
|
2018-03-11 15:56:01 +01:00
|
|
|
#define BDD_PATH "/KissCount/" // In home directory (C:\Documents and settings\UserX\)
|
2012-03-11 18:02:54 +01:00
|
|
|
#else
|
|
|
|
#define BDD_PATH "/.local/share/kisscount/"
|
|
|
|
#endif
|
2012-02-26 21:16:45 +01:00
|
|
|
#define BDD_FILE "kc.bdd"
|
2012-03-11 18:02:54 +01:00
|
|
|
|
2015-06-20 18:22:51 +02:00
|
|
|
#define INIT_SCRIPT RESOURCES_ROOT "init.sql"
|
2010-05-14 15:04:01 +02:00
|
|
|
|
2010-10-30 20:52:53 +02:00
|
|
|
|
2011-02-13 19:30:12 +01:00
|
|
|
// if (!_db.CheckSyntax(req))
|
|
|
|
// {
|
|
|
|
// wxString s = req;
|
|
|
|
// std::cout << s.mb_str() << " is invalid !\n" ;
|
|
|
|
// code_if_syntax_fail;
|
|
|
|
// return return_value;
|
|
|
|
// }
|
|
|
|
|
|
|
|
#define EXECUTE_SQL_UPDATE_WITH_CODE(req, return_value, code_if_fail, code_if_syntax_fail) \
|
2011-08-25 17:45:41 +02:00
|
|
|
do { \
|
2015-10-11 09:42:32 +02:00
|
|
|
_db.transaction(); \
|
2011-08-25 17:45:41 +02:00
|
|
|
QSqlQuery query; \
|
2011-10-22 11:37:35 +02:00
|
|
|
if (!query.exec(req)) \
|
2011-08-25 17:45:41 +02:00
|
|
|
{ \
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Update failed !\n") + req); \
|
2011-10-31 10:24:05 +01:00
|
|
|
std::cerr << __FUNCTION__ << " " << __FILE__ << " " << __LINE__ << "\n" ; \
|
2011-08-25 17:45:41 +02:00
|
|
|
std::cerr << req.toStdString() << "\n" ; \
|
|
|
|
std::cerr << query.lastError().text().toStdString() << "\n" ; \
|
2015-10-11 09:42:32 +02:00
|
|
|
_db.rollback(); \
|
2011-08-25 17:45:41 +02:00
|
|
|
code_if_fail; \
|
|
|
|
return return_value; \
|
|
|
|
} \
|
2015-10-11 09:42:32 +02:00
|
|
|
_db.commit(); \
|
2011-02-13 19:30:12 +01:00
|
|
|
} while(0);
|
|
|
|
|
2016-10-08 20:05:01 +02:00
|
|
|
#define EXECUTE_PREPARED_SQL_UPDATE_WITH_CODE(return_value, code_if_fail, code_if_syntax_fail) \
|
|
|
|
do { \
|
|
|
|
_db.transaction(); \
|
|
|
|
if (!query.exec()) \
|
|
|
|
{ \
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Update failed !\n")); \
|
|
|
|
std::cerr << __FUNCTION__ << " " << __FILE__ << " " << __LINE__ << "\n" ; \
|
|
|
|
std::cerr << query.lastQuery().toStdString() << "\n" ; \
|
|
|
|
std::cerr << query.lastError().text().toStdString() << "\n" ; \
|
|
|
|
_db.rollback(); \
|
|
|
|
code_if_fail; \
|
|
|
|
return return_value; \
|
|
|
|
} \
|
|
|
|
_db.commit(); \
|
|
|
|
} while(0);
|
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
#define EXECUTE_SQL_QUERY_WITH_CODE(req, return_value, code_if_fail, code_if_syntax_fail) \
|
2015-10-11 09:42:32 +02:00
|
|
|
do { \
|
|
|
|
if (!query.exec(req)) \
|
|
|
|
{ \
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Query failed !\n") + req); \
|
|
|
|
std::cerr << __FUNCTION__ << "\n" ; \
|
|
|
|
std::cerr << req.toStdString() << "\n" ; \
|
|
|
|
std::cerr << query.lastError().text().toStdString() << "\n" ; \
|
|
|
|
code_if_fail; \
|
|
|
|
return return_value; \
|
|
|
|
} \
|
|
|
|
} while(0);
|
2011-02-13 19:30:12 +01:00
|
|
|
|
2016-10-08 20:05:01 +02:00
|
|
|
#define EXECUTE_PREPARED_SQL_QUERY_WITH_CODE(return_value, code_if_fail, code_if_syntax_fail) \
|
|
|
|
do { \
|
|
|
|
if (!query.exec()) \
|
|
|
|
{ \
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Query failed !\n")); \
|
|
|
|
std::cerr << __FUNCTION__ << "\n" ; \
|
|
|
|
std::cerr << query.lastQuery().toStdString() << "\n" ; \
|
|
|
|
std::cerr << query.lastError().text().toStdString() << "\n" ; \
|
|
|
|
code_if_fail; \
|
|
|
|
return return_value; \
|
|
|
|
} \
|
|
|
|
} while(0);
|
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
#define EXECUTE_SQL_QUERY(req, return_value) EXECUTE_SQL_QUERY_WITH_CODE(req, return_value, {}, {})
|
2011-02-13 19:30:12 +01:00
|
|
|
|
|
|
|
#define EXECUTE_SQL_UPDATE(req, return_value) EXECUTE_SQL_UPDATE_WITH_CODE(req, return_value, {}, {})
|
|
|
|
|
2016-10-08 20:05:01 +02:00
|
|
|
#define EXECUTE_PREPARED_SQL_UPDATE(return_value) EXECUTE_PREPARED_SQL_UPDATE_WITH_CODE(return_value, {}, {})
|
|
|
|
|
|
|
|
#define EXECUTE_PREPARED_SQL_QUERY(return_value) EXECUTE_PREPARED_SQL_QUERY_WITH_CODE(return_value, {}, {})
|
|
|
|
|
2010-09-08 11:02:03 +02:00
|
|
|
class KissCount;
|
2010-10-24 16:04:56 +02:00
|
|
|
class User;
|
2010-09-08 11:02:03 +02:00
|
|
|
|
2010-05-14 15:04:01 +02:00
|
|
|
class Database
|
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
public:
|
2011-08-25 17:45:41 +02:00
|
|
|
static const int FIX_OP = (1 << 0);
|
|
|
|
static const int NON_FIX_OP = (1 << 1);
|
2011-08-16 18:22:35 +02:00
|
|
|
static const int CHECKED_OP = (1 << 2);
|
|
|
|
static const int NOT_CHECKED_OP = (1 << 3);
|
|
|
|
static const int ALL_OP = (~0);
|
|
|
|
|
2017-10-13 15:30:18 +02:00
|
|
|
static const int VERSION = 5;
|
2011-08-16 18:22:35 +02:00
|
|
|
|
2010-09-08 11:02:03 +02:00
|
|
|
Database(const char* filename, KissCount* kiss);
|
2010-05-14 15:04:01 +02:00
|
|
|
|
2012-03-20 20:57:45 +01:00
|
|
|
static QString GetDatabaseHome() { return QDir::home().path() + BDD_PATH; }
|
|
|
|
static QString GetDatabaseFile() { return QString(BDD_FILE); }
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
std::list<QString> GetUsers();
|
2011-08-25 17:45:41 +02:00
|
|
|
bool IsValidUser(const QString& user, const QString& password);
|
2010-05-15 11:21:42 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
User* LoadUser(const QString& name);
|
2010-08-26 21:28:15 +02:00
|
|
|
void LoadYear(User* user, int year);
|
2010-06-03 18:28:38 +02:00
|
|
|
|
2011-07-04 20:23:00 +02:00
|
|
|
void UpdateOperation(User* user, Operation& op, bool checkTransfert=true);
|
2011-08-25 17:45:41 +02:00
|
|
|
int AddOperation(User* user, Operation& op, bool checkTransfert=true);
|
2011-02-14 20:56:59 +01:00
|
|
|
void DeleteOperation(User* user, Operation& op);
|
2010-08-26 21:28:15 +02:00
|
|
|
void DeleteOperations(User* user, int month, int year);
|
2011-08-25 17:45:41 +02:00
|
|
|
bool LoadOperation(User* user, int id);
|
2012-04-30 21:15:51 +02:00
|
|
|
int MetaAmount(int id);
|
|
|
|
int MetaPositiveAmount(int id);
|
2010-10-24 16:04:56 +02:00
|
|
|
|
2013-02-17 18:58:32 +01:00
|
|
|
int GetAccountAmount(int id, int month, int year, bool* had_value=0, bool create_if_not_exsits=true);
|
2012-04-30 21:15:51 +02:00
|
|
|
void SetAccountAmount(int accountId, int month, int year, int amount);
|
|
|
|
int CalcAccountAmount(int id, int month, int year, bool* had_values);
|
2010-06-03 18:28:38 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
int AddAccount(User* user, Account& ac);
|
2010-08-26 21:28:15 +02:00
|
|
|
void UpdateAccount(Account& ac);
|
2011-08-25 17:45:41 +02:00
|
|
|
void DeleteAccount(User* user, Account& ac, int replacement);
|
2011-08-27 18:35:36 +02:00
|
|
|
void AddSharedAccount(Account& ac, const QString& granted);
|
|
|
|
void RemoveSharedAccount(Account& ac, int granted);
|
2010-06-21 10:53:43 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
int AddCategory(User* user, Category& category);
|
2010-08-26 21:28:15 +02:00
|
|
|
void UpdateCategory(Category& category);
|
2011-08-25 17:45:41 +02:00
|
|
|
void DeleteCategory(User* user, Category& category, int replacement);
|
2011-08-27 18:35:36 +02:00
|
|
|
bool LoadCategory(int id, const QString& name, Category& category);
|
2010-06-22 11:35:21 +02:00
|
|
|
|
2014-11-10 11:54:28 +01:00
|
|
|
int AddTag(User* user, Tag& tag);
|
|
|
|
void UpdateTag(Tag& tag);
|
|
|
|
void DeleteTag(User* user, Tag& tag, int replacement);
|
|
|
|
bool LoadTag(int id, const QString& name, Tag& tag);
|
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
std::map<int, std::vector<int> > GetAllOperations(User* user);
|
|
|
|
void GenerateMonth(User* user, int monthFrom, int yearFrom, int monthTo, int yearTo);
|
2010-06-23 14:25:00 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
void ChangePassword(User* user, const QString& password);
|
|
|
|
bool UserExists(const QString& name);
|
|
|
|
void ChangeName(User* user, const QString& name);
|
|
|
|
void NewUser(const QString& name);
|
2010-06-27 21:39:49 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
void UpdatePreference(User* user, const QString& preference);
|
2010-07-07 21:04:38 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
std::vector<Operation>* Search(User* user, QString* description, QDate* dateFrom, QDate* dateTo,
|
2012-04-30 21:15:51 +02:00
|
|
|
int* amountFrom, int* amountTo,
|
2014-11-10 11:54:28 +01:00
|
|
|
std::vector<int> categories, int types, std::vector<int> accounts, bool wildcards, std::vector<int> tags);
|
2010-08-17 18:59:19 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
void GetStats(User* user, int monthFrom, int yearFrom, int monthTo,
|
2012-04-30 21:15:51 +02:00
|
|
|
int yearTo, std::map<int, std::map<int, std::map<int, int> > >* accountAmounts,
|
2014-11-10 11:54:28 +01:00
|
|
|
std::map<int, int>* categories, std::map<int, int>* tags);
|
2010-07-14 16:22:02 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
void GetMonthStats(User* user, int month, int year, int nbDays,
|
2012-04-30 21:15:51 +02:00
|
|
|
std::map<int, std::vector<int> >* operations,
|
2014-11-10 11:54:28 +01:00
|
|
|
std::map<int, int>* categories, std::map<int, int>* tags);
|
2010-11-19 19:58:02 +01:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
void KillMe(User* user);
|
2011-08-25 17:45:41 +02:00
|
|
|
bool GetOperation(int id, Operation* op);
|
2011-08-27 18:35:36 +02:00
|
|
|
std::map<QString, QString> getSharedAccountOwners(int account);
|
|
|
|
QString getSharedAccountOwner(int account);
|
2010-10-24 16:04:56 +02:00
|
|
|
|
2012-04-30 21:15:51 +02:00
|
|
|
std::map<int, int>* GetNotChecked(User* user, int month, int year);
|
|
|
|
std::map<int, int>* GetVirtualAmount(User* user, int month, int year);
|
2010-08-21 11:49:03 +02:00
|
|
|
|
2011-03-23 20:35:29 +01:00
|
|
|
void UpdateImportPattern(User* user);
|
|
|
|
|
2012-02-26 21:16:45 +01:00
|
|
|
void GetHistory(int month, int year, QStringList& list);
|
|
|
|
|
2012-03-20 20:57:45 +01:00
|
|
|
bool ChangeDatabase(QString filename);
|
|
|
|
|
2011-02-13 19:30:12 +01:00
|
|
|
/* Database Update */
|
|
|
|
|
|
|
|
void CheckDatabaseVersion();
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
static QString HashPassword(const QString& password);
|
2010-08-26 21:28:15 +02:00
|
|
|
private:
|
2010-09-08 11:02:03 +02:00
|
|
|
KissCount* _kiss;
|
2011-08-25 17:45:41 +02:00
|
|
|
QSqlDatabase _db;
|
2010-05-14 15:04:01 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
void CreateDatabase();
|
2011-02-14 20:56:59 +01:00
|
|
|
void LinkOrUnlinkOperation(User* user, Operation& op);
|
2010-05-14 15:04:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|