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
2 changes: 1 addition & 1 deletion examples/official-site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ live examples using SQLPage's stock theme. For example, visit
Write posts in `content/blog/` as Markdown with YAML metadata: `title`, `date`
(quoted as `YYYY-MM-DD`), `icon`, and `summary`. For a link to a post elsewhere,
set `external_url` and omit the body. The title is the public URL key, so keep it
when editing an existing post. Older posts are still in the SQL migrations.
when editing an existing post.

## Landing page

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
---
title: File-based routing in SQLPage
date: '2025-07-28'
icon: route
summary: Understanding how SQLPage maps URLs to files and handles errors
---

INSERT INTO blog_posts (title, description, icon, created_at, content)
VALUES
(
'File-based routing in SQLPage',
'Understanding how SQLPage maps URLs to files and handles errors',
'route',
'2025-07-28',
'
SQLPage uses a simple file-based routing system that maps URLs directly to SQL files in your project directory.
No complex configuration is needed. Just create files and they become accessible endpoints.

Expand All @@ -16,7 +14,7 @@ This guide explains how SQLPage resolves URLs, handles different file types, and

### 1. Site Prefix Handling

If you''ve configured a [`site_prefix`](/your-first-sql-website/nginx) in your settings,
If you've configured a [`site_prefix`](/your-first-sql-website/nginx) in your settings,
SQLPage will redirect all requests that do not start with the prefix to `/<site_prefix>`.

### 2. Path Resolution Priority
Expand All @@ -27,18 +25,18 @@ SQLPage will redirect all requests that do not start with the prefix to `/<site_

**Static asset requests (other extensions)**: SQLPage serves files like CSS, JavaScript, images, or any other static content directly.

**Clean URL requests (no extension)**: SQLPage first tries to find a matching `.sql` file. If that doesn''t exist but there''s an `index.sql` file in a directory with the same name, it redirects to the directory path with a trailing slash.
**Clean URL requests (no extension)**: SQLPage first tries to find a matching `.sql` file. If that doesn't exist but there's an `index.sql` file in a directory with the same name, it redirects to the directory path with a trailing slash.

Paths with trailing spaces are rejected. On Windows, paths with trailing dots or NTFS stream names are rejected too. These spellings can refer to a different file than their URL suggests on some storage systems.

### Error Handling

When, after applying each of the rules above in order, SQLPage can''t find a requested file,
When, after applying each of the rules above in order, SQLPage can't find a requested file,
it walks up your directory structure looking for [custom `404.sql` files](/your-first-sql-website/custom_urls).

## Dynamic Routing with SQLPage

SQLPage''s file-based routing becomes powerful when combined with strategic use of 404.sql files to handle dynamic URLs. Here''s how to build APIs and pages with dynamic parameters:
SQLPage's file-based routing becomes powerful when combined with strategic use of 404.sql files to handle dynamic URLs. Here's how to build APIs and pages with dynamic parameters:

### Product Catalog with Dynamic IDs

Expand All @@ -59,7 +57,5 @@ products/

**In `products/404.sql`**:
```sql
set product_id = substr(sqlpage.path(), 1+length(''/products/''));
set product_id = substr(sqlpage.path(), 1+length('/products/'));
```
'
);

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
---
title: How I built a 360° performance monitoring tool with SQL Queries
date: '2025-01-25'
icon: shirt
summary: Alexis built a performance monitoring tool with SQLPage for a 100M€/year company
---

INSERT INTO blog_posts (title, description, icon, created_at, content)
VALUES
(
'How I built a 360° performance monitoring tool with SQL Queries',
'Alexis built a performance monitoring tool with SQLPage for a 100M€/year company',
'shirt',
'2025-01-25',
'
# How I Built And Deployed An Exhaustive Performance Monitoring Tool For a 100M€/year Company Using SQL Queries Only

### Building web applications in SQL
Expand Down Expand Up @@ -48,12 +46,12 @@ I am using SQLPage to build a 360° Performance tool for my company, integrating

Here are some pages I built using only SQL queries, allowing everyone in the company to instantly access any level of information, from the fiscal year 2024 revenue trends to the top-selling products in Marseille in October 2022.

![](https://cdn-images-1.medium.com/max/800/1*MkORbAC7oGEG-8I1mthu6A.png)
![](https://cdn-images-1.medium.com/max/800/1*MkORbAC7oGEG-8I1mthu6A.png)\

*Performance of different channels vs last year and best sellers.

![](https://cdn-images-1.medium.com/max/800/1*_3-g1om_p9ghXhdcw0zHmw.png)
![](https://cdn-images-1.medium.com/max/800/1*_3-g1om_p9ghXhdcw0zHmw.png)\

*Examples of views built with SQLPage to provide a 360° tool for the company.

### Populating a chart from a query
Expand All @@ -68,27 +66,27 @@ You can find the full list of components: [https://sql-page.com/documentation.sq

Here’s an example of a parameterized SQL query that uses the “chart” component, along with the query to feed data into it:


```sql
-- Chart Component
SELECT ''chart'' AS component,
CONCAT(''Daily Revenue from '', $start_date_comparison, '' to '', $end_date_comparison) AS title,
''area'' AS type,
''indigo'' AS color,
5 AS marker,
0 AS ymin;
-- Chart Data Query
SELECT DATE(business_date) AS x,
ROUND(SUM(value), 2) AS y
FROM data_example
WHERE DATE(business_date) BETWEEN $start_date_comparison AND $end_date_comparison
AND variable_name = ''CA''
GROUP BY DATE(business_date)
ORDER BY x ASC;
-- NB: The variables $start_date_comparison and $end_date_comparison are


```sql
-- Chart Component
SELECT 'chart' AS component,
CONCAT('Daily Revenue from ', $start_date_comparison, ' to ', $end_date_comparison) AS title,
'area' AS type,
'indigo' AS color,
5 AS marker,
0 AS ymin;

-- Chart Data Query
SELECT DATE(business_date) AS x,
ROUND(SUM(value), 2) AS y
FROM data_example
WHERE DATE(business_date) BETWEEN $start_date_comparison AND $end_date_comparison
AND variable_name = 'CA'
GROUP BY DATE(business_date)
ORDER BY x ASC;

-- NB: The variables $start_date_comparison and $end_date_comparison are
-- defined dynamically in the SQL script
```
And the result:
Expand Down Expand Up @@ -138,4 +136,3 @@ What all these projects have in common is that they were built by a single perso
- 📹Youtube tutorial videos on SQLPage channel: [https://www.youtube.com/@SQLPage/playlists](https://www.youtube.com/@SQLPage/playlists)
- 🤓github: [https://github.com/sqlpage/SQLPage/](https://github.com/sqlpage/SQLPage/)
- ☁️Host your applications: [https://datapage.app](https://datapage.app)
');
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
---
title: How to use the pagination component
date: '2025-11-10'
icon: sailboat-2
summary: A tutorial for using the pagination component
---

INSERT INTO blog_posts (title, description, icon, created_at, content)
VALUES
(
'How to use the pagination component',
'A tutorial for using the pagination component',
'sailboat-2',
'2025-11-10',
'
# How to use the pagination component

To display a large number of records from a database, it is often practical to split these data into pages. The user can thus navigate from one page to another, as well as directly to the first or last page. With SQLPage, it is possible to perform these operations using the pagination component.
Expand Down Expand Up @@ -34,17 +32,17 @@ SET pages_count = (CAST($records_count AS INTEGER) / CAST($MAX_RECORD_PER_PAGE A
```

It is possible that the number of rows in the table is greater than the estimated number of pages multiplied by the number of rows per page. In this case, it is necessary to add an additional page.

```
SET pages_count = (
CASE
WHEN MOD(CAST($records_count AS INTEGER),CAST($MAX_RECORD_PER_PAGE AS INTEGER)) = 0 THEN $pages_count
ELSE (CAST($pages_count AS INTEGER) + 1)
CASE
WHEN MOD(CAST($records_count AS INTEGER),CAST($MAX_RECORD_PER_PAGE AS INTEGER)) = 0 THEN $pages_count
ELSE (CAST($pages_count AS INTEGER) + 1)
END
);
```

We will need to transmit the page number to be displayed in the URL using the `page` parameter. We do the same for the number of the first page (`idx_page`) appearing at the left end of the pagination component.
We will need to transmit the page number to be displayed in the URL using the `page` parameter. We do the same for the number of the first page (`idx_page`) appearing at the left end of the pagination component.

![Meaning of URL parameters](blog/pagination.png)

Expand All @@ -58,15 +56,15 @@ SET idx_page = COALESCE($idx_page,1);

## Read the data

We can now read and display the data based on the active page. To do this, we simply use a table component.
We can now read and display the data based on the active page. To do this, we simply use a table component.

```
SELECT
''table'' as component;
SELECT
user_id AS id,
'table' as component;
SELECT
user_id AS id,
last_name AS "Last name",
first_name AS "First name"
first_name AS "First name"
FROM
users
ORDER BY user_id
Expand All @@ -89,13 +87,13 @@ SET previous_parameters = (
CASE
WHEN CAST($page AS INTEGER) > 1 THEN
json_object(
''page'', (CAST($page AS INTEGER) - 1),
''idx_page'', (CASE
'page', (CAST($page AS INTEGER) - 1),
'idx_page', (CASE
WHEN CAST($idx_page AS INTEGER) > 1 THEN (CAST($idx_page AS INTEGER) - 1)
ELSE $idx_page
END)
END)
)
ELSE json_object() END
ELSE json_object() END
);
```

Expand All @@ -106,11 +104,11 @@ SET next_parameters = (
CASE
WHEN CAST($page AS INTEGER) < CAST($pages_count AS INTEGER) THEN
json_object(
''page'', (CAST($page AS INTEGER) + 1),
''idx_page'', (CASE
'page', (CAST($page AS INTEGER) + 1),
'idx_page', (CASE
WHEN CAST($idx_page AS INTEGER) < (CAST($pages_count AS INTEGER) - CAST($MAX_PAGES AS INTEGER) + 1) THEN (CAST($idx_page AS INTEGER) + 1)
ELSE $idx_page
END)
END)
)
ELSE json_object() END
);
Expand All @@ -123,27 +121,27 @@ We can now add the pagination component, which is placed below the table display

```
SELECT
''pagination'' AS component,
'pagination' AS component,
(CAST($page AS INTEGER) = 1) AS first_disabled,
(CAST($page AS INTEGER) = 1) AS previous_disabled,
(CAST($page AS INTEGER) = CAST($pages_count AS INTEGER)) AS next_disabled,
(CAST($page AS INTEGER) = CAST($pages_count AS INTEGER)) AS last_disabled,
sqlpage.link(sqlpage.path(), json_object(''page'', 1, ''idx_page'', 1)) as first_link,
sqlpage.link(sqlpage.path(), json_object('page', 1, 'idx_page', 1)) as first_link,
sqlpage.link(sqlpage.path(), $previous_parameters) AS previous_link,
sqlpage.link(sqlpage.path(), $next_parameters) AS next_link,
sqlpage.link(
sqlpage.path(),
json_object(''page'', $pages_count, ''idx_page'', (
json_object('page', $pages_count, 'idx_page', (
CASE
WHEN (CAST($pages_count AS INTEGER) <= CAST($MAX_PAGES AS INTEGER)) THEN 1
ELSE (CAST($pages_count AS INTEGER) - CAST($MAX_PAGES AS INTEGER) + 1)
ELSE (CAST($pages_count AS INTEGER) - CAST($MAX_PAGES AS INTEGER) + 1)
END)
)
) AS last_link,
TRUE AS outline;
```

The final step is to generate the page numbers based on the number of pages and the index of the first page displayed to the left of the component. To do this, we use a recursive CTE query.
The final step is to generate the page numbers based on the number of pages and the index of the first page displayed to the left of the component. To do this, we use a recursive CTE query.

```
WITH RECURSIVE page_numbers AS (
Expand All @@ -155,12 +153,11 @@ WITH RECURSIVE page_numbers AS (
WHERE number < CAST($pages_count AS INTEGER)
LIMIT CAST($MAX_PAGES AS INTEGER)
)
SELECT
SELECT
number AS contents,
sqlpage.link(sqlpage.path(), json_object(''page'', number, ''idx_page'', $idx_page)) as link,
(number = CAST($page AS INTEGER)) AS active
sqlpage.link(sqlpage.path(), json_object('page', number, 'idx_page', $idx_page)) as link,
(number = CAST($page AS INTEGER)) AS active
FROM page_numbers;
```

If the added page matches the content of the `page` variable, the `active` option is set to `TRUE` so that the user knows it is the current page.
');
Loading
Loading