Skip to content

Commit be805ec

Browse files
committed
Update documentation for BLOB support and data type handling
- CHANGELOG.md : details on BLOB support and automatic MIME type detection - Add examples in extensions-to-sql.md illustrating data type conversions and JSON object structure - Update SQL examples in migrations to reflect new BLOB handling capabilities
1 parent bc76cb9 commit be805ec

File tree

4 files changed

+81
-11
lines changed

4 files changed

+81
-11
lines changed

CHANGELOG.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
- Since modals have their own url inside the page, you can now link to a modal from another page, and if you refresh a page while the modal is open, the modal will stay open.
1212
- modals now have an `open` parameter to open the modal automatically when the page is loaded.
1313
- New [download](https://sql-page.com/component.sql?component=download) component to let the user download files. The files may be stored as BLOBs in the database, local files on the server, or may be fetched from a different server.
14-
- Enhanced BLOB Support. You can now return binary data (BLOBs) directly to sqlpage, and it will automatically convert them to data URLs. This allows you to use database BLOBs directly wherever a link is expected, including in the new download component.
15-
- **PostgreSQL**: supports `BYTEA` columns
16-
- **MySQL/MariaDB**: supports `BLOB` columns
17-
- **MSSQL**: Extended support for `VARBINARY`, `BIGVARBINARY`, `BINARY`, and `IMAGE` columns
18-
- **SQLite**: Full support for `BLOB` columns
19-
- **Smart MIME Type Detection**: Automatic detection of common file types based on magic bytes:
20-
- **Images**: PNG, JPEG/JPG, GIF, BMP, WebP, SVG
21-
- **Documents**: PDF, DOCX, XLSX, PPTX
22-
- **Data**: JSON, XML, ZIP archives
14+
- **Enhanced BLOB Support**. You can now return binary data (BLOBs) directly to sqlpage, and it will automatically convert them to data URLs. This allows you to use database BLOBs directly wherever a link is expected, including in the new download component.
15+
- supports columns of type `BYTEA` (PostgreSQL), `BLOB` (MySQL, SQLite), `VARBINARY` and `IMAGE` (mssql)
16+
- Automatic detection of common file types based on magic bytes
17+
- This means you can use a BLOB wherever an image url is expected. For instance:
18+
```sql
19+
select 'list' as component;
20+
select username as title, avatar_blob as image_url
21+
from users;
22+
```
2323

2424
## v0.36.1
2525
- Fix regression introduced in v0.36.0: PostgreSQL money values showed as 0.0

examples/official-site/extensions-to-sql.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,52 @@ SET post_id = COALESCE($post_id, 0);
206206
-- Prepared statement (SQLite syntax)
207207
SELECT COALESCE(CAST(?1 AS TEXT), 0)
208208
```
209+
210+
# Data types
211+
212+
Each database has its own rich set of data types.
213+
The data modal in SQLPage itself is simpler, mainly composed of text strings and json objects.
214+
215+
### From the user to SQLPage
216+
217+
Form fields and URL parameters may contain arrays. These are converted to JSON strings before processing.
218+
219+
For instance, Loading `users.sql?user[]=Tim&user[]=Tom` will result in a single variable `$user` with the textual value `["Tim", "Tom"]`.
220+
221+
### From SQLPage to the database
222+
223+
SQLPage sends only text strings (`VARCHAR`) and `NULL`s to the database, since these are the only possible variable and function return values.
224+
225+
### From the database to SQLPage
226+
227+
Each row of data returned by a SQL query is converted to a JSON object before being passed to components.
228+
229+
- Each column becomes a key in the json object. If a row has two columns of the same name, they become an array in the json object.
230+
- Each value is converted to the closest JSON value
231+
- all number types map to json numbers, booleans to booleans, and `NULL` to `null`,
232+
- all text types map to json strings
233+
- date and time types map to json strings containing ISO datetime values
234+
- binary values (BLOBs) map to json strings containing [data URLs](https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data)
235+
236+
#### Example
237+
238+
The following PostgreSQL query:
239+
240+
```sql
241+
select
242+
1 as one,
243+
'x' as my_array, 'y' as my_array,
244+
now() as today,
245+
'<svg></svg>'::bytea as my_image;
246+
```
247+
248+
will result in the following JSON object being passed to components for rendering
249+
250+
```json
251+
{
252+
"one" : 1,
253+
"my_array" : ["x","y"],
254+
"today":"2025-08-30T06:40:13.894918+00:00",
255+
"my_image":"data:image/svg+xml;base64,PHN2Zz48L3N2Zz4="
256+
}
257+
```

examples/official-site/extensions-to-sql.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
select 'http_header' as component,
22
'public, max-age=300, stale-while-revalidate=3600, stale-if-error=86400' as "Cache-Control";
33

4-
select 'dynamic' as component, properties FROM example WHERE component = 'shell' LIMIT 1;
4+
select 'dynamic' as component, json_patch(json_extract(properties, '$[0]'), json_object(
5+
'title', 'SQLPage - Extensions to SQL'
6+
)) as properties
7+
FROM example WHERE component = 'shell' LIMIT 1;
58

69
-- Article by Matthew Larkin
710
select 'text' as component,

examples/official-site/sqlpage/migrations/65_download.sql

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,25 @@ select
104104
'
105105
## Serve an image stored as a BLOB in the database
106106
107-
In PostgreSQL, you can use the [encode(bytes, format)](https://www.postgresql.org/docs/current/functions-binarystring.html#FUNCTION-ENCODE) function to encode the file content as Base64.
107+
### Automatically detect the mime type
108+
109+
If you have a table with a column `content` that contains a BLOB
110+
(depending on the database, the type may be named `BYTEA`, `BLOB`, `VARBINARY`, or `IMAGE`),
111+
you can just return its contents directly, and SQLPage will automatically detect the mime type,
112+
and convert it to a data URL.
113+
114+
```sql
115+
select
116+
''download'' as component,
117+
content as data_url
118+
from document
119+
where id = $doc_id;
120+
```
121+
122+
### Customize the mime type
123+
124+
In PostgreSQL, you can use the [encode(bytes, format)](https://www.postgresql.org/docs/current/functions-binarystring.html#FUNCTION-ENCODE) function to encode the file content as Base64,
125+
and manually create your own data URL.
108126
109127
```sql
110128
select

0 commit comments

Comments
 (0)