/sqlcat

A dead simple SQL query builder for Go.

Primary LanguageGoMIT LicenseMIT

sqlcat

GoDoc Reference Latest Version License Name Build Status Coverage Status Report Card Status

Package sqlcat is a dead simple SQL query builder for Go. It is not an ORM nor a full-featured query builder, only SELECT is supported and you still need to write some SQL. Its job is to structurally concatenate those SQL to form a complete statement.

Features and benefits:

  • Minimal API, it only has one struct and two helper functions.
  • Helps you write queries in a structured and declarative way.
  • Outputs prepared statement to prevent SQL injection.
  • Supports PostgreSQL positional parameters.
  • No imported external dependencies.
  • Properly tested with benchmarks.

Installation

Make sure you have a working Go workspace, then:

go get github.com/h4ckedneko/sqlcat

For updating to latest stable release, do:

go get -u github.com/h4ckedneko/sqlcat

Usage

Here is a basic example for this package:

package main

import (
	"fmt"

	"github.com/h4ckedneko/sqlcat"
)

func main() {
	// Initialize the builder.
	b := &sqlcat.Builder{
		Table:   "pets",
		Columns: []string{"*"},
	}

	// Associate various context.
	b.WithOrders([]string{"name ASC"})
	b.WithLimit(30)
	b.WithOffset(30)

	// Associate a condition.
	// It prevents SQL injection.
	typ := "cat"
	cond := "type = $n"
	sqlcat.WithCondition(b, cond, typ)

	// Build the SQL query and its arguments.
	// Output sql: SELECT * FROM pets WHERE type = $1 ORDER BY name ASC LIMIT 30 OFFSET 30
	// Output args: [cat]
	sql, args := b.ToSQL()
	fmt.Println(sql)
	fmt.Println(args)
}

See examples for more advanced real-world examples.

Performance

You can run benchmarks by yourself using make bench command.

BenchmarkBuilderToSQLBasic-2             3536516               321 ns/op              40 B/op          2 allocs/op
BenchmarkBuilderToSQLCountBasic-2        3427706               348 ns/op              96 B/op          2 allocs/op
BenchmarkBuilderToSQLComplex-2            767654              1518 ns/op             736 B/op          8 allocs/op
BenchmarkBuilderToSQLCountComplex-2       901642              1322 ns/op             736 B/op          8 allocs/op

Alternatives

If you want a full-featured SQL query builder, check out these packages:

Inspiration

This package is based on Miniflux's code of its EntryQueryBuilder.

License

MIT © Lyntor Paul Figueroa. See LICENSE for full license text.