Custom script to add next meal automatically. Invoke it thus:
.read nextmeal.sqlite3
.read sequence.sqlite3
This is something NUTsqlite can't do, complicated meal ordering with
automatic changes to personal options!
*/
begin;
update mealfoods set nutr_no = null;
update options set defanal_am = (select case when defanal_am >= 45 then 45 when o.currentmeal = lastmeal then defanal_am + 1 else defanal_am end from options o, am_analysis_header);
with newcm (meal_id) as (
with cm1 (base, meal) as (
with cm (base, meal) as (
@@ -16,12 +21,14 @@ with newcm (meal_id) as (
)
update options set currentmeal = (select meal_id from newcm);
with meal_name (mn) as (
with meal (m) as (
select currentmeal % 100 from options
)
select case when m = 1 then 'BC' when m = 2 then 'DC' else 'SC' end from meal
drop view if exists z_mn;
create temp view z_mn as
with meal (m, mod2) as (
select options.currentmeal % 100, maxmeal % 2 from options, am_analysis_header
)
insert into currentmeal select NDB_No, Gm_Wgt, NutrDesc from theusual where meal_name = (select mn from meal_name);
select case when mod2 = 0 then case when m = 1 then 'K Breakfast' when m = 2 then 'K Dinner' else 'K Supper' end else case when m = 1 then 'K Breakfast' when m = 2 then 'K Dinner' else 'K Supper' end end as mn, m from meal;
insert into currentmeal select NDB_No, Gm_Wgt, NutrDesc from theusual where meal_name = (select mn from z_mn);
update options set currentmeal = (select meal_id from newcm);
update options set defanal_am = 1;
drop view if exists z_mn;
create temp view z_mn as
with meal (m) as (
select currentmeal % 100 from options
)
select case when m = 1 then 'R Breakfast' when m = 2 then 'KC Dinner' else 'R Supper' end as mn, m from meal;
insert into currentmeal select NDB_No, Gm_Wgt, NutrDesc from theusual where meal_name = (select mn from z_mn);
update nutr_def set nutopt = 0.0 where NutrDesc = 'Total Fat';
-- update nutr_def set nutopt = case when (select m from z_mn) in (1, 3) then 125.0 else 140.0 end where NutrDesc = 'Protein';
with temp (meal2nfc) as (select sum(Nutr_Val * Gm_Wgt / 100.0) from mealfoods join nut_data nd using (NDB_No) where nd.Nutr_No = 2000 and meal_id = (select max(meal_id) from mealfoods where meal_id % 100 = 2)) update nutr_def set nutopt = case when (select m from z_mn) in (1, 3) then (180.0 - (select meal2nfc from temp)) * 1.5 else -1 end where NutrDesc = 'Non-Fiber Carb';
update options set currentmeal = (select meal_id from newcm);
update nutr_def set nutopt = -1 where nutrdesc = 'Total Fat';
update nutr_def set nutopt = 0.0 where nutrdesc = 'Non-Fiber Carb';
update options set defanal_am = case when defanal_am >= 45 then 45 else defanal_am + 1 end;
drop view if exists z_mn;
create temp view z_mn as
with meal (m, mod2) as (
select options.currentmeal % 100, maxmeal % 2 from options, am_analysis_header
)
select case when mod2 = 0 then case when m = 1 then 'R Breakfast' when m = 2 then 'R Dinner' else 'R Supper' end else case when m = 1 then 'P Breakfast' when m = 2 then 'P Dinner' else 'P Supper' end end as mn from meal;
insert into currentmeal select NDB_No, Gm_Wgt, NutrDesc from theusual where meal_name = (select mn from z_mn);