2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "DomainsDatabaseHelper.h"
23 // Qt framework headers.
26 // Define the static constants.
27 const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database";
28 const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains";
30 // The default constructor.
31 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
33 void DomainsDatabaseHelper::addDatabase()
35 // Add the domain settings database.
36 QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME);
38 // Set the database name.
39 domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
42 if (domainsDatabase.open()) // Opening the database succeeded.
44 // Check to see if the domains table already exists.
45 if (domainsDatabase.tables().contains(DOMAINS_TABLE))
47 // Run schema update code.
51 // Instantiate a create table query.
52 QSqlQuery createTableQuery(domainsDatabase);
54 // Prepare the create table query.
55 createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "("
56 "_id INTEGER PRIMARY KEY, "
61 if (!createTableQuery.exec())
64 qDebug().noquote().nospace() << "Error creating table: " << domainsDatabase.lastError();
68 else // Opening the database failed.
70 // Write the last database error message to the debug output.
71 qDebug().noquote().nospace() << "Error opening database: " << domainsDatabase.lastError();