squeel

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 12 Imported by: 0

README

Tests

Squeel 🔄

Squeel is a powerful SQL-to-MongoDB query translator that enables you to write MongoDB queries using familiar SQL syntax. It bridges the gap between SQL and MongoDB's query language, making it easier for developers familiar with SQL to work with MongoDB.

✨ Features

  • 🔄 Translates SQL SELECT queries to MongoDB operations
  • 🚀 Supports complex queries including:
    • JOIN operations with $lookup
    • Aggregate functions (COUNT, SUM, AVG, MIN, MAX)
    • WHERE clauses with multiple conditions
    • GROUP BY and HAVING clauses
    • ORDER BY for sorting
    • LIMIT and OFFSET for pagination
  • 🔧 Handles UUID fields automatically (converts to Binary for uppercase collections)
  • 📦 Supports subqueries and nested field queries
  • ⚡ Maintains MongoDB's native performance characteristics

📥 Installation

go get github.com/fanfactory/data/squeel

🚀 Quick Start

import "github.com/fanfactory/data/squeel"

// Initialize a new statement
sql := "SELECT name, age FROM users WHERE age > 21 ORDER BY name DESC LIMIT 10"
statement := squeel.NewStatement(sql)

// Build the query
query := squeel.NewQuery()
query, err := statement.Build(query)
if err != nil {
    log.Fatal(err)
}

// The resulting query object can be used with MongoDB driver

📖 Usage Examples

Basic Queries
-- Simple SELECT
SELECT * FROM users

-- Select with UUID matching (automatically handles Binary conversion)
SELECT * FROM User WHERE _id = '695FF995-5DC4-4FBE-B80C-2621360D578F'

-- Select specific fields with conditions
SELECT first_name FROM user_profile WHERE _id = '695FF995-5DC4-4FBE-B80C-2621360D578F'

-- Pagination
SELECT * FROM fanchecks LIMIT 10 OFFSET 2
Advanced Queries
-- JOIN with aggregation
SELECT u.name, p.city
FROM users u
JOIN profiles p ON u.id = p.user_id
WHERE u.age > 25

-- Complex aggregation with GROUP BY, HAVING, and ORDER BY
SELECT department, AVG(salary) as avg_salary
FROM employees
WHERE hire_date >= '2020-01-01'
GROUP BY department
HAVING AVG(salary) > 50000

-- Pattern matching and complex conditions
SELECT * FROM products
WHERE name LIKE '%phone%'
  AND (category = 'Electronics' OR category = 'Accessories')
  AND price BETWEEN 100 AND 500

-- Nested field queries
SELECT * FROM questions WHERE theme.nl = 'Some Theme'

🔧 Query Object Structure

Squeel translates SQL queries into a Query struct that can be used with the MongoDB driver:

type Query struct {
    Context    context.Context
    Comment    string
    Operation  string        // "find", "findone", "aggregate", "count", "distinct"
    Collection string
    Filter     bson.D
    Projection bson.D
    Sort       bson.D
    Limit      *int64
    Offset     *int64
    Pipeline   mongo.Pipeline
    Payload    bson.D
}
Query Operations
  • find: Regular SELECT queries
  • findone: SELECT with LIMIT 1
  • aggregate: Complex queries with JOIN, GROUP BY, or aggregation functions
  • count: COUNT queries
  • distinct: SELECT DISTINCT queries
Automatic UUID Handling

Squeel automatically handles UUID fields differently based on collection naming:

  • Uppercase collections (e.g., User): UUIDs are converted to MongoDB Binary type
  • Lowercase collections (e.g., users): UUIDs remain as strings

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

Special thanks to all contributors who have helped make Squeel better!

Documentation

Index

Constants

View Source
const (
	HivePartitioning = backend.HivePartitioning
	FlatPartitioning = backend.FlatPartitioning
)
View Source
const (
	Asc  = backend.Asc
	Desc = backend.Desc
)

Variables

