You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- Here, things get a little more interesting. We are making a small app to learn our times table
52
+
-- We will display a set of cards, each one displaying the result of the multiplication a * b
53
+
select'card'as component, 5as columns;
54
+
WITH RECURSIVE cnt(x) AS (VALUES(1) UNION ALLSELECT x+1FROM cnt WHERE x<10)
55
+
SELECT
56
+
a.x||' times '||b.xas title,
57
+
CASE a.x % 4 WHEN 0 THEN 'red' WHEN 1 THEN 'green' WHEN 3 THEN 'yellow' ELSE 'blue' END as color,
58
+
a.x||' x '||b.x||' = '|| (a.x*b.x) as description,
59
+
'This is basic math'as footer,
60
+
'?x='||a.xas link -- This is the interesting part. Each card has a link. When you click the card, the current page is reloaded with '?x=a' appended to the end of the URL
61
+
FROM cnt as a, cnt as b
62
+
WHERE-- The powerful thing is here
63
+
($1->>'$.query.x') IS NULLOR-- The syntax ($1->>'$.query.x') allows us to extract the value of 'a' when the URL ends with '?x=a'. It will be null if the URL does not contain '?x='
64
+
b.x= CAST ($1->>'$.query.x'ASINT); -- So when we click the card for "a times b", we will reload the page, and display only the multiplication table of a
65
+
66
+
-- Until now, we have only read data. Let's see how we can write new data to our database
67
+
68
+
-- I am creating a temporary table for this example, but you could use an existing table in your database to persist the data
69
+
create temporary table if not exists users(name text);
select'First name'as name, true as autocomplete, true as required, 'We need your name for legal reasons.'as description;
75
+
select'Last name'as name, true as autocomplete;
76
+
select'radio'as type, 'favorite_food'as name, 'banana'as value, 'I like bananas the most'as label;
77
+
select'radio'as type, 'favorite_food'as name, 'cake'as value, 'I like cake more'as label, 'Bananas are okay, but I prefer cake'as description;
78
+
select'checkbox'as type, 'checks[]'as name, 1as value, 'Accept the terms and conditions'as label;
79
+
select'checkbox'as type, 'checks[]'as name, 2as value, 'Subscribe to the newsletter'as label;
80
+
81
+
-- We can access the values entered in the form using the syntax ($1->>'$.form.xxx') where 'xxx' is the name of one of the fields in the form
82
+
insert into users select ($1->>'$.form.First name')
83
+
where ($1->>'$.form.First name') IS NOT NULL; -- We don't want to add a line in the database if the page was loaded without entering a value in the form, so we add a WHERE clause
0 commit comments