wip
This commit is contained in:
@@ -8,6 +8,20 @@
|
||||
// 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)\
|
||||
{\
|
||||
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\
|
||||
@@ -24,6 +38,8 @@
|
||||
|
||||
#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, {}, {})
|
||||
|
||||
Database::Database()
|
||||
{
|
||||
std::ifstream bdd_file;
|
||||
@@ -202,7 +218,7 @@ void Database::LoadYear(User* user, int year)
|
||||
std::map<wxString, Account>::iterator it;
|
||||
|
||||
if (user->_operations[year] == NULL)
|
||||
user->_operations[year] = new std::map<unsigned int, std::list<operation> >();
|
||||
user->_operations[year] = new std::map<unsigned int, std::vector<operation> >();
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req = _("SELECT * FROM operation WHERE account IN('") + it->first;
|
||||
@@ -255,3 +271,50 @@ double Database::GetAccountAmount(wxString id, int month, int year)
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Database::UpdateOperation(struct operation op)
|
||||
{
|
||||
wxString req;
|
||||
req = _("UPDATE operation SET ") ;
|
||||
req += _("account='") + op.account + _("'");
|
||||
req += _(", year='") + wxString::Format(_("%d"), op.year) + _("'");
|
||||
req += _(", month='") + wxString::Format(_("%d"), op.month) + _("'");
|
||||
req += _(", day='") + wxString::Format(_("%d"), op.day) + _("'");
|
||||
req += _(", amount='") + wxString::Format(_("%.2lf"), op.amount) + _("'");
|
||||
req += _(", description=\"") + op.description + _("\"");
|
||||
req += _(", category='") + op.category + _("'");
|
||||
req += _(" WHERE id='") + op.id + _("'");
|
||||
|
||||
//std::cout << req.mb_str() << "\n";
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
}
|
||||
|
||||
void Database::AddOperation(User* user, struct operation op)
|
||||
{
|
||||
wxString req;
|
||||
req = _("INSERT INTO operation ('user', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost') VALUES ('") ;
|
||||
req += user->_id + _("'");
|
||||
req += _(", '") + op.account + _("'");
|
||||
req += _(", '") + wxString::Format(_("%d"), op.year) + _("'");
|
||||
req += _(", '") + wxString::Format(_("%d"), op.month) + _("'");
|
||||
req += _(", '") + wxString::Format(_("%d"), op.day) + _("'");
|
||||
req += _(", '") + wxString::Format(_("%.2lf"), op.amount) + _("'");
|
||||
req += _(", \"") + op.description + _("\"");
|
||||
req += _(", '") + op.category + _("'");
|
||||
if (op.fix_cost)
|
||||
req += _(", '1'") ;
|
||||
else
|
||||
req += _(", '0'") ;
|
||||
req += _(")");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
void Database::DeleteOperation(struct operation op)
|
||||
{
|
||||
wxString req;
|
||||
req = _("DELETE FROM operation WHERE id='") + op.id + _("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
@@ -24,6 +24,11 @@ class Database
|
||||
User* LoadUser(wxString name);
|
||||
void LoadYear(User* user, int year);
|
||||
double GetAccountAmount(wxString id, int month, int year);
|
||||
|
||||
void UpdateOperation(struct operation op);
|
||||
void AddOperation(User* user, struct operation op);
|
||||
void DeleteOperation(struct operation op);
|
||||
|
||||
private:
|
||||
wxSQLite3Database _db;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
User::~User()
|
||||
{
|
||||
std::map<unsigned int, std::map<unsigned int, std::list<operation> >* >::iterator it;
|
||||
std::map<unsigned int, std::map<unsigned int, std::vector<operation> >* >::iterator it;
|
||||
|
||||
for (it = _operations.begin(); it != _operations.end(); it++)
|
||||
{
|
||||
@@ -21,6 +21,16 @@ wxString User::GetCategoryName(wxString catId)
|
||||
return _preferences._categories[catId];
|
||||
}
|
||||
|
||||
wxString User::GetCategoryId(wxString catName)
|
||||
{
|
||||
std::map<wxString, wxString>::iterator it;
|
||||
for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++)
|
||||
if (it->second == catName)
|
||||
return it->first;
|
||||
|
||||
return _("0") ;
|
||||
}
|
||||
|
||||
wxString User::GetAccountName(wxString accountId)
|
||||
{
|
||||
if (_accounts.find(accountId) == _accounts.end())
|
||||
@@ -28,6 +38,16 @@ wxString User::GetAccountName(wxString accountId)
|
||||
return _accounts[accountId].name;
|
||||
}
|
||||
|
||||
wxString User::GetAccountId(wxString accountName)
|
||||
{
|
||||
std::map<wxString, wxString>::iterator it;
|
||||
for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++)
|
||||
if (it->second == accountName)
|
||||
return it->first;
|
||||
|
||||
return _("0") ;
|
||||
}
|
||||
|
||||
int User::GetCategoriesNumber()
|
||||
{
|
||||
return _preferences._categories.size();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef USER_H
|
||||
#define USER_H
|
||||
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <wx/wx.h>
|
||||
#include "Preferences.h"
|
||||
|
||||
@@ -35,11 +35,13 @@ public:
|
||||
wxString _name;
|
||||
wxString _password;
|
||||
std::map<wxString, Account> _accounts;
|
||||
std::map<unsigned int, std::map<unsigned int, std::list<operation> >* > _operations;
|
||||
std::map<unsigned int, std::map<unsigned int, std::vector<operation> >* > _operations;
|
||||
Preferences _preferences;
|
||||
|
||||
wxString GetCategoryName(wxString catId);
|
||||
wxString GetCategoryId(wxString catName);
|
||||
wxString GetAccountName(wxString accountId);
|
||||
wxString GetAccountId(wxString accountName);
|
||||
int GetCategoriesNumber();
|
||||
int GetAccountsNumber();
|
||||
int GetOperationsNumber(int month, int year);
|
||||
|
||||
Reference in New Issue
Block a user