goyesql

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: BSD-2-Clause Imports: 9 Imported by: 12

README

goyesql

This package is based on nleof/goyesql but is not compatible with it any more. This package introduces support for arbitrary tag types and changes structs and error types.

Parses a file and associate SQL queries to a map. Useful for separating SQL from code logic.

Installation

$ go get -u github.com/knadh/goyesql/v2

Usage

Create a file containing your SQL queries

-- queries.sql

-- name: list
-- some: param
-- some_other: param
SELECT *
FROM foo;

-- name: get
SELECT *
FROM foo
WHERE bar = $1;

And just call them in your code!

queries := goyesql.MustParseFile("queries.sql")
// use queries["list"] with sql/database, sqlx ...
// queries["list"].Query is the parsed SQL query string
// queries["list"].Tags is the list of arbitrary tags (some=param, some_other=param)

Scanning

Often, it's necessary to scan multiple queries from a SQL file, prepare them into *sql.Stmt and use them throught the application. goyesql comes with a helper function that helps with this. Given a goyesql map of queries, it can turn the queries into prepared statements and scan them into a struct that can be passed around.

type MySQLQueries struct {
	// This will be prepared.
	List *sql.Stmt `query:"list"`

	// This will not be prepared.
	Get  string    `query:"get"`
}

type MySQLxQueries struct {
	// These will be prepared.
	List *sqlx.Stmt `query:"list"`
	NamedList *sqlx.NamedStmt `query:"named_list"`

	// This will not be prepared.
	Get  string    `query:"get"`
}

var (
	q  MySQLQueries
	qx MySQLxQueries
)

// Here, db (*sql.DB) is your live DB connection.
err := goyesql.ScanToStruct(&q, queries, db)
if err != nil {
	log.Fatal(err)
}

// Here, db (*sqlx.DB) is your live DB connection.
err := goyesqlx.ScanToStruct(&qx, queries, db)
if err != nil {
	log.Fatal(err)
}

// Then, q.Exec(), q.QueryRow() etc.

Embedding

You can use stuffbin and ParseBytes() for embedding SQL queries in your binary.

Documentation

Overview

Package goyesql is a Go port of Yesql

It allows you to write SQL queries in separate files.

See rationale at https://github.com/krisajenkins/yesql#rationale

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanToStruct

func ScanToStruct(obj interface{}, q Queries, db *sql.DB) error

ScanToStruct prepares a given set of Queries and assigns the resulting *sql.Stmt statements to the fields of a given struct, matching based on the name in the `query` tag in the struct field names.

Types

type Err

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

Err represents an error.

func (Err) Error

func (e Err) Error() string

type Queries

type Queries map[string]*Query

Queries is a map associating a Tag to its Query

func MustParseBytes

func MustParseBytes(b []byte) Queries

MustParseBytes parses bytes but panics if an error occurs.

func MustParseFile

func MustParseFile(path string) Queries

MustParseFile calls ParseFile but panic if an error occurs

func ParseBytes

func ParseBytes(b []byte) (Queries, error)

ParseBytes parses bytes and returns Queries or an error.

func ParseFile

func ParseFile(path string) (Queries, error)

ParseFile reads a file and return Queries or an error

func ParseReader

func ParseReader(reader io.Reader) (Queries, error)

ParseReader takes an io.Reader and returns Queries or an error.

type Query

type Query struct {
	Query string
	Tags  map[string]string
}

Query is a parsed query along with tags.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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