Introduction

The logic blocks supports conditional execution through the use of if and else, allowing dynamic SQL code to be executed only when certain conditions are met.

Syntax

The syntax of these conditions is as follows:

Simple Conditions

Includes the code only if the condition is true, without an alternative.

{#if param('user_id') > 100}
    -- ...
{/if}

Conditions with Else

Allow two content alternatives. If the condition is not true, the content defined after {:else} will be included.

{#if param('user_id') > 100}
    /* Your code here */
{:else}
    /* Your code here */
{/if}

Multiple Conditions

Allow unlimited content alternatives. It evaluates conditions cascading until one is met. If none is met, it returns the content defined after {:else}.

{#if param('user_id') > 100}
    /* Your code here */
{:else if param('user_id') > 50}
    /* Your code here */
{:else if param('user_id') > 25}
    /* Your code here */
{:else}
    /* Your code here */
{/if}

Expressions

Expressions are crucial for determining the logic within if and else constructs. They can evaluate variables, constants, or any combination of the two using a variety of operators.

Values

Values can be inside expressions:

  • Query parameters: Values passed to the query. For example, param('user_id') could be a query parameter representing a user’s ID. Check out Query Parameters for more information.
    {#if param('user_id') == 64} 
    
  • Variables/Parameters: Variables can be defined in the query and used in expressions. For example, id could be a variable representing a user’s ID. Check out Parameters for more information.
    {id = param('user_id')}
    {max_id = 100}
    
    {#if id > max_id} 
      /* Your code here */
    {/if}
    

Operations

Complex operations can be performed within expressions:

  • Arithmetic Operations: Basic arithmetic operations can be performed within expressions.

    {#if param('user_id') * 2 > 100}
    
  • String Operations: String operations can be performed within expressions.

    {#if param('username').length > 5}
    {#if param('username').startsWith('vip-')}
    

Comparisons

Expressions are compared with operators, which include the following options:

  • Equality and Inequality:
    • ==: Equal to
      {#if id == 100}
      
    • !=: Not equal to
      {#if id != 100}
      
  • Greater Than or Less Than:
    • >: Greater than
      {#if id > 100}
      
    • <: Less than
      {#if id < 100}
      
    • <=: Less than or equal to
      {#if id <= 100}
      
    • >=: Greater than or equal to
      {#if id >= 100}
      
  • Multiple conditions:
    • &&: Logical AND to concatenate multiple conditions in the same expression. They must all be true to continue.
      {#if id > 100 && workspace_id = 45}
      
    • ||: Logical OR to concatenate multiple conditions in the same expression. At least one of them must be true to continue.
      {#if id > 100 || workspace_id = 45}