Update database schema

Update translations
Don't multiple account_amount at startup
Use INERT or REPLACE in queries
This commit is contained in:
2012-04-28 12:25:17 +02:00
parent 20245fec2e
commit 12cc163459
9 changed files with 177 additions and 188 deletions
+4 -9
View File
@@ -98,9 +98,9 @@ User* KissCount::GetUser()
return _user;
}
double KissCount::GetAccountAmount(int id, int month, int year)
double KissCount::GetAccountAmount(int id, int month, int year, bool* had_value)
{
return _db->GetAccountAmount(id, month, year);
return _db->GetAccountAmount(id, month, year, had_value);
}
double KissCount::CalcAccountAmount(int id, int month, int year, bool* had_values)
@@ -179,7 +179,7 @@ int KissCount::AddAccount(Account& ac)
ac.id = _db->AddAccount(_user, ac);
_user->AddAccount(ac);
SetAccountAmount(ac.id, curDate.month(), curDate.year(), 0.0);
SetAccountAmount(ac.id, curDate.month()-1, curDate.year(), 0.0);
return ac.id;
}
@@ -188,9 +188,6 @@ void KissCount::UpdateAccount(Account& ac)
{
_db->UpdateAccount(ac);
_user->UpdateAccount(ac);
// if (ac._default)
// std::sort(_user->_accounts.begin(), _user->_accounts.end(), Account());
}
void KissCount::DeleteAccount(Account& ac, int replacement)
@@ -338,7 +335,7 @@ QString default_cats[] = {
void KissCount::NewUser(const QString& name)
{
QDate curDate = QDate::currentDate();
// QDate curDate = QDate::currentDate();
Account ac = {
/*.id = */0,
/*.name = */_("Account 1"),
@@ -369,8 +366,6 @@ void KissCount::NewUser(const QString& name)
AddCategory(cat);
SetOperationOrder("ASC");
_db->GenerateMonth(_user, -1, -1, (int)curDate.month(), curDate.year());
}
void KissCount::KillMe()
+1 -1
View File
@@ -75,7 +75,7 @@ public:
double MetaAmount(int id);
double MetaPositiveAmount(int id);
double GetAccountAmount(int id, int month, int year);
double GetAccountAmount(int id, int month, int year, bool* had_values=NULL);
void SetAccountAmount(int accountId, int month, int year, double value);
double CalcAccountAmount(int id, int month, int year, bool* had_values);
+37 -50
View File
@@ -275,7 +275,7 @@ User* Database::LoadUser(const QString& name)
query.clear();
req = QString("SELECT * FROM account WHERE user='%1' ORDER BY default_account DESC, virtual, blocked, name ASC").arg(user->_id);
req = QString("SELECT * FROM account WHERE user='%1' ORDER BY default_account DESC, blocked, virtual, name ASC").arg(user->_id);
EXECUTE_SQL_QUERY_WITH_CODE(req, 0, {delete user;}, {delete user;});
@@ -406,7 +406,7 @@ void Database::LoadYear(User* user, int year)
query.clear();
}
double Database::GetAccountAmount(int id, int month, int year)
double Database::GetAccountAmount(int id, int month, int year, bool* had_value)
{
QSqlRecord set;
QString req;
@@ -422,9 +422,15 @@ double Database::GetAccountAmount(int id, int month, int year)
{
set = query.record();
res = set.value("amount").toDouble();
if (had_value)
*had_value = true;
}
else
{
SetAccountAmount(id, month, year, 0.0);
if (had_value)
*had_value = false;
}
query.clear();
@@ -759,15 +765,9 @@ void Database::SetAccountAmount(int accountId, int month, int year, double amoun
QString req;
QSqlQuery query(_db);
req = QString("UPDATE account_amount SET amount='%1' WHERE account='%2' AND month='%3' AND year='%4'").
arg(DoubleToString(amount), QString::number(accountId), QString::number(month), QString::number(year));
if (!query.exec(req))
{
req = "INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('%1', '%2', '%3', '%4')" ;
req = req.arg(QString::number(accountId), QString::number(year), QString::number(month), DoubleToString(amount));
EXECUTE_SQL_UPDATE(req, );
}
req = "INSERT or REPLACE INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('%1', '%2', '%3', '%4')" ;
req = req.arg(QString::number(accountId), QString::number(year), QString::number(month), DoubleToString(amount));
EXECUTE_SQL_UPDATE(req, );
}
int Database::AddAccount(User* user, Account& ac)
@@ -936,18 +936,30 @@ int Database::AddCategory(User* user, Category& category)
void Database::UpdateCategory(Category& category)
{
QString req;
QString req, tmp;
QString backcolor, forecolor;
backcolor = "#" ;
backcolor += QString::number(category.backcolor.red(), 16);
backcolor += QString::number(category.backcolor.green(), 16);
backcolor += QString::number(category.backcolor.blue(), 16);
tmp = QString::number(category.backcolor.red(), 16);
if (tmp.length() == 1) tmp = "0" + tmp;
backcolor += tmp;
tmp = QString::number(category.backcolor.green(), 16);
if (tmp.length() == 1) tmp = "0" + tmp;
backcolor += tmp;
tmp = QString::number(category.backcolor.blue(), 16);
if (tmp.length() == 1) tmp = "0" + tmp;
backcolor += tmp;
forecolor = "#" ;
forecolor += QString::number(category.forecolor.red(), 16);
forecolor += QString::number(category.forecolor.green(), 16);
forecolor += QString::number(category.forecolor.blue(), 16);
tmp = QString::number(category.forecolor.red(), 16);
if (tmp.length() == 1) tmp = "0" + tmp;
forecolor += tmp;
tmp = QString::number(category.forecolor.green(), 16);
if (tmp.length() == 1) tmp = "0" + tmp;
forecolor += tmp;
tmp = QString::number(category.forecolor.blue(), 16);
if (tmp.length() == 1) tmp = "0" + tmp;
forecolor += tmp;
req = "UPDATE category SET" ;
req += " parent='" + QString::number(category.parent) + "'";
@@ -1104,20 +1116,6 @@ void Database::GenerateMonth(User* user, int monthFrom, int yearFrom, int monthT
double amount;
QSqlQuery query(_db);
if (monthFrom == -1 || yearFrom == -1)
{
for (it = user->_accounts.begin(); it != user->_accounts.end(); it++)
{
req = "INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES " ;
req += "('%1', '%2', '%3', '%4')";
req = req.arg(QString::number(it->id), QString::number(yearTo),
QString::number(monthTo), "0.0");
EXECUTE_SQL_UPDATE(req, );
}
return;
}
for (it = user->_accounts.begin(); it != user->_accounts.end(); it++)
{
amount = 0.0;
@@ -1249,14 +1247,9 @@ void Database::UpdatePreference(User* user, const QString& preference)
QString value = user->_preferences[preference];
QSqlQuery query(_db);
req = QString("UPDATE preference SET name='%1', value='%2' WHERE user='%3'").arg(preference, value, QString::number(user->_id)) ;
if (!query.exec(req))
{
req = "INSERT INTO preference ('user', 'name', 'value') VALUES ('%1', '%2', '%3')" ;
req = req.arg(QString::number(user->_id), preference, value);
EXECUTE_SQL_UPDATE(req, );
}
req = "INSERT or REPLACE INTO preference ('user', 'name', 'value') VALUES ('%1', '%2', '%3')" ;
req = req.arg(QString::number(user->_id), preference, value);
EXECUTE_SQL_UPDATE(req, );
}
std::vector<Operation>* Database::Search(User* user, QString* description, QDate* dateFrom, QDate* dateTo,
@@ -1714,16 +1707,10 @@ void Database::UpdateImportPattern(User* user)
key = ImportEngine::RemoveUnused(it->first);
req = "UPDATE import_pattern SET pattern='%1', account='%2', category='%3' WHERE description='%4'" ;
req = req.arg(it->second.pattern, QString::number(it->second.account), QString::number(it->second.category), key);
if (!query.exec(req))
{
req = "INSERT INTO import_pattern ('user', 'description', 'pattern', 'account', 'category') VALUES " ;
req += "('%1', '%2', '%3', '%4', '%5')";
req = req.arg(QString::number(user->_id), key, it->second.pattern, QString::number(it->second.account), QString::number(it->second.category));
EXECUTE_SQL_UPDATE(req, );
}
req = "INSERT or REPLACE INTO import_pattern ('user', 'description', 'pattern', 'account', 'category') VALUES " ;
req += "('%1', '%2', '%3', '%4', '%5')";
req = req.arg(QString::number(user->_id), key, it->second.pattern, QString::number(it->second.account), QString::number(it->second.category));
EXECUTE_SQL_UPDATE(req, );
}
}
+1 -1
View File
@@ -109,7 +109,7 @@ public:
double MetaAmount(int id);
double MetaPositiveAmount(int id);
double GetAccountAmount(int id, int month, int year);
double GetAccountAmount(int id, int month, int year, bool* had_value=NULL);
void SetAccountAmount(int accountId, int month, int year, double amount);
double CalcAccountAmount(int id, int month, int year, bool* had_values);