/*
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 .
*/
#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