leocassarani / psql

A PostgreSQL query builder for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

psql Build Status

psql is a PostgreSQL query builder for Go.

It is intended to be an addition to, rather than a replacement for, the database/sql package in the standard library.

psql offers a convenient API for generating safe SQL queries at runtime. It uses Go's type system to ensure both identifiers (such as table and column names) and parameters (user input) are correctly escaped.

psql is not an ORM; however, it may be used as a more lightweight alternative to a full-blown ORM.

Usage

Use psql to compose your query, then call ToSQL() to convert it into an SQL string that you can pass into DB.Query() or DB.QueryRow().

query := psql.Select(
  psql.TableColumn("users", "name"),
  psql.TableColumn("users", "email"),
).Where(
  psql.NotEq(psql.TableColumn("users", "name"), psql.StringParam()),
).OrderBy(
  psql.Descending(psql.TableColumn("users", "height")),
)

// SELECT "name", "email" FROM "users" WHERE ("name" <> $1::text) ORDER BY "height"
fmt.Println(query.ToSQL())

// Run the query on a database connection, replacing $1 with "Joe".
db.Query(query.ToSQL(), query.Bindings("Joe")...)

About

A PostgreSQL query builder for Go

License:MIT License


Languages

Language:Go 100.0%