Skip to main content

transform

transform.go - Overview

This file defines the QueryTransformer type and its associated methods for transforming ClickHouse SQL queries based on provided variable values. It handles replacing variable placeholders in the SQL query with parameterized variables and determines how to handle filters based on variable information.

Detailed Documentation

VariableValue

type VariableValue struct {
Name string
Values []string
IsSelectAll bool
FieldType string
}
  • Purpose: Represents a variable's assigned value.
  • Fields:
    • Name: The name of the variable (string).
    • Values: A slice of string values assigned to the variable.
    • IsSelectAll: A boolean indicating whether "select all" is chosen for the variable.
    • FieldType: A string representing the field type of the variable (e.g., "scalar", "array", "map").

QueryTransformer

type QueryTransformer struct {
processor *QueryProcessor
variables map[string]VariableValue
originalSQL string
}
  • Purpose: Handles the transformation of queries based on variable values.
  • Fields:
    • processor: A pointer to a QueryProcessor instance, used for processing the SQL query.
    • variables: A map of variable names to their corresponding VariableValue.
    • originalSQL: The original SQL query string.

NewQueryTransformer

func NewQueryTransformer(sql string, variables []VariableValue) *QueryTransformer
  • Purpose: Creates a new QueryTransformer instance. It replaces variable placeholders in the SQL query with parameterized variables (e.g., {{variable_name}} with $variable_name).
  • Parameters:
    • sql: The original SQL query string (string).
    • variables: A slice of VariableValue structs representing the variables to be used in the transformation.
  • Returns: A pointer to a new QueryTransformer instance.

Transform

func (t *QueryTransformer) Transform() (string, error)
  • Purpose: Processes the query and returns a transformed version.
  • Parameters: None
  • Returns:
    • string: The transformed SQL query.
    • error: An error object, if any error occurred during the transformation.

transformFilter

func (t *QueryTransformer) transformFilter(variableName string, expr parser.Expr) FilterAction
  • Purpose: Determines the action to take for a given filter based on the variable's information.
  • Parameters:
    • variableName: The name of the variable associated with the filter (string).
    • expr: The parser.Expr representing the filter expression.
  • Returns: A FilterAction indicating what to do with the filter (e.g., KeepFilter, RemoveFilter, ReplaceWithExistsCheck).

Code Examples

None

Clarity and Accuracy

The documentation is based on the code provided, focusing on the purpose, parameters, and returns of each function and struct.

Include in Getting Started: NO