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 // Define the public static domain constants.
24 const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database";
25 const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains";
27 // Define the private static schema constants.
28 const int DomainsDatabaseHelper::SCHEMA_VERSION = 1;
30 // Define the public static database field names.
31 const QString DomainsDatabaseHelper::_ID = "_id";
32 const QString DomainsDatabaseHelper::DOMAIN_NAME = "domain_name";
33 const QString DomainsDatabaseHelper::JAVASCRIPT = "javascript";
35 // The default constructor.
36 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
38 void DomainsDatabaseHelper::addDatabase()
40 // Add the domain settings database.
41 QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME);
43 // Set the database name.
44 domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
47 if (domainsDatabase.open()) // Opening the database succeeded.
49 // Check to see if the domains table already exists.
50 if (domainsDatabase.tables().contains(DOMAINS_TABLE))
52 // Query the database schema version.
53 QSqlQuery getSchemaVersionQuery = domainsDatabase.exec("PRAGMA user_version");
55 // Move to the first record.
56 getSchemaVersionQuery.first();
58 // Get the current schema version.
59 int currentSchemaVersion = getSchemaVersionQuery.value(0).toInt();
61 // Check to see if the schama has been updated.
62 if (SCHEMA_VERSION > currentSchemaVersion)
64 // Run schema update code.
65 switch (currentSchemaVersion)
67 // Upgrade from schema version 0.
70 // Add the JavaScript column.
71 domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
73 // Set the default value.
74 domainsDatabase.exec("UPDATE " + DOMAINS_TABLE + " SET " + JAVASCRIPT + " = 0" );
78 // Update the schema version.
79 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
84 // Instantiate a create table query.
85 QSqlQuery createTableQuery(domainsDatabase);
87 // Prepare the create table query.
88 createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
89 _ID + " INTEGER PRIMARY KEY, " +
90 DOMAIN_NAME + " TEXT, " +
91 JAVASCRIPT + " INTEGER DEFAULT 0)"
95 if (!createTableQuery.exec())
98 qDebug().noquote().nospace() << "Error creating table: " << domainsDatabase.lastError();
101 // Set the schema version.
102 domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
105 else // Opening the database failed.
107 // Write the last database error message to the debug output.
108 qDebug().noquote().nospace() << "Error opening database: " << domainsDatabase.lastError();
112 QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
114 // Get a handle for the domains database.
115 QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
117 // Instantiate a domain lookup query.
118 QSqlQuery domainLookupQuery(domainsDatabase);
120 // Create a hostname field.
121 QSqlField hostnameField(DOMAIN_NAME, QVariant::String);
123 // Set the hostname field value.
124 hostnameField.setValue(hostname);
126 // SQL escape the hostname field.
127 QString sqlEscapedHostname = domainsDatabase.driver()->formatValue(hostnameField);
129 // Prepare the domain lookup query.
130 domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + DOMAIN_NAME + " = " + sqlEscapedHostname);
132 // Execute the query.
133 domainLookupQuery.exec();
135 // Move to the first entry.
136 domainLookupQuery.first();
139 return domainLookupQuery;