Skip to content

Commit 38f5b81

Browse files
committed
New function: sqlpage.path
fixes #114
1 parent fc4ba7d commit 38f5b81

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.15.0
44

5+
- New function: [`sqlpage.path`](https://sql.ophir.dev/functions.sql?function=path#function) to get the path of the current page.
56
- Add a new `align_right` attribute to the [table](https://sql.ophir.dev/documentation.sql?component=table#component) component to align a column to the right.
67
- Fix display of long titles in the shell component.
78
- New [`sqlpage.variables`](https://sql.ophir.dev/functions.sql?function=variables#function) function for easy handling of complex forms
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
INSERT INTO sqlpage_functions (
2+
"name",
3+
"introduced_in_version",
4+
"icon",
5+
"description_md"
6+
)
7+
VALUES (
8+
'path',
9+
'0.15.0',
10+
'slashes',
11+
'Returns the request path of the current page.
12+
This is useful to generate links to the current page, and when you have a proxy in front of your SQLPage server that rewrites the URL.
13+
14+
### Example
15+
16+
If we have a page in a file named `my page.sql` at the root of your SQLPage installation
17+
then the following SQL query:
18+
19+
```sql
20+
select ''text'' as component, sqlpage.path() as contents;
21+
```
22+
23+
will return `/my%20page.sql`.
24+
25+
> Note that the path is URL-encoded.
26+
');

src/webserver/database/sql_pseudofunctions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(super) enum StmtParam {
3535
EnvironmentVariable(String),
3636
SqlPageVersion,
3737
Literal(String),
38+
Path,
3839
}
3940

4041
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -87,6 +88,7 @@ pub(super) fn func_call_to_param(func_name: &str, arguments: &mut [FunctionArg])
8788
}
8889
"version" => StmtParam::SqlPageVersion,
8990
"variables" => parse_get_or_post(extract_single_quoted_string_optional(arguments)),
91+
"path" => StmtParam::Path,
9092
unknown_name => StmtParam::Error(format!(
9193
"Unknown function {unknown_name}({})",
9294
FormatArguments(arguments)
@@ -207,6 +209,7 @@ pub(super) fn extract_req_param_non_nested<'a>(
207209
StmtParam::SqlPageVersion => Some(Cow::Borrowed(env!("CARGO_PKG_VERSION"))),
208210
StmtParam::Literal(x) => Some(Cow::Owned(x.to_string())),
209211
StmtParam::AllVariables(get_or_post) => extract_get_or_post(*get_or_post, request),
212+
StmtParam::Path => Some(Cow::Borrowed(&request.path)),
210213
})
211214
}
212215

src/webserver/http.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ impl SingleOrVec {
324324

325325
#[derive(Debug)]
326326
pub struct RequestInfo {
327+
pub path: String,
327328
pub get_variables: ParamMap,
328329
pub post_variables: ParamMap,
329330
pub headers: ParamMap,
@@ -384,6 +385,7 @@ async fn extract_request_info(req: &mut ServiceRequest, app_state: Arc<AppState>
384385
.map(Authorization::into_scheme);
385386

386387
RequestInfo {
388+
path: req.path().to_string(),
387389
headers: param_map(headers),
388390
get_variables: param_map(get_variables),
389391
post_variables: param_map(post_variables),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
select 'text' as component,
2+
CASE sqlpage.path()
3+
WHEN '/tests/sql_test_files/it_works_path.sql' THEN 'It works !'
4+
ELSE 'It failed ! Expected %2F, got ' || sqlpage.path() || '.'
5+
END
6+
AS contents;

0 commit comments

Comments
 (0)