Skip to content

Commit d745a18

Browse files
committed
add an example to the run_sql docs
see #1214
1 parent 7f4dd3d commit d745a18

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

examples/official-site/sqlpage/migrations/38_run_sql.sql

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ VALUES (
1111
'Executes another SQL file and returns its result as a JSON array.
1212
1313
### Example
14-
14+
1515
#### Include a common header in all your pages
1616
1717
It is common to want to run the same SQL queries at the beginning of all your pages,
@@ -24,11 +24,31 @@ to include it in all your pages.
2424
select ''dynamic'' as component, sqlpage.run_sql(''common_header.sql'') as properties;
2525
```
2626
27+
#### Factorize logic between pages
28+
29+
Reuse a sqlpage query in multiple pages without duplicating code by storing the results of `run_sql` to variables:
30+
31+
##### `reusable.sql`
32+
33+
```sql
34+
select some_field from some_table;
35+
```
36+
37+
##### `index.sql`
38+
39+
```sql
40+
-- save the value of some_field from the first result row of reusable.sql into $my_var
41+
set my_var = sqlpage.run_sql(''reusable.sql'')->>0->>''some_field'';
42+
```
43+
44+
See [json in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide)
45+
for help with manipulating the json array returned by `run_sql`.
46+
2747
#### Notes
2848
2949
- **recursion**: you can use `run_sql` to include a file that itself includes another file, and so on. However, be careful to avoid infinite loops. SQLPage will throw an error if the inclusion depth is superior to `max_recursion_depth` (10 by default).
30-
- **security**: be careful when using `run_sql` to include files.
31-
- Never use `run_sql` with a user-provided parameter.
50+
- **security**: be careful when using `run_sql` to include files.
51+
- Never use `run_sql` with a user-provided parameter.
3252
- Never run a file uploaded by a user, or a file that is not under your control.
3353
- Remember that users can also run the files you include with `sqlpage.run_sql(...)` directly just by loading the file in the browser.
3454
- Make sure this does not allow users to bypass security measures you put in place such as [access control](/component.sql?component=authentication).
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# The documentation site is fully static, so we don't need to persist any data.
2-
#database_url: "sqlite::memory:?cache=shared"
2+
database_url: "sqlite::memory:?cache=shared"
33

4-
# We have a file upload example, and would like to limit the size of the uploaded files
4+
# We have a file upload example, and would like to limit the size of the uploaded files
55
max_uploaded_file_size: 256000
66

77
database_connection_acquire_timeout_seconds: 30

0 commit comments

Comments
 (0)