When looking for a SQL query builder in Go that avoids ORM-style abstractions (like struct mapping or automatic schema handling), the goal is to find a library that allows programmatic construction of SQL queries while retaining full control over the generated SQL. Here are the top options, along with their strengths and trade-offs:
1. Squirrel
GitHub: github.com/lajosbencz/gosr
Pros:
- Simple, chainable interface for building queries (SELECT/INSERT/UPDATE/DELETE).
- Lightweight and easy to integrate with
database/sql.
- Supports conditional query building (e.g., adding WHERE clauses dynamically).
- Actively maintained and well-documented.
Cons:
- Limited support for advanced SQL features (e.g., CTEs, window functions).
- No built-in support for SQL dialects (you write raw SQL snippets for complex cases).
Example:
import sq "github.com/lajosbencz/gosr"
query := sq.Select("id", "name").From("users").Where(sq.Eq{"age": 30})
sql, args := query.Build()
2. Go-SQLBuilder
GitHub: github.com/go-database/sql
Pros:
- Supports multiple SQL dialects (MySQL, PostgreSQL, SQLite, SQL Server).
- Rich API for building complex queries (JOINs, subqueries, CTEs).
- Type-safe query construction with compile-time checks.
Cons:
- More verbose than simpler libraries like Squirrel.
- Less community adoption compared to other tools.
Example:
import "github.com/go-database/sql/builder"
query := builder.Select("id", "name").From("users").Where(builder.Eq("age", 30))
sql, args := query.Build()
3. sqlf
GitHub: github.com/heroku/x
Pros:
- Safe string interpolation to prevent SQL injection.
- Minimalist design with a focus on readability.
- Works well for ad-hoc queries where you want to mix raw SQL with parameterization.
Cons:
- Not a full-featured query builder (no structured methods for WHERE clauses, JOINs, etc.).
- Requires manual SQL writing for complex logic.
Example:
import "github.com/heroku/x/hmacchain/sqlf"
query := sqlf.Sprintf("SELECT * FROM users WHERE age = %s", 30)
db.Query(query.SQL, query.Args...)
4. Didi
GitHub: github.com/posener/didi
Pros:
- Lightweight and easy to use.
- Chainable API for building queries.
- Focuses on simplicity and readability.
Cons:
- Limited features for advanced SQL (e.g., subqueries, CTEs).
- Not actively maintained (as of 2023).
Example:
import "github.com/posener/didi"
query := didi.Select("id", "name").From("users").Where("age = ?", 30)
sql, args := query.ToSQL()
5. pgconn (PostgreSQL Only)
If you're working exclusively with PostgreSQL, consider using the lower-level pgconn package from jackc/pgx. While not a traditional query builder, it provides tools for safely constructing and executing queries directly.
Pros:
- Tight integration with PostgreSQL features.
- High performance and low overhead.
Cons:
- Not portable to other databases.
- Requires manual SQL writing.
Choosing the Right Tool
- Use Squirrel for simple, idiomatic query building with broad compatibility.
- Use Go-SQLBuilder if you need multi-dialect support and structured query construction.
- Use sqlf for ad-hoc queries with safe parameterization.
- Avoid ORMs like GORM or XORM, as they include features you’re explicitly trying to avoid.
All of these tools prioritize explicit SQL control over abstraction, making them ideal for scenarios where you want to optimize queries or work with legacy schemas. For most projects, Squirrel or Go-SQLBuilder will provide the best balance of flexibility and maintainability.