Browse Source

major refactor - fix cookie issue (introduced?)

Daniel Sheffield 8 months ago
parent
commit
49a243c774

+ 0 - 9
sqlpage/apply.sql

@@ -1,9 +0,0 @@
-SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
-SELECT 'redirect' AS component
-, (
-  SELECT CASE
-    WHEN $title IN ('Products', 'Categories', 'Groups', 'Tags')
-    THEN lower($title)||'.sql' ELSE 'transactions.sql'
-  END
-) AS link
-;

+ 0 - 2
sqlpage/categories.sql

@@ -1,2 +0,0 @@
-SET title = 'Categories';
-SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;

+ 0 - 10
sqlpage/clear.sql

@@ -1,10 +0,0 @@
-SET products = NULL;
-SET categories = NULL;
-SET groups = NULL;
-SET start = NULL;
-SET end = NULL;
-SET tags = NULL;
-SET unit_volume = NULL;
-SET unit_mass = NULL;
-SET unit_count = NULL;
-SELECT 'dynamic' AS component, sqlpage.run_sql('apply.sql') AS properties;

+ 0 - 47
sqlpage/cookie.sql

@@ -1,47 +0,0 @@
-SELECT
-  'cookie' AS component,
-  'session' AS name,
-  sqlpage.random_string(32) AS value
-WHERE sqlpage.cookie('session') IS NULL
-;
-
-SELECT 'cookie' AS component,
-  'products' AS name,
-  $products AS value,
-  $products IS NULL AS remove
-;
-SELECT 'cookie' AS component,
-  'categories' AS name,
-  $categories AS value,
-  $categories IS NULL AS remove
-;
-SELECT 'cookie' AS component,
-  'groups' AS name,
-  $groups AS value,
-  $groups IS NULL AS remove
-;
-SELECT 'cookie' AS component
-, 'start' AS name
-, $start AS value
-, ($start IS NULL OR $start = '') AS remove
-;
-SELECT 'cookie' AS component
-, 'end' AS name
-, $end AS value
-, ($end IS NULL OR $end = '') AS remove
-;
-SELECT 'cookie' AS component
-, 'unit_volume' AS name
-, $unit_volume AS value
-, ($unit_volume IS NULL OR $unit_volume = '') AS remove
-;
-SELECT 'cookie' AS component
-, 'unit_mass' AS name
-, $unit_mass AS value
-, ($unit_mass IS NULL OR $unit_mass = '') AS remove
-;
-SELECT 'cookie' AS component
-, 'unit_count' AS name
-, $unit_count AS value
-, $unit_count IS NULL AS remove
-;

+ 0 - 1
sqlpage/data/Categories.sql

@@ -1 +0,0 @@
-SELECT 'dynamic' AS component, sqlpage.run_sql('data/products.sql') AS properties;

+ 0 - 1
sqlpage/data/Groups.sql

@@ -1 +0,0 @@
-SELECT 'dynamic' AS component, sqlpage.run_sql('data/products.sql') AS properties;

+ 0 - 1
sqlpage/data/Products.sql

@@ -1 +0,0 @@
-SELECT 'dynamic' AS component, sqlpage.run_sql('data/products.sql') AS properties;

+ 0 - 42
sqlpage/data/Tags.sql

