A fluent, type-safe SQL query builder for Go with Laravel-inspired syntax.
- π Fluent API - Chain methods for readable queries
- π‘οΈ SQL Injection Protection - Prepared statements & identifier validation
- π― Type Safety - Compile-time checks where possible
- π High Performance - Minimal allocations, cached reflection
- π Multi-Database - MySQL, PostgreSQL (coming soon)
- π¦ Zero Config - Works out of the box
- π§ͺ Well Tested - Comprehensive test coverage
go get github.com/biyonik/go-fluent-sqlpackage main
import (
"context"
"log"
fluentsql "github.com/biyonik/go-fluent-sql"
)
func main() {
// Connect to database
db, err := fluentsql.Connect("user:password@tcp(localhost:3306)/dbname")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create query builder
qb := fluentsql.New(db)
// Select query
var users []User
err = qb.Table("users").
Select("id", "name", "email").
Where("status", "=", "active").
WhereIn("role", []interface{}{"admin", "moderator"}).
OrderBy("created_at", "DESC").
Limit(10).
Get(&users)
// Insert
result, err := qb.Table("users").Insert(map[string]interface{}{
"name": "John Doe",
"email": "john@example.com",
})
// Update
result, err = qb.Table("users").
Where("id", "=", 1).
Update(map[string]interface{}{
"status": "inactive",
})
// Delete
result, err = qb.Table("users").
Where("status", "=", "banned").
Delete()
}
type User struct {
ID int `db:"id"`
Name string `db:"name"`
Email string `db:"email"`
}π Full Documentation
// Basic WHERE
qb.Where("column", "=", value)
qb.OrWhere("column", "!=", value)
// WHERE IN
qb.WhereIn("status", []interface{}{"active", "pending"})
qb.WhereNotIn("role", []interface{}{"banned"})
// WHERE BETWEEN
qb.WhereBetween("age", 18, 65)
qb.WhereNotBetween("score", 0, 50)
// WHERE NULL
qb.WhereNull("deleted_at")
qb.WhereNotNull("email_verified_at")
// Date queries
qb.WhereDate("created_at", "2024-01-15")
qb.WhereYear("created_at", 2024)
qb.WhereMonth("created_at", 12)
// Ordering
qb.OrderBy("created_at", "DESC")
// Pagination
qb.Limit(10).Offset(20)tx, err := fluentsql.BeginTransaction(db)
if err != nil {
return err
}
// Use transaction
err = tx.Table("users").Where("id", "=", 1).Update(data)
if err != nil {
tx.Rollback()
return err
}
err = tx.Table("logs").Insert(logData)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()migrator := migration.NewMigrator(db, migration.NewMySQLGrammar())
err := migrator.CreateTable("users", func(t *migration.Blueprint) {
t.ID()
t.String("name", 255)
t.String("email", 255).Unique()
t.String("password", 255)
t.Timestamps()
t.SoftDeletes()
})BenchmarkSelect-8 500000 2340 ns/op 1024 B/op 15 allocs/op
BenchmarkWhere-8 800000 1456 ns/op 512 B/op 8 allocs/op
BenchmarkInsert-8 600000 1892 ns/op 768 B/op 12 allocs/op
Contributions are welcome! Please read our Contributing Guide first.
# Clone the repo
git clone https://github.com/biyonik/go-fluent-sql.git
# Install dependencies
make deps
# Run tests
make test
# Run linter
make lintMIT License - see LICENSE for details.
- go-fluent-validator - Fluent validation library for Go
Inspired by Laravel's Eloquent and Query Builder.