KissCount/src/model/Database_Update.cpp

144 lines
3.9 KiB
C++
Raw Normal View History

/*
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/>.
*/
2011-08-20 14:02:47 +02:00
#include "Database.hpp"
#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");
2011-02-14 20:56:59 +01:00
/* 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");
2011-03-23 20:35:29 +01:00
/* 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");
}
}
2011-08-14 17:47:16 +02:00
wxMessageDialog dialog(0, 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);
}