GitHub - NicklasWallgren/sqlTemplate: Template engine for writing dynamic SQL qu...
source link: https://github.com/NicklasWallgren/sqlTemplate
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SQL Template library
A simple template engine for writing dynamic SQL queries.
Sometimes it can be hard to write comprehensible SQL queries with tools like SQL builders (squirrel or dbr), specially dynamic queries with optional statements and joins. It can be hard to see the overall cohesive structure of the queries, and the primary goal.
The main motivation of this library is to separate the SQL queries from the Go code, and to improve the readability of complex dynamic queries.
Check out the API Documentation http://godoc.org/github.com/NicklasWallgren/sqlTemplate
Installation
The library can be installed through go get
go get github.com/NicklasWallgren/sqlTemplate
Supported versions
We support the two major Go versions, which are 1.17 and 1.18 at the moment.
Features and benefits
- Separates SQL and Go code.
- Keeps the templated query as close as possible to the actual SQL query.
- Extensible template language with support for https://github.com/Masterminds/sprig
- No third party dependencies
// Parse parses a sql template and returns the 'QueryTemplate' Parse(namespace string, templateName string) (QueryTemplate, error) // ParseWithValuesFromMap parses a sql template with values from a map and returns the 'QueryTemplate' ParseWithValuesFromMap(namespace string, templateName string, parameters map[string]interface{}) (QueryTemplate, error) // ParseWithValuesFromStruct parses a sql template with values from a struct and returns the 'QueryTemplate' ParseWithValuesFromStruct(namespace string, templateName string, parameters interface{}) (QueryTemplate, error) // Register registers a new namespace by template root and extension Register(namespace string, templateRoot string, extensions string) error
Examples
Register a namespace and parse a template
sqlt := sqlTemplate.NewQueryTemplateEngine() sqlt.Register("users", "queries/users", ".tsql"); criteria := map[string]interface{}{"Name": "Bill", "Order": "id"} tmpl, _ := sqlt.ParseWithValuesFromMap("users", "findByName", criteria) fmt.Printf("query %v\n", tmpl.GetQuery()) fmt.Printf("query parameters %v\n", tmpl.GetParams())
-- File ./queries/users/users.tsql {{define "findByName"}} SELECT * FROM users WHERE name={{bind .Name}} {{if .Order}}ORDER BY {{.Order}}{{end}} {{end}}
Unit tests
go test -v -race $(go list ./... | grep -v vendor)
Code Guide
We use GitHub Actions to make sure the codebase is consistent (golangci-lint run
) and continuously tested (go test -v -race $(go list ./... | grep -v vendor)
). We try to keep comments at a maximum of 120 characters of length and code at 120.
Contributing
If you find any problems or have suggestions about this library, please submit an issue. Moreover, any pull request, code review and feedback are welcome.
Contributors
License
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK