wip
This commit is contained in:
@@ -5,17 +5,6 @@
|
||||
#include <map>
|
||||
#include <wx/wx.h>
|
||||
|
||||
struct operation {
|
||||
wxString id;
|
||||
unsigned int day;
|
||||
unsigned int month;
|
||||
unsigned int year;
|
||||
int amount;
|
||||
wxString description;
|
||||
wxString category;
|
||||
bool fix_cost;
|
||||
} ;
|
||||
|
||||
class Account
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
#include "Database.h"
|
||||
|
||||
// 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_QUERY_WITH_CODE(req, res, return_value, code_if_fail, code_if_syntax_fail) \
|
||||
do{\
|
||||
try\
|
||||
{\
|
||||
if (!_db.CheckSyntax(req)) \
|
||||
{\
|
||||
std::cout << req << " is invalid !\n" ; \
|
||||
code_if_syntax_fail; \
|
||||
return return_value;\
|
||||
} \
|
||||
res = _db.ExecuteQuery(req);\
|
||||
}\
|
||||
catch (wxSQLite3Exception e)\
|
||||
@@ -61,7 +63,7 @@ void Database::CreateDatabase()
|
||||
getline(init_script, line);
|
||||
wxline = wxString(line.c_str(), wxConvUTF8);
|
||||
wxline.Trim(false);
|
||||
if (!wxline.Length() || wxline.StartsWith(_("#"))) continue;
|
||||
if (!wxline.Length() || wxline.StartsWith(_("--"))) continue;
|
||||
if (!_db.CheckSyntax(wxline))
|
||||
{
|
||||
std::cout << line << " is invalid !\n" ;
|
||||
@@ -113,11 +115,9 @@ bool Database::IsValidUser(wxString user, wxString password)
|
||||
blk_SHA1_Final(sha, &sha_ctx);
|
||||
|
||||
for(int i=0; i<20; i++)
|
||||
wxSHA += wxString::Format(wxT("%02F"), (int)sha[i]);
|
||||
wxSHA += wxString::Format(wxT("%02x"), (int)sha[i]);
|
||||
req = _("SELECT name FROM user WHERE name='") + user + _("' AND password='") + wxSHA + _("'");
|
||||
|
||||
std::cout << req.mb_str() << "\n";
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set, false);
|
||||
|
||||
res = set.NextRow() ;
|
||||
@@ -149,7 +149,7 @@ User* Database::LoadUser(wxString name)
|
||||
|
||||
set.Finalize();
|
||||
|
||||
req = _("SELECT * FROM account WHERE user='") + user->_id + _("' ORDER BY default DESC, name ASC");
|
||||
req = _("SELECT * FROM account WHERE user='") + user->_id + _("' ORDER BY default_account DESC, name ASC");
|
||||
|
||||
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
|
||||
|
||||
@@ -169,13 +169,13 @@ User* Database::LoadUser(wxString name)
|
||||
if (!user->_accounts.empty())
|
||||
{
|
||||
it = user->_accounts.begin();
|
||||
req = _("SELECT DISTINCT year FROM operation WHERE account IN(") + (*it)->_id;
|
||||
req = _("SELECT DISTINCT year FROM operation WHERE account IN('") + (*it)->_id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += _(", ") + (*it)->_id ;
|
||||
req += _("', '") + (*it)->_id ;
|
||||
}
|
||||
req += _(") ORDER BY year ASC");
|
||||
req += _("') ORDER BY year ASC");
|
||||
|
||||
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
|
||||
|
||||
@@ -188,3 +188,40 @@ User* Database::LoadUser(wxString name)
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
void Database::LoadYear(User* user, int year)
|
||||
{
|
||||
wxSQLite3ResultSet set;
|
||||
wxString req;
|
||||
std::list<Account*>::iterator it;
|
||||
|
||||
if (user->_operations[year] == NULL)
|
||||
user->_operations[year] = new std::map<unsigned int, std::list<operation> >();
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req = _("SELECT * FROM operation WHERE account IN('") + (*it)->_id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += _("', '") + (*it)->_id ;
|
||||
}
|
||||
req += _("') ORDER BY fix_cost DESC, year,month,day ASC");
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set, );
|
||||
|
||||
while (set.NextRow())
|
||||
{
|
||||
operation op;
|
||||
op.id = set.GetAsString(_("id"));
|
||||
op.day = set.GetInt(_("day"));
|
||||
op.month = set.GetInt(_("month"));
|
||||
op.year = set.GetInt(_("year"));
|
||||
op.amount = set.GetInt(_("amount"));
|
||||
op.description = set.GetAsString(_("description"));
|
||||
op.category = set.GetAsString(_("category"));
|
||||
op.fix_cost = set.GetBool(_("fix_cost"));
|
||||
(*user->_operations[op.year])[op.month].push_back(op);
|
||||
}
|
||||
|
||||
set.Finalize();
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ class Database
|
||||
bool IsValidUser(wxString user, wxString password);
|
||||
|
||||
User* LoadUser(wxString name);
|
||||
void LoadYear(User* user, int year);
|
||||
private:
|
||||
wxSQLite3Database _db;
|
||||
|
||||
|
||||
11
model/User.h
11
model/User.h
@@ -6,6 +6,17 @@
|
||||
#include "Account.h"
|
||||
#include "Preferences.h"
|
||||
|
||||
struct operation {
|
||||
wxString id;
|
||||
unsigned int day;
|
||||
unsigned int month;
|
||||
unsigned int year;
|
||||
int amount;
|
||||
wxString description;
|
||||
wxString category;
|
||||
bool fix_cost;
|
||||
} ;
|
||||
|
||||
class User
|
||||
{
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user