@@ -1,42 +0,0 @@
-DROP TABLE IF EXISTS sqlpage_tags;
-CREATE TABLE IF NOT EXISTS sqlpage_tags AS (
-SELECT * FROM (SELECT count(DISTINCT txn.id) AS "Uses", tg.name AS "Name"
-FROM tags tg
-JOIN tags_map tm ON tg.id = tm.tag_id
-JOIN transactions txn ON txn.id = tm.transaction_id
-JOIN products p ON p.id = txn.product_id
-JOIN categories c ON p.category_id = c.id
-JOIN groups g ON c.group_id = g.id
-WHERE
-  (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR $products IS NULL)
-AND
-  (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR $categories IS NULL)
-AND
-  (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR $groups IS NULL)
-AND
-  (tg.name IN (SELECT v#>>'{}' FROM json_array_elements($tags::json) j(v)) OR $tags IS NULL)
-AND
-  ((ts AT TIME ZONE 'UTC') AT TIME ZONE 'Pacific/Auckland')::date
-  BETWEEN $start::date AND $end::date
-GROUP BY tg.name
-ORDER BY 1 DESC, 2) q
-UNION ALL
-SELECT count(DISTINCT txn.id) AS "Uses", count(DISTINCT tg.name)||'' AS "Name"
-FROM tags tg
-JOIN tags_map tm ON tg.id = tm.tag_id
-JOIN transactions txn ON txn.id = tm.transaction_id
-JOIN products p ON p.id = txn.product_id
-JOIN categories c ON p.category_id = c.id
-JOIN groups g ON c.group_id = g.id
-WHERE
-  (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR $products IS NULL)
-AND
-  (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR $categories IS NULL)
-AND
-  (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR $groups IS NULL)
-AND
-  (tg.name IN (SELECT v#>>'{}' FROM json_array_elements($tags::json) j(v)) OR $tags IS NULL)
-AND
-  ((ts AT TIME ZONE 'UTC') AT TIME ZONE 'Pacific/Auckland')::date
-  BETWEEN $start::date AND $end::date
-);

+ 0 - 132
sqlpage/entry.sql

@@ -1,132 +0,0 @@
-SET products = COALESCE($products, sqlpage.cookie('products'));
-SET categories = COALESCE($categories, sqlpage.cookie('categories'));
-SET groups = COALESCE($groups, sqlpage.cookie('groups'));
-SET tags = COALESCE($tags, sqlpage.cookie('tags'));
-SET unit_volume = (CASE
-  WHEN $unit_volume = '' OR $unit_volume IS NULL
-  THEN COALESCE(sqlpage.cookie('unit_volume'), 'L')
-  ELSE $unit_volume
-END);
-SET unit_mass = (CASE
-  WHEN $unit_mass = '' OR $unit_mass IS NULL
-  THEN COALESCE(sqlpage.cookie('unit_mass'), 'kg')
-  ELSE $unit_mass
-END);
-SET unit_count = COALESCE($unit_count, sqlpage.cookie('unit_count'));
-SET start = (CASE
-  WHEN $start = '' OR $start IS NULL
-  THEN COALESCE(sqlpage.cookie('start'), to_char(now()-'30 days'::interval, 'YYYY-MM-DD'))
-  ELSE $start
-END);
-SET end = CASE
-  WHEN $end = '' OR $end IS NULL
-  THEN COALESCE(sqlpage.cookie('end'), to_char(now(), 'YYYY-MM-DD'))
-  ELSE $end
-END;
-
-SET page = CASE
-  WHEN $title IN ('Products', 'Categories', 'Groups', 'Tags', 'Transactions')
-  THEN $title||'.sql'
-  ELSE NULL
-END;
-
-SELECT 'redirect' AS component
-, '404.sql?title='||COALESCE($title, '') AS link
-WHERE $page IS NULL
-;
-
-SELECT 'dynamic' AS component
-, CASE
-  WHEN $title IN ('Transactions', 'Tags')
-  THEN sqlpage.run_sql('data/'||$page)
-  WHEN $title IN ('Products', 'Categories', 'Groups')
-  THEN sqlpage.run_sql('data/products.sql')
-END;
-
-SET product_options = (
-SELECT
-  json_agg(json_build_object(
-    'label', value,
-    'value', value,
-    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v))
-  ) ORDER BY value)
-FROM (
-  SELECT v#>>'{}'
-  FROM json_array_elements($products::json) j(v)
-  UNION
-  SELECT product
-  FROM sqlpage_products
-) o(value)
-WHERE value IS NOT NULL
-);
-SET category_options = (
-SELECT
-  json_agg(json_build_object(
-    'label', value,
-    'value', value,
-    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v))
-  ) ORDER BY value)
-FROM (
-  SELECT v#>>'{}'
-  FROM json_array_elements($categories::json) j(v)
-  UNION
-  SELECT category
-  FROM sqlpage_products
-) o(value)
-WHERE value IS NOT NULL
-);
-SET group_options = (
-SELECT
-  json_agg(json_build_object(
-    'label', value,
-    'value', value,
-    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v))
-  ) ORDER BY value)
-FROM (
-  SELECT v#>>'{}'
-  FROM json_array_elements($groups::json) j(v)
-  UNION
-  SELECT "group"
-  FROM sqlpage_products
-) o(value)
-WHERE value IS NOT NULL
-);
-
-DROP TABLE IF EXISTS sqlpage_units;
-CREATE TEMPORARY TABLE IF NOT EXISTS sqlpage_units AS
-SELECT u.name, ut.name AS type FROM units u
-JOIN unit_types ut ON ut.id = u.unit_type_id
-;
-SET unit_volume_options = (
-SELECT json_agg(json_build_object(
-   'label', o.name,
-   'value', o.name,
-   'selected', o.name = $unit_volume
- ) ORDER BY o.name)
-FROM sqlpage_units o
-WHERE type = 'Volume'
-);
-SET unit_mass_options = (
-SELECT json_agg(json_build_object(
-   'label', o.name,
-   'value', o.name,
-   'selected', o.name = $unit_mass
- ) ORDER BY o.name)
-FROM sqlpage_units o
-WHERE type = 'Mass'
-);
-SET unit_count_options = (
-SELECT json_agg(json_build_object(
-   'label', o.name,
-   'value', o.name,
-   'selected', o.name IN (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v))
- ) ORDER BY o.name)
-FROM sqlpage_units o
-WHERE type = 'Count'
-);
-
-SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
-SELECT 'dynamic' AS component, sqlpage.run_sql('theme.sql') AS properties;
-SELECT 'dynamic' AS component, sqlpage.run_sql('nav.sql') AS properties;
-
-SELECT 'dynamic' AS component, sqlpage.run_sql('pages/'||$page) AS properties;

