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
7 changes: 7 additions & 0 deletions examples/official-site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ Edit `content/guides.yaml` for the guides list and `content/projects.yaml` for t
example project cards. A project's `folder` must match its folder under
[`examples/`](../).

## Function reference

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

The home page is rendered by `index.sql` and the `landing-page` custom template.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
INSERT INTO
sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES
(
'client_ip',
'0.33.0',
'network',
'Returns the IP address of the client making the HTTP request.
---
icon: network
since: 0.33.0
category: request-data
summary: Get the IP address of the client making the request.
---

Returns the IP address of the client making the HTTP request.

### Example

Expand All @@ -28,19 +23,17 @@ The function returns:

When [running behind a reverse proxy](/your-first-sql-website/nginx.sql) (e.g., Nginx, Apache, Cloudflare):
- This function will return the IP address of the reverse proxy, not the actual client
- To get the real client IP, use [`sqlpage.header`](?function=header): `sqlpage.header(''x-forwarded-for'')` or `sqlpage.header(''x-real-ip'')`
- To get the real client IP, use [`sqlpage.header`](?function=header): `sqlpage.header('x-forwarded-for')` or `sqlpage.header('x-real-ip')`
- The exact header name depends on your reverse proxy configuration

Example with reverse proxy:
```sql
-- Choose the appropriate header based on your setup
select coalesce(
sqlpage.header(''x-forwarded-for''),
sqlpage.header(''x-real-ip''),
sqlpage.header('x-forwarded-for'),
sqlpage.header('x-real-ip'),
sqlpage.client_ip()
) as real_client_ip;
```

For security-critical applications, ensure your reverse proxy is properly configured to set and validate these headers.
'
);
39 changes: 39 additions & 0 deletions examples/official-site/content/functions/cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
icon: cookie
since: 0.7.1
category: request-data
summary: Read a named cookie from the incoming request.
return_type: TEXT
parameters:
- name: name
type: TEXT
required: true
description: The name of the cookie to read.
---

