Skip to content

Commit 4c61ca5

Browse files
committed
Move URL and routing function references to Markdown
1 parent 7f74091 commit 4c61ca5

8 files changed

Lines changed: 145 additions & 235 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
icon: link
3+
since: 0.25.0
4+
category: links-and-routing
5+
summary: Build a page URL with optional query parameters and a fragment.
6+
parameters:
7+
- name: file
8+
type: TEXT
9+
required: true
10+
description: The path of the SQLPage file to link to, relative to the current file.
11+
- name: parameters
12+
type: JSON
13+
description: A JSON object with the parameters to pass to the linked file.
14+
- name: fragment
15+
type: TEXT
16+
description: An optional fragment (hash) to append to the URL to link to a specific section of the target
17+
page.
18+
---
19+
20+
Returns the URL of a SQLPage file with the given parameters.
21+
22+
### Example
23+
24+
Let's say you have a database of products, and you want the main page (`index.sql`) to link to the page of each product (`product.sql`) with the product name as a parameter.
25+
26+
In `index.sql`, you can use the `link` function to generate the URL of the product page for each product.
27+
28+
```sql
29+
select 'list' as component;
30+
select
31+
name as title,
32+
sqlpage.link('product', json_object('product_name', name)) as link
33+
from products;
34+
```
35+
36+
In `product.sql`, you can then use `$product_name` to get the name of the product from the URL parameter:
37+
38+
```sql
39+
select 'hero' as component, $product_name as title, product_info as description
40+
from products
41+
where name = $product_name;
42+
```
43+
44+
> You could also have manually constructed the URL with `CONCAT('product?product_name=', name)`,
45+
> but using `sqlpage.link` is better because it ensures that the URL is properly encoded.
46+
> `sqlpage.link` will work even if the product name contains special characters like `&`, while `CONCAT(...)` would break the URL.
47+
48+
To link directly to a section, pass its `id` as the third argument, without the `#`:
49+
50+
```sql
51+
select sqlpage.link('product.sql', json_object('product_name', name), 'product_description') as link
52+
from products;
53+
```