+ 2 - 0
sqlpage/grocery/categories.sql

@@ -0,0 +1,2 @@
+SET title = 'Categories';
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 2 - 0
sqlpage/grocery/groups.sql

@@ -0,0 +1,2 @@
+SET title = 'Groups';
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 1 - 1
sqlpage/404.sql → sqlpage/grocery/internal/404.sql

@@ -1,6 +1,6 @@
 -- TODO set response code 404
 SET title = '404 '||COALESCE(''''||$title||'''','')||' not found';
-SELECT 'dynamic' AS component, sqlpage.run_sql('theme.sql') AS properties;
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/theme.sql') AS properties;
 SELECT 'alert' AS component
 , '404 - Not found' AS title
 , 'Check the URL is correct.' AS description

+ 12 - 0
sqlpage/grocery/internal/apply.sql

@@ -0,0 +1,12 @@
+SET apply = TRUE;
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/cookie.sql') AS properties;
+SELECT 'redirect' AS component
+, (
+  SELECT CASE
+    WHEN $title IN (SELECT name FROM sqlpage_pages)
+    THEN '/grocery/'||lower($title)||'.sql'
+    ELSE '/grocery/internal/404.sql'
+  END
+) AS link
+--WHERE $clear = 'Clear'
+;

+ 22 - 0
sqlpage/grocery/internal/clear.sql

@@ -0,0 +1,22 @@
+SET products = NULL;
+SET categories = NULL;
+SET groups = NULL;
+SET start = NULL;
+SET end = NULL;
+SET unit_volume = NULL;
+SET unit_mass = NULL;
+SET unit_count = NULL;
+SET log_scale = NULL;
+SET clear = TRUE
+--SELECT 'dynamic' AS component, sqlpage.run_sql('grocery/internal/apply.sql') AS properties;
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/cookie.sql') AS properties;
+SELECT 'redirect' AS component
+, (
+  SELECT CASE
+    WHEN $title IN (SELECT name FROM sqlpage_pages)
+    THEN '/grocery/internal/apply.sql?title='||$title
+    ELSE '/grocery/internal/404.sql'
+  END
+) AS link
+--WHERE $clear = 'Clear'
+;

+ 2 - 0
sqlpage/grocery/products.sql

@@ -0,0 +1,2 @@
+SET title = 'Products';
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 2 - 0
sqlpage/grocery/tags.sql

@@ -0,0 +1,2 @@
+SET title = 'Tags';
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 2 - 0
sqlpage/grocery/transactions.sql

@@ -0,0 +1,2 @@
+SET title = 'Transactions';
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 2 - 0
sqlpage/grocery/trend.sql

@@ -0,0 +1,2 @@
+SET title = 'Trend';
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 0 - 2
sqlpage/groups.sql

@@ -1,2 +0,0 @@
-SET title = 'Groups';
-SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;

+ 0 - 2
sqlpage/products.sql

@@ -1,2 +0,0 @@
-SET title = 'Products';
-SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;

+ 3 - 3
sqlpage/data/products.sql → sqlpage/sqlpage/data/products.sql

@@ -20,10 +20,10 @@ FROM products p
 JOIN categories c ON p.category_id = c.id
 JOIN groups g ON c.group_id = g.id
 WHERE
-  (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR $products IS NULL)
+  (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR ($products IS NULL OR $products = ''))
 AND
-  (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR $categories IS NULL)
+  (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR ($groups IS NULL OR $groups = ''))
 AND
-  (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR $groups IS NULL)
+  (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR ($groups IS NULL OR $groups = ''))
 GROUP BY ROLLUP ("group", category, product)
 ;

+ 23 - 0
sqlpage/sqlpage/data/tags.sql

@@ -0,0 +1,23 @@
+DROP TABLE IF EXISTS sqlpage_tags;
+CREATE TABLE IF NOT EXISTS sqlpage_tags AS
+SELECT count(DISTINCT txn.id) AS "Uses", tg.name AS "Name"
+FROM tags tg
+JOIN tags_map tm ON tg.id = tm.tag_id
+JOIN transactions txn ON txn.id = tm.transaction_id
+JOIN products p ON p.id = txn.product_id
+JOIN categories c ON p.category_id = c.id
+JOIN groups g ON c.group_id = g.id
+WHERE
+  (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR $products IS NULL)
+AND
+  (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR $categories IS NULL)
+AND
+  (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR $groups IS NULL)
+AND
+  (tg.name IN (SELECT v#>>'{}' FROM json_array_elements($tags::json) j(v)) OR $tags IS NULL)
+AND
+  ((ts AT TIME ZONE 'UTC') AT TIME ZONE 'Pacific/Auckland')::date
+  BETWEEN $start::date AND $end::date
+GROUP BY tg.name
+ORDER BY 1 DESC, 2
+;

+ 0 - 0
sqlpage/data/Transactions.sql → sqlpage/sqlpage/data/transactions.sql


+ 40 - 0
sqlpage/sqlpage/internal/cookie.sql

@@ -0,0 +1,40 @@
+SELECT 'cookie' AS component
+, 'session' AS name
+, sqlpage.random_string(32) AS value
+WHERE sqlpage.cookie('session') IS NULL
+;
+
+SELECT 'cookie' AS component
+, name AS name
+, CASE
+    WHEN $apply::bool
+    THEN COALESCE(value, 'null')
+    WHEN $clear::bool
+    THEN 'null'
+  END::text AS value
+FROM (VALUES
+  ('products', $products),
+  ('categories', $categories),
+  ('groups', $groups),
+  ('tags', $tags),
+  ('unit_count', $unit_count)
+) AS vars(name, value)
+WHERE $apply::bool OR $clear::bool
+;
+SELECT 'cookie' AS component
+, name AS name
+, CASE
+    WHEN $apply::bool
+    THEN COALESCE(value, '')
+    WHEN $clear::bool
+    THEN ''
+  END AS value
+FROM (VALUES
+  ('start', $start),
+  ('end', $end),
+  ('unit_volume', $unit_volume),
+  ('unit_mass', $unit_mass),
+  ('log_scale', $log_scale)
+) AS vars(name, value)
+WHERE $apply::bool OR $clear::bool
+;

+ 169 - 0
sqlpage/sqlpage/internal/entry.sql

@@ -0,0 +1,169 @@
+SET apply = CASE WHEN (COALESCE($apply, '') = 'Apply') THEN TRUE ELSE FALSE END;
+SET clear = CASE WHEN (COALESCE($clear, '') = 'Clear') THEN TRUE ELSE FALSE END;
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/cookie.sql') AS properties;
+SET products = sqlpage.cookie('products')::json;
+SET categories = sqlpage.cookie('categories')::json;
+SET groups = sqlpage.cookie('groups')::json;
+SET tags = sqlpage.cookie('tags')::json;
+SET start = sqlpage.cookie('start');
+SET end = sqlpage.cookie('end');
+SET unit_volume = sqlpage.cookie('unit_volume');
+SET unit_mass = sqlpage.cookie('unit_mass');
+SET unit_count = sqlpage.cookie('unit_count')::json;
+SET log_scale = sqlpage.cookie('log_scale');
+
+SET unit_volume = (CASE
+  WHEN $unit_volume = '' OR ($unit_volume IS NULL OR $unit_volume = '')
+  THEN 'L'
+  ELSE $unit_volume
+END);
+SET unit_mass = (CASE
+  WHEN $unit_mass = '' OR ($unit_mass IS NULL OR $unit_mass = '')
+  THEN 'kg'
+  ELSE $unit_mass
+END);
+SET start = (CASE
+  WHEN $start = '' OR $start IS NULL
+  THEN to_char(now()-'30 days'::interval, 'YYYY-MM-DD')
+  ELSE $start
+END);
+SET end = CASE
+  WHEN $end = '' OR $end IS NULL
+  THEN to_char(now(), 'YYYY-MM-DD')
+  ELSE $end
+END;
+
+SET page = CASE
+  WHEN $title IN (
+    'Products', 'Categories', 'Groups', 'Tags', 'Transactions',
+    'Trend', 'Volume'
+  )
+  THEN $title||'.sql'
+  ELSE NULL
+END;
+
+SELECT 'redirect' AS component
+, '/grocery/internal/404.sql?title='||COALESCE($title, '') AS link
+WHERE ($page IS NULL OR $page = '')
+;
+
+--SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/'||$page);
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/tags.sql') AS properties
+WHERE $apply::bool OR $clear::bool;
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/transactions.sql') AS properties
+WHERE $apply::bool OR $clear::bool;
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/products.sql') AS properties
+WHERE $apply::bool OR $clear::bool;
+
+-- SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/'||sql||'.sql')
+-- FROM (SELECT UNNEST(ARRAY[
+--   'tags',
+--   'transactions',
+--   'products'
+-- ])) AS data(sql)
+-- ;
+
+
+SET product_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($products::json) j(v)
+  UNION
+  SELECT product
+  FROM sqlpage_products
+) o(value)
+WHERE value IS NOT NULL
+);
+SET category_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($categories::json) j(v)
+  UNION
+  SELECT category
+  FROM sqlpage_products
+) o(value)
+WHERE value IS NOT NULL
+);
+SET group_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($groups::json) j(v)
+  UNION
+  SELECT "group"
+  FROM sqlpage_products
+) o(value)
+WHERE value IS NOT NULL
+);
+
+SET tag_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($tags::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($tags::json) j(v)
+  UNION
+  SELECT "Name"
+  FROM sqlpage_tags
+) o(value)
+WHERE value IS NOT NULL
+);
+
+DROP TABLE IF EXISTS sqlpage_units;
+CREATE TEMPORARY TABLE IF NOT EXISTS sqlpage_units AS
+SELECT u.name, ut.name AS type FROM units u
+JOIN unit_types ut ON ut.id = u.unit_type_id
+;
+SET unit_volume_options = (
+SELECT json_agg(json_build_object(
+   'label', o.name,
+   'value', o.name,
+   'selected', o.name = $unit_volume
+ ) ORDER BY o.name)
+FROM sqlpage_units o
+WHERE type = 'Volume'
+);
+SET unit_mass_options = (
+SELECT json_agg(json_build_object(
+   'label', o.name,
+   'value', o.name,
+   'selected', o.name = $unit_mass
+ ) ORDER BY o.name)
+FROM sqlpage_units o
+WHERE type = 'Mass'
+);
+SET unit_count_options = (
+SELECT json_agg(json_build_object(
+   'label', o.name,
+   'value', o.name,
+   'selected', o.name IN (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v))
+ ) ORDER BY o.name)
+FROM sqlpage_units o
+WHERE type = 'Count'
+);
+
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/theme.sql') AS properties;
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/nav.sql') AS properties;
+
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/pages/'||$page) AS properties;

