forked from sqlpage/SQLPage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocumentation.sql
166 lines (147 loc) · 7.89 KB
/
documentation.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
-- ensure that the component exists and do not render this page if it does not
select 'redirect' as component,
'component_not_found.sql?component=' || sqlpage.url_encode($component) as link
where $component is not null and not exists (select 1 from component where name = $component);
-- This line, at the top of the page, tells web browsers to keep the page locally in cache once they have it.
select 'http_header' as component, 'public, max-age=600, stale-while-revalidate=3600, stale-if-error=86400' as "Cache-Control";
select 'dynamic' as component, properties FROM example WHERE component = 'shell' LIMIT 1;
select 'text' as component, format('SQLPage v%s documentation', sqlpage.version()) as title;
select '
If you are completely new to SQLPage, you should start by reading the [get started tutorial](get%20started.sql),
which will guide you through the process of creating your first SQLPage application.
Building an application with SQLPage is quite simple.
To create a new web page, just create a new SQL file.
For each SELECT statement that you write, the data it returns will be analyzed and rendered to the user.
The two most important concepts in SQLPage are **components** and **parameters**.
- **components** are small user interface elements that you can use to display your data in a certain way.
- *top-level* **parameters** are the properties of these components, allowing you to customize their appearance and behavior.
- *row-level* **parameters** constitute the data that you want to display in the components.
To select a component and set its top-level properties, you write the following SQL statement:
```sql
SELECT ''component_name'' AS component, ''my value'' AS top_level_parameter_1;
```
Then, you can set its row-level parameters by writing a second SELECT statement:
```sql
SELECT my_column_1 AS row_level_parameter_1, my_column_2 AS row_level_parameter_2 FROM my_table;
```
This page documents all the components provided by default in SQLPage and their parameters.
Use this as a reference when building your SQL application.
If at any point you need help, you can ask for it on the [SQLPage forum](https://github.com/lovasoa/SQLpage/discussions).
If you know some [HTML](https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/HTML_basics),
you can also easily [create your own components for your application](./custom_components.sql).
' as contents_md;
select 'list' as component, 'components' as title;
select
name as title,
description,
icon,
'?component='||name||'#component' as link,
$component = name as active
from component
order by name;
select 'text' as component,
'The "'||$component||'" component' as title,
'component' as id;
select description as contents from component where name = $component;
select 'text' as component;
select format('Introduced in SQLPage v%s.', introduced_in_version) as contents, 1 as size
from component
where name = $component and introduced_in_version IS NOT NULL;
select 'title' as component, 3 as level, 'Top-level parameters' as contents where $component IS NOT NULL;
select 'card' as component, 3 AS columns where $component IS NOT NULL;
select
name as title,
(CASE WHEN optional THEN '' ELSE 'REQUIRED. ' END) || description as description,
(CASE WHEN optional THEN '' ELSE 'REQUIRED. ' END) || description_md as description_md,
type as footer,
CASE type
WHEN 'COLOR' THEN '/colors.sql'
WHEN 'ICON' THEN 'https://tabler-icons.io/'
END AS footer_link,
CASE WHEN optional THEN 'lime' ELSE 'azure' END as color
from parameter where component = $component AND top_level
ORDER BY optional, name;
select 'title' as component, 3 as level, 'Row-level parameters' as contents
WHERE $component IS NOT NULL AND EXISTS (SELECT 1 from parameter where component = $component AND NOT top_level);
select 'card' as component, 3 AS columns where $component IS NOT NULL;
select
name as title,
(CASE WHEN optional THEN '' ELSE 'REQUIRED. ' END) || description as description,
(CASE WHEN optional THEN '' ELSE 'REQUIRED. ' END) || description_md as description_md,
type as footer,
CASE type
WHEN 'COLOR' THEN '/colors.sql'
WHEN 'ICON' THEN 'https://tabler-icons.io/'
END AS footer_link,
CASE WHEN optional THEN 'lime' ELSE 'azure' END as color
from parameter where component = $component AND NOT top_level
ORDER BY optional, name;
select
'dynamic' as component,
'[
{"component": "code"},
{
"title": "Example ' || (row_number() OVER ()) || '",
"description_md": ' || json_quote(description) || ',
"language": "sql",
"contents": ' || json_quote((
select
group_concat(
'select ' || char(10) ||
(
with t as (select * from json_tree(top.value)),
key_val as (select
CASE t.type
WHEN 'integer' THEN t.atom
WHEN 'real' THEN t.atom
WHEN 'true' THEN 'TRUE'
WHEN 'false' THEN 'FALSE'
WHEN 'null' THEN 'NULL'
WHEN 'object' THEN 'JSON(' || quote(t.value) || ')'
WHEN 'array' THEN 'JSON(' || quote(t.value) || ')'
ELSE quote(t.value)
END as val,
CASE parent.fullkey
WHEN '$' THEN t.key
ELSE parent.key
END as key
from t inner join t parent on parent.id = t.parent
where ((parent.fullkey = '$' and t.type != 'array')
or (parent.type = 'array' and parent.path = '$'))
),
key_val_padding as (select
CASE
WHEN (key LIKE '% %') or (key LIKE '%-%') THEN
format('"%w"', key)
ELSE
key
END as key,
val,
1 + max(0, max(case when length(val) < 30 then length(val) else 0 end) over () - length(val)) as padding
from key_val
)
select group_concat(
format(' %s%.*cas %s', val, padding, ' ', key),
',' || char(10)
) from key_val_padding
) || ';',
char(10)
)
from json_each(properties) AS top
)) || '
}, '||
CASE component
WHEN 'shell' THEN '{"component": "text", "contents": ""}'
WHEN 'http_header' THEN '{ "component": "text", "contents": "" }'
ELSE '
{"component": "title", "level": 3, "contents": "Result"},
{"component": "dynamic", "properties": ' || properties ||' }
'
END || '
]
' as properties
from example where component = $component AND properties IS NOT NULL;
SELECT 'title' AS component, 3 AS level, 'Examples' AS contents
WHERE EXISTS (SELECT 1 FROM example WHERE component = $component AND properties IS NULL);
SELECT 'text' AS component, description AS contents_md
FROM example WHERE component = $component AND properties IS NULL;