Implement basic food service

This commit is contained in:
2024-08-09 16:21:11 +02:00
parent b265a06ce7
commit a23ada9bb0
4 changed files with 305 additions and 18 deletions

187
food.ddl Normal file
View File

@@ -0,0 +1,187 @@
begin transaction;
create table weight (
date datetime default (datetime('now', '+2 hours')),
weight real not null
);
-- begin TRANSACTION;
-- drop table if exists weight2;
-- create table weight2 (
-- date datetime default (datetime('now', '+2 hours')),
-- weight real not null
-- );
-- insert into weight2(date, weight)
-- select date, weight from weight;
-- drop table weight;
-- alter table weight2 rename to weight;
-- commit;
create index weightDateIdx on weight(date);
create index weightDailyIdx on weight(strftime('%Y-%m-%d', date));
create index weightWeeklyIdx on weight(strftime('%Y-%W', date));
create index weightMonthlyIdx on weight(strftime('%Y-%m', date));
create index weightYearlyIdx on weight(strftime('%Y', date));
drop view if exists weightView;
drop view if exists weightDaily;
drop view if exists weightWeekly;
drop view if exists weightMonthly;
drop view if exists weightYearly;
create view weightView as
select rowid,
date,
round(weight, 2) as weight
from weight
order by date desc;
-- Daily Summary View
create view weightDaily as
select strftime('%Y-%m-%d', date) as period,
round(avg(weight), 2) as amount
from weight
group by strftime('%Y-%m-%d', date)
order by date desc;
-- Weekly Summary View
create view weightWeekly as
select strftime('%Y-%W', date) as period,
round(avg(weight), 2) as amount
from weight
group by strftime('%Y-%W', date)
order by date desc;
-- Monthly Summary View
create view weightMonthly as
select strftime('%Y-%m', date) as period,
round(avg(weight), 2) as amount
from weight
group by strftime('%Y-%m', date)
order by date desc;
-- Yearly Summary View
create view weightYearly as
select strftime('%Y', date) as period,
round(avg(weight), 2) as amount
from weight
group by strftime('%Y', date)
order by date desc;
create table food(
date datetime default (datetime('now', '+2 hours')),
food varchar not null,
description varchar,
amount real not null,
per100 real default 0,
energy generated always as (coalesce(amount, 0) * coalesce(per100, 0) / 100) stored
);
-- begin transaction;
-- drop table if exists food2;
-- create table food2(
-- date datetime default (datetime('now', '+2 hours')),
-- food varchar not null,
-- description varchar,
-- amount real not null,
-- per100 real default 0,
-- energy generated always as (coalesce(amount, 0) * coalesce(per100, 0) / 100) stored
-- );
-- insert into food2(date, food, description, amount, per100)
-- select date, food, description, amount, per100 from food;
-- drop table food;
-- alter table food2 rename to food;
-- commit;
create index dailyIdx on food(strftime('%Y-%m-%d', date));
create index weeklyIdx on food(strftime('%Y-%W', date));
create index monthlyIdx on food(strftime('%Y-%m', date));
create index yearlyIdx on food(strftime('%Y', date));
create index dateIdx on food(date);
create index foodIdx on food(food);
drop view if exists foodView;
drop view if exists daily;
drop view if exists weekly;
drop view if exists monthly;
drop view if exists yearly;
drop view if exists recent;
create view foodView as
select rowid,
date,
food,
description,
round(amount, 2) as amount,
round(per100, 2) as per100,
round(energy, 2) as energy
from food;
-- Daily Summary View
create view daily as
select strftime('%Y-%m-%d', date) as period,
round(sum(amount), 2) as amount,
round(avg(per100), 2) as avgPer100,
round(sum(energy), 2) as energy
from food
group by strftime('%Y-%m-%d', date)
order by date desc;
-- Weekly Summary View
create view weekly as
select strftime('%Y-%W', date) as period,
round(sum(amount), 2) as amount,
round(avg(per100), 2) as avgPer100,
round(sum(energy), 2) as energy
from food
group by strftime('%Y-%W', date)
order by date desc;
-- Monthly Summary View
create view monthly as
select strftime('%Y-%m', date) as period,
round(sum(amount), 2) as amount,
round(avg(per100), 2) as avgPer100,
round(sum(energy), 2) as energy
from food
group by strftime('%Y-%m', date)
order by date desc;
-- Yearly Summary View
create view yearly as
select strftime('%Y', date) as period,
round(sum(amount), 2) as amount,
round(avg(per100), 2) as avgPer100,
round(sum(energy), 2) as energy
from food
group by strftime('%Y', date)
order by date desc;
-- Recent
create view recent as
select rowid,
*
from food
order by date desc
limit 10;
drop trigger if exists getper100;
create trigger getper100 after
insert on food
begin
update food
set per100 = coalesce(
new .per100,
(
select per100
from food
where food = new .food
and per100 is not null
order by date desc
limit 1
)
)
where rowid = new .rowid;
end;
create table settings (key text, value text) --rollback;
commit;