Advanced Control Structures in SQL

Learn how to implement template engine-like functionality securely using Codatum's parameter features

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

Most of these cases can be addressed by utilizing standard SQL syntax. This article introduces methods for implementing advanced control structures in Codatum.

Before reading this section, please read the Parameter and Escape and Dynamic Reference sections.

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.

1. Conditional Data Access (If/Else)

Controls which fields or data to SELECT based on parameters.

Use Cases

  • 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

Codatum Approach

Use CASE expressions or UNION with parameters:

Template Engine Approach (not in Codatum)

For reference, this would be implemented in a template engine (Jinja2) as:

2. Optional Filter Conditions (If/Else)

Controls which conditions to include in WHERE clause based on parameters.

Use Cases

  • Allow users to combine search conditions flexibly

  • Change report filters dynamically

  • Apply filters only when specific conditions are provided

Codatum Approach

Use empty parameter handling:

Template Engine Approach (not in Codatum)

For reference, this would be implemented in a template engine (Jinja2) as:

3. Repeat by Multi Parameters (for Loop)

Apply repetition patterns using Multi Input/Select parameters.

Use Cases

  • Aggregate multiple metrics simultaneously

  • Display only user-selected metrics

  • Apply similar calculation logic to multiple columns

Codatum Approach

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

Template Engine Approach (not in Codatum)

For reference, this would be implemented in a template engine (Jinja2) as:

4. Dynamic Identifier Generation

Controls which table to query based on parameters.

Use Cases

  • Select tables by API key

  • Access date-partitioned tables

  • Generate schema or table names dynamically

Codatum Approach

Use quoted parameters:

Note: In BigQuery connections, date parameters in table names are automatically formatted as YYYYMMDD.

Template Engine Approach (not in Codatum)

For reference, this would be implemented in a template engine (Jinja2) as:

5. Macro/Function Reuse

Encapsulates reusable calculation logic into functions.

Use Cases

  • Standardize domain-specific calculation logic

  • Reuse calculation logic across multiple queries

  • Encapsulate complex conditional calculations

Codatum Approach (Using BigQuery UDF)

UDFs are database-specific features, and notation and capabilities differ between databases.

Template Engine Approach (not in Codatum)

For reference, this would be implemented in a template engine (Jinja2) as:

Benefits of Codatum's Approach

While template engines offer more flexibility, Codatum's parameter-based approach provides:

  1. SQL injection protection through proper escaping

  2. Maintainable SQL structure

  3. Better query analysis and optimization

  4. Type safety for parameters

  5. 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

Last updated

Was this helpful?