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 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.
  • variables: the included file will have access to the same variables (URL parameters, POST variables, etc.) as the calling file. 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.

Parameters

file

Path to the SQL file to execute, can be absolute, or relative to the web root (the root folder of your website sql files). In-database files, from the sqlpage_files(path, contents, last_modified) table are supported.

parameters

Optional JSON object to pass as parameters to the included SQL file. The keys of the object will be available as variables in the included file. By default, the included file will have access to the same variables as the calling file.