From dcf0a605cf1b6e101b34206a3d5bd359f3c331a5 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 3 Feb 2019 14:14:14 +0100 Subject: [PATCH] Use views for all tables and set disabled column to false for those tables that support it Signed-off-by: DL6ER --- advanced/Templates/gravity.db.schema | 10 ++++++---- gravity.sh | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/advanced/Templates/gravity.db.schema b/advanced/Templates/gravity.db.schema index 2fd9a9d1..83141f94 100644 --- a/advanced/Templates/gravity.db.schema +++ b/advanced/Templates/gravity.db.schema @@ -1,7 +1,5 @@ -CREATE TABLE whitelist (domain TEXT UNIQUE NOT NULL, comment TEXT, disabled BOOLEAN DEFAULT 0, DateAdded DATETIME); - -CREATE TABLE blacklist (domain TEXT UNIQUE NOT NULL, comment TEXT, disabled BOOLEAN DEFAULT 0, DateAdded DATETIME); - +CREATE TABLE whitelist (domain TEXT UNIQUE NOT NULL, disabled BOOLEAN DEFAULT 0, comment TEXT, DateAdded DATETIME); +CREATE TABLE blacklist (domain TEXT UNIQUE NOT NULL, disabled BOOLEAN DEFAULT 0, comment TEXT, DateAdded DATETIME); CREATE TABLE gravity (domain TEXT UNIQUE NOT NULL); CREATE VIEW vw_gravity AS SELECT DISTINCT a.domain @@ -12,3 +10,7 @@ CREATE VIEW vw_blacklist AS SELECT DISTINCT a.domain FROM blacklist a WHERE a.disabled != 1 AND a.domain NOT IN (SELECT domain from whitelist WHERE disabled != 1); + +CREATE VIEW vw_whitelist AS SELECT DISTINCT a.domain +FROM whitelist a +WHERE a.disabled != 1; diff --git a/gravity.sh b/gravity.sh index a8c92e8a..21338119 100755 --- a/gravity.sh +++ b/gravity.sh @@ -115,16 +115,27 @@ gravity_store_in_database() { gravity_Cleanup "error" fi - # Store domains in gravity database + # Store domains in gravity database table ${table} output=$( { sqlite3 "${gravityDBfile}" <<< ".import \"${source}\" ${table}"; } 2>&1 ) status="$?" if [[ "${status}" -ne 0 ]]; then - echo -e "\\n ${CROSS} Unable to create ${table} database ${gravityDBfile}\\n ${output}" + echo -e "\\n ${CROSS} Unable to create ${table} in database ${gravityDBfile}\\n ${output}" gravity_Cleanup "error" fi - # Empty $adList if it already exists, otherwise, create it + if [ "$table" == "whitelist" ] || [ "$table" == "blacklist" ]; then + # Set disabled to false + output=$( { sqlite3 "${gravityDBfile}" <<< "UPDATE ${table} SET disabled = 0 WHERE disabled IS NULL;"; } 2>&1 ) + status="$?" + + if [[ "${status}" -ne 0 ]]; then + echo -e "\\n ${CROSS} Unable to set disabled states (${table}) in database ${gravityDBfile}\\n ${output}" + gravity_Cleanup "error" + fi + fi + + # Empty $template if it already exists, otherwise, create it output=$( { : > "${template}"; } 2>&1 ) status="$?"