Skip to content

Commit 0c160be

Browse files
committed
Load function references from Markdown, starting with request context
1 parent 514c76b commit 0c160be

14 files changed

Lines changed: 195 additions & 193 deletions

File tree

‎examples/official-site/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ Edit `content/guides.yaml` for the guides list and `content/projects.yaml` for t
3838
example project cards. A project's `folder` must match its folder under
3939
[`examples/`](../).
4040

41+
## Function reference
42+
43+
Function pages in `content/functions/` use their filename as the public function
44+
name. Keep the argument list in signature order and mark mandatory arguments with
45+
`required: true`. The Markdown body can include SQL examples without escaping.
46+
Older entries still live in the SQL migrations.
47+
4148
## Landing page
4249

4350
The home page is rendered by `index.sql` and the `landing-page` custom template.

examples/official-site/sqlpage/migrations/57_client_ip.sql renamed to examples/official-site/content/functions/client_ip.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
INSERT INTO
2-
sqlpage_functions (
3-
"name",
4-
"introduced_in_version",
5-
"icon",
6-
"description_md"
7-
)
8-
VALUES
9-
(
10-
'client_ip',
11-
'0.33.0',
12-
'network',
13-
'Returns the IP address of the client making the HTTP request.
1+
---
2+
icon: network
3+
since: 0.33.0
4+
category: request-data
5+
summary: Get the IP address of the client making the request.
6+
---
7+
8+
Returns the IP address of the client making the HTTP request.
149

1510
### Example
1611

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

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

3429
Example with reverse proxy:
3530
```sql
3631
-- Choose the appropriate header based on your setup
3732
select coalesce(
38-
sqlpage.header(''x-forwarded-for''),
39-
sqlpage.header(''x-real-ip''),
33+
sqlpage.header('x-forwarded-for'),
34+
sqlpage.header('x-real-ip'),
4035
sqlpage.client_ip()
4136
) as real_client_ip;
4237
```
4338

4439
For security-critical applications, ensure your reverse proxy is properly configured to set and validate these headers.
45-
'
46-
);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
icon: cookie
3+
since: 0.7.1
4+
category: request-data
5+
summary: Read a named cookie from the incoming request.
6+
return_type: TEXT
7+
parameters:
8+
- name: name
9+
type: TEXT
10+
required: true
11+
description: The name of the cookie to read.
12+
---
13+
14+
Reads a [cookie](https://en.wikipedia.org/wiki/HTTP_cookie) with the given name from the request.
15+
Returns the value of the cookie as text, or NULL if the cookie is not present.
16+
17+
Cookies can be set using the [cookie component](documentation.sql?component=cookie#component).
18+
19+
### Example
20+
21+
#### Set a cookie
22+
23+
Set a cookie called `username` to greet the user by name every time they visit the page:
24+
25+
```sql
26+
select 'cookie' as component, 'username' as name, :username as value;
27+
28+
SELECT 'form' as component;
29+
SELECT 'username' as name, 'text' as type;
30+
```
31+
32+
#### Read a cookie
33+
34+
Read a cookie called `username` and greet the user by name:
35+
36+
```sql
37+
SELECT 'text' as component,
38+
'Hello, ' || sqlpage.cookie('username') || '!' as contents;
39+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
icon: heading
3+
since: 0.7.2
4+
category: request-data
5+
summary: Read the value of one HTTP request header.
6+
parameters:
7+
- name: name
8+
type: TEXT
9+
required: true
10+
description: The name of the HTTP header to read.
11+
---
12+
13+
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`.
14+
15+
### Example
16+
17+
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:
18+
19+
```sql
20+
INSERT INTO user_agent_log (user_agent) VALUES (sqlpage.header('user-agent'));
21+
```
22+
23+
If you need access to all headers at once, use [`sqlpage.headers()`](?function=headers) instead.

examples/official-site/sqlpage/migrations/56_headers.sql renamed to examples/official-site/content/functions/headers.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
INSERT INTO sqlpage_functions (
2-
"name",
3-
"introduced_in_version",
4-
"icon",
5-
"description_md"
6-
)
7-
VALUES (
8-
'headers',
9-
'0.33.0',
10-
'circle-dotted-letter-h',
11-
'Returns all HTTP request headers as a JSON object.
1+
---
2+
icon: circle-dotted-letter-h
3+
since: 0.33.0
4+
category: request-data
5+
summary: Read all HTTP request headers as a JSON object.
6+
---
7+
8+
Returns all HTTP request headers as a JSON object.
129

1310
### Example
1411

1512
The following displays all HTTP request headers in a list,
16-
using SQLite''s `json_each()` function.
13+
using SQLite's `json_each()` function.
1714

1815
```sql
19-
select ''list'' as component;
16+
select 'list' as component;
2017

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

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

2724
### Details
2825

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

3835
If you only need access to a single known header, use [`sqlpage.header(name)`](?function=header) instead.
39-
');
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
INSERT INTO sqlpage_functions (
2-
"name",
3-
"introduced_in_version",
4-
"icon",
5-
"description_md"
6-
)
7-
VALUES (
8-
'path',
9-
'0.15.0',
10-
'slashes',
11-
'Returns the request path of the current page.
1+
---
2+
icon: slashes
3+
since: 0.15.0
4+
category: request-data
5+
summary: Get the URL path of the current request.
6+
---
7+
8+
Returns the request path of the current page.
129
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.
1310

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

1916
```sql
20-
select ''text'' as component, sqlpage.path() as contents;
17+
select 'text' as component, sqlpage.path() as contents;
2118
```
2219

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

2522
> Note that the path is URL-encoded.
26-
');
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
icon: network
3+
since: 0.17.1
4+
category: request-data
5+
summary: Identify whether the request used HTTP or HTTPS.
6+
---
7+
8+
Returns the protocol that was used to access the current page.
9+
10+
This can be either `http` or `https`.
11+
12+
This is useful to generate links to the current page.
13+
14+
### Example
15+
16+
```sql
17+
select 'text' as component,
18+
sqlpage.protocol() || '://' || sqlpage.header('host') || sqlpage.path() as contents;
19+
```
20+
21+
will return `https://example.com/example.sql`.
22+
23+
> Note that the path is URL-encoded. The protocol is resolved in this order:
24+
> - `Forwarded` header
25+
> - `X-Forwarded-Proto` header
26+
> request target / URI

examples/official-site/sqlpage/migrations/43_request_method.sql renamed to examples/official-site/content/functions/request_method.md

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
1-
INSERT INTO
2-
sqlpage_functions (
3-
"name",
4-
"introduced_in_version",
5-
"icon",
6-
"description_md"
7-
)
8-
VALUES
9-
(
10-
'request_method',
11-
'0.21.0',
12-
'http-get',
13-
'Returns the HTTP request method (GET, POST, etc.) used to access the page.
1+
---
2+
icon: http-get
3+
since: 0.21.0
4+
category: request-data
5+
summary: Get the HTTP method, such as GET or POST.
6+
---
7+
8+
Returns the HTTP request method (GET, POST, etc.) used to access the page.
149

1510
## HTTP request methods
1611

1712
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:
1813
- **GET**: retrieve information from the server. This is the default method used by browsers when you click on a link.
19-
- **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.
14+
- **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.
2015
- **PUT**: replace the current representation of the target resource with the request payload. Most commonly used in REST APIs.
2116
- **DELETE**: remove the target resource.
2217
- **PATCH**, **HEAD**, **OPTIONS**, **CONNECT**, **TRACE**: less common methods that are used in specific situations.
2318

2419
## Require a PUT request
2520

2621
```sql
27-
select ''redirect'' as component,
28-
''/error?msg=expected+a+PUT+request'' as link
29-
where sqlpage.request_method() != ''PUT'';
22+
select 'redirect' as component,
23+
'/error?msg=expected+a+PUT+request' as link
24+
where sqlpage.request_method() != 'PUT';
3025

3126
insert into my_table (column1, column2) values (:value1, :value2);
3227
```
33-
'
34-
);

‎examples/official-site/sqlpage/migrations/08_functions.sql‎

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -12,97 +12,6 @@ CREATE TABLE IF NOT EXISTS sqlpage_function_parameters (
1212
"description_md" TEXT,
1313
"type" TEXT
1414
);
15-
INSERT INTO sqlpage_functions (
16-
"name",
17-
"return_type",
18-
"introduced_in_version",
19-
"icon",
20-
"description_md"
21-
)
22-
VALUES (
23-
'cookie',
24-
'TEXT',
25-
'0.7.1',
26-
'cookie',
27-
'Reads a [cookie](https://en.wikipedia.org/wiki/HTTP_cookie) with the given name from the request.
28-
Returns the value of the cookie as text, or NULL if the cookie is not present.
29-
30-
Cookies can be set using the [cookie component](documentation.sql?component=cookie#component).
31-
32-
### Example
33-
34-
#### Set a cookie
35-
36-
Set a cookie called `username` to greet the user by name every time they visit the page:
37-
38-
```sql
39-
select ''cookie'' as component, ''username'' as name, :username as value;
40-
41-
SELECT ''form'' as component;
42-
SELECT ''username'' as name, ''text'' as type;
43-
```
44-
45-
#### Read a cookie
46-
47-
Read a cookie called `username` and greet the user by name:
48-
49-
```sql
50-
SELECT ''text'' as component,
51-
''Hello, '' || sqlpage.cookie(''username'') || ''!'' as contents;
52-
```
53-
'
54-
);
55-
INSERT INTO sqlpage_function_parameters (
56-
"function",
57-
"index",
58-
"name",
59-
"description_md",
60-
"type"
61-
)
62-
VALUES (
63-
'cookie',
64-
1,
65-
'name',
66-
'The name of the cookie to read.',
67-
'TEXT'
68-
);
69-
INSERT INTO sqlpage_functions (
70-
"name",
71-
"introduced_in_version",
72-
"icon",
73-
"description_md"
74-
)
75-
VALUES (
76-
'header',
77-
'0.7.2',
78-
'heading',
79-
'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`.
80-
81-
### Example
82-
83-
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:
84-
85-
```sql
86-
INSERT INTO user_agent_log (user_agent) VALUES (sqlpage.header(''user-agent''));
87-
```
88-
89-
If you need access to all headers at once, use [`sqlpage.headers()`](?function=headers) instead.
90-
'
91-
);
92-
INSERT INTO sqlpage_function_parameters (
93-
"function",
94-
"index",
95-
"name",
96-
"description_md",
97-
"type"
98-
)
99-
VALUES (
100-
'header',
101-
1,
102-
'name',
103-
'The name of the HTTP header to read.',
104-
'TEXT'
105-
);
10615
INSERT INTO sqlpage_functions (
10716
"name",
10817
"return_type",

0 commit comments

Comments
 (0)