Date:

SimpleQL

The Challenge of Dynamic Queries

Working with user-provided filtering in Go applications presents several challenges:

  1. Dynamically building SQL queries based on user input
  2. Preventing SQL injection while handling user filters
  3. Creating an approachable query interface for various users
  4. Validating input against defined schemas
  5. Maintaining consistency between database queries and in-memory filtering

How DumbQL Works

DumbQL implements a straightforward query syntax that’s converted to SQL or used for in-memory filtering:

status:pending and period_months < 4 and (title:"hello world" or name:"John Doe")

Core Functionality

Query Syntax

DumbQL supports various expressions:

  • Field expressions: age >= 18, name:"John"
  • Boolean expressions: verified and premium
  • One-of expressions: status:[active, pending]
  • Boolean field shorthand: is_active (equivalent to is_active:true)

Schema Validation

DumbQL includes built-in schema validation:

schm := schema.Schema{
  "status": schema.Field{
    "type": "string",
    "enum": []string{"pending", "approved", "rejected"},
  },
  "period_months": schema.Field{
    "type": "int64",
    "min": 1,
    "max": 12,
  },
  "title": schema.Field{
    "type": "string",
    "min": 1,
    "max": 100,
  },
}

Implementation Contexts

DumbQL can be useful in several scenarios:

  1. Admin interfaces with filtering capabilities
  2. API endpoints with query parameters for filtering
  3. Condition-based alerts or notifications
  4. Report generation with user-defined filters
  5. Search functionality with complex conditions

Usage Basics

To use DumbQL in your project:

go get go.tomakado.io/dumbql

Performance Considerations

For struct matching, DumbQL provides two options:

  1. Reflection-based matching (works immediately but has runtime overhead)
  2. Code generation via the dumbqlgen tool (eliminates reflection overhead)

Conclusion

DumbQL provides a lightweight query language for Go applications that can simplify filtering logic. It bridges the gap between user-friendly query syntax and database operations while offering features like schema validation and struct matching.

FAQs

Q: What is DumbQL?

A: DumbQL is a lightweight query language for Go applications that simplifies filtering logic.

Q: How does DumbQL work?

A: DumbQL implements a straightforward query syntax that’s converted to SQL or used for in-memory filtering.

Q: What are the benefits of using DumbQL?

A: DumbQL simplifies filtering logic, provides schema validation, and eliminates SQL injection risks.

Q: How do I get started with DumbQL?

A: You can get started with DumbQL by installing the package using go get go.tomakado.io/dumbql.

Latest stories

Read More

LEAVE A REPLY

Please enter your comment!
Please enter your name here