From: gamesguru Date: Mon, 5 Oct 2020 19:13:24 +0000 (-0400) Subject: nm X-Git-Tag: 0.0.0~6 X-Git-Url: https://git.nutra.tk/v2?a=commitdiff_plain;h=d94b394669524d3bbf5317b097094f635b9a72b3;p=nutratech%2Fnt-sqlite.git nm --- diff --git a/sql/data/users.csv b/sql/data/profiles.csv similarity index 100% rename from sql/data/users.csv rename to sql/data/profiles.csv diff --git a/sql/functions.sql b/sql/functions.sql index d47e9ab..8a6f374 100644 --- a/sql/functions.sql +++ b/sql/functions.sql @@ -3,18 +3,16 @@ -------------------------------- SELECT - users.name, + profiles.name, date, - ROUND(bio_log_entry.value * 2.204, 1) AS weight, - tags, - notes + ROUND(bio_log_entry.value * 2.204, 1) AS weight FROM biometric_log - INNER JOIN users ON user_id = users.id + INNER JOIN profiles ON profile_id = profiles.id INNER JOIN bio_log_entry ON biometric_id = 2 AND log_id = biometric_log.id WHERE - users.name = 'Mark'; + profiles.name = 'Mark'; -------------------------------- -- Pulse and blood pressure @@ -22,14 +20,14 @@ WHERE SELECT DISTINCT date, - users.name, + profiles.name, tags, notes, CAST(sys.value AS int) || '/' || CAST(dia.value AS int) AS pressure, CAST(pulse.value AS int) AS pulse FROM biometric_log - INNER JOIN users ON user_id = users.id + INNER JOIN profiles ON profile_id = profiles.id INNER JOIN bio_log_entry pulse ON pulse.biometric_id = 22 AND pulse.log_id = biometric_log.id INNER JOIN bio_log_entry sys ON sys.biometric_id = 23 @@ -37,7 +35,7 @@ FROM INNER JOIN bio_log_entry dia ON dia.biometric_id = 24 AND dia.log_id = biometric_log.id WHERE - users.name = 'Mark'; + profiles.name = 'Mark'; -------------------------------- -- Height, wrist, ankle @@ -45,13 +43,13 @@ WHERE SELECT date, - users.name, + profiles.name, height.value AS height, wrist.value AS wrist, ankle.value AS ankle FROM biometric_log - INNER JOIN users ON user_id = users.id + INNER JOIN profiles ON profile_id = profiles.id LEFT JOIN bio_log_entry height ON height.biometric_id = 1 AND height.log_id = biometric_log.id LEFT JOIN bio_log_entry wrist ON wrist.biometric_id = 3 @@ -69,7 +67,7 @@ WHERE SELECT date, - users.name, + profiles.name, chest.value / 2.54 AS chest, arm.value / 2.54 AS arm, thigh.value / 2.54 AS thigh, @@ -81,7 +79,7 @@ SELECT forearm.value / 2.54 AS forearm FROM biometric_log - INNER JOIN users ON user_id = users.id + INNER JOIN profiles ON profile_id = profiles.id LEFT JOIN bio_log_entry chest ON chest.biometric_id = 5 AND chest.log_id = biometric_log.id LEFT JOIN bio_log_entry arm ON arm.biometric_id = 6 @@ -117,7 +115,7 @@ WHERE SELECT date, - users.name, + profiles.name, CAST(pectoral.value AS int) AS pec, CAST(abdominal.value AS int) AS ab, CAST(quadricep.value AS int) AS quad, @@ -127,7 +125,7 @@ SELECT CAST(suprailiac.value AS int) AS supra FROM biometric_log - INNER JOIN users ON user_id = users.id + INNER JOIN profiles ON profile_id = profiles.id LEFT JOIN bio_log_entry pectoral ON pectoral.biometric_id = 14 AND pectoral.log_id = biometric_log.id LEFT JOIN bio_log_entry abdominal ON abdominal.biometric_id = 15 diff --git a/sql/import.sql b/sql/import.sql index 465650d..19c7a18 100644 --- a/sql/import.sql +++ b/sql/import.sql @@ -14,15 +14,19 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +PRAGMA foreign_keys = 1; +-- PRAGMA integrity_check = 1; + + .mode csv -.nullvalue NULL +-- .nullvalue NULL .import '| tail -n +2 ./data/bmr_eqs.csv' bmr_eqs .import '| tail -n +2 ./data/bf_eqs.csv' bf_eqs .import '| tail -n +2 ./data/meals.csv' meals .import '| tail -n +2 ./data/biometrics.csv' biometrics -.import '| tail -n +2 ./data/users.csv' users +.import '| tail -n +2 ./data/profiles.csv' profiles .import '| tail -n +2 ./data/rda.csv' rda .import '| tail -n +2 ./data/recipes.csv' recipes diff --git a/sql/tables.sql b/sql/tables.sql index fdc63e5..1e60369 100644 --- a/sql/tables.sql +++ b/sql/tables.sql @@ -14,8 +14,6 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . -PRAGMA foreign_keys = 1; - CREATE TABLE version( id integer PRIMARY KEY AUTOINCREMENT, version text NOT NULL, created date NOT NULL, notes text ); @@ -39,10 +37,10 @@ CREATE TABLE bf_eqs ( -- -------------------------------- --- Users table +-- Profiles table -------------------------------- -CREATE TABLE users ( +CREATE TABLE profiles ( id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL UNIQUE, guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, @@ -75,14 +73,14 @@ CREATE TABLE biometrics ( CREATE TABLE biometric_log ( id integer PRIMARY KEY AUTOINCREMENT, guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, - user_id int NOT NULL, + profile_id int NOT NULL, created int DEFAULT (strftime ('%s', 'now')), updated int DEFAULT (strftime ('%s', 'now')), synced int DEFAULT -1, date int DEFAULT (strftime ('%s', 'now')), tags text, notes text, - FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE + FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE ); CREATE TABLE bio_log_entry ( @@ -130,24 +128,24 @@ CREATE TABLE meals ( CREATE TABLE food_log ( id integer PRIMARY KEY AUTOINCREMENT, guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, - user_id int NOT NULL, + profile_id int NOT NULL, date date DEFAULT CURRENT_DATE, meal_id int NOT NULL, food_id int NOT NULL, -- TODO: enforce FK constraint across two DBs? grams real NOT NULL, - FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE, + FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE, FOREIGN KEY (meal_id) REFERENCES meals (id) ON UPDATE CASCADE ); CREATE TABLE recipe_log ( id integer PRIMARY KEY AUTOINCREMENT, guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, - user_id int NOT NULL, + profile_id int NOT NULL, date date DEFAULT CURRENT_DATE, meal_id int NOT NULL, recipe_id int NOT NULL, grams real NOT NULL, - FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE, + FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE, FOREIGN KEY (meal_id) REFERENCES meals (id) ON UPDATE CASCADE, FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE ); @@ -158,13 +156,13 @@ CREATE TABLE recipe_log ( -------------------------------- CREATE TABLE rda ( - user_id int NOT NULL, + profile_id int NOT NULL, -- TODO: enforce FK constraint across two DBs? nutr_id int NOT NULL, rda real NOT NULL, synced int DEFAULT 0, - PRIMARY KEY (user_id, nutr_id), - FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE + PRIMARY KEY (profile_id, nutr_id), + FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE ); CREATE TRIGGER rda_sync @@ -172,7 +170,7 @@ CREATE TRIGGER rda_sync BEGIN UPDATE rda SET synced = 0 WHERE - NEW.user_id = user_id AND NEW.nutr_id = nutr_id; + NEW.profile_id = profile_id AND NEW.nutr_id = nutr_id; END;