Go to file
Kailash Nadh 737ad9fd9d
Merge pull request #5 from re-Tick/sqlx-context
feat(sqlx): calls sqlx methods with context to prepare sql statements
2022-12-06 16:27:06 +05:30
sqlx feat(sqlx): calls sqlx methods with context to prepare sql statements 2022-11-29 13:45:01 +05:30
tests/samples Introduce support for arbitrary tags (breaks compatibility with the source repo) 2018-01-09 13:56:04 +05:30
.gitignore initial commit 2015-11-02 10:10:32 +08:00
.travis.yml initial commit 2015-11-02 10:10:32 +08:00
LICENSE initial commit 2015-11-02 10:10:32 +08:00
README.md Fix import path in README 2021-08-20 13:54:57 +05:30
go.mod Tidy go.mod 2021-08-20 13:59:09 +05:30
go.sum Tidy go.mod 2021-08-20 13:59:09 +05:30
goyesql.go Add ScanToStruct helper 2018-02-13 14:41:54 +05:30
goyesql_test.go Add ScanToStruct helper 2018-02-13 14:41:54 +05:30
parser_line.go fix: versioning issue with v2.0~ tags 2020-02-12 12:15:49 +05:30
parser_line_test.go Fixed issue where blank comment lines were parsed as query 2018-12-03 18:28:07 +05:30
scanner.go Introduce support for arbitrary tags (breaks compatibility with the source repo) 2018-01-09 13:56:04 +05:30
scanner_test.go Introduce support for arbitrary tags (breaks compatibility with the source repo) 2018-01-09 13:56:04 +05:30

README.md

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.