Skip to main content
SQLPage allows you to access HTTP request data directly in your SQL queries through SQL parameters. This page explains how parameters work and the different types available.

Parameter Types

SQLPage supports three parameter syntaxes:

$param

GET or SET variables
WHERE id = $user_id
Accesses URL parameters or SET variables

:param

POST or SET variables
VALUES (:username, :email)
Accesses form data or SET variables

?param

GET only
WHERE category = ?filter
Accesses URL parameters exclusively

Parameter Resolution Order

SELECT $user_id as id;
Resolution order:
  1. Check SET variables (from SET statements)
  2. Check URL query parameters
  3. Return NULL if not found
Example:
GET /page.sql?user_id=42
-- $user_id = '42'
Deprecation Notice: Using $param to access POST variables is deprecated. Use :param explicitly for form data.

How Parameter Extraction Works

SQLPage uses sqlparser-rs to parse SQL and extract parameters:

Parsing Example

SELECT 
    name,
    email
FROM users
WHERE id = $user_id
  AND status = :status
  AND role IN ($role1, $role2);

Common Parameter Patterns

URL Query Parameters

users.sql
-- URL: /users.sql?id=42&role=admin

SELECT 'card' as component;

SELECT 
    name as title,
    email as description
FROM users
WHERE id = $id        -- Gets '42'
  AND role = $role;   -- Gets 'admin'

Form Submissions

create_user.sql
-- Display form
SELECT 'form' as component,
       'Create User' as title,
       'create_user.sql' as action;

SELECT 'text' as type, 'username' as name;
SELECT 'email' as type, 'email' as name;

-- Process form submission
INSERT INTO users (username, email, created_at)
SELECT :username, :email, CURRENT_TIMESTAMP
WHERE :username IS NOT NULL;

-- Redirect after insert
SELECT 'redirect' as component,
       '/users.sql' as link
WHERE :username IS NOT NULL;

Conditional Logic

products.sql
SELECT 'table' as component;

SELECT 
    product_name,
    price,
    category
FROM products
WHERE 
    ($category IS NULL OR category = $category)
    AND ($min_price IS NULL OR price >= CAST($min_price AS DECIMAL))
    AND ($max_price IS NULL OR price <= CAST($max_price AS DECIMAL))
    AND ($search IS NULL OR product_name LIKE '%' || $search || '%');
Use $param IS NULL to make filters optional. When the parameter isn’t provided, the condition is ignored.

SET Variables

Store query results in variables for later use:
-- Store a value
SET user_name = SELECT name FROM users WHERE id = $id;

-- Use the variable
SELECT 'text' as component,
       'Welcome, ' || $user_name as contents;

SET vs Parameters

FeatureGET/POST ParametersSET Variables
SourceHTTP requestQuery results
ScopeEntire requestAfter SET statement
MutabilityRead-onlyCan be overwritten
TypeAlways stringPreserves SQL type

SQLPage Functions

Access request metadata and utilities:
SELECT 
    sqlpage.request_method() as method,
    sqlpage.path() as path,
    sqlpage.protocol() as protocol,
    sqlpage.header('User-Agent') as user_agent;
Access HTTP request details:
  • sqlpage.request_method() - GET, POST, PUT, DELETE
  • sqlpage.path() - Current URL path
  • sqlpage.header(name) - HTTP header value
SELECT 
    sqlpage.cookie('session_id') as session,
    sqlpage.basic_auth_username() as username,
    sqlpage.basic_auth_password() as password;
User identification:
  • sqlpage.cookie(name) - Cookie value
  • sqlpage.basic_auth_username() - HTTP Basic Auth username
  • sqlpage.basic_auth_password() - HTTP Basic Auth password
-- Read uploaded file
SET file_contents = sqlpage.uploaded_file_path('avatar');

-- Read file from disk
SET config = sqlpage.read_file_as_text('config.json');