View Source
var (
	NewMongoBackend            = backend.NewMongoBackend
	WithMongoURI               = backend.WithMongoURI
	WithMongoDatabase          = backend.WithMongoDatabase
	WithMongoTimeout           = backend.WithMongoTimeout
	WithMongoClient            = backend.WithMongoClient
	WithMongoDB                = backend.WithMongoDB
	NewS3Backend               = backend.NewS3Backend
	NewRadixS3Client           = backend.NewRadixS3Client
	WithBucket                 = backend.WithBucket
	WithBasePrefix             = backend.WithBasePrefix
	WithPartitionStyle         = backend.WithPartitionStyle
	WithPartitionKeys          = backend.WithPartitionKeys
	WithDefaultPartitionKeys   = backend.WithDefaultPartitionKeys
	WithAutoDetectPartitions   = backend.WithAutoDetectPartitions
	WithMaxDateRangePartitions = backend.WithMaxDateRangePartitions
	WithS3Client               = backend.WithS3Client
	WithEndpoint               = backend.WithEndpoint
	WithRegion                 = backend.WithRegion
	WithCredentials            = backend.WithCredentials
	WithAnonymous              = backend.WithAnonymous
	WithUsePathStyle           = backend.WithUsePathStyle
	WithHTTPTimeout            = backend.WithHTTPTimeout
	WithDownloader             = backend.WithDownloader
	WithUploader               = backend.WithUploader
	WithPrefetchConcurrency    = backend.WithPrefetchConcurrency
)

Functions

func CSUUID

func CSUUID(uuid string) (primitive.Binary, error)

CSUUID is a package-level function that converts a UUID string to a MongoDB

Binary type using a specific reordering pattern. It removes any curly braces and hyphens from the UUID, then reorders the bytes according to the required format before converting to base64 and creating a Binary value.

func Compile

func Compile[T any](stmt *Statement, b Backend[T]) (T, error)

Compile compiles a Statement using the provided typed backend adapter.

func Execute

func Execute[R any](ctx context.Context, stmt *Statement, e Executor[R]) (R, error)

Execute executes a Statement using the provided typed executor adapter.

func HexToBase64

func HexToBase64(in string) []byte

HexToBase64 is a package-level function that converts a hexadecimal string

to its base64 encoded representation. If the hex decoding fails, it returns an empty byte slice and logs the error.

func LogDebug

func LogDebug(format string, v ...any)

LogDebug writes a debug-level log message using the errnie logging framework.

Parameters:

  • format: A format string following fmt.Printf conventions
  • v: Variable arguments to be formatted according to the format string

func Map

func Map(d bson.D) bson.M

Map converts a bson.D to a bson.M.

Types

type AnyBackend

type AnyBackend = backend.AnyBackend

Type aliases for backend adapter interfaces and implementations.

type AnyExecutor

type AnyExecutor = backend.AnyExecutor

Type aliases for backend adapter interfaces and implementations.

type Backend

type Backend[T any] = backend.Backend[T]

Type aliases for backend adapter interfaces and implementations.

type BinaryExpr

type BinaryExpr = backend.BinaryExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type ColumnExpr

type ColumnExpr = backend.ColumnExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type Connector

type Connector = backend.Connector

Type aliases for backend adapter interfaces and implementations.

type Executor

type Executor[R any] = backend.Executor[R]

Type aliases for backend adapter interfaces and implementations.

type Expr

type Expr = backend.Expr

Type aliases for Plan and abstract AST expression types defined in backend package.

type FilterPredicate

type FilterPredicate = backend.FilterPredicate

Type aliases for backend adapter interfaces and implementations.

type FuncExpr

type FuncExpr = backend.FuncExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type Join

type Join = backend.Join

Type aliases for Plan and abstract AST expression types defined in backend package.

type ListExpr

type ListExpr = backend.ListExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type LiteralExpr

type LiteralExpr = backend.LiteralExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type LogicalExpr

type LogicalExpr = backend.LogicalExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type MongoBackend

type MongoBackend = backend.MongoBackend

Type aliases for backend adapter interfaces and implementations.

type MongoConfig

type MongoConfig = backend.MongoConfig

Type aliases for backend adapter interfaces and implementations.

type MongoOption

type MongoOption = backend.MongoOption

Type aliases for backend adapter interfaces and implementations.

type MongoResult

type MongoResult = backend.MongoResult

Type aliases for backend adapter interfaces and implementations.

type OrderDirection

type OrderDirection = backend.OrderDirection

Type aliases for Plan and abstract AST expression types defined in backend package.

type OrderItem

type OrderItem = backend.OrderItem

Type aliases for Plan and abstract AST expression types defined in backend package.

type Parser

type Parser struct{}

Parser parses SQL query strings into abstract, storage-agnostic Plan structures. It has zero dependencies on any database-specific driver (MongoDB, S3, etc.).

func NewParser

func NewParser() *Parser

NewParser creates a new Parser instance.

func (*Parser) Parse

