KissCount/model/Database.cpp

885 lines
23 KiB
C++
Raw Normal View History

#include "Database.h"
2010-05-16 10:35:34 +02:00
// if (!_db.CheckSyntax(req))
// {
// wxString s = req;
// std::cout << s.mb_str() << " is invalid !\n" ;
// code_if_syntax_fail;
// return return_value;
// }
2010-06-03 18:28:38 +02:00
#define EXECUTE_SQL_UPDATE_WITH_CODE(req, return_value, code_if_fail, code_if_syntax_fail) \
do{\
try\
{\
_db.ExecuteUpdate(req);\
}\
catch (wxSQLite3Exception e)\
{\
2010-06-24 21:02:42 +02:00
std::cerr << req.mb_str() << "\n" ;\
2010-06-03 18:28:38 +02:00
std::cerr << e.GetMessage().mb_str() << "\n" ;\
code_if_fail; \
return return_value;\
}\
} while(0);
2010-05-15 11:21:42 +02:00
#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)\
{\
2010-06-24 21:02:42 +02:00
std::cerr << req.mb_str() << "\n" ;\
2010-05-15 11:21:42 +02:00
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, {}, {})
2010-06-03 18:28:38 +02:00
#define EXECUTE_SQL_UPDATE(req, return_value) EXECUTE_SQL_UPDATE_WITH_CODE(req, return_value, {}, {})
static inline wxString DoubleToString(double d)
{
wxString res;
2010-07-03 11:19:02 +02:00
res = wxString::Format(wxT("%.2lf"), d);
res.Replace(wxT(","), wxT("."));
return res;
}
Database::Database()
{
std::ifstream bdd_file;
bdd_file.open(BDD_FILE);
if (!bdd_file)
{
CreateDatabase();
}
else
2010-07-03 11:19:02 +02:00
_db.Open(wxT(BDD_FILE));
bdd_file.close();
}
void Database::CreateDatabase()
{
std::ifstream init_script;
std::string line;
2010-05-15 11:21:42 +02:00
wxString wxline;
2010-07-03 11:19:02 +02:00
wxMessageBox(_("No database found, creating a new one"), wxT("KissCount"), wxICON_EXCLAMATION | wxOK );
init_script.open(INIT_SCRIPT);
if (!init_script)
{
wxMessageBox(_(INIT_SCRIPT " not found, aborting"), _("Error"), wxICON_ERROR | wxOK );
throw "init.sql not found, aborting";
}
2010-07-03 11:19:02 +02:00
_db.Open(wxT(BDD_FILE));
do
{
getline(init_script, line);
2010-05-15 11:21:42 +02:00
wxline = wxString(line.c_str(), wxConvUTF8);
wxline.Trim(false);
2010-07-03 11:19:02 +02:00
if (!wxline.Length() || wxline.StartsWith(wxT("--"))) continue;
2010-05-15 11:21:42 +02:00
if (!_db.CheckSyntax(wxline))
{
std::cout << line << " is invalid !\n" ;
continue;
}
try
{
2010-05-15 11:21:42 +02:00
_db.ExecuteUpdate(wxline);
}
catch (...)
{
wxMessageBox(_("Error creating original database"), _("Error"), wxICON_ERROR | wxOK );
2010-05-15 11:21:42 +02:00
remove(BDD_FILE);
throw line;
}
} while (init_script);
init_script.close();
}
2010-06-27 21:39:49 +02:00
wxString Database::HashPassword(wxString password)
{
blk_SHA_CTX sha_ctx;
unsigned char sha[20];
wxString wxSHA;
blk_SHA1_Init(&sha_ctx);
blk_SHA1_Update(&sha_ctx, password.c_str(), password.Length());
blk_SHA1_Final(sha, &sha_ctx);
for(int i=0; i<20; i++)
wxSHA += wxString::Format(wxT("%02x"), (int)sha[i]);
return wxSHA;
}
std::list<wxString> Database::GetUsers()
{
std::list<wxString> res;
2010-06-24 21:02:42 +02:00
wxString req;
// Check whether value exists in table
wxSQLite3ResultSet set ;
2010-05-15 11:21:42 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("SELECT name FROM user ORDER BY name");
2010-06-24 21:02:42 +02:00
EXECUTE_SQL_QUERY(req, set, res);
while (set.NextRow())
{
res.push_front(set.GetAsString(0));
}
set.Finalize();
return res;
}
bool Database::IsValidUser(wxString user, wxString password)
{
bool res;
2010-06-27 21:39:49 +02:00
wxString req;
wxSQLite3ResultSet set;
2010-07-03 11:19:02 +02:00
req = wxT("SELECT name FROM user WHERE name='") + user + wxT("' AND password='") + HashPassword(password) + wxT("'");
2010-05-15 11:21:42 +02:00
EXECUTE_SQL_QUERY(req, set, false);
res = set.NextRow() ;
set.Finalize();
return res;
}
2010-05-15 11:21:42 +02:00
User* Database::LoadUser(wxString name)
{
wxSQLite3ResultSet set;
wxString req;
User* user;
2010-07-03 11:48:53 +02:00
Account account;
Category category;
2010-06-22 12:29:36 +02:00
2010-07-03 11:48:53 +02:00
std::vector<Account>::iterator it;
2010-05-15 11:21:42 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("SELECT * FROM user WHERE name='") + name + wxT("'");
2010-05-15 11:21:42 +02:00
EXECUTE_SQL_QUERY(req, set, NULL);
if (!set.NextRow())
return NULL;
user = new User();
2010-07-03 11:19:02 +02:00
user->_id = set.GetAsString(wxT("id"));
user->_name = set.GetAsString(wxT("name"));
user->_password = wxT("") ; // Security reasons set.GetAsString("password");
2010-05-15 11:21:42 +02:00
set.Finalize();
2010-07-03 11:19:02 +02:00
req = wxT("SELECT * FROM account WHERE user='") + user->_id + wxT("' ORDER BY default_account DESC, name ASC");
2010-05-15 11:21:42 +02:00
2010-05-15 11:45:57 +02:00
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
2010-05-15 11:21:42 +02:00
while (set.NextRow())
{
2010-07-03 11:19:02 +02:00
account.id = set.GetAsString(wxT("id"));
account.name = set.GetAsString(wxT("name"));
account.number = set.GetAsString(wxT("number"));
account.shared = set.GetBool(wxT("shared"));
account._default = set.GetBool(wxT("default_account"));
2010-06-21 13:02:02 +02:00
user->_accounts.push_back(account);
2010-05-15 11:21:42 +02:00
}
set.Finalize();
if (!user->_accounts.empty())
{
it = user->_accounts.begin();
2010-07-03 11:19:02 +02:00
req = wxT("SELECT DISTINCT year FROM operation WHERE account IN('") + it->id;
2010-05-15 11:21:42 +02:00
it++;
for (;it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req += wxT("', '") + it->id ;
2010-05-15 11:21:42 +02:00
}
2010-07-03 11:19:02 +02:00
req += wxT("')");
req += wxT(" OR user='") + user->_id + wxT("'");
req += wxT(" ORDER BY year ASC");
2010-05-15 11:21:42 +02:00
2010-05-15 11:45:57 +02:00
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
2010-05-15 11:21:42 +02:00
while (set.NextRow())
{
2010-07-03 11:19:02 +02:00
user->_operations[set.GetInt(wxT("year"))] = NULL;
2010-05-15 11:21:42 +02:00
}
set.Finalize();
}
2010-07-03 11:19:02 +02:00
req = wxT("SELECT * FROM category WHERE user='") + user->_id + wxT("' ORDER BY name ASC");
2010-05-24 20:14:15 +02:00
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
while (set.NextRow())
2010-06-22 12:29:36 +02:00
{
2010-07-03 11:19:02 +02:00
category.id = set.GetAsString(wxT("id"));
category.parent = set.GetAsString(wxT("parent"));
category.name = set.GetAsString(wxT("name"));
category.color = wxColour(set.GetAsString(wxT("color")));
category.font = set.GetAsString(wxT("font"));
if (category.name != wxT("Fixe"))
user->_categories.push_back(category);
2010-06-22 12:29:36 +02:00
else
user->_categories.insert(user->_categories.begin(), category);
2010-06-22 12:29:36 +02:00
}
2010-05-24 20:14:15 +02:00
set.Finalize();
2010-07-03 11:19:02 +02:00
req = wxT("SELECT name, value FROM preference WHERE user='") + user->_id + wxT("' ORDER BY value ASC");
2010-06-16 17:29:07 +02:00
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
while (set.NextRow())
2010-07-03 11:19:02 +02:00
user->_preferences[set.GetAsString(wxT("name"))] = set.GetAsString(wxT("value"));
2010-06-16 17:29:07 +02:00
set.Finalize();
2010-05-15 11:21:42 +02:00
return user;
}
2010-05-16 10:35:34 +02:00
void Database::LoadYear(User* user, int year)
{
wxSQLite3ResultSet set;
wxString req;
2010-07-03 11:48:53 +02:00
std::vector<Account>::iterator it;
2010-05-16 10:35:34 +02:00
if (user->_operations[year] == NULL)
2010-07-03 11:48:53 +02:00
user->_operations[year] = new std::map<unsigned int, std::vector<Operation> >();
2010-05-16 10:35:34 +02:00
2010-07-03 09:52:44 +02:00
if (!user->_accounts.size()) return;
2010-05-16 10:35:34 +02:00
it = user->_accounts.begin();
2010-07-03 11:19:02 +02:00
req = wxT("SELECT * FROM operation WHERE (account IN('") + it->id;
2010-05-16 10:35:34 +02:00
it++;
for (;it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req += wxT("', '") + it->id ;
2010-05-16 10:35:34 +02:00
}
2010-07-03 11:19:02 +02:00
req += wxT("')");
req += wxT(" OR user='") + user->_id + wxT("')");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
req += wxT(" ORDER BY fix_cost DESC, year,month,day ASC");
2010-05-16 10:35:34 +02:00
EXECUTE_SQL_QUERY(req, set, );
while (set.NextRow())
{
2010-07-03 11:48:53 +02:00
Operation op;
2010-07-03 11:19:02 +02:00
op.id = set.GetAsString(wxT("id"));
op.account = set.GetAsString(wxT("account"));
op.day = set.GetInt(wxT("day"));
op.month = set.GetInt(wxT("month"));
op.year = set.GetInt(wxT("year"));
op.amount = set.GetDouble(wxT("amount"));
op.description = set.GetAsString(wxT("description"));
op.category = set.GetAsString(wxT("category"));
op.fix_cost = set.GetBool(wxT("fix_cost"));
op.checked = set.GetBool(wxT("checked"));
2010-05-16 10:35:34 +02:00
(*user->_operations[op.year])[op.month].push_back(op);
}
set.Finalize();
}
2010-06-02 22:14:11 +02:00
double Database::GetAccountAmount(wxString id, int month, int year)
{
wxSQLite3ResultSet set;
wxString req;
double res;
2010-07-03 11:19:02 +02:00
req = wxT("SELECT amount FROM account_amount WHERE account='") + id ;
req += wxT("' AND month='") + wxString::Format(wxT("%d"), month);
req += wxT("' AND year='") + wxString::Format(wxT("%d"), year);
req += wxT("'");
2010-06-02 22:14:11 +02:00
EXECUTE_SQL_QUERY(req , set, 0.0);
if (set.NextRow())
2010-07-03 11:19:02 +02:00
res = set.GetDouble(wxT("amount"));
2010-06-02 22:14:11 +02:00
else
2010-06-21 10:53:43 +02:00
{
SetAccountAmount(month, year, id, 0.0);
res = 0.0;
}
2010-06-02 22:14:11 +02:00
set.Finalize();
return res;
}
2010-06-03 18:28:38 +02:00
2010-07-03 11:48:53 +02:00
void Database::UpdateOperation(Operation op)
2010-06-03 18:28:38 +02:00
{
wxString req;
2010-07-03 11:19:02 +02:00
req = wxT("UPDATE operation SET ") ;
req += wxT("account='") + op.account + wxT("'");
req += wxT(", year='") + wxString::Format(wxT("%d"), op.year) + wxT("'");
req += wxT(", month='") + wxString::Format(wxT("%d"), op.month) + wxT("'");
req += wxT(", day='") + wxString::Format(wxT("%d"), op.day) + wxT("'");
req += wxT(", amount='") + DoubleToString(op.amount) + wxT("'");
req += wxT(", description=\"") + op.description + wxT("\"");
req += wxT(", category='") + op.category + wxT("'");
2010-06-16 19:15:49 +02:00
if (op.checked)
2010-07-03 11:19:02 +02:00
req += wxT(", checked='1'");
2010-06-16 19:15:49 +02:00
else
2010-07-03 11:19:02 +02:00
req += wxT(", checked='0'");
req += wxT(" WHERE id='") + op.id + wxT("'");
2010-06-03 18:28:38 +02:00
EXECUTE_SQL_UPDATE(req, );
}
2010-07-03 11:48:53 +02:00
wxString Database::AddOperation(User* user, Operation op)
2010-06-03 18:28:38 +02:00
{
2010-06-21 13:02:02 +02:00
wxString req, res;
wxSQLite3ResultSet set;
2010-07-03 11:19:02 +02:00
req = wxT("INSERT INTO operation ('user', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost') VALUES ('") ;
req += user->_id + wxT("'");
req += wxT(", '") + op.account + wxT("'");
req += wxT(", '") + wxString::Format(wxT("%d"), op.year) + wxT("'");
req += wxT(", '") + wxString::Format(wxT("%d"), op.month) + wxT("'");
req += wxT(", '") + wxString::Format(wxT("%d"), op.day) + wxT("'");
req += wxT(", '") + DoubleToString(op.amount) + wxT("'");
req += wxT(", \"") + op.description + wxT("\"");
req += wxT(", '") + op.category + wxT("'");
2010-06-03 18:28:38 +02:00
if (op.fix_cost)
2010-07-03 11:19:02 +02:00
req += wxT(", '1'") ;
2010-06-03 18:28:38 +02:00
else
2010-07-03 11:19:02 +02:00
req += wxT(", '0'") ;
req += wxT(")");
2010-06-03 18:28:38 +02:00
2010-07-03 11:19:02 +02:00
EXECUTE_SQL_UPDATE(req, wxT("0"));
req = wxT("SELECT id FROM operation WHERE ");
req += wxT("user='") + user->_id + wxT("'");
req += wxT(" AND account='") + op.account + wxT("'");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), op.year) + wxT("'");
req += wxT(" AND month='") + wxString::Format(wxT("%d"), op.month) + wxT("'");
req += wxT(" AND day='") + wxString::Format(wxT("%d"), op.day) + wxT("'");
req += wxT(" AND amount='") + DoubleToString(op.amount) + wxT("'");
req += wxT(" AND description=\"") + op.description + wxT("\"");
req += wxT(" AND category='") + op.category + wxT("'");
2010-06-21 13:02:02 +02:00
if (op.fix_cost)
2010-07-03 11:19:02 +02:00
req += wxT(" AND fix_cost='1'") ;
2010-06-21 13:02:02 +02:00
else
2010-07-03 11:19:02 +02:00
req += wxT(" AND fix_cost='0'") ;
req += wxT("ORDER BY ID DESC") ;
2010-06-21 13:02:02 +02:00
2010-07-03 11:19:02 +02:00
EXECUTE_SQL_QUERY(req , set, wxT("0"));
2010-06-21 13:02:02 +02:00
if (set.NextRow())
2010-07-03 11:19:02 +02:00
res = set.GetAsString(wxT("id"));
2010-06-21 13:02:02 +02:00
else
2010-07-03 11:19:02 +02:00
res = wxT("0");
2010-06-21 13:02:02 +02:00
set.Finalize();
return res;
2010-06-03 18:28:38 +02:00
}
2010-07-03 11:48:53 +02:00
void Database::DeleteOperation(Operation op)
2010-06-03 18:28:38 +02:00
{
wxString req;
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM operation WHERE id='") + op.id + wxT("'");
2010-06-03 18:28:38 +02:00
EXECUTE_SQL_UPDATE(req, );
}
2010-06-12 15:59:27 +02:00
2010-06-24 21:02:42 +02:00
void Database::DeleteOperations(User* user, int month, int year)
{
wxString req;
2010-07-03 11:48:53 +02:00
std::vector<Account>::iterator it;
2010-06-24 21:02:42 +02:00
it = user->_accounts.begin();
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM account_amount WHERE account IN('") + it->id;
2010-06-24 21:02:42 +02:00
it++;
for (;it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req += wxT("', '") + it->id ;
2010-06-24 21:02:42 +02:00
}
2010-07-03 11:19:02 +02:00
req += wxT("')");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
2010-06-24 21:02:42 +02:00
if (month != -1)
2010-07-03 11:19:02 +02:00
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
2010-06-24 21:02:42 +02:00
EXECUTE_SQL_UPDATE(req, );
it = user->_accounts.begin();
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM operation WHERE (account IN('") + it->id;
2010-06-24 21:02:42 +02:00
it++;
for (;it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req += wxT("', '") + it->id ;
2010-06-24 21:02:42 +02:00
}
2010-07-03 11:19:02 +02:00
req += wxT("')");
req += wxT(" OR user='") + user->_id + wxT("')");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
2010-06-24 21:02:42 +02:00
if (month != -1)
2010-07-03 11:19:02 +02:00
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
2010-06-24 21:02:42 +02:00
EXECUTE_SQL_UPDATE(req, );
}
2010-06-12 15:59:27 +02:00
void Database::SetAccountAmount(int month, int year, wxString accountId, double amount)
{
wxString req;
2010-07-03 11:19:02 +02:00
req = wxT("UPDATE account_amount SET ") ;
req += wxT("amount='") + DoubleToString(amount) + wxT("'");
req += wxT(" WHERE account='") + accountId + wxT("'");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
2010-06-12 15:59:27 +02:00
2010-07-01 21:13:14 +02:00
try
{
if (!_db.ExecuteUpdate(req))
{
2010-07-03 11:19:02 +02:00
req = wxT("INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('") ;
req += accountId + wxT("'");
req += wxT(" ,'") + wxString::Format(wxT("%d"), year) + wxT("'");
req += wxT(" ,'") + wxString::Format(wxT("%d"), month) + wxT("'");
req += wxT(" ,'") + DoubleToString(amount) + wxT("'");
req += wxT(")");
2010-07-01 21:13:14 +02:00
EXECUTE_SQL_UPDATE(req, );
}
}
catch (wxSQLite3Exception e)
{
std::cerr << req.mb_str() << "\n" ;
std::cerr << e.GetMessage().mb_str() << "\n" ;
return ;
}
2010-06-12 15:59:27 +02:00
}
2010-06-21 10:53:43 +02:00
2010-07-03 11:48:53 +02:00
wxString Database::AddAccount(User* user, Account ac)
2010-06-21 10:53:43 +02:00
{
2010-06-21 13:02:02 +02:00
wxString req, res;
wxSQLite3ResultSet set;
2010-07-03 11:19:02 +02:00
req = wxT("INSERT INTO account ('user', 'name', 'number', 'shared', 'default_account') VALUES ('") ;
req += user->_id + wxT("'");
req += wxT(", '") + ac.name + wxT("'");
req += wxT(", '") + ac.number + wxT("'");
2010-06-21 10:53:43 +02:00
if (ac.shared)
2010-07-03 11:19:02 +02:00
req += wxT(", '1'") ;
2010-06-21 10:53:43 +02:00
else
2010-07-03 11:19:02 +02:00
req += wxT(", '0'") ;
2010-06-21 10:53:43 +02:00
if (ac._default)
2010-07-03 11:19:02 +02:00
req += wxT(", '1'") ;
2010-06-21 10:53:43 +02:00
else
2010-07-03 11:19:02 +02:00
req += wxT(", '0'") ;
req += wxT(")");
2010-06-21 10:53:43 +02:00
2010-07-03 11:19:02 +02:00
EXECUTE_SQL_UPDATE(req, wxT("0"));
2010-06-21 13:02:02 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("SELECT id FROM account WHERE name='") + ac.name + wxT("'") ;
req += wxT("AND user='") + user->_id + wxT("'");
2010-06-21 13:02:02 +02:00
2010-07-03 11:19:02 +02:00
EXECUTE_SQL_QUERY(req , set, wxT("0"));
2010-06-21 13:02:02 +02:00
if (set.NextRow())
2010-07-03 11:19:02 +02:00
res = set.GetAsString(wxT("id"));
2010-06-21 13:02:02 +02:00
else
2010-07-03 11:19:02 +02:00
res = wxT("0");
2010-06-21 13:02:02 +02:00
set.Finalize();
return res;
2010-06-21 10:53:43 +02:00
}
2010-07-03 11:48:53 +02:00
void Database::UpdateAccount(Account ac)
2010-06-21 10:53:43 +02:00
{
wxString req;
2010-07-03 11:19:02 +02:00
req = wxT("UPDATE account SET ") ;
req += wxT("name='") + ac.name + wxT("'");
req += wxT(", number='") + ac.number + wxT("'");
2010-06-21 10:53:43 +02:00
if (ac.shared)
2010-07-03 11:19:02 +02:00
req += wxT(", shared='1'");
2010-06-21 10:53:43 +02:00
else
2010-07-03 11:19:02 +02:00
req += wxT(", shared='0'");
2010-06-21 10:53:43 +02:00
if (ac._default)
2010-07-03 11:19:02 +02:00
req += wxT(", default_account='1'");
2010-06-21 10:53:43 +02:00
else
2010-07-03 11:19:02 +02:00
req += wxT(", default_account='0'");
req += wxT(" WHERE id='") + ac.id + wxT("'");
2010-06-21 10:53:43 +02:00
EXECUTE_SQL_UPDATE(req, );
}
2010-07-03 11:48:53 +02:00
void Database::DeleteAccount(Account ac)
2010-06-21 10:53:43 +02:00
{
wxString req;
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM account WHERE id='") + ac.id + wxT("'");
2010-06-21 10:53:43 +02:00
EXECUTE_SQL_UPDATE(req, );
}
2010-06-22 11:35:21 +02:00
2010-07-03 11:48:53 +02:00
wxString Database::AddCategory(User* user, Category category)
2010-06-22 11:35:21 +02:00
{
2010-06-22 12:29:36 +02:00
wxString req, res;
wxSQLite3ResultSet set;
wxString color;
2010-07-03 11:19:02 +02:00
color = wxT("#") ;
color += wxString::Format(wxT("%02X"), category.color.Red());
color += wxString::Format(wxT("%02X"), category.color.Green());
color += wxString::Format(wxT("%02X"), category.color.Blue());
req = wxT("INSERT INTO category ('user', 'parent', 'name', 'color', font) VALUES ('") ;
req += user->_id + wxT("'");
req += wxT(", '") + category.parent + wxT("'");
req += wxT(", '") + category.name + wxT("'");
req += wxT(", '") + color + wxT("'");
req += wxT(", '") + category.font + wxT("'");
req += wxT(")");
2010-06-22 11:35:21 +02:00
2010-07-03 11:19:02 +02:00
EXECUTE_SQL_UPDATE(req, wxT("0"));
2010-06-22 12:29:36 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("SELECT id FROM preference WHERE user='") + user->_id + wxT("'") ;
req += wxT(" AND name='") + category.name + wxT("'");
2010-06-22 12:29:36 +02:00
2010-07-03 11:19:02 +02:00
EXECUTE_SQL_QUERY(req , set, wxT("0"));
2010-06-22 12:29:36 +02:00
if (set.NextRow())
2010-07-03 11:19:02 +02:00
res = set.GetAsString(wxT("id"));
2010-06-22 12:29:36 +02:00
else
2010-07-03 11:19:02 +02:00
res = wxT("0");
2010-06-22 12:29:36 +02:00
set.Finalize();
return res;
2010-06-22 11:35:21 +02:00
}
2010-07-03 11:48:53 +02:00
void Database::UpdateCategory(Category category)
2010-06-22 11:35:21 +02:00
{
wxString req;
wxString color;
2010-06-22 11:35:21 +02:00
2010-07-03 11:19:02 +02:00
color = wxT("#") ;
color += wxString::Format(wxT("%02X"), category.color.Red());
color += wxString::Format(wxT("%02X"), category.color.Green());
color += wxString::Format(wxT("%02X"), category.color.Blue());
2010-06-22 11:35:21 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("UPDATE category SET") ;
req += wxT(" parent='") + category.parent + wxT("'");
req += wxT(", name='") + category.name + wxT("'");
req += wxT(", color='") + color + wxT("'");
req += wxT(", font='") + category.font + wxT("'");
req += wxT(" WHERE id='") + category.id + wxT("'");
2010-06-22 11:35:21 +02:00
EXECUTE_SQL_UPDATE(req, );
2010-06-22 11:35:21 +02:00
}
2010-07-03 11:48:53 +02:00
void Database::DeleteCategory(User* user, Category category)
2010-06-22 11:35:21 +02:00
{
wxString req;
2010-06-24 21:02:42 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM category WHERE id='") + category.id + wxT("'");
2010-06-22 11:35:21 +02:00
EXECUTE_SQL_UPDATE(req, );
2010-07-03 11:19:02 +02:00
req = wxT("UPDATE category SET") ;
req += wxT(" parent='0'");
req += wxT(" WHERE parent='") + category.id + wxT("'");
2010-06-22 11:35:21 +02:00
EXECUTE_SQL_UPDATE(req, );
}
2010-06-23 14:25:00 +02:00
std::map<int, std::vector<int> > Database::GetAllOperations(User* user)
{
wxString req, req2, reqUnion;
2010-06-23 14:25:00 +02:00
wxSQLite3ResultSet set, set2;
2010-07-03 11:48:53 +02:00
std::vector<Account>::iterator it;
2010-06-23 14:25:00 +02:00
std::map<int, std::vector<int> > res;
int year;
if (!user->_accounts.empty())
{
it = user->_accounts.begin();
req = wxT("SELECT DISTINCT year FROM account_amount WHERE account IN('") + it->id;
2010-06-23 14:25:00 +02:00
it++;
for (;it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req += wxT("', '") + it->id ;
2010-06-23 14:25:00 +02:00
}
2010-07-03 11:19:02 +02:00
req += wxT("')");
2010-06-23 14:25:00 +02:00
it = user->_accounts.begin();
req2 = wxT("SELECT DISTINCT year FROM operation WHERE account IN('") + it->id;
it++;
for (;it != user->_accounts.end(); it++)
{
req2 += wxT("', '") + it->id ;
}
req2 += wxT("')");
req2 += wxT(" OR user='") + user->_id + wxT("'");
req2 += wxT(" ORDER BY year ASC");
reqUnion = req + wxT(" UNION ") + req2;
EXECUTE_SQL_QUERY(reqUnion, set, res);
2010-06-23 14:25:00 +02:00
while (set.NextRow())
{
2010-07-03 11:19:02 +02:00
year = set.GetInt(wxT("year"));
2010-06-23 14:25:00 +02:00
it = user->_accounts.begin();
req = wxT("SELECT DISTINCT month FROM account_amount WHERE account IN('") + it->id;
2010-06-23 14:25:00 +02:00
it++;
for (;it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req += wxT("', '") + it->id ;
2010-06-23 14:25:00 +02:00
}
2010-07-03 11:19:02 +02:00
req += wxT("')");
req += wxT(" AND year='") + set.GetAsString(wxT("year")) + wxT("'");
2010-06-23 14:25:00 +02:00
it = user->_accounts.begin();
req2 = wxT("SELECT DISTINCT month FROM operation WHERE (account IN('") + it->id;
it++;
for (;it != user->_accounts.end(); it++)
{
req2 += wxT("', '") + it->id ;
}
req2 += wxT("')");
req2 += wxT(" OR user='") + user->_id + wxT("')");
req2 += wxT(" AND year='") + set.GetAsString(wxT("year")) + wxT("'");
req2 += wxT(" ORDER BY month ASC");
reqUnion = req + wxT(" UNION ") + req2;
EXECUTE_SQL_QUERY(reqUnion, set2, res);
2010-06-23 14:25:00 +02:00
while (set2.NextRow())
{
2010-07-03 11:19:02 +02:00
res[year].push_back(set2.GetInt(wxT("month")));
2010-06-23 14:25:00 +02:00
}
set2.Finalize();
}
set.Finalize();
}
return res;
}
2010-06-23 19:32:42 +02:00
void Database::GenerateMonth(User* user, int monthFrom, int yearFrom, int monthTo, int yearTo)
{
2010-07-03 11:48:53 +02:00
std::vector<Account>::iterator it;
2010-06-23 19:32:42 +02:00
wxString req;
wxSQLite3ResultSet set;
double amount;
if (monthFrom == -1 || yearFrom == -1)
{
for (it = user->_accounts.begin(); it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req = wxT("INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('") ;
req += it->id + wxT("'");
req += wxT(" ,'") + wxString::Format(wxT("%d"), yearTo) + wxT("'");
req += wxT(" ,'") + wxString::Format(wxT("%d"), monthTo) + wxT("'");
req += wxT(" ,'0.0'");
req += wxT(")");
2010-06-23 19:32:42 +02:00
EXECUTE_SQL_UPDATE(req, );
}
return;
}
for (it = user->_accounts.begin(); it != user->_accounts.end(); it++)
{
amount = 0.0;
2010-07-03 14:54:58 +02:00
req = wxT("SELECT SUM(amount) AS total FROM operation WHERE") ;
2010-07-03 11:19:02 +02:00
req += wxT(" account='") + it->id + wxT("'");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), yearFrom) + wxT("'");
req += wxT(" AND month='") + wxString::Format(wxT("%d"), monthFrom) + wxT("'");
2010-06-23 19:32:42 +02:00
EXECUTE_SQL_QUERY(req, set, );
2010-07-03 14:54:58 +02:00
if (set.NextRow())
amount += set.GetDouble(wxT("total"));
2010-06-23 19:32:42 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("SELECT amount FROM account_amount WHERE") ;
req += wxT(" account='") + it->id + wxT("'");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), yearFrom) + wxT("'");
req += wxT(" AND month='") + wxString::Format(wxT("%d"), monthFrom) + wxT("'");
2010-06-23 19:32:42 +02:00
EXECUTE_SQL_QUERY(req, set, );
if (set.NextRow())
2010-07-03 11:19:02 +02:00
amount += set.GetDouble(wxT("amount"));
2010-06-23 19:32:42 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('") ;
req += it->id + wxT("'");
req += wxT(" ,'") + wxString::Format(wxT("%d"), yearTo) + wxT("'");
req += wxT(" ,'") + wxString::Format(wxT("%d"), monthTo) + wxT("'");
req += wxT(" ,'") + DoubleToString(amount) + wxT("'");
req += wxT(")");
2010-06-23 19:32:42 +02:00
EXECUTE_SQL_UPDATE(req, );
}
}
2010-06-27 21:39:49 +02:00
void Database::ChangePassword(User* user, wxString password)
{
wxString req;
2010-07-03 11:19:02 +02:00
req = wxT("UPDATE user SET ") ;
req += wxT("password='") + HashPassword(password) + wxT("'");
req += wxT(" WHERE name='") + user->_name + wxT("'");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
}
bool Database::UserExists(wxString name)
{
wxSQLite3ResultSet set;
wxString req;
bool res=false;
2010-07-03 11:19:02 +02:00
req = wxT("SELECT name FROM user WHERE name='") + name + wxT("'") ;
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_QUERY(req , set, false);
if (set.NextRow())
res = true;
else
res = false;
set.Finalize();
return res;
}
void Database::ChangeName(User* user, wxString name)
{
wxString req;
2010-07-03 11:19:02 +02:00
req = wxT("UPDATE user SET ") ;
req += wxT("name='") + name + wxT("'");
req += wxT(" WHERE name='") + user->_name + wxT("'");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
}
void Database::NewUser(wxString name)
{
wxString req, id;
wxSQLite3ResultSet set;
2010-07-03 11:19:02 +02:00
req = wxT("INSERT INTO user ('name', 'password') VALUES ('") ;
req += name + wxT("'");
req += wxT(", '") + HashPassword(wxT("")) + wxT("'");
req += wxT(")");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
2010-07-03 11:19:02 +02:00
req = wxT("SELECT id FROM user WHERE ");
req += wxT("name='") + name + wxT("'");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_QUERY(req , set, );
set.NextRow();
2010-07-03 11:19:02 +02:00
id = set.GetAsString(wxT("id"));
2010-06-27 21:39:49 +02:00
set.Finalize();
2010-07-03 11:19:02 +02:00
req = wxT("SELECT * FROM default_preference");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_QUERY(req, set,);
while (set.NextRow())
{
2010-07-03 11:19:02 +02:00
req = wxT("INSERT INTO preference ('user', 'name', 'value') VALUES ('") ;
req += id + wxT("'");
req += wxT(", '") + set.GetAsString(wxT("type")) + wxT("'");
req += wxT(", '") + set.GetAsString(wxT("value")) + wxT("'");
req += wxT(")");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
}
set.Finalize();
2010-07-03 11:19:02 +02:00
req = wxT("SELECT * FROM default_category");
EXECUTE_SQL_QUERY(req, set,);
while (set.NextRow())
{
2010-07-03 11:19:02 +02:00
req = wxT("INSERT INTO category ('user', 'parent', 'name', 'color', 'font') VALUES ('") ;
req += id + wxT("'");
req += wxT(", '") + set.GetAsString(wxT("parent")) + wxT("'");
req += wxT(", '") + set.GetAsString(wxT("name")) + wxT("'");
req += wxT(", '") + set.GetAsString(wxT("color")) + wxT("'");
req += wxT(", '") + set.GetAsString(wxT("font")) + wxT("'");
req += wxT(")");
EXECUTE_SQL_UPDATE(req, );
}
set.Finalize();
2010-06-27 21:39:49 +02:00
return ;
}
2010-07-03 11:03:36 +02:00
/*
Shared accounts not currently supported
*/
2010-06-27 21:39:49 +02:00
void Database::KillMe(User* user)
{
wxString req;
2010-07-03 11:48:53 +02:00
std::vector<Account>::iterator it;
2010-06-27 21:39:49 +02:00
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM preference WHERE user='") + user->_id + wxT("'");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
if (!user->_accounts.empty())
{
it = user->_accounts.begin();
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM account_amount WHERE account IN('") + it->id;
2010-06-27 21:39:49 +02:00
it++;
for (;it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req += wxT("', '") + it->id ;
2010-06-27 21:39:49 +02:00
}
2010-07-03 11:19:02 +02:00
req += wxT("')");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
it = user->_accounts.begin();
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM operation WHERE account IN('") + it->id;
2010-06-27 21:39:49 +02:00
it++;
for (;it != user->_accounts.end(); it++)
{
2010-07-03 11:19:02 +02:00
req += wxT("', '") + it->id ;
2010-06-27 21:39:49 +02:00
}
2010-07-03 11:19:02 +02:00
req += wxT("')");
req += wxT(" OR user='") + user->_id + wxT("')");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
}
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM account WHERE user='") + user->_id + wxT("'");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM category WHERE user='") + user->_id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
2010-07-03 11:19:02 +02:00
req = wxT("DELETE FROM user WHERE id='") + user->_id + wxT("'");
2010-06-27 21:39:49 +02:00
EXECUTE_SQL_UPDATE(req, );
}