Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/HnH/qry
Write your SQL queries in raw files with all benefits of modern IDEs, use them in an easy way inside your application with all the profit of compile time constants
https://github.com/HnH/qry
go golang sql
Last synced: about 2 months ago
JSON representation
Write your SQL queries in raw files with all benefits of modern IDEs, use them in an easy way inside your application with all the profit of compile time constants
- Host: GitHub
- URL: https://github.com/HnH/qry
- Owner: HnH
- License: mit
- Created: 2019-08-20T09:01:00.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-20T18:44:16.000Z (10 months ago)
- Last Synced: 2024-07-31T20:49:56.815Z (4 months ago)
- Topics: go, golang, sql
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 35
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - qry - Tool that generates constants from files with raw SQL queries. (Database / SQL Query Builders)
- awesome-ccamel - HnH/qry - Write your SQL queries in raw files with all benefits of modern IDEs, use them in an easy way inside your application with all the profit of compile time constants (Go)
- awesome-go-extra - qry - 08-20T09:01:00Z|2021-09-30T07:55:24Z| (Generators / SQL Query Builders)
README
[![buddy pipeline](https://eu.buddy.works/treinis/qry/pipelines/pipeline/191916/badge.svg?token=b46003623254cc1f4e162f3911b6075d87b9121676c88ffebfbe34d562d75d96 "buddy pipeline")](https://eu.buddy.works/treinis/qry/pipelines/pipeline/191916)
[![codecov](https://codecov.io/gh/HnH/qry/branch/master/graph/badge.svg)](https://codecov.io/gh/HnH/qry)
[![Go Report Card](https://goreportcard.com/badge/github.com/HnH/qry)](https://goreportcard.com/report/github.com/HnH/qry)
[![Godoc Reference](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/HnH/qry)# About
**qry** is a general purpose library for storing your raw database queries in .sql files with all benefits of modern IDEs, instead of strings and constants in the code, and using them in an easy way inside your application with all the profit of compile time constants.
**qry** recursively loads all .sql files from a specified folder, parses them according to predefined rules and returns a reusable object, which is actually just a `map[string]string` with some sugar. Multiple queries inside a single file are separated with standard SQL comment syntax: `-- qry: QueryName`. A `QueryName` must match `[A-Za-z_]+`.
[gen](https://github.com/HnH/qry/tree/master/cmd/qry-gen) tool is used for automatic generation of constants for all user specified `query_names`.
# Installation
`go install github.com/HnH/qry/cmd/qry-gen@latest`
# Usage
Prepare sql files: `queries/one.sql`:
```sql
-- qry: InsertUser
INSERT INTO `users` (`name`) VALUES (?);-- qry: GetUserById
SELECT * FROM `users` WHERE `user_id` = ?;
```And the second one `queries/two.sql`:
```sql
-- qry: DeleteUsersByIds
DELETE FROM `users` WHERE `user_id` IN ({ids});
```[Gen](https://github.com/HnH/qry/tree/master/cmd/qry-gen)erate constants: `qry-gen -dir=./queries -pkg=/path/to/your/go/pkg` Will produce `/path/to/your/go/pkg/qry.go` with:
```go
package pkgconst (
// one.sql
InsertUser = "INSERT INTO `users` (`name`) VALUES (?);"
GetUserById = "SELECT * FROM `users` WHERE `user_id` = ?;"// two.sql
DeleteUsersByIds = "DELETE FROM `users` WHERE `user_id` IN ({ids});"
)
```As a best practice include this qry-gen call in your source code with go:generate prefix: `//go:generate qry-gen -dir=./queries -pkg=/path/to/your/go/pkg` and just execute `go generate` before each build.
Now it's time to use **qry** inside your project:```go
func main() {
/**
* The most obvious way is to use generated constants in the source code
*/
// INSERT INTO `users` (`name`) VALUES (?);
println(pkg.InsertUser)
// DELETE FROM `users` WHERE `user_id` IN (?,?,?);
println(qry.Query(pkg.DeleteUsersByIds).Replace("{ids}", qry.In(3)))
/**
* As an alternative you can manually parse .sql files in the directory and work with output
*/
if q, err := qry.Dir("/path/to/your/go/pkg/queries"); err != nil {
log.Fatal(err)
}// SELECT * FROM `users` WHERE `user_id` = ?;
println(q["one.sql"]["GetUserById"])
// DELETE FROM `users` WHERE `user_id` IN (?,?,?);
println(q["two.sql"]["DeleteUsersByIds"].Replace("{ids}", qry.In(3)))
}
```