]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/helpers/DomainsDatabaseHelper.cpp
Switch the Domain Settings implementation to a Dialog.
[PrivacyBrowserPC.git] / src / helpers / DomainsDatabaseHelper.cpp
index 8d127cf5f01720ae03f15ee88406ac1f0729cafb..b801a0bf4047882f9895637076012608e2fc4201 100644 (file)
 // Application headers.
 #include "DomainsDatabaseHelper.h"
 
-// Qt framework headers.
-#include <QtSql>
-
-// Define the static constants.
+// Define the public static domain constants.
 const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database";
 const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains";
 
+// Define the private static schema constants.
+const int DomainsDatabaseHelper::SCHEMA_VERSION = 1;
+
+// Define the public static database field names.
+const QString DomainsDatabaseHelper::_ID = "_id";
+const QString DomainsDatabaseHelper::DOMAIN_NAME = "domain_name";
+const QString DomainsDatabaseHelper::JAVASCRIPT = "javascript";
+
 // The default constructor.
 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
 
@@ -44,7 +49,35 @@ void DomainsDatabaseHelper::addDatabase()
         // Check to see if the domains table already exists.
         if (domainsDatabase.tables().contains(DOMAINS_TABLE))
         {
-            // Run schema update code.
+            // Query the database schema version.
+            QSqlQuery getSchemaVersionQuery = domainsDatabase.exec("PRAGMA user_version");
+
+            // Move to the first record.
+            getSchemaVersionQuery.first();
+
+            // Get the current schema version.
+            int currentSchemaVersion = getSchemaVersionQuery.value(0).toInt();
+
+            // Check to see if the schama has been updated.
+            if (SCHEMA_VERSION > currentSchemaVersion)
+            {
+                // Run schema update code.
+                switch (currentSchemaVersion)
+                {
+                    // Upgrade from schema version 0.
+                    case 0:
+                    {
+                        // Add the JavaScript column.
+                        domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + JAVASCRIPT + " INTEGER DEFAULT 0");
+
+                        // Set the default value.
+                        domainsDatabase.exec("UPDATE " + DOMAINS_TABLE + " SET " + JAVASCRIPT + " = 0" );
+                    }
+                }
+
+                // Update the schema version.
+                domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
+            }
         }
         else
         {
@@ -52,9 +85,10 @@ void DomainsDatabaseHelper::addDatabase()
             QSqlQuery createTableQuery(domainsDatabase);
 
             // Prepare the create table query.
-            createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "("
-                "_id INTEGER PRIMARY KEY, "
-                "domain_name TEXT)"
+            createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" +
+                _ID + " INTEGER PRIMARY KEY, " +
+                DOMAIN_NAME + " TEXT, " +
+                JAVASCRIPT + " INTEGER DEFAULT 0)"
             );
 
             // Execute the query.
@@ -63,6 +97,9 @@ void DomainsDatabaseHelper::addDatabase()
                 // Log any errors.
                 qDebug().noquote().nospace() << "Error creating table:  " << domainsDatabase.lastError();
             }
+
+            // Set the schema version.
+            domainsDatabase.exec("PRAGMA user_version = " + QString::number(SCHEMA_VERSION));
         }
     }
     else  // Opening the database failed.
@@ -71,3 +108,34 @@ void DomainsDatabaseHelper::addDatabase()
         qDebug().noquote().nospace() << "Error opening database:  " << domainsDatabase.lastError();
     }
 };
+
+QSqlQuery DomainsDatabaseHelper::getDomainQuery(const QString &hostname)
+{
+    // Get a handle for the domains database.
+    QSqlDatabase domainsDatabase = QSqlDatabase::database(CONNECTION_NAME);
+
+    // Instantiate a domain lookup query.
+    QSqlQuery domainLookupQuery(domainsDatabase);
+
+    // Create a hostname field.
+    QSqlField hostnameField(DOMAIN_NAME, QVariant::String);
+
+    // Set the hostname field value.
+    hostnameField.setValue(hostname);
+
+    // SQL escape the hostname field.
+    QString sqlEscapedHostname = domainsDatabase.driver()->formatValue(hostnameField);
+
+    // Prepare the domain lookup query.
+    domainLookupQuery.prepare("SELECT * FROM " + DOMAINS_TABLE + " WHERE " + DOMAIN_NAME + " = " + sqlEscapedHostname);
+
+    // Execute the query.
+    domainLookupQuery.exec();
+
+    // Move to the first entry.
+    domainLookupQuery.first();
+
+    // Return the query.
+    return domainLookupQuery;
+}
+