Coder Social home page Coder Social logo

mariusrosoga / goyesql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from knadh/goyesql

0.0 0.0 0.0 27 KB

Parse SQL files with multiple named queries and automatically prepare and scan them into structs.

License: BSD 2-Clause "Simplified" License

Go 100.00%

goyesql's Introduction

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.

goyesql's People

Contributors

nleof avatar knadh avatar rhnvrm avatar elithrar avatar tj avatar re-tick avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.