func (p *Parser) Parse(raw string) (*Plan, error)

Parse parses a raw SQL query string into an abstract Plan.

type PartitionStyle

type PartitionStyle = backend.PartitionStyle

Type aliases for backend adapter interfaces and implementations.

type Plan

type Plan = backend.Plan

Type aliases for Plan and abstract AST expression types defined in backend package.

func NewPlan

func NewPlan(raw string) *Plan

NewPlan constructs an empty Plan.

func ParseSQL

func ParseSQL(raw string) (*Plan, error)

ParseSQL parses a raw SQL query string into an abstract Plan.

type Projection

type Projection = backend.Projection

Type aliases for Plan and abstract AST expression types defined in backend package.

type Query

type Query = backend.Query

Query is an alias to backend.Query.

func NewQuery

func NewQuery() *Query

NewQuery creates a new Query instance.

type QueryBuilder

type QueryBuilder = backend.QueryBuilder

Type aliases for backend adapter interfaces and implementations.

type RadixS3Client

type RadixS3Client = backend.RadixS3Client

Type aliases for backend adapter interfaces and implementations.

type RangeExpr

type RangeExpr = backend.RangeExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type S3Backend

type S3Backend = backend.S3Backend

Type aliases for backend adapter interfaces and implementations.

type S3ClientAPI

type S3ClientAPI = backend.S3ClientAPI

Type aliases for backend adapter interfaces and implementations.

type S3Config

type S3Config = backend.S3Config

Type aliases for backend adapter interfaces and implementations.

type S3Object

type S3Object = backend.S3Object

Type aliases for backend adapter interfaces and implementations.

type S3Option

type S3Option = backend.S3Option

Type aliases for backend adapter interfaces and implementations.

type S3Query

type S3Query = backend.S3Query

Type aliases for backend adapter interfaces and implementations.

type StarExpr

type StarExpr = backend.StarExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type Statement

type Statement struct {
	// contains filtered or unexported fields
}

Statement represents a SQL statement that can be parsed into an abstract Plan and compiled or executed across any backend implementing the adapter interfaces.

func NewStatement

func NewStatement(raw string, b ...AnyBackend) *Statement

NewStatement creates a new Statement instance from a raw SQL query string and an optional backend interface.

func (*Statement) Backend

func (statement *Statement) Backend() AnyBackend

Backend returns the currently attached backend adapter interface, if any.

func (*Statement) Build

func (statement *Statement) Build(target any) (*Query, error)

Build compiles the SQL statement into the target query structure using the attached backend's QueryBuilder interface. If no backend is attached, it defaults to using the MongoDB QueryBuilder.

func (*Statement) CSUUID

func (statement *Statement) CSUUID(uuid string) (primitive.Binary, error)

CSUUID converts a UUID string to a MongoDB Binary type using a specific

reordering pattern. It removes any curly braces and hyphens from the UUID, then reorders the bytes according to the required format before converting to base64 and creating a Binary value.

func (*Statement) Compile

func (statement *Statement) Compile(b ...AnyBackend) (any, error)

Compile compiles the statement using the provided backend interface, or the statement's attached backend.

func (*Statement) Execute

func (statement *Statement) Execute(ctx context.Context, executor ...AnyExecutor) (any, error)

Execute executes the statement using the provided executor interface, or the statement's attached backend.

func (*Statement) HexToBase64

func (statement *Statement) HexToBase64(in string) []byte

HexToBase64 converts a hexadecimal string to its base64 encoded representation.

If the hex decoding fails, it returns an empty byte slice and logs the error.

func (*Statement) Parse

func (statement *Statement) Parse() (*Plan, error)

Parse parses the raw SQL statement into an abstract, storage-agnostic Plan.

func (*Statement) Stream

func (statement *Statement) Stream(ctx context.Context, streamer ...Streamer) iter.Seq2[[]byte, error]

Stream streams data for the statement using the provided streamer interface, or the statement's attached backend.

func (*Statement) WithBackend

func (statement *Statement) WithBackend(b AnyBackend) *Statement

WithBackend attaches a backend adapter interface to the statement.

type Streamer

type Streamer = backend.Streamer

Type aliases for backend adapter interfaces and implementations.

type SubqueryExpr

type SubqueryExpr = backend.SubqueryExpr

Type aliases for Plan and abstract AST expression types defined in backend package.

type TableRef

type TableRef = backend.TableRef

Type aliases for Plan and abstract AST expression types defined in backend package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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