You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/official-site/sqlpage/migrations/38_run_sql.sql
+23-3Lines changed: 23 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ VALUES (
11
11
'Executes another SQL file and returns its result as a JSON array.
12
12
13
13
### Example
14
-
14
+
15
15
#### Include a common header in all your pages
16
16
17
17
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.
24
24
select ''dynamic'' as component, sqlpage.run_sql(''common_header.sql'') as properties;
25
25
```
26
26
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
+
27
47
#### Notes
28
48
29
49
- **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.
32
52
- Never run a file uploaded by a user, or a file that is not under your control.
33
53
- Remember that users can also run the files you include with `sqlpage.run_sql(...)` directly just by loading the file in the browser.
34
54
- Make sure this does not allow users to bypass security measures you put in place such as [access control](/component.sql?component=authentication).
0 commit comments