Skip to content

Commit 4360010

Browse files
committed
document run_sql
1 parent 93c7cf2 commit 4360010

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
INSERT INTO sqlpage_functions (
2+
"name",
3+
"introduced_in_version",
4+
"icon",
5+
"description_md"
6+
)
7+
VALUES (
8+
'run_sql',
9+
'0.20.0',
10+
'login',
11+
'Executes another SQL file and returns its result as a JSON array.
12+
13+
### Example
14+
15+
#### Include a common header in all your pages
16+
17+
It is common to want to run the same SQL queries at the beginning of all your pages,
18+
to check if an user is logged in, render a header, etc.
19+
You can create a file called `common_header.sql`, and use the `dynamic` component with the `run_sql` function to include it in all your pages.
20+
21+
```sql
22+
select ''dynamic'' as component, sqlpage.run_sql(''common_header.sql'') as contents;
23+
```
24+
25+
#### Notes
26+
27+
- **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 it detects a loop.
28+
- **security**: be careful when using `run_sql` to include files. Never use `run_sql` with a user-provided parameter. Never run a file uploaded by a user, or a file that is not under your control.
29+
- **variables**: the included file will have access to the same variables (URL parameters, POST variables, etc.)
30+
as the calling file. It will not have access to uploaded files.
31+
If the included file changes the value of a variable or creates a new variable, the change will not be visible in the calling file.
32+
'
33+
);
34+
INSERT INTO sqlpage_function_parameters (
35+
"function",
36+
"index",
37+
"name",
38+
"description_md",
39+
"type"
40+
)
41+
VALUES (
42+
'run_sql',
43+
1,
44+
'file',
45+
'Path to the SQL file to execute, relative to the WEB ROOT (the root folder of your website).',
46+
'TEXT'
47+
);

0 commit comments

Comments
 (0)