examples/official-site/sqlpage/migrations/74_regex_match.sql renamed to examples/official-site/content/functions/regex_match.md

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
INSERT INTO
2-
sqlpage_functions (
3-
"name",
4-
"introduced_in_version",
5-
"icon",
6-
"description_md"
7-
)
8-
VALUES
9-
(
10-
'regex_match',
11-
'0.43.0',
12-
'regex',
13-
'Matches a text value against a regular expression and returns the capture groups as a JSON object.
1+
---
2+
icon: regex
3+
since: 0.43.0
4+
category: links-and-routing
5+
summary: Match text against a regular expression and extract capture groups.
6+
parameters:
7+
- name: pattern
8+
type: TEXT
9+
required: true
10+
description: The regular expression pattern to match against the input text. Named capture groups such
11+
as `(?<name>...)` are supported.
12+
- name: text
13+
type: TEXT
14+
required: true
15+
description: The text to match against the regular expression. Returns `NULL` when this argument is `NULL`.
16+
---
17+
18+
Matches a text value against a regular expression and returns the capture groups as a JSON object.
1419

1520
If the text matches the pattern, the result contains one entry for each capture group that matched:
1621
- key `0` contains the full match
@@ -31,16 +36,16 @@ You can put a `categories/404.sql` file in your project and extract the dynamic
3136
#### `categories/404.sql`
3237
```sql
3338
set route = sqlpage.regex_match(
34-
''/categories/(?<category>\w+)/post/(?<id>\d+)'',
39+
'/categories/(?<category>\w+)/post/(?<id>\d+)',
3540
sqlpage.path()
3641
);
3742

38-
select ''redirect'' as component, ''/404'' as link
43+
select 'redirect' as component, '/404' as link
3944
where $route is null;
4045

41-
select ''text'' as component;
46+
select 'text' as component;
4247
select
43-
''Category: '' || ($route->>''category'') || '' | Post id: '' || ($route->>''id'') as contents;
48+
'Category: ' || ($route->>'category') || ' | Post id: ' || ($route->>'id') as contents;
4449
```
4550

4651
If the current path is `/categories/sql/post/42`, `sqlpage.regex_match()` returns:
@@ -54,8 +59,8 @@ You can then use those extracted values to query your database:
5459
```sql
5560
select title, body
5661
from posts
57-
where category = $route->>''category''
58-
and id = cast($route->>''id'' as integer);
62+
where category = $route->>'category'
63+
and id = cast($route->>'id' as integer);
5964
```
6065

6166
### Details
@@ -67,37 +72,11 @@ where category = $route->>''category''
6772
- Some databases, such as MySQL and MariaDB, treat backslashes specially inside SQL strings.
6873
In those databases, you may need to write `\\w` and `\\d`, or use portable character classes such as `[A-Za-z0-9_]` and `[0-9]` instead.
6974
- In SQLite, PostgreSQL, and some other databases, you can read fields from the returned JSON with `->` and `->>`
70-
- On databases that do not support that syntax, use their JSON extraction function instead, such as `json_extract($route, ''$.category'')`
75+
- On databases that do not support that syntax, use their JSON extraction function instead, such as `json_extract($route, '$.category')`
7176
- For the full regular expression syntax supported by SQLPage, see the Rust `regex` crate documentation:
7277
[regex syntax reference](https://docs.rs/regex/latest/regex/#syntax)
7378
- If the input text is `NULL`, the function returns `NULL`
7479
- If an optional capture group does not match, that key is omitted from the JSON object
7580
- If the regular expression is invalid, SQLPage returns an error
7681

77-
The returned JSON can then be processed with your database''s JSON functions.
78-
'
79-
);
80-
81-
INSERT INTO
82-
sqlpage_function_parameters (
83-
"function",
84-
"index",
85-
"name",
86-
"description_md",
87-
"type"
88-
)
89-
VALUES
90-
(
91-
'regex_match',
92-
1,
93-
'pattern',
94-
'The regular expression pattern to match against the input text. Named capture groups such as `(?<name>...)` are supported.',
95-
'TEXT'
96-
),
97-
(
98-
'regex_match',
99-
2,
100-
'text',
101-
'The text to match against the regular expression. Returns `NULL` when this argument is `NULL`.',
102-
'TEXT'
103-
);
82+
The returned JSON can then be processed with your database's JSON functions.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
icon: variable
3+
since: 0.40.0
4+
category: links-and-routing
5+
summary: Build a link to this page with a query parameter changed or removed.
6+
parameters:
7+
- name: name
8+
type: TEXT
9+
required: true
10+
description: The name of the variable to set.
11+
- name: value
12+
type: TEXT
13+
description: The value to set the variable to.
14+
---
15+
16+
Returns a URL that is the same as the current page's URL, but with a variable set to a new value.
17+
18+
This function is useful when you want to create a link that changes a parameter on the current page, while preserving other parameters.
19+
20+
It is equivalent to `sqlpage.link(sqlpage.path(), json_patch(sqlpage.variables('get'), json_object(name, value)))`.
21+
22+
### Example
23+
24+
Let's say you have a list of products, and you want to filter them by category. You can use `sqlpage.set_variable` to create links that change the category filter, without losing other potential filters (like a search query or a sort order).
25+
26+
```sql
27+
select 'button' as component, 'sm' as size, 'center' as justify;
28+
select
29+
category as title,
30+
sqlpage.set_variable('category', category) as link,
31+
case when $category = category then 'primary' else 'secondary' end as color
32+
from categories;
33+
```
34+
35+
When `sqlpage.set_variable(...)` is used as a standalone selected column in a query that returns several rows, it runs once per returned row.
36+
This is useful for generating one link per row, as in the example above.
37+
If you need a single link reused later in the page, store it with `SET` first.
38+
39+
### Parameters
40+
- `name` (TEXT): The name of the variable to set.
41+
- `value` (TEXT): The value to set the variable to. If `NULL` is passed, the variable is removed from the URL.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
icon: percentage
3+
since: 0.12.0
4+
category: links-and-routing
5+
summary: Encode text for use in a URL.
6+
parameters:
7+
- name: string
8+
type: TEXT
9+
required: true
10+
description: The string to encode.
11+
---
12+
13+
Returns the given string, with all characters that are not allowed in a URL encoded.
14+
15+
### Example
16+
17+
```sql
18+
select 'text' as component;
19+
select 'https://example.com/?q=' || sqlpage.url_encode($user_search) as contents;
20+
```
21+
22+
#### Result
23+
24+
`https://example.com/?q=hello%20world`

‎examples/official-site/sqlpage/migrations/08_functions.sql‎

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -352,41 +352,3 @@ VALUES (
352352
'The arguments to pass to the program.',
353353
'TEXT'
354354
);
355-
INSERT INTO sqlpage_functions (
356-
"name",
357-
"introduced_in_version",
358-
"icon",
359-
"description_md"
360-
)
361-
VALUES (
362-
'url_encode',
363-
'0.12.0',
364-
'percentage',
365-
'Returns the given string, with all characters that are not allowed in a URL encoded.
366-
367-
### Example
368-
369-
```sql
370-
select ''text'' as component;
371-
select ''https://example.com/?q='' || sqlpage.url_encode($user_search) as contents;
372-
```
373-
374-
#### Result
375-
376-
`https://example.com/?q=hello%20world`
377-
'
378-
);
379-
INSERT INTO sqlpage_function_parameters (
380-
"function",
381-
"index",
382-
"name",
383-
"description_md",
384-
"type"
385-
)
386-
VALUES (
387-
'url_encode',
388-
1,
389-
'string',
390-
'The string to encode.',
391-
'TEXT'
392-
);

‎examples/official-site/sqlpage/migrations/47_link.sql‎

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)