sqb

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 17, 2021 License: MIT Imports: 5 Imported by: 0

README

sqb - SQL Query Builder

⚡ Blazing fast, Flexible, SQL Query Builder for Go

GoDoc CircleCI codecov Go Report Card

Features

  • High performance.
  • Easy to use.
  • Powerful, Flexible. You can define stmt for yourself.
  • Supported MySQL, PostgreSQL, Spanner statement.

Synopsis

When used normally

const sqlstr = "SELECT * FROM tables WHERE ?"
builder := sqb.New(sqlstr).Bind(sqb.Eq("category", 1))
query, args, err := builder.Build()
// query => "SELECT * FROM tables WHERE category = ?",
// args  => []interface{}{1}
When you want to use build cache
const sqlstr = "SELECT * FROM tables WHERE ? AND ?"
cached := sqb.New(sqlstr).Bind(sqb.Eq("category", 1))

for _, col := range columns {
    builder := cached.Bind(sqb.Eq(col, "value"))
    query, args, err  := builder.Build()
    // query => "SELECT * FROM tables WHERE category = ? AND " + col + " = ?",
    // args  => []interface{}{1, "value"}
}
Error case
const sqlstr = "SELECT * FROM tables WHERE ? OR ?"
builder := sqb.New(sqlstr).Bind(sqb.Eq("category", 1))
query, args, err  := builder.Build()
// query => "",
// args  => nil
// err   => "number of bindVars exceeds replaceable statements"

Install

Use go get to install this package.

go get -u github.com/Code-Hex/sqb

Performance

sqb is the fastest and least-memory used among currently known SQL Query builder in the benchmark. The data of chart using simple benchmark.

time

benchmark

Documentation

Index

Constants

View Source
const (
	// Question represents a '?' placeholder parameter.
	Question = iota
	// Dollar represents a '$1', '$2'... placeholder parameters.
	Dollar
	// AtMark represents a '@1', '@2'... placeholder parameters.
	AtMark
)

There is build logic using placeholder in internal/pool.go.

Variables

This section is empty.

Functions

func And

func And(left, right stmt.Expr, exprs ...stmt.Expr) *stmt.And

And creates statement for the AND boolean expression. If you want to know more details, See at stmt.And.

func AndFromMap

func AndFromMap(f ConditionalFunc, m map[string]interface{}) stmt.Expr

AndFromMap Creates a concatenated string of AND boolean expression from a map.

If there is no first argument then occurs panic. If map length is zero it returns nil. If map length is 1 it returns *stmt.Condition created by ConditionalFunc.

func Between

func Between(column string, left, right interface{}) *stmt.Condition

Between creates condition `column BETWEEN ? AND ?`.

func Eq

func Eq(column string, value interface{}) *stmt.Condition

Eq creates condition `column = ?`.

func Ge

func Ge(column string, value interface{}) *stmt.Condition

Ge creates condition `column >= ?`.

func Gt

func Gt(column string, value interface{}) *stmt.Condition

Gt creates condition `column > ?`.

func In

func In(column string, args ...interface{}) *stmt.Condition

In creates condition `column IN (?, ?, ?, ...)`.

func Le

func Le(column string, value interface{}) *stmt.Condition

Le creates condition `column <= ?`.

func Like

func Like(column string, value interface{}) *stmt.Condition

Like creates condition `column LIKE ?`.

func Lt

func Lt(column string, value interface{}) *stmt.Condition

Lt creates condition `column < ?`.

func Ne

func Ne(column string, value interface{}) *stmt.Condition

Ne creates condition `column != ?`.

func NotBetween

func NotBetween(column string, left, right interface{}) *stmt.Condition

NotBetween creates condition `column NOT BETWEEN ? AND ?`.

func NotIn

func NotIn(column string, args ...interface{}) *stmt.Condition

NotIn creates condition `column NOT IN (?, ?, ?, ...)`.

func NotLike

func NotLike(column string, value interface{}) *stmt.Condition

NotLike creates condition `column NOT LIKE ?`.

func Op

func Op(op, column string, value interface{}) *stmt.Condition

Op creates flexible compare operation.

func Or

func Or(left, right stmt.Expr, exprs ...stmt.Expr) *stmt.Or

Or creates statement for the OR boolean expression with parentheses. If you want to know more details, See at stmt.Or.

func OrFromMap

func OrFromMap(f ConditionalFunc, m map[string]interface{}) stmt.Expr

OrFromMap Creates a concatenated string of OR boolean expression from a map.

If there is no first argument then occurs panic. If map length is zero it returns nil. If map length is 1 it returns *stmt.Condition created by ConditionalFunc.

func OrderBy added in v0.0.2

func OrderBy(column string, desc bool) *stmt.OrderBy

OrderBy Creates an unary expression for ORDER BY. If you want to know more details, See at stmt.OrderBy.

func OrderByList added in v0.0.2

func OrderByList(expr *stmt.OrderBy, exprs ...*stmt.OrderBy) *stmt.OrderBy

OrderByList Creates an expression for ORDER BY from multiple *stmt.OrderBy.

This function creates like "<column_name>, <column_name> DESC". The first argument is required. If you want to know more details, See at stmt.OrderBy.

func Paren

func Paren(expr stmt.Expr) *stmt.Paren

Paren creates the expression with parentheses.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder builds sql query string.

func New

func New(opts ...Option) *Builder

New returns sql query builder.

func (*Builder) Bind

func (b *Builder) Bind(expr stmt.Expr) *Builder

Bind binds expression to bindVars. returns copied *Builder which bound expression.

func (*Builder) Build

func (b *Builder) Build(baseQuery string) (string, []interface{}, error)

Build builds sql query string, returning the built query string and a new arg list that can be executed by a database. The `query` should use the `?` bindVar. The return value uses the `?` bindVar.

type Columns added in v0.0.2

type Columns = stmt.Columns

Columns is an alias of stmt.Columns.

type ConditionalFunc

type ConditionalFunc func(column string, value interface{}) *stmt.Condition

ConditionalFunc indicates function of conditional.

type Limit added in v0.0.2

type Limit = stmt.Limit

Limit is an alias of stmt.Limit.

type Numeric added in v0.0.2

type Numeric = stmt.Numeric

Numeric is an alias of stmt.Numeric.

type Offset added in v0.0.2

type Offset = stmt.Offset

Offset is an alias of stmt.Offset.

type Option added in v0.0.2

type Option func(b *Builder)

Option represents options to build sql query.

func SetPlaceholder added in v0.0.2

func SetPlaceholder(placeholder int) Option

SetPlaceholder sets placeholder.

Default value is zero uses Question '?' as a placeholder.

type String added in v0.0.2

type String = stmt.String

String is an alias of stmt.String.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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