qry

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 7 Imported by: 0

README

buddy pipeline codecov Go Report Card Godoc Reference

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 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:

-- qry: InsertUser
INSERT INTO `users` (`name`) VALUES (?);

-- qry: GetUserById
SELECT * FROM `users` WHERE `user_id` = ?;

And the second one queries/two.sql:

-- qry: DeleteUsersByIds
DELETE FROM `users` WHERE `user_id` IN ({ids});

Generate constants: qry-gen -dir=./queries -pkg=/path/to/your/go/pkg Will produce /path/to/your/go/pkg/qry.go with:

package pkg

const (
	// 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:

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)))
}

Documentation

Overview

Package 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 tool is used for automatic generation of constants for all user specified `query_names`.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrDirSql is returned in case when directory with .sql files is unavailable
	ErrDirSql = errors.New("cannot find directory with .sql files")
	// ErrDirPkg is returned in case when directory with go package is unavailable
	ErrDirPkg = errors.New("cannot find go package directory")
)

Functions

func Dir

func Dir(dir string) (queries map[string]QuerySet, err error)

Dir recursively loads all .sql files from a specified folder and returns them as a hashmap

func In

func In(l int) string

In returns string with N sql query placeholders

Types

type File added in v1.1.0

type File struct {
	Name  string
	Items []FileItem
}

File represents parsed file with .sql queries

func DirOrdered added in v1.1.0

func DirOrdered(dir string) ([]File, error)

DirOrdered recursively loads all .sql files from a specified folder and returns them as a slice

type FileItem added in v1.1.0

type FileItem struct {
	Name  string
	Query Query
}

FileItem represents single query within a file

type Query

type Query string

Query represents single query from a .sql file

func (Query) Replace

func (q Query) Replace(o, r string) string

Replace part of a query

type QuerySet

type QuerySet map[string]Query

QuerySet represents set of queries

Directories

Path Synopsis
cmd
qry-gen
main
main

Jump to

Keyboard shortcuts

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