/*
Copyright 2010-2011 Grégory Soutadé
This file is part of KissCount.
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.
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.
You should have received a copy of the GNU General Public License
along with KissCount. If not, see .
*/
#ifndef DATABASE_H
#define DATABASE_H
#include
#include
#include
#include
#include "model.hpp"
#define BDD_FILE "/.kisscount/kc.bdd"
#define INIT_SCRIPT RESSOURCES_ROOT "init.sql"
// 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) \
do { \
QSqlQuery query; \
if (!query.exec(req)) \
{ \
QMessageBox::critical(0, _("Error"), _("Update 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);
#define EXECUTE_SQL_QUERY_WITH_CODE(req, return_value, code_if_fail, code_if_syntax_fail) \
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; \
} \
#define EXECUTE_SQL_QUERY(req, return_value) EXECUTE_SQL_QUERY_WITH_CODE(req, return_value, {}, {})
#define EXECUTE_SQL_UPDATE(req, return_value) EXECUTE_SQL_UPDATE_WITH_CODE(req, return_value, {}, {})
class KissCount;
class User;
class Database
{
public:
static const int FIX_OP = (1 << 0);
static const int NON_FIX_OP = (1 << 1);
static const int CHECKED_OP = (1 << 2);
static const int NOT_CHECKED_OP = (1 << 3);
static const int ALL_OP = (~0);
static const int VERSION = 2;
Database(const char* filename, KissCount* kiss);
std::list GetUsers();
bool IsValidUser(const QString& user, const QString& password);
User* LoadUser(const QString& name);
void LoadYear(User* user, int year);
void UpdateOperation(User* user, Operation& op, bool checkTransfert=true);
int AddOperation(User* user, Operation& op, bool checkTransfert=true);
void DeleteOperation(User* user, Operation& op);
void DeleteOperations(User* user, int month, int year);
bool LoadOperation(User* user, int id);
double MetaAmount(int id);
double MetaPositiveAmount(int id);
double GetAccountAmount(int id, int month, int year);
void SetAccountAmount(int accountId, int month, int year, double amount);
double CalcAccountAmount(int id, int month, int year, bool* had_values);
int AddAccount(User* user, Account& ac);
void UpdateAccount(Account& ac);
void DeleteAccount(User* user, Account& ac, int replacement);
void AddSharedAccount(Account& ac, const QString& granted);
void RemoveSharedAccount(Account& ac, int granted);
int AddCategory(User* user, Category& category);
void UpdateCategory(Category& category);
void DeleteCategory(User* user, Category& category, int replacement);
bool LoadCategory(int id, const QString& name, Category& category);
std::map > GetAllOperations(User* user);
void GenerateMonth(User* user, int monthFrom, int yearFrom, int monthTo, int yearTo);
void ChangePassword(User* user, const QString& password);
bool UserExists(const QString& name);
void ChangeName(User* user, const QString& name);
void NewUser(const QString& name);
void UpdatePreference(User* user, const QString& preference);
std::vector* Search(User* user, QString* description, QDate* dateFrom, QDate* dateTo,
int* amountFrom, int* amountTo,
std::vector categories, int types, std::vector accounts, bool wildcards);
void GetStats(User* user, int monthFrom, int yearFrom, int monthTo,
int yearTo, std::map > >* accountAmounts,
std::map* categories);
void GetMonthStats(User* user, int month, int year, int nbDays,
std::map >* operations,
std::map* categories);
void KillMe(User* user);
bool GetOperation(int id, Operation* op);
std::map getSharedAccountOwners(int account);
QString getSharedAccountOwner(int account);
std::map* GetNotChecked(User* user, int month, int year);
std::map* GetVirtualAmount(User* user, int month, int year);
void UpdateImportPattern(User* user);
/* Database Update */
void CheckDatabaseVersion();
static QString HashPassword(const QString& password);
private:
KissCount* _kiss;
QSqlDatabase _db;
void CreateDatabase();
void LinkOrUnlinkOperation(User* user, Operation& op);
};
#endif