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
Add sqlpage.set_variable(name, value) function and update docs (#1124)
* feat: Add sqlpage.set_variable function
Co-authored-by: contact <contact@ophir.dev>
* Refactor: Fix set_variable serialization and update tests
Co-authored-by: contact <contact@ophir.dev>
* fix tests: no json_extract on mssql
* Refactor: Update URLParameters handling in set_variable function
- Replaced serde_json::Map with a custom URLParameters struct for better management of URL parameters.
- Introduced methods for handling single and vector values in URLParameters.
- Updated tests to reflect changes in the set_variable function's behavior.
* cargo fmt
* clippy
* retsore set var test
* remove redundant test
* ensure set_variable only takes into account GET variables, not SET
* factor url parameter setting code
* v0.40
* sqlpage.set_variable links to "?" when no parameter is present
- Renamed URLParameters module for clarity and removed the deprecated url_parameter_deserializer.
- Updated the set_variable function to return parameters directly instead of appending to a URL.
- Adjusted related function calls to reflect changes in URL parameter management.
---------
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Copy file name to clipboardExpand all lines: CHANGELOG.md
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,22 @@
1
1
# CHANGELOG.md
2
2
3
-
## unrelease
3
+
## 0.40.0 (unreleased)
4
+
- Fixed a bug in `sqlpage.link`: a link with no path (link to the current page) and no url parameter now works as expected. It used to keep the existing url parameters instead of removing them. `sqlpage.link('', '{}')` now returns `'?'` instead of the empty string.
- Returns a URL with the specified variable set to the given value, preserving other existing variables.
7
+
- This is a shorthand for `sqlpage.link(sqlpage.path(), json_patch(sqlpage.variables('get'), json_object(name, value)))`.
4
8
-**Variable System Improvements**: URL and POST parameters are now immutable, preventing accidental modification. User-defined variables created with `SET` remain mutable.
5
9
-**BREAKING**: `$variable` no longer accesses POST parameters. Use `:variable` instead.
6
10
-**What changed**: Previously, `$x` would return a POST parameter value if no GET parameter named `x` existed.
7
11
-**Fix**: Replace `$x` with `:x` when you need to access form field values.
8
12
-**Example**: Change `SELECT $username` to `SELECT :username` when reading form submissions.
9
-
-**BREAKING**: `SET $name` no longer overwrites GET (URL) parameters when a URL parameter with the same name exists.
13
+
-**BREAKING**: `SET $name` no longer makes GET (URL) parameters inaccessible when a URL parameter with the same name exists.
10
14
-**What changed**: `SET $name = 'value'` would previously overwrite the URL parameter `$name`. Now it creates an independent SET variable that shadows the URL parameter.
11
15
-**Fix**: This is generally the desired behavior. If you need to access the original URL parameter after setting a variable with the same name, extract it from the JSON returned by `sqlpage.variables('get')`.
12
16
-**Example**: If your URL is `page.sql?name=john`, and you do `SET $name = 'modified'`, then:
13
17
-`$name` will be `'modified'` (the SET variable)
14
18
- The original URL parameter is still preserved and accessible:
Copy file name to clipboardExpand all lines: Cargo.toml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
[package]
2
2
name = "sqlpage"
3
-
version = "0.39.1"
3
+
version = "0.40.0"
4
4
edition = "2021"
5
5
description = "Build data user interfaces entirely in SQL. A web server that takes .sql files and formats the query result using pre-made configurable professional-looking components."
'Returns a URL that is the same as the current page''s URL, but with a variable set to a new value.
14
+
15
+
This function is useful when you want to create a link that changes a parameter on the current page, while preserving other parameters.
16
+
17
+
It is equivalent to `sqlpage.link(sqlpage.path(), json_patch(sqlpage.variables(''get''), json_object(name, value)))`.
18
+
19
+
### Example
20
+
21
+
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).
22
+
23
+
```sql
24
+
select ''button'' as component, ''sm'' as size, ''center'' as justify;
25
+
select
26
+
category as title,
27
+
sqlpage.set_variable(''category'', category) as link,
28
+
case when $category = category then ''primary'' else ''secondary'' end as color
29
+
from categories;
30
+
```
31
+
32
+
### Parameters
33
+
- `name` (TEXT): The name of the variable to set.
34
+
- `value` (TEXT): The value to set the variable to. If `NULL` is passed, the variable is removed from the URL.
0 commit comments