Reads a [cookie](https://en.wikipedia.org/wiki/HTTP_cookie) with the given name from the request.
Returns the value of the cookie as text, or NULL if the cookie is not present.

Cookies can be set using the [cookie component](documentation.sql?component=cookie#component).

### Example

#### Set a cookie

Set a cookie called `username` to greet the user by name every time they visit the page:

```sql
select 'cookie' as component, 'username' as name, :username as value;

SELECT 'form' as component;
SELECT 'username' as name, 'text' as type;
```

#### Read a cookie

Read a cookie called `username` and greet the user by name:

```sql
SELECT 'text' as component,
'Hello, ' || sqlpage.cookie('username') || '!' as contents;
```
23 changes: 23 additions & 0 deletions examples/official-site/content/functions/header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
icon: heading
since: 0.7.2
category: request-data
summary: Read the value of one HTTP request header.
parameters:
- name: name
type: TEXT
required: true
description: The name of the HTTP header to read.
---

Reads one [HTTP request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers). Names are case-insensitive: `sqlpage.header('User-Agent')` and `sqlpage.header('user-agent')` read the same value. An absent header returns SQL `NULL`.

### Example

Log the [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) of the browser making the request in the database:

```sql
INSERT INTO user_agent_log (user_agent) VALUES (sqlpage.header('user-agent'));
```

If you need access to all headers at once, use [`sqlpage.headers()`](?function=headers) instead.
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
INSERT INTO sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES (
'headers',
'0.33.0',
'circle-dotted-letter-h',
'Returns all HTTP request headers as a JSON object.
---
icon: circle-dotted-letter-h
since: 0.33.0
category: request-data
summary: Read all HTTP request headers as a JSON object.
---

Returns all HTTP request headers as a JSON object.

### Example

The following displays all HTTP request headers in a list,
using SQLite''s `json_each()` function.
using SQLite's `json_each()` function.

```sql
select ''list'' as component;
select 'list' as component;

select key as title, value as description
from json_each(sqlpage.headers()); -- json_each() is SQLite only
```

If not on SQLite, use your [database''s JSON function](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).
If not on SQLite, use your [database's JSON function](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).

### Details

Expand All @@ -36,4 +33,3 @@ This is useful when you need to:
- Access multiple headers at once

If you only need access to a single known header, use [`sqlpage.header(name)`](?function=header) instead.
');
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
INSERT INTO sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES (
'path',
'0.15.0',
'slashes',
'Returns the request path of the current page.
---
icon: slashes
since: 0.15.0
category: request-data
summary: Get the URL path of the current request.
---

Returns the request path of the current page.
This is useful to generate links to the current page, and when you have a proxy in front of your SQLPage server that rewrites the URL.

### Example
Expand All @@ -17,10 +14,9 @@ If we have a page in a file named `my page.sql` at the root of your SQLPage inst
then the following SQL query:

```sql
select ''text'' as component, sqlpage.path() as contents;
select 'text' as component, sqlpage.path() as contents;
```

will return `/my%20page.sql`.

> Note that the path is URL-encoded.
');
26 changes: 26 additions & 0 deletions examples/official-site/content/functions/protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
icon: network
since: 0.17.1
category: request-data
summary: Identify whether the request used HTTP or HTTPS.
---

Returns the protocol that was used to access the current page.

This can be either `http` or `https`.

This is useful to generate links to the current page.

### Example

```sql
select 'text' as component,
sqlpage.protocol() || '://' || sqlpage.header('host') || sqlpage.path() as contents;
```

will return `https://example.com/example.sql`.

> Note that the path is URL-encoded. The protocol is resolved in this order:
> - `Forwarded` header
> - `X-Forwarded-Proto` header
> request target / URI
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
INSERT INTO
sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES
(
'request_method',
'0.21.0',
'http-get',
'Returns the HTTP request method (GET, POST, etc.) used to access the page.
---
icon: http-get
since: 0.21.0
category: request-data
summary: Get the HTTP method, such as GET or POST.
---

Returns the HTTP request method (GET, POST, etc.) used to access the page.

## HTTP request methods

HTTP request methods (also known as verbs) are used to indicate the desired action to be performed on the identified resource. The most common methods are:
- **GET**: retrieve information from the server. This is the default method used by browsers when you click on a link.
- **POST**: submit data to be processed by the server. SQLPage''s [form component](/component.sql?component=form) defaults to POST; a plain HTML form defaults to GET.
- **POST**: submit data to be processed by the server. SQLPage's [form component](/component.sql?component=form) defaults to POST; a plain HTML form defaults to GET.
- **PUT**: replace the current representation of the target resource with the request payload. Most commonly used in REST APIs.
- **DELETE**: remove the target resource.
- **PATCH**, **HEAD**, **OPTIONS**, **CONNECT**, **TRACE**: less common methods that are used in specific situations.

## Require a PUT request

```sql
select ''redirect'' as component,
''/error?msg=expected+a+PUT+request'' as link
where sqlpage.request_method() != ''PUT'';
select 'redirect' as component,
'/error?msg=expected+a+PUT+request' as link
where sqlpage.request_method() != 'PUT';

insert into my_table (column1, column2) values (:value1, :value2);
```
'
);
91 changes: 0 additions & 91 deletions examples/official-site/sqlpage/migrations/08_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,97 +12,6 @@ CREATE TABLE IF NOT EXISTS sqlpage_function_parameters (
"description_md" TEXT,
"type" TEXT
);
INSERT INTO sqlpage_functions (
"name",
"return_type",
"introduced_in_version",
"icon",
"description_md"
)
VALUES (
'cookie',
'TEXT',
'0.7.1',
'cookie',
'Reads a [cookie](https://en.wikipedia.org/wiki/HTTP_cookie) with the given name from the request.
Returns the value of the cookie as text, or NULL if the cookie is not present.

Cookies can be set using the [cookie component](documentation.sql?component=cookie#component).

### Example

#### Set a cookie

Set a cookie called `username` to greet the user by name every time they visit the page:

```sql
select ''cookie'' as component, ''username'' as name, :username as value;

SELECT ''form'' as component;
SELECT ''username'' as name, ''text'' as type;
```

#### Read a cookie

Read a cookie called `username` and greet the user by name:

```sql
SELECT ''text'' as component,
''Hello, '' || sqlpage.cookie(''username'') || ''!'' as contents;
```
'
);
INSERT INTO sqlpage_function_parameters (
"function",
"index",
"name",
"description_md",
"type"
)
VALUES (
'cookie',
1,
'name',
'The name of the cookie to read.',
'TEXT'
);
INSERT INTO sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES (
'header',
'0.7.2',
'heading',
'Reads one [HTTP request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers). Names are case-insensitive: `sqlpage.header(''User-Agent'')` and `sqlpage.header(''user-agent'')` read the same value. An absent header returns SQL `NULL`.

### Example

Log the [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) of the browser making the request in the database:

```sql
INSERT INTO user_agent_log (user_agent) VALUES (sqlpage.header(''user-agent''));
```

If you need access to all headers at once, use [`sqlpage.headers()`](?function=headers) instead.
'
);
INSERT INTO sqlpage_function_parameters (
"function",
"index",
"name",
"description_md",
"type"
)
VALUES (
'header',
1,
'name',
'The name of the HTTP header to read.',
'TEXT'
);
INSERT INTO sqlpage_functions (
"name",
"return_type",
Expand Down
Loading
Loading