/*
- * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2022-2023 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
*
// Define the private static schema constants.
const int CookiesDatabase::SCHEMA_VERSION = 0;
-// Define the public static database constants.
+// Define the public static constants.
const QString CookiesDatabase::CONNECTION_NAME = "cookies_database";
const QString CookiesDatabase::COOKIES_TABLE = "cookies";
-
-// Define the public static database field names.
-const QString CookiesDatabase::_ID = "_id";
const QString CookiesDatabase::DOMAIN = "domain";
-const QString CookiesDatabase::NAME = "name";
-const QString CookiesDatabase::PATH = "path";
const QString CookiesDatabase::EXPIRATION_DATE = "expiration_date";
const QString CookiesDatabase::HTTP_ONLY = "http_only";
+const QString CookiesDatabase::ID = "_id";
+const QString CookiesDatabase::NAME = "name";
+const QString CookiesDatabase::PATH = "path";
const QString CookiesDatabase::SECURE = "secure";
const QString CookiesDatabase::VALUE = "value";
// Prepare the create table query.
createTableQuery.prepare("CREATE TABLE " + COOKIES_TABLE + "(" +
- _ID + " INTEGER PRIMARY KEY, " +
- DOMAIN + " TEXT NOT NULL, " +
- NAME + " TEXT NOT NULL, " +
- PATH + " TEXT NOT NULL, " +
- EXPIRATION_DATE + " TEXT, " +
- HTTP_ONLY + " INTEGER NOT NULL DEFAULT 0, " +
- SECURE + " INTEGER NOT NULL DEFAULT 0, " +
- VALUE + " TEXT NOT NULL)"
- );
+ ID + " INTEGER PRIMARY KEY, " +
+ DOMAIN + " TEXT NOT NULL, " +
+ NAME + " TEXT NOT NULL, " +
+ PATH + " TEXT NOT NULL, " +
+ EXPIRATION_DATE + " TEXT, " +
+ HTTP_ONLY + " INTEGER NOT NULL DEFAULT 0, " +
+ SECURE + " INTEGER NOT NULL DEFAULT 0, " +
+ VALUE + " TEXT NOT NULL)");
// Execute the query.
if (!createTableQuery.exec())
QSqlQuery addCookieQuery(cookiesDatabase);
// Prepare the add cookie query.
- addCookieQuery.prepare("INSERT INTO " + COOKIES_TABLE + " (" + DOMAIN + ", " + NAME + ", " + PATH + ", " + EXPIRATION_DATE + ", " + HTTP_ONLY + ", " + SECURE + ", " + VALUE + ") "
- "VALUES (:domain, :name, :path, :expiration_date, :http_only, :secure, :value)"
+ addCookieQuery.prepare("INSERT INTO " + COOKIES_TABLE + " (" +
+ DOMAIN + ", " +
+ NAME + ", " +
+ PATH + ", " +
+ EXPIRATION_DATE + ", " +
+ HTTP_ONLY + ", " +
+ SECURE + ", " +
+ VALUE + ") "
+ "VALUES (:domain, :name, :path, :expiration_date, :http_only, :secure, :value)"
);
// Bind the values.
countCookiesQuery.setForwardOnly(true);
// Prepare the query.
- countCookiesQuery.prepare("SELECT " + _ID + " FROM " + COOKIES_TABLE);
+ countCookiesQuery.prepare("SELECT " + ID + " FROM " + COOKIES_TABLE);
// Execute the query.
countCookiesQuery.exec();
QSqlQuery deleteCookieQuery(cookiesDatabase);
// Prepare the delete cookie query.
- deleteCookieQuery.prepare("DELETE FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path");
+ deleteCookieQuery.prepare("DELETE FROM " + COOKIES_TABLE + " WHERE " +
+ DOMAIN + " = :domain AND " +
+ NAME + " = :name AND " +
+ PATH + " = :path");
// Bind the values.
deleteCookieQuery.bindValue(":domain", cookie.domain());
cookieQuery.setForwardOnly(true);
// Prepare the cookies query.
- cookieQuery.prepare("SELECT * FROM " + COOKIES_TABLE + " WHERE " + _ID + " = :id");
+ cookieQuery.prepare("SELECT * FROM " + COOKIES_TABLE + " WHERE " + ID + " = :id");
// Bind the values.
cookieQuery.bindValue(":id", id);
isDurableQuery.setForwardOnly(true);
// Prepare the is durable query.
- isDurableQuery.prepare("SELECT " + _ID + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path");
+ isDurableQuery.prepare("SELECT " + ID + " FROM " + COOKIES_TABLE + " WHERE " +
+ DOMAIN + " = :domain AND " +
+ NAME + " = :name AND " +
+ PATH + " = :path");
// Bind the values.
isDurableQuery.bindValue(":domain", cookie.domain());
QSqlQuery isUpdateQuery(cookiesDatabase);
// Prepare the is update query.
- isUpdateQuery.prepare("SELECT " + EXPIRATION_DATE + " , " + HTTP_ONLY + " , " + SECURE + " , " + VALUE + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " +
- NAME + " = :name AND " + PATH + " = :path");
+ isUpdateQuery.prepare("SELECT " + EXPIRATION_DATE + " , " +
+ HTTP_ONLY + " , " +
+ SECURE + " , " +
+ VALUE +
+ " FROM " + COOKIES_TABLE +
+ " WHERE " + DOMAIN + " = :domain AND " +
+ NAME + " = :name AND " +
+ PATH + " = :path");
// Bind the values.
isUpdateQuery.bindValue(":domain", cookie.domain());
if (isUpdateQuery.isValid()) // The cookie exists in the database.
{
// Check to see if the cookie data has changed.
- if ((QDateTime::fromString(isUpdateQuery.value(0).toString(), Qt::ISODate) != cookie.expirationDate()) ||
- (isUpdateQuery.value(1).toBool() != cookie.isHttpOnly()) ||
- (isUpdateQuery.value(2).toBool() != cookie.isSecure()) ||
- (isUpdateQuery.value(3).toString().toUtf8() != cookie.value())) // The cookies data has changed.
+ if ((QDateTime::fromString(isUpdateQuery.value(EXPIRATION_DATE).toString(), Qt::ISODate) != cookie.expirationDate()) ||
+ (isUpdateQuery.value(HTTP_ONLY).toBool() != cookie.isHttpOnly()) ||
+ (isUpdateQuery.value(SECURE).toBool() != cookie.isSecure()) ||
+ (isUpdateQuery.value(VALUE).toString().toUtf8() != cookie.value())) // The cookies data has changed.
{
//qDebug() << "The durable cookie data has changed.";
QSqlQuery updateCookieQuery(cookiesDatabase);
// Prepare the edit cookie query.
- updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE + " SET " + EXPIRATION_DATE + " = :expiration_date , " + HTTP_ONLY + " = :http_only , " + SECURE + " = :secure , " +
- VALUE + " = :value WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path");
+ updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE +
+ " SET " + EXPIRATION_DATE + " = :expiration_date , " +
+ HTTP_ONLY + " = :http_only , " +
+ SECURE + " = :secure , " +
+ VALUE + " = :value " +
+ "WHERE " + DOMAIN + " = :domain AND " +
+ NAME + " = :name AND " +
+ PATH + " = :path");
// Bind the values.
updateCookieQuery.bindValue(":domain", cookie.domain());
oldCookieQuery.setForwardOnly(true);
// Prepare the old cookie query.
- oldCookieQuery.prepare("SELECT " + _ID + " FROM " + COOKIES_TABLE + " WHERE " + DOMAIN + " = :domain AND " + NAME + " = :name AND " + PATH + " = :path");
+ oldCookieQuery.prepare("SELECT " + ID + " FROM " + COOKIES_TABLE +
+ " WHERE " + DOMAIN + " = :domain AND " +
+ NAME + " = :name AND " +
+ PATH + " = :path");
// Bind the values.
oldCookieQuery.bindValue(":domain", oldCookie.domain());
QSqlQuery updateCookieQuery(cookiesDatabase);
// Prepare the update cookie query.
- updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE + " SET " + DOMAIN + " = :domain , " + NAME + " = :name , " + PATH + " = :path , " + EXPIRATION_DATE + " = :expiration_date , " +
- HTTP_ONLY + " = :http_only , " + SECURE + " = :secure , " + VALUE + " = :value WHERE " + _ID + " = :id");
+ updateCookieQuery.prepare("UPDATE " + COOKIES_TABLE +
+ " SET " + DOMAIN + " = :domain , " +
+ NAME + " = :name , " +
+ PATH + " = :path , " +
+ EXPIRATION_DATE + " = :expiration_date , " +
+ HTTP_ONLY + " = :http_only , " +
+ SECURE + " = :secure , " +
+ VALUE + " = :value " +
+ "WHERE " + ID + " = :id");
// Bind the values.
updateCookieQuery.bindValue(":id", oldCookieQuery.value(0).toLongLong());