Popularity
2.3
Stable
Activity
10.0
Stable
38
2
0

Programming language: Go
License: MIT License
Tags: Database     SQL Query Builders    
Add another 'SQL Query Builders' Package

README

builq

build-img pkg-img reportcard-img coverage-img version-img

Easily build queries in Go.

Rationale

The simplest way to represent SQL query is a string. But the query arguments and their indexing ($1, $2 etc) require additional attention. This tiny library helps to build queries and handles parameter indexing.

Features

  • Simple and easy.
  • Tested.
  • Dependency-free.

See GUIDE.md for more details.

Install

Go version 1.19+

go get github.com/cristalhq/builq

Example

cols := builq.Columns{"foo, bar"}

var b builq.Builder
b.Addf("SELECT %s FROM %s", cols, "users").
    Addf("WHERE active IS TRUE").
    Addf("AND user_id = %$ OR user = %$", 42, "root")

query, args, err := b.Build()
if err != nil {
    panic(err)
}

fmt.Printf("query:\n%v", query)
fmt.Printf("args:\n%v", args)

// Output:
//
// query:
// SELECT foo, bar FROM users
// WHERE active IS TRUE
// AND user_id = $1 OR user = $2
// args:
// [42 root]

See examples: examples_test.go.

Documentation

See these docs.

License

[MIT License](LICENSE).


*Note that all licence references and agreements mentioned in the builq README section above are relevant to that project's source code only.