KissCount/src/model/Database.hpp

171 lines
7.1 KiB
C++
Raw Normal View History

2010-07-10 16:34:30 +02:00
/*
2011-01-29 13:04:15 +01:00
Copyright 2010-2011 Grégory Soutadé
2010-07-10 16:34:30 +02:00
This file is part of KissCount.
2010-07-10 16:34:30 +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
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
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
*/
#ifndef DATABASE_H
#define DATABASE_H
#include <list>
#include <wx/wxsqlite3.h>
#include <wx/wx.h>
2011-08-20 14:02:47 +02:00
#include <controller/KissCount.hpp>
#include "model.hpp"
2010-05-15 11:21:42 +02:00
2011-02-20 14:22:31 +01:00
#define BDD_FILE "/.kisscount/kc.bdd"
2011-03-06 17:20:42 +01:00
#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{ \
try \
{ \
_db.ExecuteUpdate(req); \
} \
catch (wxSQLite3Exception e) \
{ \
wxMessageBox(_("Update failed !\n") + req, _("Error"), wxICON_ERROR | wxOK); \
std::cerr << __FUNCTION__ << "\n" ; \
std::cerr << req.mb_str() << "\n" ; \
std::cerr << e.GetMessage().mb_str() << "\n" ; \
code_if_fail; \
return return_value; \
} \
} while(0);
#define EXECUTE_SQL_QUERY_WITH_CODE(req, res, return_value, code_if_fail, code_if_syntax_fail) \
do{ \
try \
{ \
res = _db.ExecuteQuery(req); \
} \
catch (wxSQLite3Exception e) \
{ \
wxMessageBox(_("Query failed !\n") + req, _("Error"), wxICON_ERROR | wxOK); \
std::cerr << __FUNCTION__ << "\n" ; \
std::cerr << req.mb_str() << "\n" ; \
std::cerr << e.GetMessage().mb_str() << "\n" ; \
code_if_fail; \
return return_value; \
} \
} while(0);
#define EXECUTE_SQL_QUERY(req, res, return_value) EXECUTE_SQL_QUERY_WITH_CODE(req, res, return_value, {}, {})
#define EXECUTE_SQL_UPDATE(req, return_value) EXECUTE_SQL_UPDATE_WITH_CODE(req, 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
class Database
{
public:
static const int FIX_OP = 1;//(1 << 0);
static const int NON_FIX_OP = 2;//(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;
2010-09-08 11:02:03 +02:00
Database(const char* filename, KissCount* kiss);
std::list<wxString> GetUsers();
bool IsValidUser(const wxString& user, const wxString& password);
2010-05-15 11:21:42 +02:00
User* LoadUser(const wxString& name);
void LoadYear(User* user, int year);
2010-06-03 18:28:38 +02:00
void UpdateOperation(User* user, Operation& op, bool checkTransfert=true);
wxString AddOperation(User* user, Operation& op, bool checkTransfert=true);
2011-02-14 20:56:59 +01:00
void DeleteOperation(User* user, Operation& op);
void DeleteOperations(User* user, int month, int year);
2010-10-24 16:04:56 +02:00
bool LoadOperation(User* user, const wxString& id);
double MetaAmount(const wxString& id);
double MetaPositiveAmount(const wxString& id);
double GetAccountAmount(const wxString& id, int month, int year);
void SetAccountAmount(const wxString& accountId, int month, int year, double amount);
double CalcAccountAmount(const wxString& id, int month, int year, bool* had_values);
2010-06-03 18:28:38 +02:00
wxString AddAccount(User* user, Account& ac);
void UpdateAccount(Account& ac);
void DeleteAccount(User* user, Account& ac, const wxString& replacement);
void AddSharedAccount(Account& ac, const wxString& granted);
2010-10-24 16:04:56 +02:00
void RemoveSharedAccount(Account& ac, const wxString& granted);
2010-06-21 10:53:43 +02:00
wxString AddCategory(User* user, Category& category);
void UpdateCategory(Category& category);
void DeleteCategory(User* user, Category& category, const wxString& replacement);
2010-10-24 16:04:56 +02:00
bool LoadCategory(const wxString& id, const wxString& name, Category& category);
2010-06-22 11:35:21 +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
void ChangePassword(User* user, const wxString& password);
bool UserExists(const wxString& name);
void ChangeName(User* user, const wxString& name);
void NewUser(const wxString& name);
2010-06-27 21:39:49 +02:00
void UpdatePreference(User* user, const wxString& preference);
std::vector<Operation>* Search(User* user, wxString* description, wxDateTime* dateFrom, wxDateTime* dateTo,
wxString* amountFrom, wxString* amountTo,
std::vector<wxString> categories, int types, std::vector<wxString> accounts, bool wildcards);
void GetStats(User* user, const wxString& monthFrom, const wxString& yearFrom, const wxString& monthTo,
const wxString& yearTo, std::map<wxString, std::map<int, std::map<int, double> > >* accountAmounts,
std::map<wxString, double>* categories);
2010-11-19 19:58:02 +01:00
void GetMonthStats(User* user, const wxString& month, const wxString& year, int nbDays,
std::map<wxString, std::vector<double> >* operations,
std::map<wxString, double>* categories);
void KillMe(User* user);
bool GetOperation(const wxString& id, Operation* op);
2010-10-24 16:04:56 +02:00
std::map<wxString, wxString> getSharedAccountOwners(const wxString& account);
wxString getSharedAccountOwner(const wxString& account);
std::map<wxString, double>* GetNotChecked(User* user, int month, int year);
2011-02-14 20:56:59 +01:00
std::map<wxString, double>* GetVirtualAmount(User* user, int month, int year);
2011-03-23 20:35:29 +01:00
void UpdateImportPattern(User* user);
/* Database Update */
void CheckDatabaseVersion();
private:
2010-09-08 11:02:03 +02:00
KissCount* _kiss;
wxSQLite3Database _db;
void CreateDatabase();
wxString HashPassword(const wxString& password);
2011-02-14 20:56:59 +01:00
void LinkOrUnlinkOperation(User* user, Operation& op);
};
#endif