+ 18 - 28
sqlpage/nav.sql → sqlpage/sqlpage/internal/nav.sql

@@ -1,37 +1,18 @@
 SELECT 'tab' AS component, TRUE AS center;
 
-SELECT'Products' AS title
-, 'products' AS id
-, $title = 'Products' AS active
-, 'products.sql' AS link
-;
-SELECT 'Categories' AS title
-, 'categories' AS id
-, $title = 'Categories' AS active
-, 'categories.sql' AS link
-;
-SELECT 'Groups' AS title
-, 'groups' AS id
-, $title = 'Groups' AS active
-, 'groups.sql' AS link
-;
-SELECT 'Tags' AS title
-, 'tags' AS id
-, $title = 'Tags' AS active
-, 'tags.sql' AS link
-;
-SELECT 'Transactions' AS title
-, 'transactions' AS id
-, $title = 'Transactions' AS active
-, 'transactions.sql' AS link
-;
+SELECT title
+, lower(title) AS id
+, $title = title AS active
+, '/grocery/'||lower(title)||'.sql' AS link
+FROM (
+  SELECT name FROM sqlpage_pages
+) AS tabs(title)
 
 SELECT 'form' AS component
 , 'filter' AS id
 , 'get' AS method
 , '' AS title
-, 'Apply' AS validate
-, 'apply.sql' AS action
+, '' AS validate
 ;
 
 SELECT 'hidden' AS type
