queries

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2024 License: MIT Imports: 11 Imported by: 0

README

Queries

The queries library for Go implements a file based SQL query management system. Only PostgreSQL is supported.

Installing

  go get -u github.com/boringsql/queries

Usage

Writing and using SQL code in Go code can be inneficient for several reasons. Not only it diminishes the advantages of a statically typed language, it also worsens editor support for syntax highlighting and indentation, and hinders query reusability with other tools. The queries library has been created to address these issues by providing a better way to manage and use SQL queries in Go.

With queries you define your SQL code in files like sql/users.sql

-- name: get-user-by-id
SELECT *
FROM users 
WHERE user_id = :user_id AND deleted_at is null

-- name: update-user-last-login
UPDATE users
SET last_login_at = current_timestamp
WHERE user_id = :user_id

Load all the respective files into QueryStore using three different ways

err = queryStore.LoadFromFile("sql/users.sql")
if err != nil {
  return err
}

err = queryStore.LoadFromDir("sql/")
if err != nil {
  return err
}

//go:embed sql/*
var sqlFS embed.FS
err = queryStore.LoadFromEmbed("sql/")
if err != nil {
  return err
}

Once you get the query loaded you can access them by their name and prepare the named parameter mapping

getUser := queryStore.MustHaveQuery("get-user-by-id")
args := getUser.Prepare(map[string]interface{}{
  "user_id": 123,
})

err = db.Get(&user, getUser.Query(), args...)
if err != nil {
  return err
}

Query format

The recommende use of the queries library is to switch from the default positional parameter notation ($1, $2, etc. - dollar quited sign followed by the parameter position) to psql variable definition.

The benefit of the variable definition is better visual control. Other aspect is the inter-operability with other PostgreSQL tools. Notably regresql.

If you prefer the default dolar sign positional parameters, you can skip the argument preparation (queryStore.Prepare) and use the query.Raw.

Credits

The queries library is heavily influenced (and in some cases re-uses part of the logic) by

Documentation

Overview

Credits to https://github.com/gchaincl/dotsql

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Query

type Query struct {
	Name         string
	Raw          string
	OrdinalQuery string
	Mapping      map[string]int
	NamedArgs    []sql.NamedArg
}

func NewQuery

func NewQuery(name, query string) *Query

func (*Query) Prepare

func (q *Query) Prepare(args map[string]interface{}) []interface{}

Prepare the arguments for the ordinal query. Missing arguments will be returned as nil

func (*Query) Query

func (q *Query) Query() string

Query returns ordinal query

func (*Query) RawQuery

func (q *Query) RawQuery() string

type QueryStore

type QueryStore struct {
	// contains filtered or unexported fields
}

func NewQueryStore

func NewQueryStore() *QueryStore

NewQueryStore setups new query store

func (*QueryStore) LoadFromDir

func (s *QueryStore) LoadFromDir(path string) error

func (*QueryStore) LoadFromEmbed

func (qs *QueryStore) LoadFromEmbed(sqlFS embed.FS, path string) error

func (*QueryStore) LoadFromFile

func (s *QueryStore) LoadFromFile(fileName string) (err error)

LoadFromFile loads query/queries from specified file

func (*QueryStore) MustHaveQuery

func (s *QueryStore) MustHaveQuery(name string) *Query

MustHaveQuery returns query or panics on error

func (*QueryStore) Query

func (s *QueryStore) Query(name string) (*Query, error)

Query retrieve query by given name

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

func (*Scanner) Run

func (s *Scanner) Run(fileName string, io *bufio.Scanner) map[string]string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL