First code review

This commit is contained in:
2010-07-04 16:33:25 +02:00
parent 7d2143853e
commit 5999035959
25 changed files with 718 additions and 741 deletions

View File

@@ -1,42 +1,42 @@
#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;
// }
// 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)\
{\
std::cerr << req.mb_str() << "\n" ;\
std::cerr << e.GetMessage().mb_str() << "\n" ;\
code_if_fail; \
return return_value;\
}\
} while(0);
do{ \
try \
{ \
_db.ExecuteUpdate(req); \
} \
catch (wxSQLite3Exception e) \
{ \
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)\
{\
std::cerr << req.mb_str() << "\n" ;\
std::cerr << e.GetMessage().mb_str() << "\n" ;\
code_if_fail; \
return return_value;\
}\
} while(0);
do{ \
try \
{ \
res = _db.ExecuteQuery(req); \
} \
catch (wxSQLite3Exception e) \
{ \
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, {}, {})
@@ -54,62 +54,62 @@ static inline wxString DoubleToString(double d)
Database::Database()
{
std::ifstream bdd_file;
std::ifstream bdd_file;
bdd_file.open(BDD_FILE);
bdd_file.open(BDD_FILE);
if (!bdd_file)
if (!bdd_file)
{
CreateDatabase();
CreateDatabase();
}
else
_db.Open(wxT(BDD_FILE));
else
_db.Open(wxT(BDD_FILE));
bdd_file.close();
bdd_file.close();
}
void Database::CreateDatabase()
{
std::ifstream init_script;
std::string line;
wxString wxline;
std::ifstream init_script;
std::string line;
wxString wxline;
wxMessageBox(_("No database found, creating a new one"), wxT("KissCount"), wxICON_EXCLAMATION | wxOK );
wxMessageBox(_("No database found, creating a new one"), wxT("KissCount"), wxICON_EXCLAMATION | wxOK );
init_script.open(INIT_SCRIPT);
init_script.open(INIT_SCRIPT);
if (!init_script)
if (!init_script)
{
wxMessageBox(_(INIT_SCRIPT " not found, aborting"), _("Error"), wxICON_ERROR | wxOK );
throw "init.sql not found, aborting";
wxMessageBox(_(INIT_SCRIPT " not found, aborting"), _("Error"), wxICON_ERROR | wxOK );
throw "init.sql not found, aborting";
}
_db.Open(wxT(BDD_FILE));
_db.Open(wxT(BDD_FILE));
do
do
{
getline(init_script, line);
wxline = wxString(line.c_str(), wxConvUTF8);
wxline.Trim(false);
if (!wxline.Length() || wxline.StartsWith(wxT("--"))) continue;
if (!_db.CheckSyntax(wxline))
getline(init_script, line);
wxline = wxString(line.c_str(), wxConvUTF8);
wxline.Trim(false);
if (!wxline.Length() || wxline.StartsWith(wxT("--"))) continue;
if (!_db.CheckSyntax(wxline))
{
std::cout << line << " is invalid !\n" ;
continue;
std::cout << line << " is invalid !\n" ;
continue;
}
try
try
{
_db.ExecuteUpdate(wxline);
_db.ExecuteUpdate(wxline);
}
catch (...)
catch (...)
{
wxMessageBox(_("Error creating original database"), _("Error"), wxICON_ERROR | wxOK );
remove(BDD_FILE);
throw line;
wxMessageBox(_("Error creating original database"), _("Error"), wxICON_ERROR | wxOK );
remove(BDD_FILE);
throw line;
}
} while (init_script);
init_script.close();
init_script.close();
}
@@ -149,7 +149,7 @@ std::list<wxString> Database::GetUsers()
}
bool Database::IsValidUser(wxString user, wxString password)
{
{
bool res;
wxString req;
wxSQLite3ResultSet set;
@@ -237,7 +237,7 @@ User* Database::LoadUser(wxString name)
category.color = wxColour(set.GetAsString(wxT("color")));
category.font = set.GetAsString(wxT("font"));
if (category.name != wxT("Fixe"))
user->_categories.push_back(category);
user->_categories.push_back(category);
else
user->_categories.insert(user->_categories.begin(), category);
}
@@ -327,145 +327,145 @@ double Database::GetAccountAmount(wxString id, int month, int year)
void Database::UpdateOperation(Operation op)
{
wxString req;
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("'");
if (op.checked)
req += wxT(", checked='1'");
else
req += wxT(", checked='0'");
req += wxT(" WHERE id='") + op.id + wxT("'");
wxString req;
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("'");
if (op.checked)
req += wxT(", checked='1'");
else
req += wxT(", checked='0'");
req += wxT(" WHERE id='") + op.id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
EXECUTE_SQL_UPDATE(req, );
}
wxString Database::AddOperation(User* user, Operation op)
{
wxString req, res;
wxSQLite3ResultSet set;
wxSQLite3ResultSet set;
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("'");
if (op.fix_cost)
req += wxT(", '1'") ;
else
req += wxT(", '0'") ;
req += wxT(")");
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("'");
if (op.fix_cost)
req += wxT(", '1'") ;
else
req += wxT(", '0'") ;
req += wxT(")");
EXECUTE_SQL_UPDATE(req, wxT("0"));
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("'");
if (op.fix_cost)
req += wxT(" AND fix_cost='1'") ;
else
req += wxT(" AND fix_cost='0'") ;
req += wxT("ORDER BY ID DESC") ;
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("'");
if (op.fix_cost)
req += wxT(" AND fix_cost='1'") ;
else
req += wxT(" AND fix_cost='0'") ;
req += wxT("ORDER BY ID DESC") ;
EXECUTE_SQL_QUERY(req , set, wxT("0"));
EXECUTE_SQL_QUERY(req , set, wxT("0"));
if (set.NextRow())
res = set.GetAsString(wxT("id"));
else
res = wxT("0");
if (set.NextRow())
res = set.GetAsString(wxT("id"));
else
res = wxT("0");
set.Finalize();
set.Finalize();
return res;
return res;
}
void Database::DeleteOperation(Operation op)
{
wxString req;
req = wxT("DELETE FROM operation WHERE id='") + op.id + wxT("'");
wxString req;
req = wxT("DELETE FROM operation WHERE id='") + op.id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
EXECUTE_SQL_UPDATE(req, );
}
void Database::DeleteOperations(User* user, int month, int year)
{
wxString req;
std::vector<Account>::iterator it;
wxString req;
std::vector<Account>::iterator it;
it = user->_accounts.begin();
req = wxT("DELETE FROM account_amount WHERE account IN('") + it->id;
it++;
for (;it != user->_accounts.end(); it++)
{
req += wxT("', '") + it->id ;
}
req += wxT("')");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
if (month != -1)
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
it = user->_accounts.begin();
req = wxT("DELETE FROM account_amount WHERE account IN('") + it->id;
it++;
for (;it != user->_accounts.end(); it++)
{
req += wxT("', '") + it->id ;
}
req += wxT("')");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
if (month != -1)
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
EXECUTE_SQL_UPDATE(req, );
EXECUTE_SQL_UPDATE(req, );
it = user->_accounts.begin();
req = wxT("DELETE FROM operation WHERE (account IN('") + it->id;
it++;
for (;it != user->_accounts.end(); it++)
{
req += wxT("', '") + it->id ;
}
req += wxT("')");
req += wxT(" OR user='") + user->_id + wxT("')");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
if (month != -1)
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
it = user->_accounts.begin();
req = wxT("DELETE FROM operation WHERE (account IN('") + it->id;
it++;
for (;it != user->_accounts.end(); it++)
{
req += wxT("', '") + it->id ;
}
req += wxT("')");
req += wxT(" OR user='") + user->_id + wxT("')");
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
if (month != -1)
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
EXECUTE_SQL_UPDATE(req, );
EXECUTE_SQL_UPDATE(req, );
}
void Database::SetAccountAmount(int month, int year, wxString accountId, double amount)
{
wxString req;
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("'");
wxString req;
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("'");
try
{
if (!_db.ExecuteUpdate(req))
{
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(")");
EXECUTE_SQL_UPDATE(req, );
}
}
catch (wxSQLite3Exception e)
{
std::cerr << req.mb_str() << "\n" ;
std::cerr << e.GetMessage().mb_str() << "\n" ;
return ;
}
try
{
if (!_db.ExecuteUpdate(req))
{
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(")");
EXECUTE_SQL_UPDATE(req, );
}
}
catch (wxSQLite3Exception e)
{
std::cerr << req.mb_str() << "\n" ;
std::cerr << e.GetMessage().mb_str() << "\n" ;
return ;
}
}
wxString Database::AddAccount(User* user, Account ac)
@@ -473,62 +473,62 @@ wxString Database::AddAccount(User* user, Account ac)
wxString req, res;
wxSQLite3ResultSet set;
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("'");
if (ac.shared)
req += wxT(", '1'") ;
else
req += wxT(", '0'") ;
if (ac._default)
req += wxT(", '1'") ;
else
req += wxT(", '0'") ;
req += wxT(")");
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("'");
if (ac.shared)
req += wxT(", '1'") ;
else
req += wxT(", '0'") ;
if (ac._default)
req += wxT(", '1'") ;
else
req += wxT(", '0'") ;
req += wxT(")");
EXECUTE_SQL_UPDATE(req, wxT("0"));
EXECUTE_SQL_UPDATE(req, wxT("0"));
req = wxT("SELECT id FROM account WHERE name='") + ac.name + wxT("'") ;
req += wxT("AND user='") + user->_id + wxT("'");
req = wxT("SELECT id FROM account WHERE name='") + ac.name + wxT("'") ;
req += wxT("AND user='") + user->_id + wxT("'");
EXECUTE_SQL_QUERY(req , set, wxT("0"));
EXECUTE_SQL_QUERY(req , set, wxT("0"));
if (set.NextRow())
res = set.GetAsString(wxT("id"));
else
res = wxT("0");
if (set.NextRow())
res = set.GetAsString(wxT("id"));
else
res = wxT("0");
set.Finalize();
set.Finalize();
return res;
return res;
}
void Database::UpdateAccount(Account ac)
{
wxString req;
req = wxT("UPDATE account SET ") ;
req += wxT("name='") + ac.name + wxT("'");
req += wxT(", number='") + ac.number + wxT("'");
if (ac.shared)
req += wxT(", shared='1'");
else
req += wxT(", shared='0'");
if (ac._default)
req += wxT(", default_account='1'");
else
req += wxT(", default_account='0'");
req += wxT(" WHERE id='") + ac.id + wxT("'");
wxString req;
req = wxT("UPDATE account SET ") ;
req += wxT("name='") + ac.name + wxT("'");
req += wxT(", number='") + ac.number + wxT("'");
if (ac.shared)
req += wxT(", shared='1'");
else
req += wxT(", shared='0'");
if (ac._default)
req += wxT(", default_account='1'");
else
req += wxT(", default_account='0'");
req += wxT(" WHERE id='") + ac.id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
EXECUTE_SQL_UPDATE(req, );
}
void Database::DeleteAccount(Account ac)
{
wxString req;
req = wxT("DELETE FROM account WHERE id='") + ac.id + wxT("'");
wxString req;
req = wxT("DELETE FROM account WHERE id='") + ac.id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
EXECUTE_SQL_UPDATE(req, );
}
wxString Database::AddCategory(User* user, Category category)
@@ -589,17 +589,17 @@ void Database::UpdateCategory(Category category)
void Database::DeleteCategory(User* user, Category category)
{
wxString req;
wxString req;
req = wxT("DELETE FROM category WHERE id='") + category.id + wxT("'");
req = wxT("DELETE FROM category WHERE id='") + category.id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
EXECUTE_SQL_UPDATE(req, );
req = wxT("UPDATE category SET") ;
req += wxT(" parent='0'");
req += wxT(" WHERE parent='") + category.id + wxT("'");
req = wxT("UPDATE category SET") ;
req += wxT(" parent='0'");
req += wxT(" WHERE parent='") + category.id + wxT("'");
EXECUTE_SQL_UPDATE(req, );
EXECUTE_SQL_UPDATE(req, );
}
std::map<int, std::vector<int> > Database::GetAllOperations(User* user)
@@ -838,7 +838,7 @@ void Database::NewUser(wxString name)
/*
Shared accounts not currently supported
*/
*/
void Database::KillMe(User* user)
{
wxString req;

View File

@@ -49,10 +49,10 @@ class Database
void KillMe(User* user);
private:
wxSQLite3Database _db;
wxSQLite3Database _db;
void CreateDatabase();
wxString HashPassword(wxString password);
void CreateDatabase();
wxString HashPassword(wxString password);
};
#endif

