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
Copy file name to clipboardExpand all lines: examples/official-site/sqlpage/migrations/01_documentation.sql
+24-2Lines changed: 24 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -311,7 +311,7 @@ When loading the page, the value for `:username` will be `NULL` if no value has
311
311
']')),
312
312
('form', 'This example illustrates the use of the `select` type.
313
313
In this select input, the various options are hardcoded, but they could also be loaded from a database table,
314
-
using a function to convert the rows into a json array like
314
+
[using a function to convert the rows into a json array](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) like
315
315
- `json_group_array()` in SQLite,
316
316
- `json_agg()` in Postgres,
317
317
- `JSON_ARRAYAGG()` in MySQL, or
@@ -341,6 +341,8 @@ The target page will then receive the value as a JSON array of strings, which yo
341
341
- the [`OPENJSON`](https://learn.microsoft.com/fr-fr/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16) function in Microsoft SQL Server.
342
342
- in MySQL, json manipulation is less straightforward: see [the SQLPage MySQL json example](https://github.com/lovasoa/SQLpage/tree/main/examples/mysql%20json%20handling)
343
343
344
+
[More information on how to handle JSON in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).
345
+
344
346
The target page could then look like this:
345
347
346
348
```sql
@@ -749,7 +751,7 @@ you can use the `dynamic` component to generate the table columns dynamically.
749
751
For that, you will need to return JSON objects from your SQL query, where the keys are the column names,
750
752
and the values are the cell contents.
751
753
752
-
Databases offer utilities to generate JSON objects from query results:
754
+
Databases [offer utilities to generate JSON objects from query results](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide)
753
755
- In PostgreSQL, you can use the [`json_build_object`](https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING)
754
756
function for a fixed number of columns, or [`json_object_agg`](https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE) for a dynamic number of columns.
755
757
- In SQLite, you can use the [`json_object`](https://www.sqlite.org/json1.html) function for a fixed number of columns,
@@ -888,6 +890,26 @@ SELECT ''dynamic'' AS component, ''
888
890
889
891
[View the result of this query, as well as an example of how to generate a dynamic menu
890
892
based on the database contents](./examples/dynamic_shell.sql).
893
+
', NULL),
894
+
('dynamic', '
895
+
## Dynamic tables
896
+
897
+
The `dynamic` component can be used to generate [tables](?component=table#component) with dynamic columns,
898
+
using [your database''s JSON functions](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).
899
+
900
+
For instance, let''s say we have a table with three columns: user_id, name, and role.
901
+
We want to create a table where each row is a user, and each column is a role.
902
+
We will return a set of json objects that look like this: `{"name": "Alice", "admin": true, "editor": false, "viewer": true}`
903
+
```sql
904
+
SELECT ''table'' AS component;
905
+
SELECT ''dynamic'' AS component,
906
+
json_patch(
907
+
json_object(''name'', name),
908
+
json_object_agg(role, is_admin)
909
+
) AS properties
910
+
FROM users
911
+
GROUP BY name;
912
+
```
891
913
', NULL);
892
914
893
915
INSERT INTO component(name, icon, description) VALUES
Copy file name to clipboardExpand all lines: examples/official-site/sqlpage/migrations/11_json.sql
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ VALUES (
31
31
'
32
32
Creates an API endpoint that will allow developers to easily query a list of users stored in your database.
33
33
34
-
You should use the json functions provided by your database to form the value you pass to the `contents` property.
34
+
You should use [the json functions provided by your database](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) to form the value you pass to the `contents` property.
35
35
To build a json array out of rows from the database, you can use:
0 commit comments