@@ -103,10 +84,19 @@ SELECT 'Unit (count)' AS label
 , TRUE AS dropdown
 , '' AS value
 , $unit_count_options AS options;
+
+SELECT 'Apply' AS value
+, 'apply' AS name
+, '' AS label
+, 'blue' AS color
+, 1 AS width
+, 'submit' AS type
+, '/grocery/internal/apply.sql' AS formaction
+;
 SELECT 'Clear' AS value
 , 'clear' AS name
 , '' AS label
 , 1 AS width
 , 'submit' AS type
-, 'clear.sql?title='||$title AS formaction
+, '/grocery/internal/clear.sql' AS formaction
 ;

+ 0 - 0
sqlpage/theme.sql → sqlpage/sqlpage/internal/theme.sql


+ 10 - 0
sqlpage/sqlpage/migrations/000_constants.sql

@@ -0,0 +1,10 @@
+CREATE TABLE IF NOT EXISTS sqlpage_pages
+(
+    name text PRIMARY KEY
+);
+INSERT INTO sqlpage_pages
+SELECT UNNEST(ARRAY[
+  'Products', 'Categories', 'Groups',
+  'Tags',
+  'Transactions', 'Trend', 'Volume'
+]);

+ 0 - 0
sqlpage/pages/Categories.sql → sqlpage/sqlpage/pages/Categories.sql