View File

@@ -1,6 +1,5 @@
#include "User.h"
User::~User()
{
std::map<unsigned int, std::map<unsigned int, std::vector<Operation> >* >::iterator it;
@@ -49,27 +48,27 @@ wxString User::GetCategoryId(wxString catName)
if (it->name == catName)
return it->id;
return wxT("0") ;
return wxT("0") ;
}
wxString User::GetAccountName(wxString accountId)
{
std::vector<Account>::iterator it;
for (it=_accounts.begin(); it !=_accounts.end(); it++)
if (it->id == accountId)
return it->name;
std::vector<Account>::iterator it;
for (it=_accounts.begin(); it !=_accounts.end(); it++)
if (it->id == accountId)
return it->name;
return _("Unknown") ;
return _("Unknown") ;
}
wxString User::GetAccountId(wxString accountName)
{
std::vector<Account>::iterator it;
for (it=_accounts.begin(); it !=_accounts.end(); it++)
if (it->name == accountName)
return it->id;
std::vector<Account>::iterator it;
for (it=_accounts.begin(); it !=_accounts.end(); it++)
if (it->name == accountName)
return it->id;
return wxT("0") ;
return wxT("0") ;
}
int User::GetCategoriesNumber()

View File

@@ -12,7 +12,7 @@
class User
{
public:
public:
~User();
wxString _id;