From a395e1eba30cf242d0f68605a5021581fb4bb607 Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:34 +0000 Subject: [PATCH] Move HTTP and server-control references to Markdown --- .../content/components/cookie.md | 54 ++++++++ .../cookie/examples/01-set-a-cookie.md | 15 +++ .../official-site/content/components/debug.md | 9 ++ .../debug/examples/01-component-data.md | 11 ++ .../debug/examples/02-function-result.md | 5 + .../content/components/dynamic.md | 13 ++ .../dynamic/examples/01-component-rows.md | 8 ++ .../dynamic/examples/02-json-file.md | 32 +++++ .../dynamic/examples/03-include-sql.md | 22 +++ .../dynamic/examples/04-shared-shell.md | 29 ++++ .../components/dynamic/examples/05-tables.md | 18 +++ .../content/components/http_header.md | 44 ++++++ .../http_header/examples/01-cache-control.md | 14 ++ .../http_header/examples/02-redirect.md | 8 ++ .../http_header/examples/03-custom-header.md | 7 + .../official-site/content/components/log.md | 26 ++++ .../components/log/examples/01-message.md | 39 ++++++ .../components/redirect.md} | 58 +++----- .../redirect/examples/01-require-login.md | 6 + .../content/components/status_code.md | 36 +++++ .../status_code/examples/01-not-found.md | 6 + .../sqlpage/migrations/01_documentation.sql | 126 ------------------ .../sqlpage/migrations/04_http_header.sql | 116 ---------------- .../sqlpage/migrations/05_cookie.sql | 125 ----------------- .../sqlpage/migrations/06_debug.sql | 25 ---- .../sqlpage/migrations/48_status_code.sql | 65 --------- .../sqlpage/migrations/66_log_component.sql | 61 --------- .../migrations/82_reference_catalog.sql | 9 +- 28 files changed, 419 insertions(+), 568 deletions(-) create mode 100644 examples/official-site/content/components/cookie.md create mode 100644 examples/official-site/content/components/cookie/examples/01-set-a-cookie.md create mode 100644 examples/official-site/content/components/debug.md create mode 100644 examples/official-site/content/components/debug/examples/01-component-data.md create mode 100644 examples/official-site/content/components/debug/examples/02-function-result.md create mode 100644 examples/official-site/content/components/dynamic.md create mode 100644 examples/official-site/content/components/dynamic/examples/01-component-rows.md create mode 100644 examples/official-site/content/components/dynamic/examples/02-json-file.md create mode 100644 examples/official-site/content/components/dynamic/examples/03-include-sql.md create mode 100644 examples/official-site/content/components/dynamic/examples/04-shared-shell.md create mode 100644 examples/official-site/content/components/dynamic/examples/05-tables.md create mode 100644 examples/official-site/content/components/http_header.md create mode 100644 examples/official-site/content/components/http_header/examples/01-cache-control.md create mode 100644 examples/official-site/content/components/http_header/examples/02-redirect.md create mode 100644 examples/official-site/content/components/http_header/examples/03-custom-header.md create mode 100644 examples/official-site/content/components/log.md create mode 100644 examples/official-site/content/components/log/examples/01-message.md rename examples/official-site/{sqlpage/migrations/09_redirect.sql => content/components/redirect.md} (55%) create mode 100644 examples/official-site/content/components/redirect/examples/01-require-login.md create mode 100644 examples/official-site/content/components/status_code.md create mode 100644 examples/official-site/content/components/status_code/examples/01-not-found.md delete mode 100644 examples/official-site/sqlpage/migrations/04_http_header.sql delete mode 100644 examples/official-site/sqlpage/migrations/05_cookie.sql delete mode 100644 examples/official-site/sqlpage/migrations/06_debug.sql delete mode 100644 examples/official-site/sqlpage/migrations/48_status_code.sql delete mode 100644 examples/official-site/sqlpage/migrations/66_log_component.sql diff --git a/examples/official-site/content/components/cookie.md b/examples/official-site/content/components/cookie.md new file mode 100644 index 000000000..4b5076d36 --- /dev/null +++ b/examples/official-site/content/components/cookie.md @@ -0,0 +1,54 @@ +--- +icon: cookie +category: http-responses +summary: Set a cookie in the visitor's browser. +top: + - name: domain + type: TEXT + description: The domain for which the cookie will be sent. If not specified, the cookie will be sent for + all domains. + - name: expires + type: TIMESTAMP + description: The date at which the cookie expires (either a timestamp or a date object). If not specified, + the cookie will expire when the browser is closed. + - name: http_only + type: BOOLEAN + description: Whether the cookie should only be accessible via HTTP and not via client-side scripts. If + explicitly set to false, the cookie will be accessible via both HTTP and client-side scripts. + - name: max_age + type: INTEGER + description: The maximum age of the cookie in seconds. number of seconds until the cookie expires. If both + Expires and Max-Age are set, Max-Age has precedence. + - name: name + type: TEXT + required: true + description: The name of the cookie to set. + - name: path + type: TEXT + description: The path for which the cookie will be sent. If not specified, the cookie will be sent for + all paths. + - name: remove + type: BOOLEAN + description: Set to TRUE to remove the cookie from the client browser. When specified, other parameters + are ignored. + - name: same_site + type: TEXT + description: Whether the cookie should only be sent for requests originating from the same site. See owasp.org/www-community/SameSite. + `strict` is the recommended and default value, but you may want to set it to `lax` if you want your users + to keep their session when they click on a link to your site from an external site. + - name: secure + type: BOOLEAN + description: Whether the cookie should only be sent over a secure (HTTPS) connection. Defaults to TRUE. + - name: value + type: TEXT + description: The value of the cookie to set. +--- + +Sets a cookie in the client browser, used for session management and storing user-related information. + +This component creates a single cookie. Since cookies need to be set before the response body is sent to the client, +this component should be **placed at the top of the page**, before any other components that generate output. + +After being set, a cookie can be accessed anywhere in your SQL code using the `sqlpage.cookie('cookie_name')` pseudo-function. + +Note that if your site is accessed over HTTP (and not HTTPS), you have to set `false as secure` to force browsers to accept your cookies. diff --git a/examples/official-site/content/components/cookie/examples/01-set-a-cookie.md b/examples/official-site/content/components/cookie/examples/01-set-a-cookie.md new file mode 100644 index 000000000..6f7947543 --- /dev/null +++ b/examples/official-site/content/components/cookie/examples/01-set-a-cookie.md @@ -0,0 +1,15 @@ +Set the cookie before producing any page content: + +```sql +SELECT 'cookie' as component, + 'username' as name, + 'John Doe' as value, + FALSE AS secure; -- You can remove this if the site is served over HTTPS. +``` + +On a later request, read the value the browser sent with [`sqlpage.cookie`](functions.sql?function=cookie). Setting a response cookie does not change the current request. + +```sql +SELECT 'text' as component, + 'Your name is ' || COALESCE(sqlpage.cookie('username'), 'not known to us') as contents; +``` diff --git a/examples/official-site/content/components/debug.md b/examples/official-site/content/components/debug.md new file mode 100644 index 000000000..1d4d1732f --- /dev/null +++ b/examples/official-site/content/components/debug.md @@ -0,0 +1,9 @@ +--- +icon: bug +category: server-logic +summary: Inspect the values passed to a component as JSON. +--- + +Visualize any set of values as JSON. +Can be used to display all the parameters passed to the component. +Useful for debugging: just replace the name of the component you want to debug with 'debug', and see all the top-level and row-level parameters that are passed to it, and their types. diff --git a/examples/official-site/content/components/debug/examples/01-component-data.md b/examples/official-site/content/components/debug/examples/01-component-data.md new file mode 100644 index 000000000..98ece7b12 --- /dev/null +++ b/examples/official-site/content/components/debug/examples/01-component-data.md @@ -0,0 +1,11 @@ +--- +rows: + - component: debug + my_top_level_property: true + - x: y + z: 42 + - a: b + c: null +--- + +At any time, if you are confused about what data you are passing to a component, just replace the component name with 'debug' to see all the parameters that are passed to it. diff --git a/examples/official-site/content/components/debug/examples/02-function-result.md b/examples/official-site/content/components/debug/examples/02-function-result.md new file mode 100644 index 000000000..c8f6a57bc --- /dev/null +++ b/examples/official-site/content/components/debug/examples/02-function-result.md @@ -0,0 +1,5 @@ +Show the result of a SQLPage function: + +```sql +select 'debug' as component, sqlpage.environment_variable('HOME'); +``` diff --git a/examples/official-site/content/components/dynamic.md b/examples/official-site/content/components/dynamic.md new file mode 100644 index 000000000..69246e50d --- /dev/null +++ b/examples/official-site/content/components/dynamic.md @@ -0,0 +1,13 @@ +--- +icon: repeat +category: server-logic +summary: Render SQLPage components described by JSON data. +top: + - name: properties + type: JSON + description: A json object or array that contains the names and properties of other components. +--- + +Renders other components, given their properties as JSON. +If you are looking for a way to run FOR loops, to share similar code between pages of your site, +or to render multiple components for every line returned by your SQL query, then this is the component to use diff --git a/examples/official-site/content/components/dynamic/examples/01-component-rows.md b/examples/official-site/content/components/dynamic/examples/01-component-rows.md new file mode 100644 index 000000000..422acb34a --- /dev/null +++ b/examples/official-site/content/components/dynamic/examples/01-component-rows.md @@ -0,0 +1,8 @@ +--- +rows: + - component: dynamic + properties: '[{"component":"text"}, {"contents":"Hello, I am a dynamic component !", "italics":true}]' +--- + +The dynamic component has a single top-level property named `properties`, but it can render any number of other components. +Let's start with something simple to illustrate the logic. We'll render a `text` component with two row-level properties: `contents` and `italics`. diff --git a/examples/official-site/content/components/dynamic/examples/02-json-file.md b/examples/official-site/content/components/dynamic/examples/02-json-file.md new file mode 100644 index 000000000..93ed89c7e --- /dev/null +++ b/examples/official-site/content/components/dynamic/examples/02-json-file.md @@ -0,0 +1,32 @@ +## Static component data stored in `.json` files + +You can also store the data for a component in a `.json` file, and load it using the `dynamic` component. + +This is particularly useful to create a single [shell](?component=shell#component) defining the site's overall appearance and menus, +and displaying it on all pages without duplicating its code. + +The following will load the data for a `shell` component from a file named `shell.json`, +using the [`sqlpage.read_file_as_text`](/functions.sql?function=read_file_as_text) function. + +```sql +SELECT 'dynamic' AS component, sqlpage.read_file_as_text('shell.json') AS properties; +``` + +and `shell.json` would be placed at the website's root and contain the following: + +```json +{ + "component": "shell", + "title": "SQLPage documentation", + "link": "/", + "menu_item": [ + {"link": "index.sql", "title": "Home"}, + {"title": "Community", "submenu": [ + {"link": "blog.sql", "title": "Blog"}, + {"link": "https//github.com/sqlpage/SQLPage/issues", "title": "Issues"}, + {"link": "https//github.com/sqlpage/SQLPage/discussions", "title": "Discussions"}, + {"link": "https//github.com/sqlpage/SQLPage", "title": "Github"} + ]} + ] +} +``` diff --git a/examples/official-site/content/components/dynamic/examples/03-include-sql.md b/examples/official-site/content/components/dynamic/examples/03-include-sql.md new file mode 100644 index 000000000..52ed2af27 --- /dev/null +++ b/examples/official-site/content/components/dynamic/examples/03-include-sql.md @@ -0,0 +1,22 @@ +## Including another SQL file + +To avoid repeating the same code on multiple pages, you can include another SQL file using the `dynamic` component +together with the [`sqlpage.run_sql`](/functions.sql?function=run_sql) function. + +For instance, the following will include the file `shell.sql` at the top of the page, +and pass it a `$title` variable to display the page title. + +```sql +SELECT 'dynamic' AS component, + sqlpage.run_sql('shell.sql', json_object('title', 'SQLPage documentation')) AS properties; +``` + +And `shell.sql` could contain the following: + +```sql +SELECT 'shell' AS component, + COALESCE($title, 'Default title') AS title, + '/my_icon.png' AS icon, + 'products' AS menu_item, + 'about' AS menu_item; +``` diff --git a/examples/official-site/content/components/dynamic/examples/04-shared-shell.md b/examples/official-site/content/components/dynamic/examples/04-shared-shell.md new file mode 100644 index 000000000..702a152a4 --- /dev/null +++ b/examples/official-site/content/components/dynamic/examples/04-shared-shell.md @@ -0,0 +1,29 @@ +## Dynamic shell + +On databases without a native JSON type (such as the default SQLite database), +you can use the `dynamic` component to generate +json data to pass to components that expect it. + +This example generates a menu similar to the [shell example](?component=shell#component), but without using a native JSON type. + +```sql +SELECT 'dynamic' AS component, ' +{ + "component": "shell", + "title": "SQLPage documentation", + "link": "/", + "menu_item": [ + {"link": "index.sql", "title": "Home"}, + {"title": "Community", "submenu": [ + {"link": "blog.sql", "title": "Blog"}, + {"link": "https//github.com/sqlpage/SQLPage/issues", "title": "Issues"}, + {"link": "https//github.com/sqlpage/SQLPage/discussions", "title": "Discussions"}, + {"link": "https//github.com/sqlpage/SQLPage", "title": "Github"} + ]} + ] +} +' AS properties +``` + +[View the result of this query, as well as an example of how to generate a dynamic menu +based on the database contents](./examples/dynamic_shell.sql). diff --git a/examples/official-site/content/components/dynamic/examples/05-tables.md b/examples/official-site/content/components/dynamic/examples/05-tables.md new file mode 100644 index 000000000..5bdc8c87e --- /dev/null +++ b/examples/official-site/content/components/dynamic/examples/05-tables.md @@ -0,0 +1,18 @@ +## Dynamic tables + +The `dynamic` component can be used to generate [tables](?component=table#component) with dynamic columns, +using [your database's JSON functions](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide). + +For instance, let's say we have a table with three columns: user_id, name, and role. +We want to create a table where each row is a user, and each column is a role. +We will return a set of json objects that look like this: `{"name": "Alice", "admin": true, "editor": false, "viewer": true}` +```sql +SELECT 'table' AS component; +SELECT 'dynamic' AS component, + json_patch( + json_object('name', name), + json_object_agg(role, is_admin) + ) AS properties +FROM users +GROUP BY name; +``` diff --git a/examples/official-site/content/components/http_header.md b/examples/official-site/content/components/http_header.md new file mode 100644 index 000000000..0c56e47d8 --- /dev/null +++ b/examples/official-site/content/components/http_header.md @@ -0,0 +1,44 @@ +--- +icon: world-www +category: http-responses +summary: Set response headers for caching, content types, and more. +top: + - name: Access-Control-Allow-Origin + type: TEXT + description: Specifies which origins are allowed to access the resource in a cross-origin request, used + for implementing Cross-Origin Resource Sharing (CORS). + - name: Cache-Control + type: TEXT + description: Directives for how long the page should be cached by the browser. Set this to max-age=N to + keep the page in cache for N seconds. + - name: Content-Disposition + type: TEXT + description: Provides instructions on how the response content should be displayed or handled by the client, + such as inline or as an attachment. + - name: Location + type: TEXT + description: Specifies the URL to redirect the client to, usually used in 3xx redirection responses. + - name: Set-Cookie + type: TEXT + description: Sets a cookie in the client browser, used for session management and storing user-related + information. +--- + +An advanced component to set arbitrary HTTP headers: can be used to set a custom caching policy to your pages, or implement custom redirections, for example. +If you are a beginner, you probably don't need this component. + +When used, this component has to be the first component in the page, because once the page is sent to the browser, it is too late to change the headers. + +HTTP headers are additional pieces of information sent with responses to web requests that provide instructions +or metadata about the data being sent — for example, +setting cache control directives to control caching behavior +or specifying the content type of a response. + +Any valid HTTP header name can be used as a top-level parameter for this component. +The examples shown here are just that, examples; and you can create any custom header +if needed simply by declaring it. + +If your header's name contains a dash or any other special character, +you will have to use your database's quoting mechanism to declare it. +In standard SQL, you can use double quotes to quote identifiers (like "X-My-Header"), +in Microsoft SQL Server, you can use square brackets (like [X-My-Header]). diff --git a/examples/official-site/content/components/http_header/examples/01-cache-control.md b/examples/official-site/content/components/http_header/examples/01-cache-control.md new file mode 100644 index 000000000..4c10e8f04 --- /dev/null +++ b/examples/official-site/content/components/http_header/examples/01-cache-control.md @@ -0,0 +1,14 @@ +--- +rows: + - component: http_header + Cache-Control: public, max-age=600, stale-while-revalidate=3600, stale-if-error=86400 +--- + +Set cache control directives for caching behavior. In this example, the response can be cached by the browser + and served from the cache for up to 600 seconds (10 minutes) after it is first requested. + During that time, even if the cached response becomes stale (outdated), the browser can still use it (stale-while-revalidate) + for up to 3600 seconds (1 hour) while it retrieves a fresh copy from the server in the background. + If there is an error while trying to retrieve a fresh copy from the server, + the browser can continue to serve the stale response for up to 86400 seconds (24 hours) (stale-if-error) instead of showing an error page. + This caching behavior helps improve the performance and responsiveness of the website by reducing the number of requests made to the server + and allowing the browser to serve content from its cache when appropriate. diff --git a/examples/official-site/content/components/http_header/examples/02-redirect.md b/examples/official-site/content/components/http_header/examples/02-redirect.md new file mode 100644 index 000000000..cb0d1db78 --- /dev/null +++ b/examples/official-site/content/components/http_header/examples/02-redirect.md @@ -0,0 +1,8 @@ +--- +rows: + - component: http_header + Location: /another-page.sql +--- + +Redirect the user to another page. In this example, the user is redirected to a file named another-page.sql at the root of the website. The current page will not be displayed at all. + This is useful in particular for content creation pages that contain only INSERT statements, because you can redirect the user to the page that lists the content after it has been created. diff --git a/examples/official-site/content/components/http_header/examples/03-custom-header.md b/examples/official-site/content/components/http_header/examples/03-custom-header.md new file mode 100644 index 000000000..2b746e83f --- /dev/null +++ b/examples/official-site/content/components/http_header/examples/03-custom-header.md @@ -0,0 +1,7 @@ +--- +rows: + - component: http_header + X-My-Header: my value +--- + +Set a custom non-standard header for the response. In this example, the response will include a custom header named X-My-Header with the value "my value". diff --git a/examples/official-site/content/components/log.md b/examples/official-site/content/components/log.md new file mode 100644 index 000000000..4a8a8bac9 --- /dev/null +++ b/examples/official-site/content/components/log.md @@ -0,0 +1,26 @@ +--- +icon: logs +since: 0.37.1 +category: server-logic +summary: Write a message to the SQLPage server log. +top: + - name: level + type: TEXT + description: How important the message is. One of 'trace', 'debug', 'info' (default), 'warn', 'error'. + Not case-sensitive. Controls the level shown in the logs. + - name: message + type: TEXT + required: true + description: The text to write to the server logs. It is printed when the page runs. +--- + +A component that writes messages to the server logs. +When a page runs, it prints your message to the terminal/console (standard error). +Use it to track what happens and troubleshoot issues. + +## Reading log messages + +- Running from a terminal (Linux, macOS, or Windows PowerShell/Command Prompt): they show up in the window. +- Docker: run `docker logs `. +- Linux service (systemd): run `journalctl -u sqlpage`. +- This component's output is written to [standard error (stderr)](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)). SQLPage request access logs are separate and are written to standard output (stdout). diff --git a/examples/official-site/content/components/log/examples/01-message.md b/examples/official-site/content/components/log/examples/01-message.md new file mode 100644 index 000000000..d09da451c --- /dev/null +++ b/examples/official-site/content/components/log/examples/01-message.md @@ -0,0 +1,39 @@ +### Record a simple message + +This writes "Hello, World!" to the server logs. + +```sql +SELECT 'log' as component, 'Hello, World!' as message; +``` + +Example output: + +```text +[2025-09-13T22:30:14.722Z INFO sqlpage::log from "x.sql" statement 1] Hello, World! +``` + +### Set the importance (level) + +Choose how important the message is. + +```sql +SELECT 'log' as component, 'error' as level, 'This is an error message' as message; +``` + +Example output: + +```text +[2025-09-13T22:30:14.722Z ERROR sqlpage::log from "x.sql" statement 2] This is an error message +``` + +### Log dynamic information + +Include variables like a username. + +```sql +set username = 'user' + +select 'log' as component, + '403 - failed for ' || coalesce($username, 'None') as message, + 'error' as level; +``` diff --git a/examples/official-site/sqlpage/migrations/09_redirect.sql b/examples/official-site/content/components/redirect.md similarity index 55% rename from examples/official-site/sqlpage/migrations/09_redirect.sql rename to examples/official-site/content/components/redirect.md index eb6942125..4ab88c356 100644 --- a/examples/official-site/sqlpage/migrations/09_redirect.sql +++ b/examples/official-site/content/components/redirect.md @@ -1,7 +1,16 @@ -INSERT INTO component (name, description, icon, introduced_in_version) -VALUES ( - 'redirect', - 'Redirects the user to another page. +--- +icon: arrow-right +since: 0.7.2 +category: http-responses +summary: Send the visitor to another URL. +top: + - name: link + type: TEXT + required: true + description: The URL to redirect the user to. +--- + +Redirects the user to another page. This component helps you: 1. Send users to a different page @@ -16,8 +25,8 @@ A common use case is error handling. You may want to proceed with the rest of a ```sql SELECT - ''redirect'' AS component, - ''error_page.sql'' AS link + 'redirect' AS component, + 'error_page.sql' AS link WHERE NOT your_condition; -- The rest of the page is only executed if the condition is true @@ -30,40 +39,5 @@ it will fail if the HTTP headers have already been sent by the time it is execut > **Important difference from [http_header](?component=http_header)** > -> This component completely stops the page from running after it''s called. +> This component completely stops the page from running after it's called. > This makes it a good choice for protecting sensitive information from unauthorized users. - -', - 'arrow-right', - '0.7.2' - ); --- Insert the parameters for the http_header component into the parameter table -INSERT INTO parameter ( - component, - name, - description, - type, - top_level, - optional - ) -VALUES ( - 'redirect', - 'link', - 'The URL to redirect the user to.', - 'TEXT', - TRUE, - FALSE - ); --- Insert an example usage of the http_header component into the example table -INSERT INTO example (component, description) -VALUES ( - 'redirect', - ' -Redirect a user to the login page if they are not logged in: - -```sql -SELECT ''redirect'' AS component, ''login.sql'' AS link -WHERE NOT EXISTS (SELECT 1 FROM login_session WHERE id = sqlpage.cookie(''session_id'')); -``` -' - ); \ No newline at end of file diff --git a/examples/official-site/content/components/redirect/examples/01-require-login.md b/examples/official-site/content/components/redirect/examples/01-require-login.md new file mode 100644 index 000000000..70a84f3ea --- /dev/null +++ b/examples/official-site/content/components/redirect/examples/01-require-login.md @@ -0,0 +1,6 @@ +Redirect a user to the login page if they are not logged in: + +```sql +SELECT 'redirect' AS component, 'login.sql' AS link +WHERE NOT EXISTS (SELECT 1 FROM login_session WHERE id = sqlpage.cookie('session_id')); +``` diff --git a/examples/official-site/content/components/status_code.md b/examples/official-site/content/components/status_code.md new file mode 100644 index 000000000..4baaea56b --- /dev/null +++ b/examples/official-site/content/components/status_code.md @@ -0,0 +1,36 @@ +--- +icon: error-404 +category: http-responses +summary: Set the HTTP status code returned by a page. +top: + - name: status + type: INTEGER + required: true + description: HTTP status code (e.g., 200 OK, 401 Unauthorized, 409 Conflict) +--- + +Sets the HTTP response code for the current page. + +This is an advanced technical component. +You typically need it when building internet-facing APIs and websites, +but you may not need it for simple internal applications. + +- Indicating operation results when using [SQLPage as an API](?component=json) + - `200`: *OK*, for successful operations + - `201`: *Created*, for successful record insertion + - `404`: *Not Found*, for missing resources + - `500`: *Internal Server Error*, for failed operations +- Handling data validation errors + - `400`: *Bad Request*, for invalid data +- Enforcing access controls + - `403`: *Forbidden*, for unauthorized access + - `401`: *Unauthorized*, for unauthenticated access +- Tracking system health + - `500`: *Internal Server Error*, for failed operations + +For search engine optimization: +- Use `404` for deleted content to remove outdated URLs from search engines +- For redirection from one page to another, use + - `301` (moved permanently), or + - `302` (moved temporarily) +- Use `503` during maintenance diff --git a/examples/official-site/content/components/status_code/examples/01-not-found.md b/examples/official-site/content/components/status_code/examples/01-not-found.md new file mode 100644 index 000000000..81fae347e --- /dev/null +++ b/examples/official-site/content/components/status_code/examples/01-not-found.md @@ -0,0 +1,6 @@ +Set the HTTP status code to 404, indicating that the requested resource was not found. +Useful in combination with [`404.sql` files](/your-first-sql-website/custom_urls.sql): + +```sql +SELECT 'status_code' as component, 404 as status; +``` diff --git a/examples/official-site/sqlpage/migrations/01_documentation.sql b/examples/official-site/sqlpage/migrations/01_documentation.sql index 4379db196..f0d138803 100644 --- a/examples/official-site/sqlpage/migrations/01_documentation.sql +++ b/examples/official-site/sqlpage/migrations/01_documentation.sql @@ -109,129 +109,3 @@ For smaller files, this is easier and faster to use than creating a separate SQL json('[{"component":"csv", "title": "Download my data", "filename": "people", "icon": "file-download", "color": "green", "separator": ";", "bom": true}, '|| '{"Forename": "Ophir", "Surname": "Lojkine", "Pseudonym": "lovasoa"},' || '{"Forename": "Linus", "Surname": "Torvalds", "Pseudonym": "torvalds"}]')); - - -INSERT INTO component(name, icon, description) VALUES - ('dynamic', 'repeat', 'Renders other components, given their properties as JSON. -If you are looking for a way to run FOR loops, to share similar code between pages of your site, -or to render multiple components for every line returned by your SQL query, then this is the component to use'); - -INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'dynamic', * FROM (VALUES - -- top level - ('properties', 'A json object or array that contains the names and properties of other components.', 'JSON', TRUE, TRUE) -) x; - -INSERT INTO example(component, description, properties) VALUES - ('dynamic', 'The dynamic component has a single top-level property named `properties`, but it can render any number of other components. -Let''s start with something simple to illustrate the logic. We''ll render a `text` component with two row-level properties: `contents` and `italics`. -', json('[{"component":"dynamic", "properties": "[{\"component\":\"text\"}, {\"contents\":\"Hello, I am a dynamic component !\", \"italics\":true}]"}]')), - ('dynamic', ' -## Static component data stored in `.json` files - -You can also store the data for a component in a `.json` file, and load it using the `dynamic` component. - -This is particularly useful to create a single [shell](?component=shell#component) defining the site''s overall appearance and menus, -and displaying it on all pages without duplicating its code. - -The following will load the data for a `shell` component from a file named `shell.json`, -using the [`sqlpage.read_file_as_text`](/functions.sql?function=read_file_as_text) function. - -```sql -SELECT ''dynamic'' AS component, sqlpage.read_file_as_text(''shell.json'') AS properties; -``` - -and `shell.json` would be placed at the website''s root and contain the following: - -```json -{ - "component": "shell", - "title": "SQLPage documentation", - "link": "/", - "menu_item": [ - {"link": "index.sql", "title": "Home"}, - {"title": "Community", "submenu": [ - {"link": "blog.sql", "title": "Blog"}, - {"link": "https//github.com/sqlpage/SQLPage/issues", "title": "Issues"}, - {"link": "https//github.com/sqlpage/SQLPage/discussions", "title": "Discussions"}, - {"link": "https//github.com/sqlpage/SQLPage", "title": "Github"} - ]} - ] -} -``` -', NULL), -('dynamic', ' -## Including another SQL file - -To avoid repeating the same code on multiple pages, you can include another SQL file using the `dynamic` component -together with the [`sqlpage.run_sql`](/functions.sql?function=run_sql) function. - -For instance, the following will include the file `shell.sql` at the top of the page, -and pass it a `$title` variable to display the page title. - -```sql -SELECT ''dynamic'' AS component, - sqlpage.run_sql(''shell.sql'', json_object(''title'', ''SQLPage documentation'')) AS properties; -``` - -And `shell.sql` could contain the following: - -```sql -SELECT ''shell'' AS component, - COALESCE($title, ''Default title'') AS title, - ''/my_icon.png'' AS icon, - ''products'' AS menu_item, - ''about'' AS menu_item; -``` - -', NULL), - ('dynamic', ' -## Dynamic shell - -On databases without a native JSON type (such as the default SQLite database), -you can use the `dynamic` component to generate -json data to pass to components that expect it. - -This example generates a menu similar to the [shell example](?component=shell#component), but without using a native JSON type. - -```sql -SELECT ''dynamic'' AS component, '' -{ - "component": "shell", - "title": "SQLPage documentation", - "link": "/", - "menu_item": [ - {"link": "index.sql", "title": "Home"}, - {"title": "Community", "submenu": [ - {"link": "blog.sql", "title": "Blog"}, - {"link": "https//github.com/sqlpage/SQLPage/issues", "title": "Issues"}, - {"link": "https//github.com/sqlpage/SQLPage/discussions", "title": "Discussions"}, - {"link": "https//github.com/sqlpage/SQLPage", "title": "Github"} - ]} - ] -} -'' AS properties -``` - -[View the result of this query, as well as an example of how to generate a dynamic menu -based on the database contents](./examples/dynamic_shell.sql). -', NULL), - ('dynamic', ' -## Dynamic tables - -The `dynamic` component can be used to generate [tables](?component=table#component) with dynamic columns, -using [your database''s JSON functions](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide). - -For instance, let''s say we have a table with three columns: user_id, name, and role. -We want to create a table where each row is a user, and each column is a role. -We will return a set of json objects that look like this: `{"name": "Alice", "admin": true, "editor": false, "viewer": true}` -```sql -SELECT ''table'' AS component; -SELECT ''dynamic'' AS component, - json_patch( - json_object(''name'', name), - json_object_agg(role, is_admin) - ) AS properties -FROM users -GROUP BY name; -``` -', NULL); diff --git a/examples/official-site/sqlpage/migrations/04_http_header.sql b/examples/official-site/sqlpage/migrations/04_http_header.sql deleted file mode 100644 index 1667e4260..000000000 --- a/examples/official-site/sqlpage/migrations/04_http_header.sql +++ /dev/null @@ -1,116 +0,0 @@ --- Insert the http_header component into the component table -INSERT INTO component (name, description, icon) -VALUES ( - 'http_header', - ' -An advanced component to set arbitrary HTTP headers: can be used to set a custom caching policy to your pages, or implement custom redirections, for example. -If you are a beginner, you probably don''t need this component. - -When used, this component has to be the first component in the page, because once the page is sent to the browser, it is too late to change the headers. - -HTTP headers are additional pieces of information sent with responses to web requests that provide instructions -or metadata about the data being sent — for example, -setting cache control directives to control caching behavior -or specifying the content type of a response. - -Any valid HTTP header name can be used as a top-level parameter for this component. -The examples shown here are just that, examples; and you can create any custom header -if needed simply by declaring it. - -If your header''s name contains a dash or any other special character, -you will have to use your database''s quoting mechanism to declare it. -In standard SQL, you can use double quotes to quote identifiers (like "X-My-Header"), -in Microsoft SQL Server, you can use square brackets (like [X-My-Header]). - ', - 'world-www' - ); --- Insert the parameters for the http_header component into the parameter table -INSERT INTO parameter ( - component, - name, - description, - type, - top_level, - optional - ) -VALUES ( - 'http_header', - 'Cache-Control', - 'Directives for how long the page should be cached by the browser. Set this to max-age=N to keep the page in cache for N seconds.', - 'TEXT', - TRUE, - TRUE - ), - ( - 'http_header', - 'Content-Disposition', - 'Provides instructions on how the response content should be displayed or handled by the client, such as inline or as an attachment.', - 'TEXT', - TRUE, - TRUE - ), - ( - 'http_header', - 'Location', - 'Specifies the URL to redirect the client to, usually used in 3xx redirection responses.', - 'TEXT', - TRUE, - TRUE - ), - ( - 'http_header', - 'Set-Cookie', - 'Sets a cookie in the client browser, used for session management and storing user-related information.', - 'TEXT', - TRUE, - TRUE - ), - ( - 'http_header', - 'Access-Control-Allow-Origin', - 'Specifies which origins are allowed to access the resource in a cross-origin request, used for implementing Cross-Origin Resource Sharing (CORS).', - 'TEXT', - TRUE, - TRUE - ); - --- Insert an example usage of the http_header component into the example table -INSERT INTO example (component, description, properties) -VALUES ( - 'http_header', - 'Set cache control directives for caching behavior. In this example, the response can be cached by the browser - and served from the cache for up to 600 seconds (10 minutes) after it is first requested. - During that time, even if the cached response becomes stale (outdated), the browser can still use it (stale-while-revalidate) - for up to 3600 seconds (1 hour) while it retrieves a fresh copy from the server in the background. - If there is an error while trying to retrieve a fresh copy from the server, - the browser can continue to serve the stale response for up to 86400 seconds (24 hours) (stale-if-error) instead of showing an error page. - This caching behavior helps improve the performance and responsiveness of the website by reducing the number of requests made to the server - and allowing the browser to serve content from its cache when appropriate.', - JSON( - '[{ - "component": "http_header", - "Cache-Control": "public, max-age=600, stale-while-revalidate=3600, stale-if-error=86400" - }]' - ) - ), - ( - 'http_header', - 'Redirect the user to another page. In this example, the user is redirected to a file named another-page.sql at the root of the website. The current page will not be displayed at all. - This is useful in particular for content creation pages that contain only INSERT statements, because you can redirect the user to the page that lists the content after it has been created.', - JSON( - '[{ - "component": "http_header", - "Location": "/another-page.sql" - }]' - ) - ), - ( - 'http_header', - 'Set a custom non-standard header for the response. In this example, the response will include a custom header named X-My-Header with the value "my value".', - JSON( - '[{ - "component": "http_header", - "X-My-Header": "my value" - }]' - ) - ); \ No newline at end of file diff --git a/examples/official-site/sqlpage/migrations/05_cookie.sql b/examples/official-site/sqlpage/migrations/05_cookie.sql deleted file mode 100644 index 8e2f2db4f..000000000 --- a/examples/official-site/sqlpage/migrations/05_cookie.sql +++ /dev/null @@ -1,125 +0,0 @@ --- Insert the http_header component into the component table -INSERT INTO component (name, description, icon) -VALUES ( - 'cookie', - ' -Sets a cookie in the client browser, used for session management and storing user-related information. - -This component creates a single cookie. Since cookies need to be set before the response body is sent to the client, -this component should be **placed at the top of the page**, before any other components that generate output. - -After being set, a cookie can be accessed anywhere in your SQL code using the `sqlpage.cookie(''cookie_name'')` pseudo-function. - -Note that if your site is accessed over HTTP (and not HTTPS), you have to set `false as secure` to force browsers to accept your cookies.', - 'cookie' - ); --- Insert the parameters for the http_header component into the parameter table -INSERT INTO parameter ( - component, - name, - description, - type, - top_level, - optional - ) -VALUES ( - 'cookie', - 'name', - 'The name of the cookie to set.', - 'TEXT', - TRUE, - FALSE - ), - ( - 'cookie', - 'value', - 'The value of the cookie to set.', - 'TEXT', - TRUE, - TRUE - ), - ( - 'cookie', - 'path', - 'The path for which the cookie will be sent. If not specified, the cookie will be sent for all paths.', - 'TEXT', - TRUE, - TRUE - ), - ( - 'cookie', - 'domain', - 'The domain for which the cookie will be sent. If not specified, the cookie will be sent for all domains.', - 'TEXT', - TRUE, - TRUE - ), - ( - 'cookie', - 'secure', - 'Whether the cookie should only be sent over a secure (HTTPS) connection. Defaults to TRUE.', - 'BOOLEAN', - TRUE, - TRUE - ), - ( - 'cookie', - 'http_only', - 'Whether the cookie should only be accessible via HTTP and not via client-side scripts. If explicitly set to false, the cookie will be accessible via both HTTP and client-side scripts.', - 'BOOLEAN', - TRUE, - TRUE - ), - ( - 'cookie', - 'remove', - 'Set to TRUE to remove the cookie from the client browser. When specified, other parameters are ignored.', - 'BOOLEAN', - TRUE, - TRUE - ), - ( - 'cookie', - 'max_age', - 'The maximum age of the cookie in seconds. number of seconds until the cookie expires. If both Expires and Max-Age are set, Max-Age has precedence.', - 'INTEGER', - TRUE, - TRUE - ), - ( - 'cookie', - 'expires', - 'The date at which the cookie expires (either a timestamp or a date object). If not specified, the cookie will expire when the browser is closed.', - 'TIMESTAMP', - TRUE, - TRUE - ), - ( - 'cookie', - 'same_site', - 'Whether the cookie should only be sent for requests originating from the same site. See owasp.org/www-community/SameSite. `strict` is the recommended and default value, but you may want to set it to `lax` if you want your users to keep their session when they click on a link to your site from an external site.', - 'TEXT', - TRUE, - TRUE - ); --- Insert an example usage of the http_header component into the example table -INSERT INTO example (component, description) -VALUES ( - 'cookie', - 'Set the cookie before producing any page content: - -```sql -SELECT ''cookie'' as component, - ''username'' as name, - ''John Doe'' as value, - FALSE AS secure; -- You can remove this if the site is served over HTTPS. -``` - -On a later request, read the value the browser sent with [`sqlpage.cookie`](functions.sql?function=cookie). Setting a response cookie does not change the current request. - -```sql -SELECT ''text'' as component, - ''Your name is '' || COALESCE(sqlpage.cookie(''username''), ''not known to us'') as contents; -``` - ' - ); diff --git a/examples/official-site/sqlpage/migrations/06_debug.sql b/examples/official-site/sqlpage/migrations/06_debug.sql deleted file mode 100644 index 7df4263b5..000000000 --- a/examples/official-site/sqlpage/migrations/06_debug.sql +++ /dev/null @@ -1,25 +0,0 @@ --- Insert the http_header component into the component table -INSERT INTO component (name, description, icon) -VALUES ( - 'debug', - 'Visualize any set of values as JSON. -Can be used to display all the parameters passed to the component. -Useful for debugging: just replace the name of the component you want to debug with ''debug'', and see all the top-level and row-level parameters that are passed to it, and their types.', - 'bug' - ); --- Insert an example usage of the http_header component into the example table -INSERT INTO example (component, description, properties) -VALUES ( - 'debug', - 'At any time, if you are confused about what data you are passing to a component, just replace the component name with ''debug'' to see all the parameters that are passed to it.', - JSON('[{"component": "debug", "my_top_level_property": true}, {"x": "y", "z": 42}, {"a": "b", "c": null}]') - ), - ( - 'debug', - 'Show the result of a SQLPage function: - -```sql -select ''debug'' as component, sqlpage.environment_variable(''HOME''); -```', - NULL - ); \ No newline at end of file diff --git a/examples/official-site/sqlpage/migrations/48_status_code.sql b/examples/official-site/sqlpage/migrations/48_status_code.sql deleted file mode 100644 index f80d4266a..000000000 --- a/examples/official-site/sqlpage/migrations/48_status_code.sql +++ /dev/null @@ -1,65 +0,0 @@ --- Insert the status_code component into the component table -INSERT INTO - component (name, description, icon) -VALUES - ( - 'status_code', - 'Sets the HTTP response code for the current page. - -This is an advanced technical component. -You typically need it when building internet-facing APIs and websites, -but you may not need it for simple internal applications. - -- Indicating operation results when using [SQLPage as an API](?component=json) - - `200`: *OK*, for successful operations - - `201`: *Created*, for successful record insertion - - `404`: *Not Found*, for missing resources - - `500`: *Internal Server Error*, for failed operations -- Handling data validation errors - - `400`: *Bad Request*, for invalid data -- Enforcing access controls - - `403`: *Forbidden*, for unauthorized access - - `401`: *Unauthorized*, for unauthenticated access -- Tracking system health - - `500`: *Internal Server Error*, for failed operations - -For search engine optimization: -- Use `404` for deleted content to remove outdated URLs from search engines -- For redirection from one page to another, use - - `301` (moved permanently), or - - `302` (moved temporarily) -- Use `503` during maintenance', - 'error-404' - ); - --- Insert the parameters for the status_code component into the parameter table -INSERT INTO - parameter ( - component, - name, - description, - type, - top_level, - optional - ) -VALUES - ( - 'status_code', - 'status', - 'HTTP status code (e.g., 200 OK, 401 Unauthorized, 409 Conflict)', - 'INTEGER', - TRUE, - FALSE - ); - -INSERT INTO example (component, description) -VALUES ( - 'status_code', - ' -Set the HTTP status code to 404, indicating that the requested resource was not found. -Useful in combination with [`404.sql` files](/your-first-sql-website/custom_urls.sql): - -```sql -SELECT ''status_code'' as component, 404 as status; -``` -'); diff --git a/examples/official-site/sqlpage/migrations/66_log_component.sql b/examples/official-site/sqlpage/migrations/66_log_component.sql deleted file mode 100644 index 3a2cb083b..000000000 --- a/examples/official-site/sqlpage/migrations/66_log_component.sql +++ /dev/null @@ -1,61 +0,0 @@ -INSERT INTO component(name, icon, introduced_in_version, description) VALUES -('log', 'logs', '0.37.1', 'A component that writes messages to the server logs. -When a page runs, it prints your message to the terminal/console (standard error). -Use it to track what happens and troubleshoot issues. - -## Reading log messages - -- Running from a terminal (Linux, macOS, or Windows PowerShell/Command Prompt): they show up in the window. -- Docker: run `docker logs `. -- Linux service (systemd): run `journalctl -u sqlpage`. -- This component''s output is written to [standard error (stderr)](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)). SQLPage request access logs are separate and are written to standard output (stdout). -'); - -INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'log', * FROM (VALUES - -- top level - ('message', 'The text to write to the server logs. It is printed when the page runs.', 'TEXT', TRUE, FALSE), - ('level', 'How important the message is. One of ''trace'', ''debug'', ''info'' (default), ''warn'', ''error''. Not case-sensitive. Controls the level shown in the logs.', 'TEXT', TRUE, TRUE) -) x; - -INSERT INTO example(component, description) VALUES -('log', ' -### Record a simple message - -This writes "Hello, World!" to the server logs. - -```sql -SELECT ''log'' as component, ''Hello, World!'' as message; -``` - -Example output: - -```text -[2025-09-13T22:30:14.722Z INFO sqlpage::log from "x.sql" statement 1] Hello, World! -``` - -### Set the importance (level) - -Choose how important the message is. - -```sql -SELECT ''log'' as component, ''error'' as level, ''This is an error message'' as message; -``` - -Example output: - -```text -[2025-09-13T22:30:14.722Z ERROR sqlpage::log from "x.sql" statement 2] This is an error message -``` - -### Log dynamic information - -Include variables like a username. - -```sql -set username = ''user'' - -select ''log'' as component, - ''403 - failed for '' || coalesce($username, ''None'') as message, - ''error'' as level; -``` -') diff --git a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql index e897f6b0f..2c198e598 100644 --- a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql +++ b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql @@ -30,18 +30,11 @@ WITH copy(name, category, summary) AS (VALUES - ('cookie', 'http-responses', 'Set a cookie in the visitor''s browser.'), ('csv', 'http-responses', 'Export query rows as a downloadable CSV file.'), ('download', 'http-responses', 'Send a file directly to the visitor.'), - ('http_header', 'http-responses', 'Set response headers for caching, content types, and more.'), ('json', 'http-responses', 'Return query results as JSON for an API.'), - ('redirect', 'http-responses', 'Send the visitor to another URL.'), - ('rss', 'http-responses', 'Publish an RSS feed from query results.'), - ('status_code', 'http-responses', 'Set the HTTP status code returned by a page.'), + ('rss', 'http-responses', 'Publish an RSS feed from query results.') - ('debug', 'server-logic', 'Inspect the values passed to a component as JSON.'), - ('dynamic', 'server-logic', 'Render SQLPage components described by JSON data.'), - ('log', 'server-logic', 'Write a message to the SQLPage server log.') ) UPDATE component SET (catalog_category, catalog_summary) = (copy.category, copy.summary) FROM copy WHERE component.name = copy.name;