+ 0 - 0
sqlpage/pages/Groups.sql → sqlpage/sqlpage/pages/Groups.sql


+ 0 - 0
sqlpage/pages/Products.sql → sqlpage/sqlpage/pages/Products.sql


+ 3 - 0
sqlpage/pages/Tags.sql → sqlpage/sqlpage/pages/Tags.sql

@@ -7,5 +7,8 @@ SELECT
 ;
 SELECT * FROM sqlpage_tags;
 
+SELECT count("Uses") AS "Uses", count("Name")::text AS "Name"
+FROM sqlpage_tags;
+
 SELECT '# No Data' AS "Notice"
 WHERE NOT EXISTS(SELECT * FROM sqlpage_tags LIMIT 1);

+ 0 - 0
sqlpage/pages/Transactions.sql → sqlpage/sqlpage/pages/Transactions.sql


+ 6 - 0
sqlpage/sqlpage/pages/Trend.sql

@@ -0,0 +1,6 @@
+SELECT 'chart' AS component
+, 'Cost by Weight' AS title
+, 'line' AS type
+, $logarithmic AS logarithmic
+, TRUE AS time
+;

+ 0 - 2
sqlpage/tags.sql

@@ -1,2 +0,0 @@
-SET title = 'Tags';
-SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;

+ 0 - 2
sqlpage/transactions.sql

@@ -1,2 +0,0 @@
-SET title = 'Transactions';
-SELECT 'dynamic' AS component, sqlpage.run_sql('entry.sql') AS properties;