Skip to content

Commit f430685

Browse files
committed
add support for environment variables
1 parent 69a1aab commit f430685

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# CHANGELOG.md
22

33
## 0.11.0 (unreleased)
4+
- Support for **environment variables** ! You can now read environment variables from sql code using `sqlpage.environment_variable('VAR_NAME')`.
45
- Better support for connection options in mssql.
56
- New icons (see https://tabler-icons.io/changelog)
67
- New version of the CSS library (see https://preview.tabler.io/changelog.html)

examples/official-site/sqlpage/migrations/08_functions.sql

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,39 @@ Currently running from `/home/user/my_sqlpage_website`
235235
The current working directory is the directory from which the SQLPage server process was started.
236236
By default, this is also the directory from which `.sql` files are loaded and served.
237237
However, this can be changed by setting the `web_root` [configuration option](https://github.com/lovasoa/SQLpage/blob/main/configuration.md).
238-
');
238+
'
239+
);
240+
INSERT INTO sqlpage_functions (
241+
"name",
242+
"introduced_in_version",
243+
"icon",
244+
"description_md"
245+
)
246+
VALUES (
247+
'enviroment_variable',
248+
'0.11.0',
249+
'variable',
250+
'Returns the value of the given [environment variable](https://en.wikipedia.org/wiki/Environment_variable).
251+
252+
### Example
253+
254+
```sql
255+
SELECT ''text'' AS component;
256+
SELECT ''The value of the HOME environment variable is '' AS contents;
257+
SELECT sqlpage.environment_variable(''HOME'') as contents, true as code;
258+
```'
259+
);
260+
INSERT INTO sqlpage_function_parameters (
261+
"function",
262+
"index",
263+
"name",
264+
"description_md",
265+
"type"
266+
)
267+
VALUES (
268+
'enviroment_variable',
269+
1,
270+
'name',
271+
'The name of the environment variable to read. Must be a literal string.',
272+
'TEXT'
273+
);

src/webserver/database/sql_pseudofunctions.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub(super) enum StmtParam {
2727
HashPassword(Box<StmtParam>),
2828
RandomString(usize),
2929
CurrentWorkingDir,
30+
EnvironmentVariable(String),
3031
}
3132

3233
pub(super) fn func_call_to_param(func_name: &str, arguments: &mut [FunctionArg]) -> StmtParam {
@@ -43,6 +44,8 @@ pub(super) fn func_call_to_param(func_name: &str, arguments: &mut [FunctionArg])
4344
"random_string" => extract_integer("random_string", arguments)
4445
.map_or_else(StmtParam::Error, StmtParam::RandomString),
4546
"current_working_directory" => StmtParam::CurrentWorkingDir,
47+
"environment_variable" => extract_single_quoted_string("environment_variable", arguments)
48+
.map_or_else(StmtParam::Error, StmtParam::EnvironmentVariable),
4649
unknown_name => StmtParam::Error(format!(
4750
"Unknown function {unknown_name}({})",
4851
FormatArguments(arguments)
@@ -75,6 +78,10 @@ pub(super) fn extract_req_param<'a>(
7578
.map_or(Ok(None), |x| hash_password(&x).map(Cow::Owned).map(Some))?,
7679
StmtParam::RandomString(len) => Some(Cow::Owned(random_string(*len))),
7780
StmtParam::CurrentWorkingDir => cwd()?,
81+
StmtParam::EnvironmentVariable(var) => std::env::var(var)
82+
.map(Cow::Owned)
83+
.map(Some)
84+
.with_context(|| format!("Unable to read environment variable {var}"))?,
7885
})
7986
}
8087

0 commit comments

Comments
 (0)