Skip to content

Tiny (pure bytes.Buffer) implementation of SQL Query Builder

Notifications You must be signed in to change notification settings

kitsuniru/puresql-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

puresql-go

Tiny (pure bytes.Buffer) and opinionated implementation of SQL Query Builder

Download: go get github.com/hot-moms/puresql-go@latest

Package puresql-go is a lightweight implementation of an SQL query builder written purely in Golang's bytes.Buffer.

This package provides an easy-to-use interface for building SQL queries programmatically, allowing developers to quickly and efficiently generate complex queries with minimal coding.

With puresql-go, building SQL queries is as simple as writing out the desired query in Golang code, making it an ideal solution for teams working with databases powered by SQL.

By utilizing Golang's powerful byte-based buffer system, puresql-go provides superior performance and precision in generating valid SQL scripts. Get started quickly and with ease by leveraging this versatile tool for all your SQL query building needs.

TODO:

  • ADD clauses
  • Equal to X
  • Contained by X
  • Any of X
  • Field is X (type)
  • Compare field with X
  • Between X and Y
  • OR clauses

Usage

Query:

const _sql = `SELECT hub FROM hubs`

	var (
		name            = "Github"
		companies       = []string{"Apple", "Microsoft", "Github"}
		types           = []string{"Commercial", "Non-commercial"}
		year_from int32 = 1990
		year_to   int32 = 2023
	)

	sB := sql.Init(_sql)
	sB.ContainedByStrings("company", companies)
	sB.BetweenInts("year", &year_from, &year_to)
	sB.EqualToString("name", &name)
	sB.AnyOfStrings("type", types)
	sB.OrderBy(nil, nil, "last_update DESC")
	sB.Limit(12)

	fmt.Println(sB.ToSQL())

Result

Query: SELECT hub FROM hubs WHERE (company @> ($1)) AND (year BETWEEN $2 AND $3) AND (name = $4) AND (type = ANY($5)) ORDER BY last_update DESC LIMIT 12

Arguments: [Apple Microsoft Github], 1990, 2023, Github, [Commercial Non-commercial]


2023, Archie Iwakura (hot-moms)

About

Tiny (pure bytes.Buffer) implementation of SQL Query Builder

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages