SQLPage built-in functions
In addition to normal SQL functions supported by your database, SQLPage provides a few special functions to help you extract data from user requests.
These functions are special, because they are not executed inside your database, but by SQLPage itself before sending the query to your database. Thus, they require all the parameters to be known at the time the query is sent to your database. Function parameters cannot reference columns from the rest of your query.
SQLPage functions
The sqlpage.run_sql function
Introduced in SQLPage 0.20.0.
Executes another SQL file and returns its result as a JSON array.
Example
Include a common header in all your pages
It is common to want to run the same SQL queries at the beginning of all your pages,
to check if an user is logged in, render a header, etc.
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.
select 'dynamic' as component, sqlpage.run_sql('common_header.sql') as properties;
Notes
- 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 8. - security: be careful when using
run_sql
to include files. Never userun_sql
with a user-provided parameter. Never run a file uploaded by a user, or a file that is not under your control. - variables: the included file will have access to the same variables (URL parameters, POST variables, etc.) as the calling file. It will not have access to uploaded files. 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.