144 lines
3.9 KiB
C++
144 lines
3.9 KiB
C++
/*
|
|
Copyright 2011 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "Database.h"
|
|
|
|
#define ON_ERROR(m) \
|
|
wxMessageBox(wxT(m), _("Error"), wxICON_ERROR | wxOK); \
|
|
throw std::string(m);
|
|
|
|
typedef void (*update_func)(wxSQLite3Database& _db) ;
|
|
|
|
#define UPDATE_TABLE(from, to, step) EXECUTE_SQL_UPDATE_WITH_CODE(req, , throw std::string("Error while upgrading from version " from " to version " to ", step " step);, });
|
|
|
|
static void Version_1_to_2(wxSQLite3Database& _db)
|
|
{
|
|
wxString req ;
|
|
wxSQLite3ResultSet set;
|
|
|
|
/* Category */
|
|
req = wxT("ALTER TABLE category ADD fix_cost CHAR(1)");
|
|
|
|
UPDATE_TABLE("1", "2", "1");
|
|
|
|
req = wxT("UPDATE category SET fix_cost='0'");
|
|
|
|
UPDATE_TABLE("1", "2", "2");
|
|
|
|
req = wxT("UPDATE category SET fix_cost='1' WHERE id IN (");
|
|
req += wxT("SELECT MIN(category.id) FROM category, user WHERE category.user = user.id");
|
|
req += wxT(")");
|
|
|
|
UPDATE_TABLE("1", "2", "3");
|
|
|
|
/* Account */
|
|
req = wxT("ALTER TABLE account ADD virtual CHAR(1)");
|
|
|
|
UPDATE_TABLE("1", "2", "4");
|
|
|
|
req = wxT("UPDATE account SET virtual='0'");
|
|
|
|
UPDATE_TABLE("1", "2", "5");
|
|
|
|
/* Operation */
|
|
req = wxT("ALTER TABLE operation ADD virtual CHAR(1)");
|
|
|
|
UPDATE_TABLE("1", "2", "6");
|
|
|
|
req = wxT("UPDATE operation SET virtual='0'");
|
|
|
|
UPDATE_TABLE("1", "2", "7");
|
|
|
|
/* Import Pattern */
|
|
req = wxT("CREATE TABLE import_pattern(id INTEGER PRIMARY KEY, user REFERENCES user(id), description VARCHAR(255), pattern VARCHAR(255), account REFERENCES account(id), category REFERENCES category(id))");
|
|
|
|
UPDATE_TABLE("1", "2", "8");
|
|
}
|
|
|
|
static update_func updates[] = {
|
|
Version_1_to_2
|
|
};
|
|
|
|
void Database::CheckDatabaseVersion()
|
|
{
|
|
wxString req ;
|
|
wxSQLite3ResultSet set;
|
|
long int version;
|
|
int i;
|
|
|
|
req = wxT("SELECT db_version FROM kisscount");
|
|
|
|
EXECUTE_SQL_QUERY_WITH_CODE(req, set, , ON_ERROR("Unable to get database version"), {});
|
|
|
|
if (!set.GetAsString(wxT("db_version")).ToLong(&version))
|
|
{
|
|
ON_ERROR("Invalid database version");
|
|
}
|
|
|
|
if (version == DATABASE_VERSION) return;
|
|
|
|
if (version > DATABASE_VERSION)
|
|
{
|
|
ON_ERROR("Your current version of KissCount is too old and can't load this database. Please upgrade KissCount");
|
|
}
|
|
|
|
version--;
|
|
|
|
// Maybe one update is impossible
|
|
for(i=version; i<DATABASE_VERSION-1; i++)
|
|
{
|
|
if (!updates[i])
|
|
{
|
|
ON_ERROR("The database cannot be upgraded. Please use an older version of KissCount");
|
|
}
|
|
}
|
|
|
|
wxMessageDialog dialog(NULL, wxT("You use an old database model. KissCount will try to upgrade it to the lastest version"), wxT("KissCount"), wxICON_INFORMATION | wxOK | wxCANCEL);
|
|
if (dialog.ShowModal() == wxID_CANCEL)
|
|
throw std::string("can't load this database");
|
|
|
|
_db.Begin();
|
|
|
|
try
|
|
{
|
|
for(i=version; i<DATABASE_VERSION-1; i++)
|
|
{
|
|
updates[i](_db);
|
|
}
|
|
|
|
req = wxT("UPDATE kisscount SET db_version='") + wxString::Format(wxT("%d"), DATABASE_VERSION) + wxT("'");
|
|
|
|
EXECUTE_SQL_UPDATE_WITH_CODE(req, , throw std::string("Unable to set new database version"), {});
|
|
}
|
|
catch (std::string e)
|
|
{
|
|
std::cout << e << std::endl;
|
|
|
|
_db.Rollback();
|
|
|
|
throw e;
|
|
}
|
|
|
|
_db.Commit();
|
|
|
|
wxMessageBox(wxT("Database upgrade successful !"), _("KissCount"), wxICON_INFORMATION | wxOK);
|
|
}
|
|
|
|
|