From 69c3e2b04adc91f7e16be9becf1367c5a820728c Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:32 +0000 Subject: [PATCH] Move the remaining runtime function references to Markdown --- examples/official-site/README.md | 1 - .../functions/configuration_directory.md | 33 +++ .../functions/current_working_directory.md | 26 +++ .../content/functions/environment_variable.md | 21 ++ .../official-site/content/functions/exec.md | 42 ++++ .../functions/run_sql.md} | 60 ++--- .../content/functions/version.md | 8 + .../content/functions/web_root.md | 30 +++ .../sqlpage/migrations/08_functions.sql | 210 ------------------ .../migrations/82_reference_catalog.sql | 22 -- .../database/sqlpage_functions/README.md | 2 + 11 files changed, 185 insertions(+), 270 deletions(-) create mode 100644 examples/official-site/content/functions/configuration_directory.md create mode 100644 examples/official-site/content/functions/current_working_directory.md create mode 100644 examples/official-site/content/functions/environment_variable.md create mode 100644 examples/official-site/content/functions/exec.md rename examples/official-site/{sqlpage/migrations/38_run_sql.sql => content/functions/run_sql.md} (67%) create mode 100644 examples/official-site/content/functions/version.md create mode 100644 examples/official-site/content/functions/web_root.md diff --git a/examples/official-site/README.md b/examples/official-site/README.md index 478528ae0..eb5ffe063 100644 --- a/examples/official-site/README.md +++ b/examples/official-site/README.md @@ -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 diff --git a/examples/official-site/content/functions/configuration_directory.md b/examples/official-site/content/functions/configuration_directory.md new file mode 100644 index 000000000..ee7c260e4 --- /dev/null +++ b/examples/official-site/content/functions/configuration_directory.md @@ -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. diff --git a/examples/official-site/content/functions/current_working_directory.md b/examples/official-site/content/functions/current_working_directory.md new file mode 100644 index 000000000..07c9ba069 --- /dev/null +++ b/examples/official-site/content/functions/current_working_directory.md @@ -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. diff --git a/examples/official-site/content/functions/environment_variable.md b/examples/official-site/content/functions/environment_variable.md new file mode 100644 index 000000000..b46b60549 --- /dev/null +++ b/examples/official-site/content/functions/environment_variable.md @@ -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; +``` diff --git a/examples/official-site/content/functions/exec.md b/examples/official-site/content/functions/exec.md new file mode 100644 index 000000000..97c847a20 --- /dev/null +++ b/examples/official-site/content/functions/exec.md @@ -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. diff --git a/examples/official-site/sqlpage/migrations/38_run_sql.sql b/examples/official-site/content/functions/run_sql.md similarity index 67% rename from examples/official-site/sqlpage/migrations/38_run_sql.sql rename to examples/official-site/content/functions/run_sql.md index 314755f2e..9a077cc29 100644 --- a/examples/official-site/sqlpage/migrations/38_run_sql.sql +++ b/examples/official-site/content/functions/run_sql.md @@ -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 @@ -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 @@ -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) @@ -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' - ); diff --git a/examples/official-site/content/functions/version.md b/examples/official-site/content/functions/version.md new file mode 100644 index 000000000..4a5327aaa --- /dev/null +++ b/examples/official-site/content/functions/version.md @@ -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. diff --git a/examples/official-site/content/functions/web_root.md b/examples/official-site/content/functions/web_root.md new file mode 100644 index 000000000..e56340181 --- /dev/null +++ b/examples/official-site/content/functions/web_root.md @@ -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. diff --git a/examples/official-site/sqlpage/migrations/08_functions.sql b/examples/official-site/sqlpage/migrations/08_functions.sql index 3a4015baf..90502d472 100644 --- a/examples/official-site/sqlpage/migrations/08_functions.sql +++ b/examples/official-site/sqlpage/migrations/08_functions.sql @@ -12,213 +12,3 @@ CREATE TABLE IF NOT EXISTS sqlpage_function_parameters ( "description_md" TEXT, "type" TEXT ); -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'current_working_directory', - '0.11.0', - 'folder-question', - '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. -' - ); -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'web_root', - '0.42.0', - 'folder-code', - '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. -' - ); -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'configuration_directory', - '0.42.0', - 'folder-cog', - '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. -' - ); -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'environment_variable', - '0.11.0', - 'variable', - '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; -```' - ); -INSERT INTO sqlpage_function_parameters ( - "function", - "index", - "name", - "description_md", - "type" - ) -VALUES ( - 'environment_variable', - 1, - 'name', - 'The name of the environment variable to read. Must be a literal string.', - 'TEXT' - ); -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'version', - '0.11.0', - 'git-commit', - 'Returns the current version of SQLPage as a string.' - ); -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'exec', - '0.12.0', - 'terminal-2', - '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. -' - ); -INSERT INTO sqlpage_function_parameters ( - "function", - "index", - "name", - "description_md", - "type" - ) -VALUES ( - 'exec', - 1, - 'program', - 'The name of the program to execute. Must be a literal string.', - 'TEXT' - ), - ( - 'exec', - 2, - 'arguments...', - 'The arguments to pass to the program.', - 'TEXT' - ); diff --git a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql index b08b16d23..462adbd05 100644 --- a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql +++ b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql @@ -25,11 +25,6 @@ ALTER TABLE sqlpage_functions ADD COLUMN catalog_category TEXT REFERENCES refere ALTER TABLE sqlpage_functions ADD COLUMN catalog_summary TEXT; ALTER TABLE sqlpage_function_parameters ADD COLUMN optional BOOLEAN NOT NULL DEFAULT FALSE; -UPDATE sqlpage_function_parameters SET optional = TRUE WHERE ("function", name) IN ( - ('exec', 'arguments...'), - ('run_sql', 'parameters') -); - WITH copy(name, category, summary) AS (VALUES ('big_number', 'display-data', 'Highlight a metric with a trend or progress indicator.'), ('card', 'display-data', 'Arrange records in cards with images, links, or embedded content.'), @@ -84,23 +79,6 @@ WITH copy(name, category, summary) AS (VALUES UPDATE component SET (catalog_category, catalog_summary) = (copy.category, copy.summary) FROM copy WHERE component.name = copy.name; -WITH copy(name, category, summary) AS (VALUES - - - - - - ('configuration_directory', 'server-and-runtime', 'Locate the directory for SQLPage configuration and templates.'), - ('current_working_directory', 'server-and-runtime', 'Get the working directory of the SQLPage server process.'), - ('environment_variable', 'server-and-runtime', 'Read a named environment variable from the server.'), - ('exec', 'server-and-runtime', 'Run a program on the server and capture its output.'), - ('run_sql', 'server-and-runtime', 'Run another SQLPage file and return its rows as JSON.'), - ('version', 'server-and-runtime', 'Read the version of SQLPage serving the page.'), - ('web_root', 'server-and-runtime', 'Locate the directory that holds the site''s public files.') -) -UPDATE sqlpage_functions SET (catalog_category, catalog_summary) = (copy.category, copy.summary) -FROM copy WHERE sqlpage_functions.name = copy.name; - -- The catalog, topic navigation, and reference pages use the same entries and links. CREATE VIEW reference_entry AS WITH entries AS ( diff --git a/src/webserver/database/sqlpage_functions/README.md b/src/webserver/database/sqlpage_functions/README.md index 00fde024c..7a5dc676e 100644 --- a/src/webserver/database/sqlpage_functions/README.md +++ b/src/webserver/database/sqlpage_functions/README.md @@ -27,6 +27,8 @@ sqlpage_functions! { } ``` +Add the matching Markdown page to the [function reference](../../../../examples/official-site/content/functions/). The registry test checks that each public function has a page. + The [`sqlpage_functions!`](function_traits.rs) macro generates the `SqlPageFunctionName` enum the SQL engine dispatches on. Per-function argument extraction, dispatch, and return-value conversion are handled generically in [`function_traits.rs`](function_traits.rs) by