Skip to content

Commit fce48cc

Browse files
committed
Comment the example 'index.sql'
1 parent 81064de commit fce48cc

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

index.sql

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
-- Welcome to SQLPage ! This is a short demonstration of a few things you can do with SQLPage
2+
3+
-- The first SELECT in your page allow you to customize your web page, giving it a title and a description
4+
select 'sqlpage' as title, '/' as link, 'en' as lang, 'My cool app' as description;
5+
6+
-- Making a web page with SQLPage works by using a set of predefined "components"
7+
-- and filling them with contents from the results of your SQL queries
8+
9+
-- Let's start with the text component :
10+
select 'text' as component,
11+
'Welcome to SQLPage' as title; -- The text component has a property called "title" that we use to set the title of our block of text
12+
-- We are now inside the text component. Each row that will be returned by our SELECT queries will be a span of text
13+
select 'I can''t believe I am writing a website with nothing but SQL ! ' as contents,
14+
true as italics; -- The text component has a property called "contents" and another called "italics".
15+
select 'This is a normal SQL query. This text is hardcoded, but it could as well come from a database.' as contents;
16+
17+
select 'default' as component;
18+
select 'Welcome to sqlpage!', 'Look at how easy it is to write a website !';
19+
20+
-- We can switch to another component at any time just with a select statement.
21+
-- Let's draw a chart
22+
select 'chart' as component,
23+
'Prix du pétrole' as title,
24+
'bar' as type,
25+
'time' as xtitle,
26+
'price' as ytitle,
27+
true as stacked;
28+
-- Inside the chart component, we have access to the "series", "label", and "value" properties
29+
select 'Russia' as series, '2022-01' as label, 2 as value
30+
union select 'Russia', '2022-02',4
31+
union select 'Russia', '2022-03',2;
32+
select 'Brasil' as series, '2022-01' as label, 4 as value
33+
union select 'Brasil', '2022-03',1
34+
union select 'Brasil', '2022-04',1;
35+
36+
-- Let's make a new chart, this time generating the data with a more complex query
37+
select 'chart' as component, 'Conjecture de syracuse' as title, 'area' as type;
38+
WITH RECURSIVE cnt(x,y) AS (
39+
VALUES(0,15) UNION ALL
40+
SELECT
41+
x+1,
42+
CASE y%2 WHEN 0 THEN y/2 ELSE 3*y+1 END
43+
FROM cnt WHERE x<12
44+
) SELECT 'syracuse' as series, x, y from cnt;
45+
46+
select 'table' as component, true as sort, true as search;
47+
-- The table component lets you just select your data as it is, without imposing a particular set of properties
48+
select 'John' as "First Name", 'Doe' as "Last Name", 1994 as "Birth Date"
49+
union select 'Jane', 'Smith', 1989;
50+
51+
-- 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, 5 as columns;
54+
WITH RECURSIVE cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<10)
55+
SELECT
56+
a.x || ' times ' || b.x as 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.x as 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 NULL OR -- 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' AS INT); -- 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);
70+
71+
-- Let's display a form to our users
72+
select 'form' as component, 'Create' as validate, 'New User' as title;
73+
select 'number' as type, 'age' as placeholder;
74+
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, 1 as value, 'Accept the terms and conditions' as label;
79+
select 'checkbox' as type, 'checks[]' as name, 2 as 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
84+
85+
-- Let's show the users we have in our database
86+
select 'list' as component, 'Users' as title;
87+
select name as title from users;

sqlpage/templates/text.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{#if title}}
33
<h1 id="{{id}}" class="my-3 {{#if (default center true)}}text-center{{/if}}">{{title}}</h1>
44
{{/if}}
5-
<p class="mx-auto" style="max-width: {{default width 80}}ch" {{#if (not title)}}id="{{id}}"{{/if}}>
5+
<p class="{{#if center}}mx-auto{{/if}}" style="max-width: {{default width 80}}ch" {{#if (not title)}}id="{{id}}"{{/if}}>
66
{{contents}}
77
{{~#each_row~}}
88
{{~#if link~}}

0 commit comments

Comments
 (0)