Skip to content

henvic/pgq

Repository files navigation

pgq

GoDoc Build Status Coverage Status

pgq is a query builder for PostgreSQL written in Go.

It is a fork of Squirrel more suitable for working with the PostgreSQL database when you are able to use the native PostgreSQL protocol directly, rather than the slower textual protocol used by database/sql.

You can use it with the pgx driver.

Usage example

Something like this:

sql, args, err := pgq.Update("employees").
	Set("salary_bonus", pgq.Expr("salary_bonus + 1000")).
	From("accounts").
	Where("accounts.team = ?", "engineering").
	Returning("id", "name", "salary")

if err != nil {
	panic(err) // bug: this should never happen.
}

type employee struct {
	ID string
	Name string
	Salary int
}
var data []employee
rows, err := pool.Query(context.Background(), sql, args...)
if err == nil {
	defer rows.Close()
	data, err = pgx.CollectRows(rows, pgx.RowTo[employee])
}

Main benefits

  • API is crafted with only PostgreSQL compatibility so it has a somewhat lean API.
  • It uses ANY and ALL operators for slices by default, which means it supports slices out of the box and you get to reuse your prepared statements.
  • It's throughly tested (including integration tests to check for invalid queries being generated).
  • If you already use pgx with Squirrel and the native PostgreSQL protocol, switching is very straightforward with just a few breaking changes (example: Alias is a type rather than a function).

Main drawback

  • It's still a query builder. You can go a long way writing pure SQL queries. Consider doing so.

FAQ

Why forking a query builder then? Whatever it takes to make people ditch using an ORM, I guess.

See also