-- Save uploaded file
SELECT sqlpage.exec('mv', 
    sqlpage.uploaded_file_path('document'),
    '/uploads/' || :filename
);
File operations:
  • sqlpage.uploaded_file_path(name) - Temporary path to uploaded file
  • sqlpage.read_file_as_text(path) - Read file contents
  • sqlpage.read_file_as_data_url(path) - Read as data URL
-- Hash password
SET password_hash = sqlpage.hash_password(:password);

-- Generate random token
SET session_token = sqlpage.random_string(32);

-- URL encode
SET encoded = sqlpage.url_encode($user_input);
Security functions:
  • sqlpage.hash_password(text) - Bcrypt password hash
  • sqlpage.random_string(length) - Cryptographically secure random string
  • sqlpage.url_encode(text) - URL-safe encoding

Parameter Safety & SQL Injection

SQLPage automatically protects against SQL injection:
-- ✅ SAFE: Parameters are escaped
SELECT * FROM users WHERE id = $id;

-- Even if $id = "1 OR 1=1", query becomes:
SELECT * FROM users WHERE id = '1 OR 1=1';
-- (No injection possible)
SQLPage uses prepared statements, so parameters are always escaped.

Type Casting

Parameters are strings by default. Cast them to the appropriate type:
-- Numeric comparisons
WHERE price > CAST($min_price AS DECIMAL)
  AND quantity >= CAST($min_qty AS INTEGER);

-- Date comparisons  
WHERE created_at >= CAST($start_date AS DATE)
  AND created_at <= CAST($end_date AS DATE);

-- Boolean (database-specific)
WHERE active = CAST($is_active AS BOOLEAN);  -- PostgreSQL
WHERE active = CASE WHEN $is_active = 'true' THEN 1 ELSE 0 END;  -- SQLite/MySQL
SQLPage casts parameters to TEXT/VARCHAR when binding to the database. Explicit casting ensures correct comparisons.

Array Parameters

Handle multi-value parameters from checkboxes or multi-selects:
SELECT 'form' as component;

SELECT 'checkbox' as type,
       'tags[]' as name,
       tag_id as value,
       tag_name as label
FROM tags;

Advanced Patterns

Computed Function Parameters

-- SQLPage functions can take computed values
SET username = sqlpage.header('X-User-Name');
SET user_hash = sqlpage.hash_password(
    COALESCE(:password, sqlpage.random_string(16))
);

Nested Function Calls

-- Functions within functions
SET encoded_json = sqlpage.url_encode(
    json_object(
        'user', :username,
        'token', sqlpage.random_string(32)
    )
);

Conditional Parameter Usage

-- Different queries based on parameter
SELECT 'table' as component;

SELECT * FROM admin_users
WHERE $view = 'admin'

UNION ALL

SELECT * FROM regular_users  
WHERE $view != 'admin' OR $view IS NULL;

Best Practices

-- Check required parameters
SELECT 'redirect' as component,
       '/error.sql?msg=Missing user ID' as link
WHERE $user_id IS NULL;

-- Validate parameter format
SELECT 'redirect' as component,
       '/error.sql?msg=Invalid email' as link
WHERE :email NOT LIKE '%@%';
  • $param for URL/GET parameters
  • :param for form/POST data
  • ?param when you specifically need GET-only
  • SET variables for computed values
-- Provide defaults
SELECT COALESCE($page_size, 20) as page_size;

-- Optional filters
WHERE ($category IS NULL OR category = $category);

-- Required parameters
WHERE $user_id IS NOT NULL;
-- Always cast when comparing numbers
WHERE price > CAST($min AS DECIMAL);

-- Validate numeric input
WHERE $id ~ '^[0-9]+$'  -- PostgreSQL regex
   OR $id GLOB '[0-9]*' -- SQLite

Next Steps

Components

Learn about component rendering

Functions Reference

Explore all SQLPage functions

Security

Best practices for secure applications

Examples

See parameters in real applications

Build docs developers (and LLMs) love