Skip to content

Commit 7dc8683

Browse files
committed
sqlpage.environment_variable('inexistant') = null
fixes #746
1 parent a8f6d33 commit 7dc8683

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- Temporarily disable automatic tick amount calculation in the chart component. This was causing issues with mislabeled x-axis data, because of a bug in ApexCharts.
3131
- Add a new `max_recursion_depth` configuration option to limit the depth of recursion allowed in the `run_sql` function.
3232
- Fix a bug where the results of the `JSON` function in sqlite would be interpreted as a string instead of a json object.
33+
- Fix a bug where the `sqlpage.environment_variable` function would return an error if the environment variable was not set. Now it returns `null` instead.
3334

3435
## 0.31.0 (2024-11-24)
3536

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ async fn current_working_directory() -> anyhow::Result<String> {
8989
}
9090

9191
/// Returns the value of an environment variable.
92-
async fn environment_variable(name: Cow<'_, str>) -> anyhow::Result<Cow<'_, str>> {
93-
std::env::var(&*name)
94-
.with_context(|| format!("unable to access the environment variable {name}"))
95-
.map(Cow::Owned)
92+
async fn environment_variable(name: Cow<'_, str>) -> anyhow::Result<Option<Cow<'_, str>>> {
93+
match std::env::var(&*name) {
94+
Ok(value) => Ok(Some(Cow::Owned(value))),
95+
Err(std::env::VarError::NotPresent) if name.contains("=") || name.contains("\0") => anyhow::bail!("Invalid environment variable name: {name:?}. Environment variable names cannot contain an equals sign or a null character."),
96+
Err(std::env::VarError::NotPresent) => Ok(None),
97+
Err(err) => Err(err).with_context(|| format!("unable to read the environment variable {name:?}"))
98+
}
9699
}
97100

98101
/// Executes an external command and returns its output.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select 'text' as component, coalesce(sqlpage.environment_variable('I_DO_NOT_EXIST'), 'It works !') as contents;

0 commit comments

Comments
 (0)