Browse Source

use json for options instead of vars

Daniel Sheffield 8 months ago
parent
commit
ad1a272ff2
2 changed files with 65 additions and 95 deletions
  1. 61 92
      sqlpage/sqlpage/internal/entry.sql
  2. 4 3
      sqlpage/sqlpage/internal/nav.sql

+ 61 - 92
sqlpage/sqlpage/internal/entry.sql

@@ -61,98 +61,67 @@ WHERE $apply::bool OR $clear::bool;
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/units.sql') AS properties
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/units.sql') AS properties
 WHERE $apply::bool OR $clear::bool;
 WHERE $apply::bool OR $clear::bool;
 
 
-SET products_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 categories_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 groups_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 tags_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
-);
-
-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'
+SET filter_options = (
+  SELECT json_agg(json_build_object('name', k, 'options', o))
+  FROM (
+    SELECT o.k||'[]', json_agg(
+      json_build_object('label', v, 'value', v, 'selected', s)
+      ORDER BY v)
+    FROM (
+      SELECT DISTINCT o.k, CASE o.k
+          WHEN 'products' THEN product
+          WHEN 'categories' THEN category
+          WHEN 'groups' THEN "group"
+        END, v.k IS NOT NULL --, v.v
+      FROM sqlpage_products
+      CROSS JOIN UNNEST(ARRAY['products', 'categories', 'groups']) o(k)
+      LEFT JOIN json_each_text(sqlpage.variables()::json) v(k,v)
+      ON v.k = o.k
+      AND (CASE o.k
+        WHEN 'products' THEN product
+        WHEN 'categories' THEN category
+        WHEN 'groups' THEN "group"
+      END) IN (
+        SELECT j.v#>>'{}' FROM json_array_elements(v.v::json) j(v)
+      )
+    ) AS o(k, v, s)
+    WHERE v IS NOT NULL
+    GROUP BY o.k
+    UNION ALL
+    SELECT o.k||'[]', json_agg(
+      json_build_object('label', v, 'value', v, 'selected', s)
+      ORDER BY v)
+    FROM (
+      SELECT DISTINCT 'tags', "Name", v.k IS NOT NULL
+      FROM sqlpage_tags
+      LEFT JOIN json_each_text(sqlpage.variables()::json) v(k,v)
+      ON v.k = 'tags' AND "Name" IN (
+        SELECT j.v#>>'{}' FROM json_array_elements(v.v::json) j(v)
+      )
+    ) AS o(k, v, s)
+    WHERE v IS NOT NULL
+    GROUP BY o.k
+    UNION ALL
+    SELECT CASE o.k
+      WHEN 'count' THEN 'unit_'||o.k||'[]'
+      ELSE 'unit_'||o.k END, json_agg(
+        json_build_object('label', v, 'value', v, 'selected', s)
+        ORDER BY v)
+    FROM (
+      SELECT DISTINCT o.k, name, v.k IS NOT NULL
+      FROM sqlpage_units
+      CROSS JOIN UNNEST(ARRAY['volume', 'mass', 'count']) o(k)
+      LEFT JOIN json_each_text(sqlpage.variables()::json) v(k,v)
+      ON v.k = 'unit_'||o.k AND (CASE o.k
+        WHEN 'count' THEN name IN (
+          SELECT j.v#>>'{}' FROM json_array_elements(v.v::json) j(v)
+        )
+        ELSE name = v.v END)
+      WHERE lower(type) = o.k
+    ) AS o(k, v, s)
+    WHERE v IS NOT NULL
+    GROUP BY o.k
+  ) q(k, o)
 );
 );
 
 
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/theme.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/theme.sql') AS properties;

+ 4 - 3
sqlpage/sqlpage/internal/nav.sql

@@ -27,12 +27,13 @@ SELECT 'hidden' AS type
 ;
 ;
 
 
 SELECT v#>>'{name}' AS name
 SELECT v#>>'{name}' AS name
+--, fo.j#>>'{options}' AS label
 , v#>>'{label}' AS label
 , v#>>'{label}' AS label
 , COALESCE(c.value#>>'{type}', v#>>'{type}') AS type
 , COALESCE(c.value#>>'{type}', v#>>'{type}') AS type
 , v#>>'{dropdown}' AS dropdown
 , v#>>'{dropdown}' AS dropdown
 , v#>>'{multiple}' AS multiple
 , v#>>'{multiple}' AS multiple
 , v#>>'{width}' AS width
 , v#>>'{width}' AS width
-, o.value AS options
+, o.j#>'{options}' AS options
 , CASE v#>>'{type}'
 , CASE v#>>'{type}'
   WHEN 'checkbox' THEN 'true'
   WHEN 'checkbox' THEN 'true'
   ELSE v.value#>>'{}'
   ELSE v.value#>>'{}'
@@ -44,8 +45,8 @@ END AS checked
 FROM json_array_elements(sqlpage.read_file_as_text('sqlpage/json/filters.json')::json) j(v)
 FROM json_array_elements(sqlpage.read_file_as_text('sqlpage/json/filters.json')::json) j(v)
 LEFT JOIN json_each($filter_config::json) c(k,value)
 LEFT JOIN json_each($filter_config::json) c(k,value)
 ON c.k = (v#>>'{name}')
 ON c.k = (v#>>'{name}')
-LEFT JOIN json_each(sqlpage.variables()::json) o(key, value)
-ON o.key = replace(v#>>'{name}', '[]', '')||'_options'
+LEFT JOIN json_array_elements($filter_options::json) o(j)
+ON (o.j#>>'{name}') = (v#>>'{name}')
 LEFT JOIN json_each(sqlpage.variables()::json) v(key, value)
 LEFT JOIN json_each(sqlpage.variables()::json) v(key, value)
 ON v.key = (v#>>'{name}')
 ON v.key = (v#>>'{name}')
 ;
 ;