/* Copyright 2010 Grégory Soutadé This file is part of KissCount. 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. 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. You should have received a copy of the GNU General Public License along with KissCount. If not, see . */ #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_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, {}, {}) #define ESCAPE_CHARS(s) { \ if (s.Find(wxT("\\\"")) == wxNOT_FOUND) \ s.Replace(wxT("\""), wxT("\\\""), true); \ if (s.Find(wxT("\\\'")) == wxNOT_FOUND) \ s.Replace(wxT("\'"), wxT("\\\'"), true); \ } static inline wxString DoubleToString(double d) { wxString res; res = wxString::Format(wxT("%.2lf"), d); res.Replace(wxT(","), wxT(".")); return res; } Database::Database(const char* filename, KissCount* kiss) : _kiss(kiss) { std::ifstream bdd_file; if (filename) { bdd_file.open(filename, std::ifstream::in); if (!bdd_file.good()) { wxMessageBox(_("Unable to open Database"), _("Error"), wxICON_ERROR | wxOK ); throw std::string("Unable to open ") + filename; } _db.Open(wxString(filename, wxConvUTF8)); if (!_db.IsOpen()) { wxMessageBox(_("Unable to open Database"), _("Error"), wxICON_ERROR | wxOK ); throw std::string("Unable to open ") + filename; } } else { // If default BDD file, assume this can be the first load bdd_file.open(BDD_FILE, std::ifstream::in); if (!bdd_file.good()) { CreateDatabase(); } else { _db.Open(wxT(BDD_FILE)); if (!_db.IsOpen()) { wxMessageBox(_("Unable to open Database"), _("Error"), wxICON_ERROR | wxOK ); throw std::string("Unable to open ") + BDD_FILE; } } } bdd_file.close(); } void Database::CreateDatabase() { std::ifstream init_script; std::string line; wxString wxline; wxString message = _("No database found, would you like to create a new one ?\n\n"); message += _("!! Warning !! If there was a bug, the old database will be suppressed !"); wxMessageDialog dialog(NULL, message, wxT("KissCount"), wxYES_NO); if (dialog.ShowModal() == wxID_NO) throw std::string("No database") ; init_script.open(INIT_SCRIPT); if (!init_script) { wxMessageBox(_(INIT_SCRIPT " not found, aborting"), _("Error"), wxICON_ERROR | wxOK ); throw "init.sql not found, aborting"; } _db.Open(wxT(BDD_FILE)); if (!_db.IsOpen()) { wxMessageBox(_("Unable to open Database"), _("Error"), wxICON_ERROR | wxOK ); throw std::string("Unable to open ") + BDD_FILE; } 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)) { std::cout << line << " is invalid !\n" ; continue; } try { _db.ExecuteUpdate(wxline); } catch (...) { wxMessageBox(_("Error creating original database"), _("Error"), wxICON_ERROR | wxOK ); remove(BDD_FILE); throw line; } } while (init_script); init_script.close(); } wxString Database::HashPassword(const wxString& password) { blk_SHA_CTX sha_ctx; unsigned char sha[20]; wxString wxSHA; blk_SHA1_Init(&sha_ctx); blk_SHA1_Update(&sha_ctx, password.mb_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 Database::GetUsers() { std::list res; wxString req; // Check whether value exists in table wxSQLite3ResultSet set ; req = wxT("SELECT name FROM user ORDER BY name"); EXECUTE_SQL_QUERY(req, set, res); while (set.NextRow()) { res.push_back(set.GetAsString(0)); } set.Finalize(); return res; } bool Database::IsValidUser(const wxString& user, const wxString& password) { bool res; wxString req; wxSQLite3ResultSet set; req = wxT("SELECT name FROM user WHERE name='") + user + wxT("' AND password='") + HashPassword(password) + wxT("'"); EXECUTE_SQL_QUERY(req, set, false); res = set.NextRow() ; set.Finalize(); return res; } User* Database::LoadUser(const wxString& name) { wxSQLite3ResultSet set; wxString req; User* user; Account account; Category category; std::vector::iterator it; req = wxT("SELECT * FROM user WHERE name='") + name + wxT("'"); EXECUTE_SQL_QUERY(req, set, NULL); if (!set.NextRow()) return NULL; user = new User(this); user->_id = set.GetAsString(wxT("id")); user->_name = set.GetAsString(wxT("name")); user->_password = wxT("") ; // Security reasons set.GetAsString("password"); user->_preferences[wxT("operation_order")] = wxT("ASC") ; set.Finalize(); req = wxT("SELECT * FROM account WHERE user='") + user->_id + wxT("' ORDER BY default_account DESC, name ASC"); EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) { 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.blocked = set.GetBool(wxT("blocked")); account._default = set.GetBool(wxT("default_account")); account.is_owner = true; user->_accounts.push_back(account); } set.Finalize(); req = wxT("SELECT * FROM account WHERE id IN (SELECT account FROM shared_account WHERE user='") + user->_id + wxT("') ORDER BY name ASC"); EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) { 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.blocked = set.GetBool(wxT("blocked")); account._default = set.GetBool(wxT("default_account")); account.is_owner = false; user->_accounts.push_back(account); } set.Finalize(); if (!user->_accounts.empty()) { it = user->_accounts.begin(); req = wxT("SELECT DISTINCT year 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(" ORDER BY year ASC"); EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) { user->_operations[set.GetInt(wxT("year"))] = NULL; } set.Finalize(); } req = wxT("SELECT * FROM category WHERE user='") + user->_id + wxT("' ORDER BY name ASC"); EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) { category.id = set.GetAsString(wxT("id")); category.parent = set.GetAsString(wxT("parent")); category.name = set.GetAsString(wxT("name")); category.backcolor = wxColour(set.GetAsString(wxT("backcolor"))); category.forecolor = wxColour(set.GetAsString(wxT("forecolor"))); category.font = set.GetAsString(wxT("font")); if (category.name != _("Fix")) { user->_categories.push_back(category); user->_categoriesFonts.push_back(_kiss->ExtractFont(category.font)); } else { user->_categories.insert(user->_categories.begin(), category); user->_categoriesFonts.insert(user->_categoriesFonts.begin(), _kiss->ExtractFont(category.font)); } } set.Finalize(); req = wxT("SELECT name, value FROM preference WHERE user='") + user->_id + wxT("' ORDER BY value ASC"); EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) user->_preferences[set.GetAsString(wxT("name"))] = set.GetAsString(wxT("value")); set.Finalize(); return user; } void Database::LoadYear(User* user, int year) { wxSQLite3ResultSet set; wxString req; std::vector::iterator it; if (user->_operations[year] == NULL) user->_operations[year] = new std::map >(); if (!user->_accounts.size()) return; it = user->_accounts.begin(); req = wxT("SELECT * 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("'"); req += wxT(" ORDER BY fix_cost DESC, year, month ASC, day "); req += user->_preferences[wxT("operation_order")]; EXECUTE_SQL_QUERY(req, set, ); while (set.NextRow()) { Operation op; op.id = set.GetAsString(wxT("id")); op.parent = set.GetAsString(wxT("parent")); 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")); op.transfert = set.GetAsString(wxT("transfert")); op.formula = set.GetAsString(wxT("formula")); op.meta = set.GetBool(wxT("meta")); (*user->_operations[op.year])[op.month].push_back(op); } user->ResolveGroups(year); set.Finalize(); } double Database::GetAccountAmount(const wxString& id, int month, int year) { wxSQLite3ResultSet set; wxString req; double res; 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("'"); EXECUTE_SQL_QUERY(req , set, 0.0); if (set.NextRow()) res = set.GetDouble(wxT("amount")); else { SetAccountAmount(month, year, id, 0.0); res = 0.0; } set.Finalize(); return res; } bool Database::GetOperation(const wxString& id, Operation* op) { wxSQLite3ResultSet set; wxString req; req = wxT("SELECT * FROM operation WHERE id='") + id + wxT("'"); EXECUTE_SQL_QUERY(req, set, false); if (!set.NextRow()) return false; op->id = set.GetAsString(wxT("id")); op->parent = set.GetAsString(wxT("parent")); 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")); op->transfert = set.GetAsString(wxT("transfert")); op->formula = set.GetAsString(wxT("formula")); op->meta = set.GetBool(wxT("meta")); return true; } void Database::LinkOrUnlinkOperation(Operation& op) { Operation linked; wxString req; wxSQLite3ResultSet set; if (op.transfert.Length()) { // No one or not linked if (!GetOperation(op.transfert, &linked) || op.description != linked.description || op.amount != -linked.amount || op.account == linked.account) { req = wxT("UPDATE operation SET transfert='' where id='") + op.id + wxT("'") ; EXECUTE_SQL_UPDATE(req, ); op.transfert = wxT(""); return; } } // Not Linked else { req = wxT("SELECT id FROM operation WHERE transfert='") + op.id + wxT("'"); EXECUTE_SQL_QUERY(req, set, ); if (set.NextRow()) { req = wxT("UPDATE operation SET transfert='' where id='") + set.GetAsString(wxT("id")) + wxT("'") ; EXECUTE_SQL_UPDATE(req, ); } req = wxT("SELECT id FROM operation WHERE description=\"") + op.description + wxT("\""); req += wxT(" AND month='") + wxString::Format(wxT("%d"), op.month) + wxT("'"); req += wxT(" AND year='") + wxString::Format(wxT("%d"), op.year) + wxT("'"); req += wxT(" AND amount='") + DoubleToString(-op.amount) + wxT("'"); req += wxT(" AND meta='0'"); req += wxT(" AND account !='") + op.account + wxT("'"); req += wxT(" AND (transfert='' OR transfert IS NULL)"); EXECUTE_SQL_QUERY(req, set, ); // Don't need to link if (!set.NextRow()) return ; // Link linked.id = set.GetAsString(wxT("id")); op.transfert = linked.id; req = wxT("UPDATE operation SET transfert='") + linked.id + wxT("' where id='") + op.id + wxT("'") ; EXECUTE_SQL_UPDATE(req, ); req = wxT("UPDATE operation SET transfert='") + op.id + wxT("' where id='") + linked.id + wxT("'") ; EXECUTE_SQL_UPDATE(req, ); } } void Database::UpdateOperation(Operation& op) { wxString req; LinkOrUnlinkOperation(op); ESCAPE_CHARS(op.description); req = wxT("UPDATE operation SET ") ; req += wxT("parent='") + op.parent + wxT("'"); 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(", transfert='") + op.transfert + wxT("'"); if (op.meta) req += wxT(", meta='1'"); else req += wxT(", meta='0'"); req += wxT(", formula='") + op.formula + wxT("'"); req += wxT(" WHERE id='") + op.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); LinkOrUnlinkOperation(op); } wxString Database::AddOperation(User* user, Operation& op) { wxString req, res; wxSQLite3ResultSet set; ESCAPE_CHARS(op.description); req = wxT("INSERT INTO operation ('user', 'parent', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost', 'formula', 'transfert', 'meta') VALUES ('") ; req += user->_id + wxT("'"); req += wxT(", '") + op.parent + 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(", '") + op.formula + wxT("'"); req += wxT(", '") + op.transfert + wxT("'"); if (op.meta) req += wxT(", '1'") ; else req += wxT(", '0'") ; req += wxT(")"); EXECUTE_SQL_UPDATE(req, wxT("0")); res = _db.GetLastRowId().ToString(); op.id = res; LinkOrUnlinkOperation(op); return res; } void Database::DeleteOperation(Operation& op) { wxString req; req = wxT("DELETE FROM operation WHERE id='") + op.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); LinkOrUnlinkOperation(op); } void Database::DeleteOperations(User* user, int month, int year) { wxString req; std::vector::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("'"); 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("'"); EXECUTE_SQL_UPDATE(req, ); } bool Database::LoadOperation(User* user, const wxString& id) { wxSQLite3ResultSet set; wxString req; bool ret = false; std::vector::iterator it; req = wxT("SELECT * FROM operation WHERE id='") + id + wxT("'"); EXECUTE_SQL_QUERY(req, set, false); if (set.NextRow()) { Operation op; op.id = set.GetAsString(wxT("id")); op.parent = set.GetAsString(wxT("parent")); 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")); op.transfert = set.GetAsString(wxT("transfert")); op.formula = set.GetAsString(wxT("formula")); op.meta = set.GetBool(wxT("meta")); for (it = (*user->_operations[op.year])[op.month].begin(); it != (*user->_operations[op.year])[op.month].end(); it++) { if (!op.fix_cost && it->fix_cost) continue; if (op.fix_cost && !it->fix_cost) { it--; break; } if (it->day > op.day) { it--; break; } } if (it == (*user->_operations[op.year])[op.month].end()) (*user->_operations[op.year])[op.month].push_back(op); else (*user->_operations[op.year])[op.month].insert(it, op); // if (op.fix_cost) // else // (*user->_operations[op.year])[op.month].push_back(op); ret = true; } set.Finalize(); return ret; } // We may not have access to all operations if we have a shared account double Database::MetaAmount(const wxString& id) { wxSQLite3ResultSet set; wxString req; double res = 0.0; req = wxT("SELECT SUM(amount) as amount FROM operation WHERE parent='") + id + wxT("'"); EXECUTE_SQL_QUERY(req, set, false); if (set.NextRow()) res = set.GetDouble(wxT("amount")); set.Finalize(); return res; } // Idem double Database::MetaPositiveAmount(const wxString& id) { wxSQLite3ResultSet set; wxString req; double res = 0.0; req = wxT("SELECT SUM(amount) as amount FROM operation WHERE amount > 0 AND parent='") + id + wxT("'"); EXECUTE_SQL_QUERY(req, set, false); if (set.NextRow()) res = set.GetDouble(wxT("amount")); set.Finalize(); return res; } void Database::SetAccountAmount(int month, int year, const 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("'"); 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) { wxString req; req = wxT("INSERT INTO account ('user', 'name', 'number', 'shared', 'blocked', '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.blocked) 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")); return _db.GetLastRowId().ToString(); } 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.blocked) req += wxT(", blocked='1'"); else req += wxT(", blocked='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, ); if (!ac.shared && ac.is_owner) { req = wxT("DELETE FROM shared_account WHERE account='") + ac.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } } void Database::DeleteAccount(User* user, Account& ac) { wxString req; if (ac.is_owner) { if (ac.shared) { req = wxT("DELETE FROM shared_account WHERE account='") + ac.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } req = wxT("DELETE FROM account WHERE id='") + ac.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); req = wxT("DELETE FROM account_amount WHERE account='") + ac.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } else RemoveSharedAccount(ac, user->_id); } void Database::AddSharedAccount(Account& ac, const wxString& granted) { wxString req; wxSQLite3ResultSet set; req = wxT("SELECT id FROM user WHERE name='") + granted + wxT("'"); EXECUTE_SQL_QUERY(req, set, ); req = wxT("INSERT INTO shared_account ('account', 'user') VALUES ('") + ac.id + wxT("', '") + set.GetAsString(wxT("id")) + wxT("')"); EXECUTE_SQL_UPDATE(req, ); if (!ac.shared) { ac.shared = true; UpdateAccount(ac); } } void Database::RemoveSharedAccount(Account& ac, const wxString& granted) { wxString req; wxSQLite3ResultSet set; req = wxT("DELETE FROM shared_account WHERE user='") + granted + wxT("' AND account='") + ac.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); req = wxT("SELECT COUNT(user) AS cnt FROM shared_account WHERE account='") + ac.id + wxT("'"); EXECUTE_SQL_QUERY(req, set, ); if (!set.GetInt(wxT("cnt"))) { ac.shared = false; UpdateAccount(ac); } } wxString Database::AddCategory(User* user, Category& category) { wxString req; wxString backcolor, forecolor; backcolor = wxT("#") ; backcolor += wxString::Format(wxT("%02X"), category.backcolor.Red()); backcolor += wxString::Format(wxT("%02X"), category.backcolor.Green()); backcolor += wxString::Format(wxT("%02X"), category.backcolor.Blue()); forecolor = wxT("#") ; forecolor += wxString::Format(wxT("%02X"), category.forecolor.Red()); forecolor += wxString::Format(wxT("%02X"), category.forecolor.Green()); forecolor += wxString::Format(wxT("%02X"), category.forecolor.Blue()); req = wxT("INSERT INTO category ('user', 'parent', 'name', 'backcolor', 'forecolor', font) VALUES ('") ; req += user->_id + wxT("'"); req += wxT(", '") + category.parent + wxT("'"); req += wxT(", '") + category.name + wxT("'"); req += wxT(", '") + backcolor + wxT("'"); req += wxT(", '") + forecolor + wxT("'"); req += wxT(", '") + category.font + wxT("'"); req += wxT(")"); EXECUTE_SQL_UPDATE(req, wxT("0")); return _db.GetLastRowId().ToString(); } void Database::UpdateCategory(Category& category) { wxString req; wxString backcolor, forecolor; backcolor = wxT("#") ; backcolor += wxString::Format(wxT("%02X"), category.backcolor.Red()); backcolor += wxString::Format(wxT("%02X"), category.backcolor.Green()); backcolor += wxString::Format(wxT("%02X"), category.backcolor.Blue()); forecolor = wxT("#") ; forecolor += wxString::Format(wxT("%02X"), category.forecolor.Red()); forecolor += wxString::Format(wxT("%02X"), category.forecolor.Green()); forecolor += wxString::Format(wxT("%02X"), category.forecolor.Blue()); req = wxT("UPDATE category SET") ; req += wxT(" parent='") + category.parent + wxT("'"); req += wxT(", name='") + category.name + wxT("'"); req += wxT(", backcolor='") + backcolor + wxT("'"); req += wxT(", forecolor='") + forecolor + wxT("'"); req += wxT(", font='") + category.font + wxT("'"); req += wxT(" WHERE id='") + category.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } void Database::DeleteCategory(User* user, Category& category) { wxString req; req = wxT("DELETE FROM category WHERE id='") + category.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); req = wxT("UPDATE category SET") ; req += wxT(" parent='0'"); req += wxT(" WHERE parent='") + category.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } bool Database::LoadCategory(const wxString& id, const wxString& name, Category& category) { wxSQLite3ResultSet set; wxString req; bool ret = false ; if (id.Length()) req = wxT("SELECT * FROM category WHERE id='") + id + wxT("'"); else req = wxT("SELECT * FROM category WHERE name='") + name + wxT("'"); EXECUTE_SQL_QUERY(req, set, false); if (set.NextRow()) { category.id = set.GetAsString(wxT("id")); category.parent = set.GetAsString(wxT("parent")); category.name = set.GetAsString(wxT("name")); category.backcolor = wxColour(set.GetAsString(wxT("backcolor"))); category.forecolor = wxColour(set.GetAsString(wxT("forecolor"))); category.font = set.GetAsString(wxT("font")); ret = true; } set.Finalize(); return ret; } std::map > Database::GetAllOperations(User* user) { wxString req, req2, reqUnion; wxSQLite3ResultSet set, set2; std::vector::iterator it; std::map > res; int year; if (!user->_accounts.empty()) { it = user->_accounts.begin(); req = wxT("SELECT DISTINCT year FROM account_amount WHERE account IN('") + it->id; it++; for (;it != user->_accounts.end(); it++) { req += wxT("', '") + it->id ; } req += wxT("')"); 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); while (set.NextRow()) { year = set.GetInt(wxT("year")); it = user->_accounts.begin(); req = wxT("SELECT DISTINCT month 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='") + set.GetAsString(wxT("year")) + wxT("'"); 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); while (set2.NextRow()) { res[year].push_back(set2.GetInt(wxT("month"))); } set2.Finalize(); } set.Finalize(); } return res; } void Database::GenerateMonth(User* user, int monthFrom, int yearFrom, int monthTo, int yearTo) { std::vector::iterator it; wxString req; wxSQLite3ResultSet set; double amount; if (monthFrom == -1 || yearFrom == -1) { for (it = user->_accounts.begin(); it != user->_accounts.end(); it++) { 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(")"); EXECUTE_SQL_UPDATE(req, ); } return; } for (it = user->_accounts.begin(); it != user->_accounts.end(); it++) { amount = 0.0; req = wxT("SELECT SUM(amount) AS total FROM operation 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("'"); req += wxT(" AND meta='0'"); EXECUTE_SQL_QUERY(req, set, ); if (set.NextRow()) amount += set.GetDouble(wxT("total")); 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("'"); EXECUTE_SQL_QUERY(req, set, ); if (set.NextRow()) amount += set.GetDouble(wxT("amount")); 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(")"); EXECUTE_SQL_UPDATE(req, ); } } void Database::ChangePassword(User* user, const wxString& password) { wxString req; req = wxT("UPDATE user SET ") ; req += wxT("password='") + HashPassword(password) + wxT("'"); req += wxT(" WHERE name='") + user->_name + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } bool Database::UserExists(const wxString& name) { wxSQLite3ResultSet set; wxString req; bool res=false; req = wxT("SELECT name FROM user WHERE name='") + name + wxT("'") ; EXECUTE_SQL_QUERY(req , set, false); if (set.NextRow()) res = true; else res = false; set.Finalize(); return res; } void Database::ChangeName(User* user, const wxString& name) { wxString req; req = wxT("UPDATE user SET ") ; req += wxT("name='") + name + wxT("'"); req += wxT(" WHERE name='") + user->_name + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } void Database::NewUser(const wxString& name) { wxString req; req = wxT("INSERT INTO user ('name', 'password') VALUES ('") ; req += name + wxT("'"); req += wxT(", '") + HashPassword(wxT("")) + wxT("'"); req += wxT(")"); EXECUTE_SQL_UPDATE(req, ); } /* Shared accounts not currently supported */ void Database::KillMe(User* user) { wxString req; std::vector::iterator it; req = wxT("DELETE FROM preference WHERE user='") + user->_id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); if (!user->_accounts.empty()) { for (it = user->_accounts.begin(); it != user->_accounts.end(); it++) DeleteAccount(user, *it); it = user->_accounts.begin(); if (it->is_owner) req = wxT("DELETE FROM operation WHERE account IN('") + it->id; else req = wxT("DELETE FROM operation WHERE account IN('-1"); it++; for (;it != user->_accounts.end(); it++) { if (it->is_owner) req += wxT("', '") + it->id ; } req += wxT("')"); req += wxT(" OR (user='") + user->_id + wxT("' AND account='')"); EXECUTE_SQL_UPDATE(req, ); } req = wxT("DELETE FROM category WHERE user='") + user->_id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); req = wxT("DELETE FROM user WHERE id='") + user->_id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } void Database::UpdatePreference(User* user, const wxString& preference) { wxString req; wxString value = user->_preferences[preference]; req = wxT("UPDATE preference SET ") ; req += wxT("name='") + preference + wxT("'"); req += wxT(", value='") + value + wxT("'"); req += wxT(" WHERE user='") + user->_id + wxT("'"); try { if (!_db.ExecuteUpdate(req)) { req = wxT("INSERT INTO preference ('user', 'name', 'value') VALUES ('") ; req += user->_id + wxT("'"); req += wxT(" ,'") + preference + wxT("'"); req += wxT(" ,'") + value + wxT("'"); req += wxT(")"); EXECUTE_SQL_UPDATE(req, ); } } catch (wxSQLite3Exception e) { std::cerr << req.mb_str() << "\n" ; std::cerr << e.GetMessage().mb_str() << "\n" ; return ; } } std::vector* Database::Search(User* user, wxString* description, wxDateTime* dateFrom, wxDateTime* dateTo, wxString* amountFrom, wxString* amountTo, std::vector categories, int types, std::vector accounts, bool wildcards) { wxSQLite3ResultSet set; wxString req; bool firstCond = false; std::vector::iterator it; std::vector::iterator accountIt; std::vector* res = new std::vector; int i; wxString dayFrom, monthFrom, yearFrom; wxString dayTo, monthTo, yearTo; wxString desc; if (dateFrom) { dayFrom = wxString::Format(wxT("%d"), dateFrom->GetDay()-1); monthFrom = wxString::Format(wxT("%d"), dateFrom->GetMonth()); yearFrom = wxString::Format(wxT("%d"), dateFrom->GetYear()); } if (dateTo) { dayTo = wxString::Format(wxT("%d"), dateTo->GetDay()-1); monthTo = wxString::Format(wxT("%d"), dateTo->GetMonth()); yearTo = wxString::Format(wxT("%d"), dateTo->GetYear()); } req = wxT("SELECT * from operation WHERE "); if (description) { desc = *description; ESCAPE_CHARS(desc); if (wildcards) req += wxT("description LIKE '%") + desc + wxT("%'"); else req += wxT("description=\"") + desc + wxT("\""); firstCond = true; } if (dateFrom) { if (firstCond) req += wxT(" AND ") ; else firstCond = true; req += wxT("("); req += wxT("year >= ") + yearFrom ; req += wxT(" AND (month > '") + monthFrom + wxT("' OR (month == '") + monthFrom + wxT("' AND day >= '") + dayFrom + wxT("'))"); req += wxT(")"); } if (dateTo) { if (firstCond) req += wxT(" AND ") ; else firstCond = true; req += wxT("("); req += wxT("year <= ") + yearTo ; req += wxT(" AND (month < '") + monthTo + wxT("' OR (month == '") + monthTo + wxT("' AND day <= '") + dayTo + wxT("'))"); req += wxT(")"); } if (amountFrom) { if (firstCond) req += wxT(" AND ") ; else firstCond = true; req += wxT("ABS(amount) >= ") + *amountFrom; } if (amountTo) { if (firstCond) req += wxT(" AND ") ; else firstCond = true; req += wxT("ABS(amount) <= ") + *amountTo; } if (categories.size()) { if (firstCond) req += wxT(" AND ") ; else firstCond = true; req += wxT("category IN ('"); it = categories.begin(); req += *it; it++; for (; it != categories.end(); it++) req += wxT("', '") + *it ; req += wxT("')"); } if (types) { if ((types & FIX_OP) ^ (types & NON_FIX_OP)) { if (firstCond) req += wxT(" AND ") ; else firstCond = true; if (types & FIX_OP) req += wxT(" fix_cost='1'"); else req += wxT(" fix_cost='0'"); } if ((types & CHECKED_OP) ^ (types & NOT_CHECKED_OP)) { if (firstCond) req += wxT(" AND ") ; else firstCond = true; if (types & CHECKED_OP) req += wxT(" checked='1'"); else req += wxT(" checked='0'"); } } if (firstCond) req += wxT(" AND ") ; else firstCond = true; if (accounts.size()) { req += wxT("(account IN ('"); it = accounts.begin(); req += *it; it++; for (; it != accounts.end(); it++) req += wxT("', '") + *it ; req += wxT("')"); req += wxT(" OR user ='") + user->_id + wxT("')"); } else { req += wxT("(account IN ('"); accountIt = user->_accounts.begin(); req += accountIt->id; accountIt++; for (;accountIt != user->_accounts.end(); accountIt++) { req += wxT("', '") + accountIt->id ; } req += wxT("')"); req += wxT(" OR user ='") + user->_id + wxT("')"); } req += wxT(" ORDER BY year ") ; req += user->_preferences[wxT("operation_order")] ; req += wxT(", month ") + user->_preferences[wxT("operation_order")] ; req += wxT(", day ") + user->_preferences[wxT("operation_order")] ; // std::cout << req.mb_str() << "\n"; EXECUTE_SQL_QUERY(req, set, res); while (set.NextRow()) { Operation op; op.id = set.GetAsString(wxT("id")); op.parent = set.GetAsString(wxT("parent")); 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")); op.transfert = set.GetAsString(wxT("transfert")); op.formula = set.GetAsString(wxT("formula")); op.meta = set.GetBool(wxT("meta")); res->push_back(op); } for(i=0; i < (int)res->size(); i++) if ((*res)[i].parent.Length()) user->Group(res, (*res)[i]); return res; } void Database::GetStats(User* user, const wxString& monthFrom, const wxString& yearFrom, const wxString& monthTo, const wxString& yearTo, std::map > >* accountAmounts, std::map* categories) { wxSQLite3ResultSet set; wxString req, req2; std::vector::iterator accountIt; std::vector::iterator categoryIt; if (!user->_accounts.empty()) { if (accountAmounts) { for (accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++) { req = wxT("SELECT month, year, amount FROM account_amount WHERE account ='") + accountIt->id + wxT("'"); req += wxT(" AND (year > '") + yearFrom + wxT("' OR (year == '") + yearFrom + wxT("' AND month >= '") + monthFrom + wxT("'))"); req += wxT(" AND (year < '") + yearTo + wxT("' OR (year == '") + yearTo + wxT("' AND month <= '") + monthTo + wxT("'))"); EXECUTE_SQL_QUERY(req, set, ); while (set.NextRow()) { (*accountAmounts)[accountIt->id][set.GetInt(wxT("year"))][set.GetInt(wxT("month"))] = set.GetInt(wxT("amount")); } set.Finalize(); } } if (categories) { for (categoryIt = user->_categories.begin(); categoryIt != user->_categories.end(); categoryIt++) { req = wxT("SELECT SUM(amount) as amount FROM operation AS o1 WHERE category='") + categoryIt->id + wxT("'"); accountIt = user->_accounts.begin(); req += wxT(" AND (account IN('") + accountIt->id; accountIt++; for (;accountIt != user->_accounts.end(); accountIt++) { req += wxT("', '") + accountIt->id ; } req += wxT("')"); req += wxT(" OR user='") + user->_id + wxT("')"); req += wxT(" AND (year > '") + yearFrom + wxT("' OR (year == '") + yearFrom + wxT("' AND month >= '") + monthFrom + wxT("'))"); req += wxT(" AND (year < '") + yearTo + wxT("' OR (year == '") + yearTo + wxT("' AND month <= '") + monthTo + wxT("'))"); req += wxT(" AND meta='0'"); req2 = req + wxT(" AND (transfert='' OR transfert IS NULL)"); req2 += wxT(" AND amount < 0"); EXECUTE_SQL_QUERY(req2, set, ); if (set.NextRow()) { (*categories)[categoryIt->id] = -set.GetDouble(wxT("amount")); } set.Finalize(); // Transfert on blocked accounts must be computed req2 = req + wxT(" AND transfert != ''"); req2 = req + wxT(" AND (SELECT blocked FROM account WHERE id=o1.account)"); req2 += wxT(" AND amount > 0"); EXECUTE_SQL_QUERY(req2, set, ); if (set.NextRow()) { (*categories)[categoryIt->id] += set.GetDouble(wxT("amount")); } set.Finalize(); } } } } void Database::GetMonthStats(User* user, const wxString& month, const wxString& year, int nbDays, std::map >* operations, std::map* categories) { wxSQLite3ResultSet set; wxString req; std::vector::iterator accountIt; int previous_amount, previous_day, cur_day; if (!user->_accounts.empty()) { for (accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++) { req = wxT("SELECT amount FROM account_amount WHERE account ='") + accountIt->id + wxT("'"); req += wxT(" AND year = '") + year + wxT("'"); req += wxT(" AND month = '") + month + wxT("'"); EXECUTE_SQL_QUERY(req, set, ); while (set.NextRow()) { (*operations)[accountIt->id].clear(); previous_amount = set.GetInt(wxT("amount")); } set.Finalize(); req = wxT("SELECT day, amount FROM operation WHERE"); req += wxT(" account = '") + accountIt->id + wxT("'"); req += wxT(" AND year = '") + year + wxT("'"); req += wxT(" AND month = '") + month + wxT("'"); req += wxT(" AND meta='0'"); req += wxT(" ORDER BY day ASC"); EXECUTE_SQL_QUERY(req, set, ); cur_day = previous_day = -1; while (set.NextRow()) { cur_day = set.GetInt(wxT("day")); while (cur_day != previous_day) { (*operations)[accountIt->id].push_back(previous_amount); previous_day++; } previous_amount += set.GetDouble(wxT("amount")); (*operations)[accountIt->id][cur_day] = previous_amount; } set.Finalize(); while (cur_day < nbDays) { (*operations)[accountIt->id].push_back(previous_amount); cur_day++; } } } // Fill categories GetStats(user, month, year, month, year, NULL, categories) ; } std::map* Database::GetNotChecked(User* user, int month, int year) { std::vector::iterator accountIt; std::map* res = new std::map; wxSQLite3ResultSet set; wxString req; for (accountIt = user->_accounts.begin() ;accountIt != user->_accounts.end(); accountIt++) { req = wxT("SELECT SUM(amount) AS amount FROM operation WHERE account='") + accountIt->id + wxT("'"); req += wxT(" AND checked='0'"); req += wxT(" AND meta='0'"); req += wxT(" AND (year < '") + wxString::Format(wxT("%d"), year) + wxT("'") ; req += wxT(" OR (year == '") + wxString::Format(wxT("%d"), year) + wxT("'") ; req += wxT(" AND month < '") + wxString::Format(wxT("%d"), month) + wxT("'") ; req += wxT("))"); EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, delete res, delete res); if (set.NextRow()) (*res)[accountIt->id] = set.GetDouble(wxT("amount")); } return res; } std::map Database::getSharedAccountOwners(const wxString& account) { std::map res; wxSQLite3ResultSet set, set2; wxString req; req = wxT("SELECT user FROM shared_account WHERE account='") + account + wxT("'"); EXECUTE_SQL_QUERY(req, set, res); while(set.NextRow()) { req = wxT("SELECT name FROM user WHERE id='") + set.GetAsString(wxT("user")) + wxT("'"); EXECUTE_SQL_QUERY(req, set2, res); res[set2.GetAsString(wxT("name"))] = set.GetAsString(wxT("user")); } return res; } wxString Database::getSharedAccountOwner(const wxString& account) { wxSQLite3ResultSet set, set2; wxString req; req = wxT("SELECT user FROM account WHERE id='") + account + wxT("'"); EXECUTE_SQL_QUERY(req, set, wxT("")); while(set.NextRow()) { req = wxT("SELECT name FROM user WHERE id='") + set.GetAsString(wxT("user")) + wxT("'"); EXECUTE_SQL_QUERY(req, set2, wxT("")); } return set2.GetAsString(wxT("name")); }