/* 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 #include #include #include #include #include #include #include "Database.hpp" #define ON_ERROR(m) \ QMessageBox::critical(0, _("Error"), m); \ throw m; typedef void (*update_func)(QSqlDatabase& _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(QSqlDatabase& _db) { QString req ; /* Category */ req = "ALTER TABLE category ADD fix_cost CHAR(1)"; UPDATE_TABLE("1", "2", "1"); req = "UPDATE category SET fix_cost='0'"; UPDATE_TABLE("1", "2", "2"); req = "UPDATE category SET fix_cost='1' WHERE id IN ("; req += "SELECT MIN(category.id) FROM category, user WHERE category.user = user.id"; req += ")"; UPDATE_TABLE("1", "2", "3"); /* Account */ req = "ALTER TABLE account ADD virtual CHAR(1)"; UPDATE_TABLE("1", "2", "4"); req = "UPDATE account SET virtual='0'"; UPDATE_TABLE("1", "2", "5"); /* Operation */ req = "ALTER TABLE operation ADD virtual CHAR(1)"; UPDATE_TABLE("1", "2", "6"); req = "UPDATE operation SET virtual='0'"; UPDATE_TABLE("1", "2", "7"); /* Import Pattern */ req = "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() { QString req ; QSqlRecord set ; QSqlQuery query(_db); long int version; int i; req = "SELECT db_version FROM kisscount"; EXECUTE_SQL_QUERY_WITH_CODE(req, , ON_ERROR("Unable to get database version"), {}); query.next(); set = query.record(); if (!(version = set.value("db_version").toInt())) { 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