Advanced Control Structures in SQL
Learn how to implement template engine-like functionality securely using Codatum's parameter features
Last updated
Learn how to implement template engine-like functionality securely using Codatum's parameter features
Last updated
There are situations where you might want to branch or repeat processes based on parameter input or reuse the same process functionally. In such cases, many SQL tools use template engines like Jinja to generate dynamic SQL queries.
However, in Codatum, security concerns such as SQL Injection are prioritized, and template engines cannot be used. By maintaining syntax parsing, Codatum ensures:
Accurate escaping based on context, reducing security risks
Easier debugging by pinpointing the exact location of errors
Context-based suggestions for improved usability
These benefits make syntax parsing essential for a secure and user-friendly experience in Codatum.
You should read also the Parameter Escaping: Security and Dynamic Identifier Support
section:
Most of these cases can be addressed by utilizing standard SQL syntax. This article introduces methods for implementing advanced control structures in Codatum.
Note: The Template Engine examples in this article are based on the syntax of the Jinja2/Jinja template language. Actual tools may use similar but different syntax or proprietary extensions.
Controls which fields or data to SELECT based on parameters.
Apply different business logic by region/country
Change data aggregation granularity dynamically
Restrict data access based on user permission levels
Provide different data views by department/team
Use CASE expressions or UNION with parameters:
For reference, this would be implemented in a template engine (Jinja2) as:
Controls which conditions to include in WHERE clause based on parameters.
Allow users to combine search conditions flexibly
Change report filters dynamically
Apply filters only when specific conditions are provided
Use empty parameter handling:
For reference, this would be implemented in a template engine (Jinja2) as:
Apply repetition patterns using Multi Input/Select parameters.
Aggregate multiple metrics simultaneously
Display only user-selected metrics
Apply similar calculation logic to multiple columns
Use Text Multi Input parameter with CASE expressions to control which metrics to aggregate:
While this approach requires more code than template engines, it provides better reliability and maintainability:
Each metric is explicitly defined, making it easier to detect errors at compile time
Column names and types can be validated by the SQL engine
Changes to table schema are less likely to break the query
For reference, this would be implemented in a template engine (Jinja2) as:
Controls which table to query based on parameters.
Select tables by API key
Access date-partitioned tables
Generate schema or table names dynamically
Use quoted parameters:
Note: In BigQuery connections, date parameters in table names are automatically formatted as YYYYMMDD.
You should read the Dynamic Identifier Support
section for further understanding:
For reference, this would be implemented in a template engine (Jinja2) as:
Encapsulates reusable calculation logic into functions.
Standardize domain-specific calculation logic
Reuse calculation logic across multiple queries
Encapsulate complex conditional calculations
UDFs are database-specific features, and notation and capabilities differ between databases.
For reference, this would be implemented in a template engine (Jinja2) as:
While template engines offer more flexibility, Codatum's parameter-based approach provides:
SQL injection protection through proper escaping
Maintainable SQL structure
Better query analysis and optimization
Type safety for parameters
Clear separation between dynamic values and SQL structure
When template engine functionality is required, consider:
Restructuring queries to use standard SQL features
Breaking complex queries into multiple simpler queries
Using CTEs for query organization
Leveraging database-specific features when available