Architecture Overview
SQLPage operates as a complete web application stack in a single executable:Detailed Architecture Diagram
Request-Response Flow
1. Request Reception
When SQLPage receives an HTTP request:- Method: GET, POST, PUT, DELETE, etc.
- Path:
/users.sqlor/api/data.json - Query parameters:
?id=42&filter=active - POST data: Form submissions, JSON payloads
- Headers: Cookies, authentication, content-type
2. Routing & File Discovery
The routing system determines which SQL file to execute based on the URL path. See Routing for details.3. SQL Parsing & Parameter Extraction
- Original SQL
- After Parsing
$param- GET/URL parameters (or SET variables):param- POST/form parameters (or SET variables)?param- GET parameters only
- Extract parameters - Replace
$id,:namewith database-specific placeholders ($1,?,@p1) - Identify SQLPage functions - Detect
sqlpage.function()calls for special handling - Validate syntax - Ensure queries are well-formed before execution
SQLPage transforms your SQL parameters into database-specific placeholders:
- PostgreSQL:
$1,$2,$3 - MySQL/ODBC:
?,?,? - SQL Server:
@p1,@p2,@p3 - SQLite:
?1,?2,?3
4. Query Execution
Each SQL statement in your file executes sequentially:- Statements run in order, top to bottom
- Results stream immediately (no buffering)
- Errors stop execution by default
- Transactions can span multiple statements
5. Component Rendering
Query results are mapped to UI components:- First result row defines the component (e.g.,
'list' as component) - Top-level properties from first row configure the component
- Subsequent rows become items within that component
- New component selection closes previous component and starts new one
6. Response Generation
HTML Response (Default)
HTML Response (Default)
JSON Response
JSON Response
?_sqlpage_format=json or use the json component.CSV Response
CSV Response
csv component for data exports.Streaming Architecture
SQLPage uses streaming throughout its architecture for optimal performance:Component Lifecycle
Each component follows a three-phase lifecycle:Component Initialization
First row with Renders the before_list template with top-level properties.
component column triggers creation:Row Iteration
Subsequent rows without Each row renders the list_content template.
component column are items:Database Connection Pooling
SQLPage maintains a connection pool using sqlx:- Pool size: Configurable via
max_database_pool_size - Lazy acquisition: Connections acquired only when needed
- Connection reuse: Single connection per request by default
- Transaction support:
BEGINstarts transaction,COMMIT/ROLLBACKends it
Error Handling
When errors occur during execution:- Development Mode
- Production Mode
- Error message and stack trace
- SQL query with syntax highlighting
- Line number and position
- Suggestions for fixing
environment = "production" in configuration.
Performance Optimizations
SQLPage employs several optimizations:| Optimization | Description |
|---|---|
| Prepared Statements | Queries cached and reused with different parameters |
| Streaming | Results sent as they arrive, no buffering |
| File Caching | Parsed SQL files cached in memory |
| Template Compilation | Handlebars templates pre-compiled |
| Connection Pooling | Database connections reused across requests |
Next Steps
Routing
Learn how URLs map to SQL files
Components
Understand the component rendering system
SQL Parameters
Master parameter handling and types
Configuration
Configure SQLPage for your needs