load_table_schema

command
v3.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

LoadTableSchema Example

This example demonstrates how to use the LoadTableSchema functionality to dynamically load PostgreSQL table schemas and use them with CEL expressions.

Features Demonstrated

  • Static Schema Definition: Manual definition of PostgreSQL table schemas
  • Dynamic Schema Loading: Loading table schemas directly from a PostgreSQL database
  • CEL Integration: Using loaded schemas in CEL expressions
  • Type Conversion: Converting CEL expressions to SQL conditions

Running the Example

Prerequisites
  • Go 1.24 or later
  • PostgreSQL database (for dynamic schema loading example)
Static Schema Example
go run main.go

This will demonstrate CEL to SQL conversion using a pre-defined schema.

Dynamic Schema Example
  1. Set up a PostgreSQL database with a users table:
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL,
    age INTEGER,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN DEFAULT TRUE,
    tags TEXT[]
);
  1. Update the connection string in main.go:
connStr := "postgres://user:password@localhost:5432/mydb?sslmode=disable"
  1. Uncomment the dynamic schema example call in main():
// exampleWithDynamicSchema(ctx)
  1. Run the example:
go run main.go

Sample Output

=== Example 1: Pre-defined Schema ===
CEL: user.name == "John Doe"
SQL: user.name = 'John Doe'

CEL: user.age > 30 && user.is_active
SQL: user.age > 30 AND user.is_active

CEL: user.email.contains("@example.com")
SQL: POSITION('@example.com' IN user.email) > 0

CEL: "admin" in user.tags
SQL: 'admin' = ANY(user.tags)

CEL: user.created_at > timestamp("2023-01-01T00:00:00Z")
SQL: user.created_at > '2023-01-01T00:00:00Z'

Key Benefits

  1. Schema Flexibility: Adapt to changing database schemas without code changes
  2. Type Safety: CEL provides compile-time type checking
  3. SQL Generation: Automatic conversion to PostgreSQL-compatible SQL
  4. PostgreSQL Features: Full support for arrays, timestamps, and other PostgreSQL types

Use Cases

  • Dynamic query builders
  • API filtering systems
  • Configuration-driven data access
  • Multi-tenant applications with varying schemas

Documentation

Overview

Package main demonstrates loading table schema dynamically from PostgreSQL.

Jump to

Keyboard shortcuts

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