Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions examples/official-site/content/functions/link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
icon: link
since: 0.25.0
category: links-and-routing
summary: Build a page URL with optional query parameters and a fragment.
parameters:
- name: file
type: TEXT
required: true
description: The path of the SQLPage file to link to, relative to the current file.
- name: parameters
type: JSON
description: A JSON object with the parameters to pass to the linked file.
- name: fragment
type: TEXT
description: An optional fragment (hash) to append to the URL to link to a specific section of the target
page.
---

Returns the URL of a SQLPage file with the given parameters.

### Example

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.

In `index.sql`, you can use the `link` function to generate the URL of the product page for each product.

```sql
select 'list' as component;
select
name as title,
sqlpage.link('product', json_object('product_name', name)) as link
from products;
```

In `product.sql`, you can then use `$product_name` to get the name of the product from the URL parameter:

```sql
select 'hero' as component, $product_name as title, product_info as description
from products
where name = $product_name;
```

> You could also have manually constructed the URL with `CONCAT('product?product_name=', name)`,
> but using `sqlpage.link` is better because it ensures that the URL is properly encoded.
> `sqlpage.link` will work even if the product name contains special characters like `&`, while `CONCAT(...)` would break the URL.

To link directly to a section, pass its `id` as the third argument, without the `#`:

```sql
select sqlpage.link('product.sql', json_object('product_name', name), 'product_description') as link
from products;
```
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
INSERT INTO
sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES
(
'regex_match',
'0.43.0',
'regex',
'Matches a text value against a regular expression and returns the capture groups as a JSON object.
---
icon: regex
since: 0.43.0
category: links-and-routing
summary: Match text against a regular expression and extract capture groups.
parameters:
- name: pattern
type: TEXT
required: true
description: The regular expression pattern to match against the input text. Named capture groups such
as `(?<name>...)` are supported.
- name: text
type: TEXT
required: true
description: The text to match against the regular expression. Returns `NULL` when this argument is `NULL`.
---

Matches a text value against a regular expression and returns the capture groups as a JSON object.

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

select ''redirect'' as component, ''/404'' as link
select 'redirect' as component, '/404' as link
where $route is null;

select ''text'' as component;
select 'text' as component;
select
''Category: '' || ($route->>''category'') || '' | Post id: '' || ($route->>''id'') as contents;
'Category: ' || ($route->>'category') || ' | Post id: ' || ($route->>'id') as contents;
```

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

### Details
Expand All @@ -67,37 +72,11 @@ where category = $route->>''category''
- Some databases, such as MySQL and MariaDB, treat backslashes specially inside SQL strings.
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.
- In SQLite, PostgreSQL, and some other databases, you can read fields from the returned JSON with `->` and `->>`
- On databases that do not support that syntax, use their JSON extraction function instead, such as `json_extract($route, ''$.category'')`
- On databases that do not support that syntax, use their JSON extraction function instead, such as `json_extract($route, '$.category')`
- For the full regular expression syntax supported by SQLPage, see the Rust `regex` crate documentation:
[regex syntax reference](https://docs.rs/regex/latest/regex/#syntax)
- If the input text is `NULL`, the function returns `NULL`
- If an optional capture group does not match, that key is omitted from the JSON object
- If the regular expression is invalid, SQLPage returns an error

The returned JSON can then be processed with your database''s JSON functions.
'
);

INSERT INTO
sqlpage_function_parameters (
"function",
"index",
"name",
"description_md",
"type"
)
VALUES
(
'regex_match',
1,
'pattern',
'The regular expression pattern to match against the input text. Named capture groups such as `(?<name>...)` are supported.',
'TEXT'
),
(
'regex_match',
2,
'text',
'The text to match against the regular expression. Returns `NULL` when this argument is `NULL`.',
'TEXT'
);
The returned JSON can then be processed with your database's JSON functions.
41 changes: 41 additions & 0 deletions examples/official-site/content/functions/set_variable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
icon: variable
since: 0.40.0
category: links-and-routing
summary: Build a link to this page with a query parameter changed or removed.
parameters:
- name: name
type: TEXT
required: true
description: The name of the variable to set.
- name: value
type: TEXT
description: The value to set the variable to.
---

Returns a URL that is the same as the current page's URL, but with a variable set to a new value.

This function is useful when you want to create a link that changes a parameter on the current page, while preserving other parameters.

It is equivalent to `sqlpage.link(sqlpage.path(), json_patch(sqlpage.variables('get'), json_object(name, value)))`.

### Example

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).

```sql
select 'button' as component, 'sm' as size, 'center' as justify;
select
category as title,
sqlpage.set_variable('category', category) as link,
case when $category = category then 'primary' else 'secondary' end as color
from categories;
```

When `sqlpage.set_variable(...)` is used as a standalone selected column in a query that returns several rows, it runs once per returned row.
This is useful for generating one link per row, as in the example above.
If you need a single link reused later in the page, store it with `SET` first.

### Parameters
- `name` (TEXT): The name of the variable to set.
- `value` (TEXT): The value to set the variable to. If `NULL` is passed, the variable is removed from the URL.
24 changes: 24 additions & 0 deletions examples/official-site/content/functions/url_encode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
icon: percentage
since: 0.12.0
category: links-and-routing
summary: Encode text for use in a URL.
parameters:
- name: string
type: TEXT
required: true
description: The string to encode.
---

Returns the given string, with all characters that are not allowed in a URL encoded.

### Example

```sql
select 'text' as component;
select 'https://example.com/?q=' || sqlpage.url_encode($user_search) as contents;
```

#### Result

`https://example.com/?q=hello%20world`
38 changes: 0 additions & 38 deletions examples/official-site/sqlpage/migrations/08_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -352,41 +352,3 @@ VALUES (
'The arguments to pass to the program.',
'TEXT'
);
INSERT INTO sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES (
'url_encode',
'0.12.0',
'percentage',
'Returns the given string, with all characters that are not allowed in a URL encoded.

### Example

```sql
select ''text'' as component;
select ''https://example.com/?q='' || sqlpage.url_encode($user_search) as contents;
```

#### Result

`https://example.com/?q=hello%20world`
'
);
INSERT INTO sqlpage_function_parameters (
"function",
"index",
"name",
"description_md",
"type"
)
VALUES (
'url_encode',
1,
'string',
'The string to encode.',
'TEXT'
);
79 changes: 0 additions & 79 deletions examples/official-site/sqlpage/migrations/47_link.sql

This file was deleted.

Loading
Loading