From d12e00c6fb42ffdab7656f025480615f3989253a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Sat, 21 Aug 2010 09:54:04 +0200 Subject: [PATCH] Some optimizations on empty wxString comparison and on Search (Database) --- src/model/Database.cpp | 40 ++++++++++++++++++----------------- src/model/User.cpp | 2 +- src/view/AccountPanel.cpp | 18 ++++++++-------- src/view/GenerateDialog.cpp | 4 ++-- src/view/PreferencesPanel.cpp | 10 ++++----- src/view/SearchPanel.cpp | 20 +++++++++--------- src/view/wxUI.cpp | 2 +- 7 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/model/Database.cpp b/src/model/Database.cpp index ae0d8ed..9f62bac 100644 --- a/src/model/Database.cpp +++ b/src/model/Database.cpp @@ -1030,6 +1030,23 @@ std::vector* Database::Search(User* user, wxString* description, wxDa std::vector::iterator it; std::vector::iterator accountIt; std::vector* res = new std::vector; + + wxString dayFrom, monthFrom, yearFrom; + wxString dayTo, monthTo, yearTo; + + 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 "); @@ -1043,9 +1060,8 @@ std::vector* Database::Search(User* user, wxString* description, wxDa { if (firstCond) req += wxT(" AND ") ; else firstCond = true; req += wxT("("); - req += wxT(" year >= '") + wxString::Format(wxT("%d"), dateFrom->GetYear()) + wxT("'"); - //req += wxT(" AND month >= '") + wxString::Format(wxT("%d"), dateFrom->GetMonth()) + wxT("'"); - // req += wxT(" AND day >= '") + wxString::Format(wxT("%d"), dateFrom->GetDay()) + wxT("'"); + req += wxT("year >= ") + yearFrom ; + req += wxT(" AND (month > '") + monthFrom + wxT("' OR (month == '") + monthFrom + wxT("' AND day >= '") + dayFrom + wxT("'))"); req += wxT(")"); } @@ -1053,9 +1069,8 @@ std::vector* Database::Search(User* user, wxString* description, wxDa { if (firstCond) req += wxT(" AND ") ; else firstCond = true; req += wxT("("); - req += wxT(" year <= '") + wxString::Format(wxT("%d"), dateTo->GetYear()) + wxT("'"); - //req += wxT(" AND month <= '") + wxString::Format(wxT("%d"), dateTo->GetMonth()) + wxT("'"); - // req += wxT(" AND day <= '") + wxString::Format(wxT("%d"), dateTo->GetDay()) + wxT("'"); + req += wxT("year <= ") + yearTo ; + req += wxT(" AND (month < '") + monthTo + wxT("' OR (month == '") + monthTo + wxT("' AND day <= '") + dayTo + wxT("'))"); req += wxT(")"); } @@ -1134,19 +1149,6 @@ std::vector* Database::Search(User* user, wxString* description, wxDa op.category = set.GetAsString(wxT("category")); op.fix_cost = set.GetBool(wxT("fix_cost")); op.checked = set.GetBool(wxT("checked")); - - if (dateFrom && - (int)op.year == dateFrom->GetYear() && - (int)op.month <= dateFrom->GetMonth() && - (int)op.day < dateFrom->GetDay()-1) - continue; - - if (dateTo && - (int)op.year == dateTo->GetYear() && - (int)op.month >= dateTo->GetMonth() && - (int)op.day > dateTo->GetDay()-1) - continue; - res->push_back(op); } diff --git a/src/model/User.cpp b/src/model/User.cpp index eca4ac9..cdf3a43 100644 --- a/src/model/User.cpp +++ b/src/model/User.cpp @@ -110,7 +110,7 @@ wxLanguage User::GetLanguage() wxString res = _preferences[wxT("language")]; long val; - if (res == wxT("")) + if (!res.Length()) return wxLANGUAGE_ENGLISH ; res.ToLong(&val); diff --git a/src/view/AccountPanel.cpp b/src/view/AccountPanel.cpp index a3ff9c4..57d5e92 100644 --- a/src/view/AccountPanel.cpp +++ b/src/view/AccountPanel.cpp @@ -536,14 +536,14 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) _grid->SetCellValue(event.GetRow(), DEBIT, wxT("")); value = _grid->GetCellValue(event.GetRow(), DESCRIPTION); - if (value != wxT("")) + if (value.Length()) { new_op.description = value; op_complete--; } value = _grid->GetCellValue(event.GetRow(), DATE); - if (value != wxT("")) + if (value.Length()) { date.ParseFormat(value, wxT("%d/%m/%Y")); new_op.day = date.GetDay()-1; @@ -553,7 +553,7 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) } value = _grid->GetCellValue(event.GetRow(), DEBIT); - if (value != wxT("")) + if (value.Length()) { value.ToDouble(&new_op.amount); new_op.amount *= -1.0; @@ -561,36 +561,36 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) } value = _grid->GetCellValue(event.GetRow(), CREDIT); - if (value != wxT("")) + if (value.Length()) { value.ToDouble(&new_op.amount); op_complete--; } value = _grid->GetCellValue(event.GetRow(), CATEGORY); - if (value != wxT("")) + if (value.Length()) { new_op.category = user->GetCategoryId(value); op_complete--; } value = _grid->GetCellValue(event.GetRow(), ACCOUNT); - if (value != wxT("")) + if (value.Length()) { new_op.account = user->GetAccountId(value); op_complete--; } value = _grid->GetCellValue(event.GetRow(), CHECKED); - if (value != wxT("") && value != wxT("0")) + if (value.Length() && value != wxT("0")) new_op.checked = true; else new_op.checked = false; op_complete--; if (event.GetCol() == DESCRIPTION && - _grid->GetCellValue(event.GetRow(), CATEGORY) == wxT("") && - _grid->GetCellValue(event.GetRow(), ACCOUNT) == wxT("")) + (!_grid->GetCellValue(event.GetRow(), CATEGORY).Length() || + !_grid->GetCellValue(event.GetRow(), ACCOUNT).Length())) { if (_kiss->SearchPreviousOperation(&op_tmp, new_op.description, _curMonth, _curYear)) { diff --git a/src/view/GenerateDialog.cpp b/src/view/GenerateDialog.cpp index 28bf025..70df8ae 100644 --- a/src/view/GenerateDialog.cpp +++ b/src/view/GenerateDialog.cpp @@ -146,7 +146,7 @@ void GenerateDialog::OnYearFromChange(wxCommandEvent& event) _monthFrom->Clear(); - if (years == wxT("")) + if (!years.Length()) { _monthFrom->Append(wxT("")); return; @@ -193,7 +193,7 @@ void GenerateDialog::OnOK(wxCommandEvent& event) { int monthFrom, yearFrom, monthTo, yearTo, i; - if (_yearFrom->GetString(_yearFrom->GetCurrentSelection()) == wxT("")) + if (!_yearFrom->GetString(_yearFrom->GetCurrentSelection()).Length()) { monthFrom = -1; yearFrom = -1; diff --git a/src/view/PreferencesPanel.cpp b/src/view/PreferencesPanel.cpp index 6e22535..dec1e86 100644 --- a/src/view/PreferencesPanel.cpp +++ b/src/view/PreferencesPanel.cpp @@ -277,27 +277,27 @@ void PreferencesPanel::OnAccountModified(wxGridEvent& event) inModification = true; value = _accountsGrid->GetCellValue(row, ACCOUNT_NAME); - if (value != wxT("")) + if (value.Length()) { new_account.name = value; op_complete--; } value = _accountsGrid->GetCellValue(row, ACCOUNT_NUMBER); - if (value != wxT("")) + if (value.Length()) { new_account.number = value; op_complete--; } value = _accountsGrid->GetCellValue(row, ACCOUNT_SHARED); - if (value != wxT("") && value != wxT("0")) + if (value.Length() && value != wxT("0")) new_account.shared = true; else new_account.shared = false; value = _accountsGrid->GetCellValue(row, ACCOUNT_DEFAULT); - if (value != wxT("") && value != wxT("0")) + if (value.Length() && value != wxT("0")) new_account._default = true; else new_account._default = false; @@ -430,7 +430,7 @@ void PreferencesPanel::OnCategoryModified(wxGridEvent& event) inModification = true; value = _categoriesGrid->GetCellValue(row, CATEGORY_NAME); - if (value != wxT("")) + if (value.Length()) { new_cat.name = value; op_complete--; diff --git a/src/view/SearchPanel.cpp b/src/view/SearchPanel.cpp index 75d3a7e..7a45062 100644 --- a/src/view/SearchPanel.cpp +++ b/src/view/SearchPanel.cpp @@ -135,7 +135,7 @@ void SearchPanel::OnButtonSearch(wxCommandEvent& event) return; } - if (_amountFrom->GetLineText(0) != wxT("")) + if (_amountFrom->GetLineText(0).Length()) { amountFrom = new wxString; *amountFrom = _amountFrom->GetLineText(0); @@ -149,7 +149,7 @@ void SearchPanel::OnButtonSearch(wxCommandEvent& event) if (af < 0) af *= -1; } - if (_amountTo->GetLineText(0) != wxT("")) + if (_amountTo->GetLineText(0).Length()) { amountTo = new wxString; *amountTo = _amountTo->GetLineText(0); @@ -174,7 +174,7 @@ void SearchPanel::OnButtonSearch(wxCommandEvent& event) _grid->DeleteRows(1, _grid->GetNumberRows()-1); - if (_description->GetLineText(0) != wxT("")) + if (_description->GetLineText(0).Length()) { description = new wxString; *description = _description->GetLineText(0); @@ -273,14 +273,14 @@ void SearchPanel::OnOperationModified(wxGridEvent& event) _grid->SetCellValue(event.GetRow(), DEBIT, wxT("")); value = _grid->GetCellValue(event.GetRow(), DESCRIPTION); - if (value != wxT("")) + if (value.Length()) { new_op.description = value; op_complete--; } value = _grid->GetCellValue(event.GetRow(), DATE); - if (value != wxT("")) + if (value.Length()) { date.ParseFormat(value, wxT("%d/%m/%Y")); new_op.day = date.GetDay()-1; @@ -290,7 +290,7 @@ void SearchPanel::OnOperationModified(wxGridEvent& event) } value = _grid->GetCellValue(event.GetRow(), DEBIT); - if (value != wxT("")) + if (value.Length()) { value.ToDouble(&new_op.amount); new_op.amount *= -1.0; @@ -298,28 +298,28 @@ void SearchPanel::OnOperationModified(wxGridEvent& event) } value = _grid->GetCellValue(event.GetRow(), CREDIT); - if (value != wxT("")) + if (value.Length()) { value.ToDouble(&new_op.amount); op_complete--; } value = _grid->GetCellValue(event.GetRow(), CATEGORY); - if (value != wxT("")) + if (value.Length()) { new_op.category = user->GetCategoryId(value); op_complete--; } value = _grid->GetCellValue(event.GetRow(), ACCOUNT); - if (value != wxT("")) + if (value.Length()) { new_op.account = user->GetAccountId(value); op_complete--; } value = _grid->GetCellValue(event.GetRow(), CHECKED); - if (value != wxT("") && value != wxT("0")) + if (value.Length() && value != wxT("0")) new_op.checked = true; else new_op.checked = false; diff --git a/src/view/wxUI.cpp b/src/view/wxUI.cpp index 318f9f9..85e17ba 100644 --- a/src/view/wxUI.cpp +++ b/src/view/wxUI.cpp @@ -168,7 +168,7 @@ void wxUI::LoadUser() if (_statsPanel) delete _statsPanel; - if (user->_preferences[wxT("language")] != wxT("")) + if (user->_preferences[wxT("language")].Length()) SetLanguage(user->GetLanguage()); _accountPanel = new AccountPanel(_kiss, this);