Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## unreleased

- A table row action without a link leaves its cell empty. ([#1709](https://github.com/sqlpage/SQLPage/pull/1709))
- SQLPage reports syntax errors before running any statements in the affected SQL file. ([#1706](https://github.com/sqlpage/SQLPage/discussions/1706))
- OIDC logs focus on configuration and failures. SQLPage omits tokens, cookie values, user claims, and callback and logout query strings from OIDC diagnostics.
- Standard alert descriptions have higher text contrast.
Expand Down
2 changes: 1 addition & 1 deletion examples/official-site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Name each example with a zero-padded number and a short, hyphenated title. Keep

- **[`code/01-display-html-source.sql`](reference/components/code/01-display-html-source.sql)** runs inline. Start with a block comment containing a title and optional Markdown. The SQL after the comment supplies the copyable source, preview, and direct URL.
- **[`shell/01-dark-shell-with-navigation-menus.standalone.sql`](reference/components/shell/01-dark-shell-with-navigation-menus.standalone.sql)** runs only when its direct URL is opened. Use this form when the example controls the page shell or HTTP response.
- **[`cookie/01-set-and-read-a-cookie.md`](reference/components/cookie/01-set-and-read-a-cookie.md)** displays a walkthrough. Start with a `# Title` and use fenced code blocks for SQL that needs an application database, changes state, or spans multiple files.
- **[`form/01-separate-submission-handler.md`](reference/components/form/01-separate-submission-handler.md)** displays a walkthrough. Start with a `# Title` and use fenced code blocks for SQL that needs an application database, changes state, or spans multiple files.

```sql
/*
Expand Down
12 changes: 10 additions & 2 deletions examples/official-site/component.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ select 'redirect' as component,
where not exists (select 1 from component where name = $component);

select 'http_header' as component,
'public, max-age=600, stale-while-revalidate=3600, stale-if-error=86400' as "Cache-Control",
case when $component in ('list', 'table') or sqlpage.request_method() <> 'GET' then 'private, no-store'
else 'public, max-age=600, stale-while-revalidate=3600, stale-if-error=86400' end as "Cache-Control",
printf('<%s>; rel="canonical"', sqlpage.link('component', json_object('component', $component))) as "Link";

select 'dynamic' as component, sqlpage.run_sql('sqlpage/docs-reference.sql', json_object(
Expand All @@ -13,11 +14,18 @@ select 'dynamic' as component, sqlpage.run_sql('sqlpage/docs-reference.sql', jso
select 'title' as component, 2 as level, 'Examples' as contents, 'examples' as id
where exists (select 1 from example where component = $component);

select 'text' as component,
'For examples that use sample tables, download the [SQLite sample data](/examples/reference/sample-data.sql.txt). In a fresh SQLPage app, run `sqlite3 sqlpage/sqlpage.db < sample-data.sql.txt` from the app directory. Save the example as a SQL page and point any links to that file.' as contents_md
where exists (
select 1 from example where component = $component
and (source glob '*demo_*' or source glob '*from customers*' or source glob '*from customer_feedback*' or source glob '*from tickets*')
);

select 'dynamic' as component, json_array(
json_object(
'component', 'docs-example', 'number', number, 'title', title,
'description_md', description, 'source', source,
'preview', preview
'preview', preview, 'standalone_url', case when not preview then '/' || path end
),
coalesce(sqlpage.run_sql(case when preview then path end), '[]'),
case when preview then json_object('component', 'docs-example', 'end_result', true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
-- delete expired sessions
delete from user_sessions where created_at < datetime('now', '-1 day');

-- check that the
-- Verify the password before creating a session.
SELECT 'authentication' AS component,
'login.sql?failed' AS link, -- redirect to the login page on error
(SELECT password_hash FROM users WHERE username = :username) AS password_hash, -- this is a hash of the password 'admin'
:password AS password; -- this is the password that the user sent through our form in 'index.sql'
'/examples/authentication/login.sql?failed' AS link, -- redirect to the login page on error
(SELECT password_hash FROM users WHERE username = :username) AS password_hash,
:password AS password;

-- if we haven't been redirected, then the password is correct
-- create a new session
insert into user_sessions (session_token, username) values (sqlpage.random_string(32), :username)
returning 'cookie' as component, 'session_token' as name, session_token as value;
-- Remembered sessions keep their browser cookie for up to one day.
set token = sqlpage.random_string(32);
insert into user_sessions (session_token, username) values ($token, :username);
select 'cookie' as component, 'session_token' as name, $token as value where :remember is null;
select 'cookie' as component, 'session_token' as name, $token as value, 86400 as max_age where :remember is not null;

-- redirect to the authentication example home page
select 'redirect' as component, '/examples/authentication' as link;
select 'redirect' as component, '/examples/authentication' as link;
2 changes: 1 addition & 1 deletion examples/official-site/examples/authentication/index.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- redirect the user to the login page if they are not logged in
-- this query should be present at the top of every page that requires authentication
set user_role = (select role from users natural join user_sessions where session_token = sqlpage.cookie('session_token'));
set user_role = (select role from users natural join user_sessions where session_token = sqlpage.cookie('session_token') and created_at >= datetime('now', '-1 day'));
select 'redirect' as component, 'login.sql' as link where $user_role is null;

select 'dynamic' as component,
Expand Down
6 changes: 6 additions & 0 deletions examples/official-site/examples/reference/entries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set token = sqlpage.cookie('docs_entry');
select 'form' as component, 'Your sample entry' as title, '/examples/reference/save-entry.sql' as action, 'Save entry' as validate;
select 'name' as name, 'Sample name' as label, true as required, 60 as maxlength,
(select name from demo_entries where token = $token) as value;
select 'list' as component, 'Your saved entry' as title, 'No saved entry yet' as empty_title;
select name as title from demo_entries where token = $token;
17 changes: 17 additions & 0 deletions examples/official-site/examples/reference/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const events = new EventSource(
"/reference/components/json/04-stream-server-sent-events.standalone.sql",
);
const customers = document.querySelector("#customers");
events.onmessage = (event) => {
const row = JSON.parse(event.data);
const item = document.createElement("li");
item.textContent = row.name;
customers.append(item);
};
events.onerror = () => {
events.close();
document.querySelector("#stream-status").textContent = customers.children
.length
? "Stream closed."
: "No customers received.";
};
4 changes: 4 additions & 0 deletions examples/official-site/examples/reference/events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select 'shell' as component, 'Stream of customers' as title,
'/examples/reference/events.js' as javascript_module;
select 'text' as component, 'Customers received from the stream' as title;
select 'html' as component, '<ol id="customers" aria-live="polite"></ol><p id="stream-status" role="status">Connecting…</p>' as html;
9 changes: 9 additions & 0 deletions examples/official-site/examples/reference/poem.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set poem = case when $action = 'saved' then sqlpage.cookie('docs_poem') else :poem end;
select 'cookie' as component, 'docs_poem' as name, :poem as value, false as secure
where $action = 'save' and :poem is not null and length(:poem) between 1 and 200;
select 'text' as component, case when $action in ('save', 'saved') then 'Saved poem' else 'Poem preview' end as title,
coalesce($poem, 'No poem saved yet.') as contents
where length(coalesce($poem, '')) between 1 and 200 or $action = 'saved';
select 'text' as component, 'Enter a poem of 1 to 200 characters.' as contents
where $action in ('save', 'preview') and length(coalesce($poem, '')) not between 1 and 200;
select 'text' as component, '[Read the saved poem](/examples/reference/poem.sql?action=saved) | [Write another poem](/component.sql?component=button#example-7)' as contents_md;
21 changes: 21 additions & 0 deletions examples/official-site/examples/reference/record.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set states = case when json_valid(sqlpage.cookie('docs_projects')) then sqlpage.cookie('docs_projects') else '{}' end;
set record_id = (select id from demo_projects where id = cast($id as integer));
set key = '$."' || $record_id || '"';
set state = coalesce(json_extract($states, $key), (select initial_status from demo_projects where id = $record_id));
set next_state = case when :operation = 'delete' then 'Removed' when :operation = 'use' then 'In use'
when :operation = 'restore' then 'Available' end;
select 'cookie' as component, 'docs_projects' as name, json_set($states, $key, $next_state) as value, false as secure
where $record_id is not null and $next_state is not null;
select 'redirect' as component, '/examples/reference/record.sql?id=' || $record_id as link
where $next_state is not null and $record_id is not null;

select 'text' as component, name as title, 'Status: ' || $state as contents from demo_projects where id = $record_id;
select 'form' as component, 'Apply to this project' as validate
where $record_id is not null;
select 'operation' as name, 'Action' as label, 'select' as type,
json_array(json_object('label', 'Start using', 'value', 'use'),
json_object('label', 'Remove', 'value', 'delete'),
json_object('label', 'Restore', 'value', 'restore')) as options,
case $action when 'delete' then 'delete' when 'restore' then 'restore' else 'use' end as value;
select 'text' as component,
'[Back to the list](/component.sql?component=list#example-4) | [Back to the table](/component.sql?component=table#example-11)' as contents_md;
9 changes: 9 additions & 0 deletions examples/official-site/examples/reference/save-entry.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
select 'redirect' as component, '/examples/reference/entries.sql' as link
where sqlpage.request_method() <> 'POST' or length(trim(coalesce(:name, ''))) not between 1 and 60;
set token = coalesce((select token from demo_entries where token = sqlpage.cookie('docs_entry')), sqlpage.random_string(32));
delete from demo_entries where updated_at < datetime('now', '-1 day');
insert into demo_entries(token, name) values ($token, trim(:name))
on conflict(token) do update set name = excluded.name, updated_at = current_timestamp;
select 'cookie' as component, 'docs_entry' as name, $token as value, 86400 as max_age,
false as secure;
select 'redirect' as component, '/examples/reference/entries.sql' as link;
4 changes: 4 additions & 0 deletions examples/official-site/examples/reference/second-page.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select 'dynamic' as component,
sqlpage.run_sql('examples/reference/shared-shell.sql', json_object('title', 'Second page')) as properties;
select 'text' as component, 'Two pages, one menu' as title,
'[Open the other page](/reference/components/dynamic/03-include-another-sql-file.standalone.sql) to see the same menu with a different page title.' as contents_md;
8 changes: 8 additions & 0 deletions examples/official-site/examples/reference/shared-shell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"component": "shell",
"title": "Shared JSON navigation",
"menu_item": [
{ "title": "Component catalog", "link": "/components.sql" },
{ "title": "SQL functions", "link": "/functions.sql" }
]
}
3 changes: 3 additions & 0 deletions examples/official-site/examples/reference/shared-shell.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select 'shell' as component, coalesce($title, 'Catalog') as title,
json_group_array(json_object('title', name, 'link', '/component.sql?component=' || name) order by name) as menu_item
from component where name in ('card', 'chart', 'map');
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# HTTP basic authentication and sessions

[Try signing in and out with the sample accounts](/examples/authentication/).

### Usage with HTTP basic authentication

The most basic usage of the authentication component is with the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@ Big numbers with change indicators and progress bars
*/
select 'big_number' as "component";

select
'Sales' as "title",
75 as "value",
'%' as "unit",
'#sales_dashboard' as "title_link",
TRUE as "title_link_new_tab",
'#sales_details' as "value_link",
FALSE as "value_link_new_tab",
'Conversion rate' as "description",
9 as "change_percent",
75 as "progress_percent",
'blue' as "progress_color";
select 'Resolved tickets' as title, count(*) as value, 'Goal: 50 tickets' as description,
round(100.0 * count(*) / 50) as progress_percent, 'blue' as progress_color
from tickets;

select
'Revenue' as "title",
'4,300' as "value",
'$' as "unit",
'Year on year' as "description",
-8 as "change_percent";
with daily as (
select date(resolved_at) as day, count(*) as total from tickets group by day
), changes as (
select day, total, lag(total) over (order by day) as previous from daily
)
select 'Latest daily resolutions' as title, total as value, day as description,
round(100.0 * (total - previous) / nullif(previous, 0)) as change_percent
from changes order by day desc limit 1;
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
/*
Big numbers with dropdowns and customized layout
*/
select
'big_number' as "component",
3 as "columns",
'colorfull_dashboard' as "id";

select
'Users' as "title",
'1,234' as "value",
'red' as "color",
'#users' as "title_link",
FALSE as "title_link_new_tab",
'#users_details' as "value_link",
TRUE as "value_link_new_tab";
Choose a revenue period

select
'Orders' as "title",
56 as "value",
'green' as "color",
'#orders' as "title_link",
TRUE as "title_link_new_tab";

select
'Revenue' as "title",
'9,876' as "value",
'€' as "unit",
'blue' as "color",
-7 as "change_percent",
JSON('{"label":"This week","link":"?days=7&component=big_number#colorfull_dashboard"}') as "dropdown_item",
JSON('{"label":"This month","link":"?days=30&component=big_number#colorfull_dashboard"}') as "dropdown_item",
JSON('{"label":"This quarter","link":"?days=90&component=big_number#colorfull_dashboard"}') as "dropdown_item";
The period ends on the most recent sample order. Choose a week, month or quarter to recalculate revenue and compare it with the preceding period of the same length.
*/
set days = case cast($days as integer) when 30 then 30 when 90 then 90 else 7 end;
select 'big_number' as component, 3 as columns;
select 'Customers' as title, count(*) as value from customers;
select 'Orders' as title, count(*) as value from demo_orders;
set latest = (select max(ordered_on) from demo_orders);
set period_start = date($latest, '-' || $days || ' days');
with choices(days, label) as (values (7, 'This week'), (30, 'This month'), (90, 'This quarter')),
revenue as (
select sum(case when ordered_on > $period_start then amount end) as current_total,
sum(case when ordered_on <= $period_start then amount end) as previous_total
from demo_orders where ordered_on > date($period_start, '-' || $days || ' days')
)
select 'Revenue' as title, current_total as value,
round(100.0 * (current_total - previous_total) / nullif(previous_total, 0)) as change_percent, '$' as unit,
'Last ' || $days || ' days of sample orders' as description,
json((select json_group_array(json_object(
'label', label,
'link', '/reference/components/big_number/02-dropdowns-and-layout.sql?days=' || days,
'active', json(iif(days = cast($days as integer), 'true', 'false'))
) order by days = cast($days as integer) desc, days) from choices)) as dropdown_item
from revenue;
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/*
Description of a link and selection of the current page
*/
select 'breadcrumb' as "component";

select
'Home' as "title",
'/' as "link",
TRUE as "active";

select
'Articles' as "title",
'/blog.sql' as "link",
'Stay informed with the latest news' as "description";
Follow a link to move through the library. The row whose position matches the selected section sets `active` to true and marks the current page.
*/
set current = min(3, max(1, coalesce(cast($section as integer), 3)));
select 'breadcrumb' as component;
with pages(position, name) as (values (1, 'Library'), (2, 'Articles'), (3, 'JSON in SQL'))
select name as title, position = cast($current as integer) as active,
'Open ' || name as description,
'/reference/components/breadcrumb/02-link-description-and-current-page.sql?section=' || position as link
from pages where position <= cast($current as integer) order by position;

select
'JSON in SQL' as "title",
'/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide' as "link",
'Learn advanced json functions for MySQL, SQLite, PostgreSQL, and SQL Server' as "description";
select 'text' as component, 'Current section: ' || case cast($current as integer)
when 1 then 'Library'
when 2 then 'Articles'
when 3 then 'JSON in SQL' end as contents;
select 'Go deeper in the library' as contents, '/reference/components/breadcrumb/02-link-description-and-current-page.sql?section=' || ($current + 1) as link where cast($current as integer) < 3;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Multiple buttons sending the same form to different pages.
We use `'' AS validate` to remove the submit button from inside the form itself,
and instead use the button component to submit the form to pages with different GET variables.

In the target page, we could then use the GET variable `$action` to determine what to do with the form data.
Preview displays the submitted poem. Save stores it in this browser, where the saved-poem link can read it on the next visit.
*/
select
'form' as "component",
Expand All @@ -15,19 +15,21 @@ select

select
'textarea' as "type",
'Poem' as "name",
'poem' as "name",
true as required,
200 as maxlength,
'Write a poem' as "placeholder";

select 'button' as "component";

select
'/examples/show_variables.sql?action=save' as "link",
'/examples/reference/poem.sql?action=save' as "link",
'poem' as "form",
'primary' as "color",
'Save' as "title";

select
'/examples/show_variables.sql?action=preview' as "link",
'/examples/reference/poem.sql?action=preview' as "link",
'poem' as "form",
'yellow' as "outline",
'Preview' as "title";
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ select
'/sqlpage_introduction_video.mp4' as "link",
'Download Video' as "title",
'download' as "icon",
'Introduction Video.webm' as "download",
'Introduction Video.mp4' as "download",
'nofollow' as "rel";
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,8 @@ A dashboard with Markdown descriptions

You can use cards to display a dashboard with quick access to important information. Use [markdown](https://www.markdownguide.org/basic-syntax) to format the text.
*/
select
'card' as "component",
4 as "columns";

select
'**152** sales today' as "description_md",
TRUE as "active",
'currency-euro' as "icon";

select
'**13** new users' as "description_md",
'user-plus' as "icon",
'green' as "color";

select
'**2** complaints' as "description_md",
'alert-circle' as "icon",
'danger' as "color",
'/examples/show_variables.sql?action=view_complaints' as "link",
'red-lt' as "background_color";

select
'**1** pending support request' as "description_md",
'mail-question' as "icon",
'warning' as "color";
select 'card' as component, 3 as columns;
select '**' || count(*) || '** active customers' as description_md, 'users' as icon,
true as active from customers where status = 'Active';
select '**' || sum(seats) || '** seats' as description_md, 'armchair' as icon from customers;
select '**' || count(*) || '** resolved tickets' as description_md, 'check' as icon from tickets;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ A basic example of carousel
*/
select
'carousel' as "component",
true as controls,
'cats1' as "name",
'Famous Database Animals' as "title";

Expand Down
Loading
Loading