• Stars
    star
    87
  • Rank 365,632 (Top 8 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 2 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily build SQL queries in Go.

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.
  • Safe and fast.
  • Tested.
  • Language-agnostic.
  • Dependency-free.

See docs or GUIDE.md for more details.

Install

Go version 1.19+

go get github.com/cristalhq/builq

Example

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

q := builq.New()
q("SELECT %s FROM %s", cols, "users")
q("WHERE active IS TRUE")
q("AND user_id = %$ OR user = %$", 42, "root")

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

debug := q.DebugBuild()

fmt.Println("query:")
fmt.Println(query)
fmt.Println("\nargs:")
fmt.Println(args)
fmt.Println("\ndebug:")
fmt.Println(debug)

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

See examples: example_test.go.

License

MIT License.

More Repositories

1

jwt

Safe, simple and fast JSON Web Tokens for Go
Go
640
star
2

aconfig

Simple, useful and opinionated config loader.
Go
512
star
3

base64

Faster base64 encoding for Go
Go
177
star
4

hedgedhttp

Hedged HTTP client which helps to reduce tail latency at scale.
Go
118
star
5

acmd

Simple, useful and opinionated CLI package in Go.
Go
108
star
6

atomix

Simple and easy wrappers for Go sync/atomic package.
Go
31
star
7

sqlmetrics

Prometheus metrics for Go database/sql via VictoriaMetrics/metrics
Go
23
star
8

oauth2

OAuth2 client in Go
Go
22
star
9

synx

Better sync package for Go.
Go
22
star
10

.github

Common stuff across cristalhq organization
Go
18
star
11

otp

One time password for Go.
Go
17
star
12

redis

WIP. Redis client for Go
Go
16
star
13

sse

Server-Sent Events (SSE) library for Go
Go
11
star
14

pgstats

Postgres statistics for Go
Go
11
star
15

bencode

Bencode encoding and decoding in Go.
Go
10
star
16

pgxutil

Go jackc/pgx helper to write proper transactions
Go
7
star
17

qrcode

QR code for Go.
Go
6
star
18

dbump

Go database schema migrator library (See `cristalhq/dbumper` tool)
Go
6
star
19

hedgedgrpc

Hedged Go GRPC client which helps to reduce tail latency at scale.
Go
6
star
20

ipfilterware

Go HTTP middleware to filter clients by IP address
Go
5
star
21

fastid

Fast ID generator based on timestamp, sequence number and worker id.
Go
5
star
22

httpx

Better net/http for Go
Go
4
star
23

dbg

Debug helpers for Go
Go
4
star
24

dsvreader

Fast reader for delimiter-separated data
Go
4
star
25

jsn

Go package to easily construct JSON without defined types.
Go
4
star
26

cron

A cron library for Go.
Go
3
star
27

obfid

Obfuscating ID based on modular multiplicative inverse.
Go
3
star
28

glob

Glob pattern matching in Go.
Go
3
star
29

dotenv

WIP
Go
3
star
30

benchmarks

Benchmarks for cristalhq and other solutions
Go
3
star
31

errorx

Better `errors` package for Go
Go
3
star
32

ternary

Ternary logic for Go.
Go
3
star
33

fresbi

WIP: Fast Reliable ElasticSearch Bulk Indexer
Makefile
3
star
34

netx

Better net package for Go
Go
3
star
35

bson

BSON for Go
Go
3
star
36

envx

Go environment utils
Go
2
star
37

dbumper

Database schema migrator.
Go
2
star
38

natsort

Natural sorting in Go, see Wikipedia.
Go
2
star
39

timex

Better `time` package for Go
Go
1
star
40

fasthedged

1
star
41

testt

Go
1
star
42

mathx

Go
1
star
43

appx

Go library for building applications. Dramatically simple.
Go
1
star
44

dynconst

Dynamic constants for Go application
Go
1
star
45

feeds

Library for Atom, JSON and RSS feeds creation and parsing.
Go
1
star
46

lvlbp

Leveled byte pool
Go
1
star
47

typex

Type helpers for Go
Go
1
star
48

magneturi

A Go package for magnet links
Go
1
star
49

flagx

Go flag utils
Go
1
star