Skip to content

Commit 2fed231

Browse files
committed
add an example to illustrate json handling in mysql
see apache/datafusion-sqlparser-rs#1019 see #146
1 parent 51f5840 commit 2fed231

File tree

6 files changed

+84
-1
lines changed

6 files changed

+84
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Handling json data in MySQL
2+
3+
This demonstrates both how to produce json data from a SQL query in MySQL
4+
and how to consume json data from SQLPage.
5+
6+
![](./screenshots/app.png)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
services:
2+
web:
3+
image: lovasoa/sqlpage:main # main is cutting edge, use lovasoa/sqlpage:latest for the latest stable version
4+
ports:
5+
- "8080:8080"
6+
volumes:
7+
- .:/var/www
8+
- ./sqlpage:/etc/sqlpage
9+
depends_on:
10+
- db
11+
environment:
12+
DATABASE_URL: mysql://root:secret@db/sqlpage
13+
db: # The DB environment variable can be set to "mariadb" or "postgres" to test the code with different databases
14+
ports:
15+
- "3306:3306"
16+
image: mariadb:10.6 # support for json_table was added in mariadb 10.6
17+
environment:
18+
MYSQL_ROOT_PASSWORD: secret
19+
MYSQL_DATABASE: sqlpage
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
select 'form' as component, 'Create a new Group' as title, 'Create' as validate;
2+
select 'Name' as name;
3+
4+
insert into groups(name) select :Name where :Name is not null;
5+
6+
select 'list' as component, 'Groups' as title, 'No group yet' as empty_title;
7+
select name as title from groups;
8+
9+
select 'form' as component, 'Add an user' as title, 'Add' as validate;
10+
select 'UserName' as name, 'Name' as label;
11+
select
12+
'Memberships[]' as name,
13+
'Group memberships' as label,
14+
'select' as type,
15+
TRUE as multiple,
16+
'press ctrl to select multiple values' as description,
17+
json_arrayagg(json_object("label", name, "value", id)) as options
18+
from groups;
19+
20+
insert into users(name) select :UserName where :UserName is not null;
21+
insert into group_members(group_id, user_id)
22+
select CAST(json_unquote(json_elems.json_value) AS INT), last_insert_id()
23+
from (
24+
with recursive json_elems(n, json_value) as (
25+
select 0, json_extract(:Memberships, '$[0]')
26+
union all
27+
select n + 1, json_extract(:Memberships, concat('$[', n + 1, ']'))
28+
from json_elems
29+
where json_value is not null
30+
) select * from json_elems where json_value is not null
31+
) as json_elems
32+
where :Memberships is not null;
33+
34+
select 'list' as component, 'Users' as title, 'No user yet' as empty_title;
35+
select
36+
users.name as title,
37+
group_concat(groups.name) as description
38+
from users
39+
left join group_members on users.id = group_members.user_id
40+
left join groups on groups.id = group_members.group_id
41+
group by users.id, users.name;
122 KB
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
create table users (
2+
id int primary key auto_increment,
3+
name varchar(255) not null
4+
);
5+
6+
create table groups (
7+
id int primary key auto_increment,
8+
name varchar(255) not null
9+
);
10+
11+
create table group_members (
12+
group_id int not null,
13+
user_id int not null,
14+
primary key (group_id, user_id),
15+
foreign key (group_id) references groups (id),
16+
foreign key (user_id) references users (id)
17+
);

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ This creates a more compact (but arguably less user-friendly) alternative to a s
324324
In this case, you should add square brackets to the name of the field.
325325
The target page will then receive the value as a JSON array of strings, which you can iterate over using
326326
- the `json_each` function [in SQLite](https://www.sqlite.org/json1.html) and [Postgres](https://www.postgresql.org/docs/9.3/functions-json.html),
327-
- the [`JSON_TABLE`](https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html) function in MySQL (which you''ll need to wrap in a function, because SQLPage cannot parse the non-standard syntax of this function)
328327
- the [`OPENJSON`](https://learn.microsoft.com/fr-fr/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16) function in Microsoft SQL Server.
328+
- in MySQL, json manipulation is less straightforward: see [the SQLPage MySQL json example](https://github.com/lovasoa/SQLpage/tree/main/examples/mysql%20json%20handling)
329329
330330
The target page could then look like this:
331331

0 commit comments

Comments
 (0)