utils

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2025 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package utils provides common utility functions used throughout the Housekeeper codebase.

This package contains shared utilities that are used by multiple packages to avoid code duplication and ensure consistent behavior across the application.

Identifier Utilities (identifier.go)

The identifier utilities provide consistent handling of ClickHouse SQL identifiers, including proper backtick quoting for names that may contain special characters or reserved keywords.

Value Type Utilities (validation.go)

The value type utilities provide proper validation for different data types commonly used in ClickHouse SQL generation, particularly for named collection parameters and other configuration values.

## BacktickIdentifier

The primary function for adding backticks to identifiers, handling both simple and qualified names:

// Simple identifier
name := utils.BacktickIdentifier("users")
// Result: `users`

// Qualified identifier
qualified := utils.BacktickIdentifier("analytics.events")
// Result: `analytics`.`events`

// Already backticked (not double-backticked)
existing := utils.BacktickIdentifier("`users`")
// Result: `users`

## BacktickQualifiedName

Specialized function for database-qualified names, commonly used for tables, views, and dictionaries:

db := "analytics"
table := "events"
qualified := utils.BacktickQualifiedName(&db, table)
// Result: `analytics`.`events`

// Without database prefix
simple := utils.BacktickQualifiedName(nil, "users")
// Result: `users`

## Helper Functions

Additional utilities for working with backticked identifiers:

// Check if already backticked
if utils.IsBackticked(name) {
	// Already has backticks
}

// Remove backticks
clean := utils.StripBackticks("`database`.`table`")
// Result: database.table

## IsNumericValue and IsBooleanValue

Value type validation functions for properly formatting SQL values:

// Validate numeric values (uses strconv.ParseFloat)
if utils.IsNumericValue("123.45") {
	// Can be used without quotes in SQL
}

// Validate boolean values (case-insensitive)
if utils.IsBooleanValue("TRUE") {
	// Can be used without quotes in SQL
}

// Example usage for SQL value formatting
value := "123.45"
if utils.IsNumericValue(value) || utils.IsBooleanValue(value) {
	sql += value // No quotes needed
} else {
	sql += "'" + value + "'" // Add quotes for string values
}

Usage Guidelines

These utilities should be used whenever generating or manipulating SQL identifiers to ensure consistent formatting across all generated DDL statements. They handle edge cases like:

  • Identifiers that are already backticked
  • Qualified names with multiple parts (database.schema.table)
  • Empty strings and nil pointers
  • Special characters in identifiers

The utilities are designed to be safe and idempotent - calling BacktickIdentifier on an already backticked identifier will not double-backtick it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BacktickIdentifier

func BacktickIdentifier(name string) string

BacktickIdentifier adds backticks around an identifier, handling nested identifiers. It properly handles database.table.column style identifiers by backticking each part.

Examples:

  • "table" -> "`table`"
  • "database.table" -> "`database`.`table`"
  • "db.schema.table" -> "`db`.`schema`.`table`"
  • "`table`" -> "`table`" (already backticked, not double-backticked)
  • "" -> ""

This function is used throughout the codebase for consistent identifier formatting in generated DDL statements.

func BacktickQualifiedName

func BacktickQualifiedName(database *string, name string) string

BacktickQualifiedName formats a qualified name (database.name) with proper backticks. If database is nil or empty, only the name is backticked.

Examples:

  • ("analytics", "events") -> "`analytics`.`events`"
  • (nil, "events") -> "`events`"
  • ("", "events") -> "`events`"

This is commonly used for formatting table, view, and dictionary names that may include a database prefix.

func IsBackticked

func IsBackticked(s string) bool

IsBackticked checks if a string is already wrapped in backticks.

Examples:

  • "`table`" -> true
  • "table" -> false
  • "`db`.`table`" -> false (qualified name, not a single backticked identifier)
  • "" -> false

func IsBooleanValue

func IsBooleanValue(value string) bool

IsBooleanValue checks if a string represents a boolean value. This is case-insensitive and supports various boolean representations.

Examples:

  • "true" -> true
  • "TRUE" -> true
  • "True" -> true
  • "false" -> true
  • "FALSE" -> true
  • "1" -> false (use IsNumericValue for numeric booleans)
  • "yes" -> false
  • "" -> false

func IsNumericValue

func IsNumericValue(value string) bool

IsNumericValue checks if a string represents a valid numeric value. This uses strconv.ParseFloat to properly validate numeric formats, including integers, floats, and scientific notation.

Examples:

  • "123" -> true
  • "123.45" -> true
  • "-123.45" -> true
  • "1.23e-4" -> true (scientific notation)
  • "1.23E+5" -> true (scientific notation)
  • "abc" -> false
  • "1.2.3" -> false (multiple decimal points)
  • "" -> false
  • "." -> false
  • "-" -> false

func StripBackticks

func StripBackticks(s string) string

StripBackticks removes backticks from an identifier if present.

Examples:

  • "`table`" -> "table"
  • "table" -> "table"
  • "`db`.`table`" -> "db.table"
  • "" -> ""

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL