diff --git a/examples/official-site/content/functions/link.md b/examples/official-site/content/functions/link.md new file mode 100644 index 000000000..9d4d37354 --- /dev/null +++ b/examples/official-site/content/functions/link.md @@ -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; +``` diff --git a/examples/official-site/sqlpage/migrations/74_regex_match.sql b/examples/official-site/content/functions/regex_match.md similarity index 61% rename from examples/official-site/sqlpage/migrations/74_regex_match.sql rename to examples/official-site/content/functions/regex_match.md index 8e0540458..f8e9e6699 100644 --- a/examples/official-site/sqlpage/migrations/74_regex_match.sql +++ b/examples/official-site/content/functions/regex_match.md @@ -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 `(?...)` 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 @@ -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/(?\w+)/post/(?\d+)'', + '/categories/(?\w+)/post/(?\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: @@ -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 @@ -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 `(?...)` 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. diff --git a/examples/official-site/content/functions/set_variable.md b/examples/official-site/content/functions/set_variable.md new file mode 100644 index 000000000..1b1692e49 --- /dev/null +++ b/examples/official-site/content/functions/set_variable.md @@ -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. diff --git a/examples/official-site/content/functions/url_encode.md b/examples/official-site/content/functions/url_encode.md new file mode 100644 index 000000000..f9adb2818 --- /dev/null +++ b/examples/official-site/content/functions/url_encode.md @@ -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` diff --git a/examples/official-site/sqlpage/migrations/08_functions.sql b/examples/official-site/sqlpage/migrations/08_functions.sql index 3620a668b..4967882fd 100644 --- a/examples/official-site/sqlpage/migrations/08_functions.sql +++ b/examples/official-site/sqlpage/migrations/08_functions.sql @@ -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' - ); diff --git a/examples/official-site/sqlpage/migrations/47_link.sql b/examples/official-site/sqlpage/migrations/47_link.sql deleted file mode 100644 index 0d2949983..000000000 --- a/examples/official-site/sqlpage/migrations/47_link.sql +++ /dev/null @@ -1,79 +0,0 @@ -INSERT INTO - sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES - ( - 'link', - '0.25.0', - 'link', - '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; -``` -' - ); - -INSERT INTO - sqlpage_function_parameters ( - "function", - "index", - "name", - "description_md", - "type" - ) -VALUES - ( - 'link', - 1, - 'file', - 'The path of the SQLPage file to link to, relative to the current file.', - 'TEXT' - ), - ( - 'link', - 2, - 'parameters', - 'A JSON object with the parameters to pass to the linked file.', - 'JSON' - ), - ( - 'link', - 3, - 'fragment', - 'An optional fragment (hash) to append to the URL to link to a specific section of the target page.', - 'TEXT' - ); diff --git a/examples/official-site/sqlpage/migrations/72_set_variable.sql b/examples/official-site/sqlpage/migrations/72_set_variable.sql deleted file mode 100644 index 94ae7aad1..000000000 --- a/examples/official-site/sqlpage/migrations/72_set_variable.sql +++ /dev/null @@ -1,64 +0,0 @@ -INSERT INTO - sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES - ( - 'set_variable', - '0.40.0', - 'variable', - '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. -' - ); - -INSERT INTO - sqlpage_function_parameters ( - "function", - "index", - "name", - "description_md", - "type" - ) -VALUES - ( - 'set_variable', - 1, - 'name', - 'The name of the variable to set.', - 'TEXT' - ), - ( - 'set_variable', - 2, - 'value', - 'The value to set the variable to.', - 'TEXT' - ); diff --git a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql index 334cdef9a..00171de58 100644 --- a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql +++ b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql @@ -28,12 +28,10 @@ ALTER TABLE sqlpage_function_parameters ADD COLUMN optional BOOLEAN NOT NULL DEF UPDATE sqlpage_function_parameters SET optional = TRUE WHERE ("function", name) IN ( ('exec', 'arguments...'), ('hmac', 'algorithm'), - ('link', 'parameters'), ('link', 'fragment'), ('oidc_logout_url', 'redirect_uri'), ('persist_uploaded_file', 'destination_folder'), ('persist_uploaded_file', 'allowed_extensions'), ('persist_uploaded_file', 'mode'), ('persist_uploaded_files', 'destination_folder'), ('persist_uploaded_files', 'allowed_extensions'), ('persist_uploaded_files', 'mode'), - ('run_sql', 'parameters'), - ('set_variable', 'value') + ('run_sql', 'parameters') ); WITH copy(name, category, summary) AS (VALUES @@ -92,10 +90,6 @@ FROM copy WHERE component.name = copy.name; WITH copy(name, category, summary) AS (VALUES - ('link', 'links-and-routing', 'Build a page URL with optional query parameters and a fragment.'), - ('regex_match', 'links-and-routing', 'Match text against a regular expression and extract capture groups.'), - ('set_variable', 'links-and-routing', 'Build a link to this page with a query parameter changed or removed.'), - ('url_encode', 'links-and-routing', 'Encode text for use in a URL.'), ('persist_uploaded_file', 'files-and-uploads', 'Save an uploaded file and return its path.'), ('persist_uploaded_files', 'files-and-uploads', 'Save multiple uploads from one field and return their file details.'),