Some more tables and using views in query
This commit is contained in:
parent
6554cb2b6f
commit
09765fdb24
2 changed files with 40 additions and 3 deletions
|
@ -38,6 +38,27 @@ CREATE TABLE IF NOT EXISTS orders (
|
|||
--------------------------------------------
|
||||
-- Recipe/Cookie related tables
|
||||
--------------------------------------------
|
||||
-- Notes: the unit type can be defined in terms
|
||||
-- of volume or weight instead. Here we choose
|
||||
-- to use static si-prefixes in relevant tables.
|
||||
|
||||
-- What types of ingredients do we handle
|
||||
CREATE TABLE IF NOT EXISTS ingredients (
|
||||
ingredient_id INTEGER PRIMARY KEY,
|
||||
ingredient_name VARCHAR(50) NOT NULL UNIQUE,
|
||||
preferred_unit VARCHAR(50) NOT NULL CHECK (preferred_unit IN ('g', 'ml'))
|
||||
);
|
||||
|
||||
-- What ingredients are in what cookies
|
||||
CREATE TABLE IF NOT EXISTS recipe_contents (
|
||||
cookie_id INT NOT NULL,
|
||||
ingredient_id INT NOT NULL,
|
||||
quantity INT NOT NULL,
|
||||
unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')),
|
||||
PRIMARY KEY (cookie_id, ingredient_id),
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id),
|
||||
FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id)
|
||||
);
|
||||
|
||||
-- Describes ingredients and stock.
|
||||
CREATE TABLE IF NOT EXISTS raw_materials (
|
||||
|
@ -66,12 +87,28 @@ CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
|
|||
CREATE TABLE IF NOT EXISTS pallets (
|
||||
pallet_id INTEGER PRIMARY KEY,
|
||||
cookie_id INT NOT NULL,
|
||||
-- order_id INT NOT NULL,
|
||||
order_id INT, -- This should be not null
|
||||
status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')),
|
||||
production_date DATE NOT NULL,
|
||||
delivery_date DATE DEFAULT NULL,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id)
|
||||
-- FOREIGN KEY (order_id) REFERENCES orders(order_id)
|
||||
FOREIGN KEY (order_id) REFERENCES orders(order_id)
|
||||
);
|
||||
|
||||
--------------------------------------------
|
||||
-- Views
|
||||
--------------------------------------------
|
||||
|
||||
-- Pallet
|
||||
CREATE VIEW IF NOT EXISTS pallets_view AS
|
||||
SELECT
|
||||
pallet_id,
|
||||
cookie_name,
|
||||
order_id,
|
||||
status,
|
||||
production_date,
|
||||
delivery_date
|
||||
FROM pallets
|
||||
JOIN cookies ON pallets.cookie_id = cookies.cookie_id;
|
||||
|
||||
PRAGMA foreign_keys = ON;
|
|
@ -127,7 +127,7 @@ public class Database {
|
|||
try {
|
||||
Statement stmt = conn.createStatement();
|
||||
StringBuilder query = new StringBuilder(
|
||||
"SELECT cookie_name, status FROM pallets JOIN cookies ON pallets.cookie_id = cookies.cookie_id");
|
||||
"SELECT cookie_name, status FROM pallets_view");
|
||||
|
||||
// r is validated here
|
||||
if (r.isPresent()) {
|
||||
|
|
Loading…
Reference in a new issue