Skip to content

Commit 3788c61

Browse files
committed
Give server and runtime functions editable reference files
1 parent 9aa852b commit 3788c61

11 files changed

Lines changed: 185 additions & 270 deletions

File tree

‎examples/official-site/README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ example project cards. A project's `folder` must match its folder under
4545
Function pages in `content/functions/` use their filename as the public function
4646
name. Keep the argument list in signature order and mark mandatory arguments with
4747
`required: true`. The Markdown body can include SQL examples without escaping.
48-
Older entries still live in the SQL migrations.
4948

5049
## Landing page
5150

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
icon: folder-cog
3+
since: 0.42.0
4+
category: server-and-runtime
5+
summary: Locate the directory for SQLPage configuration and templates.
6+
---
7+
8+
Returns the configuration directory where SQLPage looks for `sqlpage.json`, templates, and migrations.
9+
10+
### Example
11+
12+
```sql
13+
SELECT 'text' AS component;
14+
SELECT 'Configuration files are in ' AS contents;
15+
SELECT sqlpage.configuration_directory() as contents, true as code;
16+
```
17+
18+
#### Result
19+
20+
Configuration files are in `/home/user/my_sqlpage_website/sqlpage`
21+
22+
#### Notes
23+
24+
The configuration directory is where SQLPage looks for:
25+
- `sqlpage.json` (the configuration file)
26+
- `templates/` (custom component templates)
27+
- `migrations/` (database migration files)
28+
29+
By default, it is `./sqlpage` relative to the current working directory, but it can be changed using:
30+
- the `--config-dir` command line argument
31+
- the `SQLPAGE_CONFIGURATION_DIRECTORY` or `CONFIGURATION_DIRECTORY` environment variable
32+
33+
This function is useful when you need to reference configuration-related files in your SQL code.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
icon: folder-question
3+
since: 0.11.0
4+
category: server-and-runtime
5+
summary: Get the working directory of the SQLPage server process.
6+
---
7+
8+
Returns the [current working directory](https://en.wikipedia.org/wiki/Working_directory) of the SQLPage server process.
9+
10+
### Example
11+
12+
```sql
13+
SELECT 'text' AS component;
14+
SELECT 'Currently running from ' AS contents;
15+
SELECT sqlpage.current_working_directory() as contents, true as code;
16+
```
17+
18+
#### Result
19+
20+
Currently running from `/home/user/my_sqlpage_website`
21+
22+
#### Notes
23+
24+
The current working directory is the directory from which the SQLPage server process was started.
25+
By default, this is also the directory from which `.sql` files are loaded and served.
26+
However, this can be changed by setting the `web_root` [configuration option](https://github.com/sqlpage/SQLPage/blob/main/configuration.md).
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
icon: variable
3+
since: 0.11.0
4+
category: server-and-runtime
5+
summary: Read a named environment variable from the server.
6+
parameters:
7+
- name: name
8+
type: TEXT
9+
required: true
10+
description: The name of the environment variable to read. Must be a literal string.
11+
---
12+
13+
Returns the value of the given [environment variable](https://en.wikipedia.org/wiki/Environment_variable).
14+
15+
### Example
16+
17+
```sql
18+
SELECT 'text' AS component;
19+
SELECT 'The value of the HOME environment variable is ' AS contents;
20+
SELECT sqlpage.environment_variable('HOME') as contents, true as code;
21+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
icon: terminal-2
3+
since: 0.12.0
4+
category: server-and-runtime
5+
summary: Run a program on the server and capture its output.
6+
parameters:
7+
- name: program
8+
type: TEXT
9+
required: true
10+
description: The name of the program to execute. Must be a literal string.
11+
- name: arguments...
12+
type: TEXT
13+
description: The arguments to pass to the program.
14+
---
15+
16+
Executes a shell command and returns its output as text.
17+
18+
### Example
19+
20+
#### Fetch data from a remote API using curl
21+
22+
```sql
23+
select 'card' as component;
24+
select value->>'name' as title, value->>'email' as description
25+
from json_each(sqlpage.exec('curl', 'https://jsonplaceholder.typicode.com/users'));
26+
```
27+
28+
#### Notes
29+
30+
- This function is disabled by default for security reasons. You can enable it by setting `"allow_exec" : true` in `sqlpage/sqlpage.json`. Enable it only if you trust all the users that can access your SQLPage server files (both locally and on the database).
31+
- Be careful when using this function, as it can be used to execute arbitrary shell commands on your server. Do not use it with untrusted input.
32+
- The command is executed in the current working directory of the SQLPage server process.
33+
- The command is executed with the same user as the SQLPage server process.
34+
- The environment variables of the SQLPage server process are passed to the command, including potentially sensitive variables such as `DATABASE_URL`.
35+
- The command is executed asynchronously, but the SQLPage server has to wait for it to finish before sending the result to the client.
36+
This means that the SQLPage server will not be blocked while the command is running, it will be able to serve other requests, but it will not be able to serve the current request until the command has finished.
37+
You should generally avoid long running commands.
38+
- If the program name is NULL, the result will be NULL.
39+
- If any argument is NULL, it will be passed to the command as an empty string.
40+
- If the command exits with a non-zero exit code, the function will raise an error.
41+
- Arbitrary SQL operations are not allowed as sqlpage function arguments. Use `SET` to assign the result of a SQL query to a variable, and then use that variable as an argument to `sqlpage.exec`.
42+
- When `sqlpage.exec(...)` is a standalone selected column, the command runs once per returned row. Use `SET command_result = sqlpage.exec(...)` first if the command should run only once for the page.

examples/official-site/sqlpage/migrations/38_run_sql.sql renamed to examples/official-site/content/functions/run_sql.md

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
INSERT INTO sqlpage_functions (
2-
"name",
3-
"introduced_in_version",
4-
"icon",
5-
"description_md"
6-
)
7-
VALUES (
8-
'run_sql',
9-
'0.20.0',
10-
'login',
11-
'Executes another SQL file and returns its result as a JSON array.
1+
---
2+
icon: login
3+
since: 0.20.0
4+
category: server-and-runtime
5+
summary: Run another SQLPage file and return its rows as JSON.
6+
parameters:
7+
- name: file
8+
type: TEXT
9+
required: true
10+
description: |-
11+
Path to the SQL file to execute, can be absolute, or relative to the web root (the root folder of your website sql files).
12+
In-database files, from the sqlpage_files(path, contents, last_modified) table are supported.
13+
- name: parameters
14+
type: JSON
15+
description: Optional JSON object to pass as parameters to the included SQL file. The keys of the object
16+
will be available as variables in the included file. By default, the included file will have access to
17+
the same variables as the calling file.
18+
---
19+
20+
Executes another SQL file and returns its result as a JSON array.
1221

1322
### Example
1423

@@ -21,7 +30,7 @@ and use the [`dynamic`](documentation.sql?component=dynamic#component) component
2130
to include it in all your pages.
2231

2332
```sql
24-
select ''dynamic'' as component, sqlpage.run_sql(''common_header.sql'') as properties;
33+
select 'dynamic' as component, sqlpage.run_sql('common_header.sql') as properties;
2534
```
2635

2736
#### Factorize logic between pages
@@ -38,7 +47,7 @@ select some_field from some_table;
3847

3948
```sql
4049
-- save the value of some_field from the first result row of reusable.sql into $my_var
41-
set my_var = sqlpage.run_sql(''reusable.sql'')->>0->>''some_field'';
50+
set my_var = sqlpage.run_sql('reusable.sql')->>0->>'some_field';
4251
```
4352

4453
See [json in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide)
@@ -65,31 +74,8 @@ You can pass parameters to the included file, as if it had been with a URL param
6574
For instance, you can use:
6675

6776
```sql
68-
sqlpage.run_sql(''included_file.sql'', json_object(''param1'', ''value1'', ''param2'', ''value2''))
77+
sqlpage.run_sql('included_file.sql', json_object('param1', 'value1', 'param2', 'value2'))
6978
```
7079

7180
Which will make `$param1` and `$param2` available in the included file.
7281
[More information about building JSON objects in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).
73-
'
74-
);
75-
INSERT INTO sqlpage_function_parameters (
76-
"function",
77-
"index",
78-
"name",
79-
"description_md",
80-
"type"
81-
)
82-
VALUES (
83-
'run_sql',
84-
1,
85-
'file',
86-
'Path to the SQL file to execute, can be absolute, or relative to the web root (the root folder of your website sql files).
87-
In-database files, from the sqlpage_files(path, contents, last_modified) table are supported.',
88-
'TEXT'
89-
),(
90-
'run_sql',
91-
2,
92-
'parameters',
93-
'Optional JSON object to pass as parameters to the included SQL file. The keys of the object will be available as variables in the included file. By default, the included file will have access to the same variables as the calling file.',
94-
'JSON'
95-
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
icon: git-commit
3+
since: 0.11.0
4+
category: server-and-runtime
5+
summary: Read the version of SQLPage serving the page.
6+
---
7+
8+
Returns the current version of SQLPage as a string.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
icon: folder-code
3+
since: 0.42.0
4+
category: server-and-runtime
5+
summary: Locate the directory that holds the site's public files.
6+
---
7+
8+
Returns the web root directory where SQLPage serves `.sql` files from.
9+
10+
### Example
11+
12+
```sql
13+
SELECT 'text' AS component;
14+
SELECT 'SQL files are served from ' AS contents;
15+
SELECT sqlpage.web_root() as contents, true as code;
16+
```
17+
18+
#### Result
19+
20+
SQL files are served from `/home/user/my_sqlpage_website`
21+
22+
#### Notes
23+
24+
The web root is the directory from which `.sql` files are loaded and served.
25+
By default, it is the current working directory, but it can be changed using:
26+
- the `--web-root` command line argument
27+
- the `web_root` [configuration option](https://github.com/sqlpage/SQLPage/blob/main/configuration.md) in `sqlpage.json`
28+
- the `WEB_ROOT` environment variable
29+
30+
This is more reliable than `sqlpage.current_working_directory()` when you need to reference the location of your SQL files.

0 commit comments

Comments
 (0)