Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/official-site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ example project cards. A project's `folder` must match its folder under
Function pages in `content/functions/` use their filename as the public function
name. Keep the argument list in signature order and mark mandatory arguments with
`required: true`. The Markdown body can include SQL examples without escaping.
Older entries still live in the SQL migrations.

## Landing page

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
icon: folder-cog
since: 0.42.0
category: server-and-runtime
summary: Locate the directory for SQLPage configuration and templates.
---

Returns the configuration directory where SQLPage looks for `sqlpage.json`, templates, and migrations.

### Example

```sql
SELECT 'text' AS component;
SELECT 'Configuration files are in ' AS contents;
SELECT sqlpage.configuration_directory() as contents, true as code;
```

#### Result

Configuration files are in `/home/user/my_sqlpage_website/sqlpage`

#### Notes

The configuration directory is where SQLPage looks for:
- `sqlpage.json` (the configuration file)
- `templates/` (custom component templates)
- `migrations/` (database migration files)

By default, it is `./sqlpage` relative to the current working directory, but it can be changed using:
- the `--config-dir` command line argument
- the `SQLPAGE_CONFIGURATION_DIRECTORY` or `CONFIGURATION_DIRECTORY` environment variable

This function is useful when you need to reference configuration-related files in your SQL code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
icon: folder-question
since: 0.11.0
category: server-and-runtime
summary: Get the working directory of the SQLPage server process.
---

Returns the [current working directory](https://en.wikipedia.org/wiki/Working_directory) of the SQLPage server process.

### Example

```sql
SELECT 'text' AS component;
SELECT 'Currently running from ' AS contents;
SELECT sqlpage.current_working_directory() as contents, true as code;
```

#### Result

Currently running from `/home/user/my_sqlpage_website`

#### Notes

The current working directory is the directory from which the SQLPage server process was started.
By default, this is also the directory from which `.sql` files are loaded and served.
Setting [`web_root`](https://github.com/sqlpage/SQLPage/blob/main/configuration.md) changes where SQLPage serves files. It does not change the process working directory. Use [`sqlpage.web_root()`](?function=web_root) to get the directory actually being served.
21 changes: 21 additions & 0 deletions examples/official-site/content/functions/environment_variable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
icon: variable
since: 0.11.0
category: server-and-runtime
summary: Read a named environment variable from the server.
parameters:
- name: name
type: TEXT
required: true
description: The name of the environment variable to read. Must be a literal string.
---

Returns the value of the given [environment variable](https://en.wikipedia.org/wiki/Environment_variable).

### Example

```sql
SELECT 'text' AS component;
SELECT 'The value of the HOME environment variable is ' AS contents;
SELECT sqlpage.environment_variable('HOME') as contents, true as code;
```
42 changes: 42 additions & 0 deletions examples/official-site/content/functions/exec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
icon: terminal-2
since: 0.12.0
category: server-and-runtime
summary: Run a program on the server and capture its output.
parameters:
- name: program
type: TEXT
required: true
description: The name of the program to execute. Must be a literal string.
- name: arguments...
type: TEXT
description: The arguments to pass to the program.
---

Executes a shell command and returns its output as text.

### Example

#### Fetch data from a remote API using curl

```sql
select 'card' as component;
select value->>'name' as title, value->>'email' as description
from json_each(sqlpage.exec('curl', 'https://jsonplaceholder.typicode.com/users'));
```

#### Notes

- 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).
- 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.
- The command is executed in the current working directory of the SQLPage server process.
- The command is executed with the same user as the SQLPage server process.
- The environment variables of the SQLPage server process are passed to the command, including potentially sensitive variables such as `DATABASE_URL`.
- The command is executed asynchronously, but the SQLPage server has to wait for it to finish before sending the result to the client.
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.
You should generally avoid long running commands.
- If the program name is NULL, the result will be NULL.
- If any argument is NULL, it will be passed to the command as an empty string.
- If the command exits with a non-zero exit code, the function will raise an error.
- 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`.
- 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.
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
INSERT INTO sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES (
'run_sql',
'0.20.0',
'login',
'Executes another SQL file and returns its result as a JSON array.
---
icon: login
since: 0.20.0
category: server-and-runtime
summary: Run another SQLPage file and return its rows as JSON.
parameters:
- name: file
type: TEXT
required: true
description: |-
Path to the SQL file to execute, can be absolute, or relative to the web root (the root folder of your website sql files).
In-database files, from the sqlpage_files(path, contents, last_modified) table are supported.
- name: parameters
type: JSON
description: 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.
---

Executes another SQL file and returns its result as a JSON array.

### Example

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

```sql
select ''dynamic'' as component, sqlpage.run_sql(''common_header.sql'') as properties;
select 'dynamic' as component, sqlpage.run_sql('common_header.sql') as properties;
```

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

```sql
-- save the value of some_field from the first result row of reusable.sql into $my_var
set my_var = sqlpage.run_sql(''reusable.sql'')->>0->>''some_field'';
set my_var = sqlpage.run_sql('reusable.sql')->>0->>'some_field';
```

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

```sql
sqlpage.run_sql(''included_file.sql'', json_object(''param1'', ''value1'', ''param2'', ''value2''))
sqlpage.run_sql('included_file.sql', json_object('param1', 'value1', 'param2', 'value2'))
```

Which will make `$param1` and `$param2` available in the included file.
[More information about building JSON objects in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).
'
);
INSERT INTO sqlpage_function_parameters (
"function",
"index",
"name",
"description_md",
"type"
)
VALUES (
'run_sql',
1,
'file',
'Path to the SQL file to execute, can be absolute, or relative to the web root (the root folder of your website sql files).
In-database files, from the sqlpage_files(path, contents, last_modified) table are supported.',
'TEXT'
),(
'run_sql',
2,
'parameters',
'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.',
'JSON'
);
8 changes: 8 additions & 0 deletions examples/official-site/content/functions/version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
icon: git-commit
since: 0.11.0
category: server-and-runtime
summary: Read the version of SQLPage serving the page.
---

Returns the current version of SQLPage as a string.
30 changes: 30 additions & 0 deletions examples/official-site/content/functions/web_root.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
icon: folder-code
since: 0.42.0
category: server-and-runtime
summary: Locate the directory that holds the site's public files.
---

Returns the web root directory where SQLPage serves `.sql` files from.

### Example

```sql
SELECT 'text' AS component;
SELECT 'SQL files are served from ' AS contents;
SELECT sqlpage.web_root() as contents, true as code;
```

#### Result

SQL files are served from `/home/user/my_sqlpage_website`

#### Notes

The web root is the directory from which `.sql` files are loaded and served.
By default, it is the current working directory, but it can be changed using:
- the `--web-root` command line argument
- the `web_root` [configuration option](https://github.com/sqlpage/SQLPage/blob/main/configuration.md) in `sqlpage.json`
- the `WEB_ROOT` environment variable

This is more reliable than `sqlpage.current_working_directory()` when you need to reference the location of your SQL files.
Loading
Loading