Help us shape the future of Go! Take the 2026 Go Developer Survey

pqcomp

package module
v0.0.0-...-631b19c Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2016 License: MIT Imports: 4 Imported by: 1

README

pqcomp GoDoc Build Status codecov.io

Dead simple query builder that support null types from sql package, but also provide interface Appearer. Detailed information and examples can be found in the documentation.

Benchmarks

BenchmarkComposer_AddExpr-4      	 5000000	       470 ns/op	     100 B/op	       1 allocs/op
BenchmarkComposer_Placeholder-4  	10000000	       130 ns/op	       4 B/op	       2 allocs/op
BenchmarkComposer_Args-4         	300000000	         4.89 ns/op	       0 B/op	       0 allocs/op
BenchmarkComposer_New-4          	  200000	     15311 ns/op	    5120 B/op	       4 allocs/op

Documentation

Overview

Package pqcomp provides dead simple query builder that support null types from sql package, but also provide interface Appearer.

Benchmarks

To be comprehensive solution, query builder needs to be optimized. Some of the benchmark results:

BenchmarkComposer_AddExpr-4      	 5000000	       470 ns/op	     100 B/op	       1 allocs/op
BenchmarkComposer_Placeholder-4  	10000000	       130 ns/op	       4 B/op	       2 allocs/op
BenchmarkComposer_Args-4         	300000000	         4.89 ns/op	       0 B/op	       0 allocs/op
BenchmarkComposer_New-4          	  200000	     15311 ns/op	    5120 B/op	       4 allocs/op
Example
package main

import (
	"fmt"

	"database/sql"

	"github.com/piotrkowalczuk/pqcomp"
)

func main() {
	var (
		uquery, wquery string
	)
	comp := pqcomp.New(1, 1, 1, 3)
	update := comp.Compose()
	where := comp.Compose()

	comp.AddArg(10)

	update.AddExpr("u.username", pqcomp.Equal, "johnsnow")
	update.AddExpr("u.first_name", pqcomp.Equal, "John")
	update.AddExpr("u.last_name", pqcomp.Equal, &sql.NullString{String: "Snow", Valid: true})

	where.AddExpr("u.id", pqcomp.Equal, 1)
	where.AddExpr("u.age", pqcomp.GreaterThan, &sql.NullInt64{Int64: 1000, Valid: false})

	if update.Len() == 0 || where.Len() == 0 {
		return
	}

	for update.Next() {
		if update.First() {
			uquery += "SET "
		} else {
			uquery += ", "
		}
		uquery += fmt.Sprintf("%s %s %s", update.Key(), update.Oper(), update.PlaceHolder())
	}
	for where.Next() {
		if where.First() {
			wquery += "WHERE "
		} else {
			wquery += ", "
		}
		wquery += fmt.Sprintf("%s %s %s", where.Key(), where.Oper(), where.PlaceHolder())
	}

	fmt.Println(where.Args()...)
	fmt.Println(update.Args()...)
	fmt.Println(comp.Args()...)
	fmt.Printf("UPDATE users AS u %s %s LIMIT $1 \n", uquery, wquery)

}
Output:
1
johnsnow John &{Snow true}
10 johnsnow John &{Snow true} 1
UPDATE users AS u SET u.username = $2, u.first_name = $3, u.last_name = $4 WHERE u.id = $5 LIMIT $1

Index

Examples

Constants

View Source
const (
	// Like represents LIKE operator.
	Like = "LIKE"
	// In represents IN operator.
	In = "IN"
	// IsNull represents IS NULL keywords.
	IsNull = "IS NULL"
	// IsNotNull represents IS NOT NULL keywords.
	IsNotNull = "IS NOT NULL"
	// Is represents IS operator.
	Is = "IS"
	// Equal represents equal operator.
	Equal = "="
	// NotEqual represents not equal operator.
	NotEqual = "<>"
	// GreaterThan represents greater than operator.
	GreaterThan = ">"
	// LessThan represents lower than operator.
	LessThan = "<"
	// GTE represents greater than or equal operator.
	GreaterThanOrEqual = ">="
	// LTE represents lower than or equal operator.
	LessThanOrEqual = "<="
	// DESC represents descendant way of sorting.
	Descendant = "DESC"
	// ASC represents ascendant way of sorting.
	Ascendant = "ASC"
	// Any ...
	Any = "ANY"
	// All ...
	All = "ALL"
	// Contains ...
	Contains = "@>"
	// IsContainedBy ...
	IsContainedBy = "<@"
	// Overlap ...
	Overlap = "??"
	// Exists ...
	Exists = "?"
	// ExistsAny ...`
	ExistsAny = "?|"
	// ExistsAll ...
	ExistsAll = "?&"
)

Variables

View Source
var (
	// Empty ...
	Empty = struct{}{}
)

Functions

This section is empty.

Types

type Appearer

type Appearer interface {
	// Appear returns true if object should be used by AddExpr method.
	// Otherwise it is ignored.
	Appear() bool
}

Appearer wraps Appear function.

type Composer

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

Composer is some sort of stateful iterator that helps to build complex SQL queries.

func New

func New(args, nbOfExpressions int, nbOfExpressionsForEachChild ...int) *Composer

New allocates new Composer and pre-allocates space for given amount of arguments and expressions. Each child expression passed to the constructor creates new child Composer with pre-allocated space for expressions.

func (*Composer) AddArg

func (c *Composer) AddArg(arg interface{})

AddArg add static argument.

func (*Composer) AddExpr

func (c *Composer) AddExpr(key, operator string, value interface{})

AddExpr adds expression if value meet certain requirements. To know more please read the source code.

func (*Composer) Args

func (c *Composer) Args() []interface{}

Args returns slice of arguments that was passed to the composer or to any child.

func (*Composer) Compose

func (c *Composer) Compose(nbOfChildExpressions ...int) (comp *Composer)

Compose returns next available composer or if pool of pre-allocated Composer's is empty allocates new one.

func (*Composer) First

func (b *Composer) First() bool

First returns true cursor is on first position.

func (*Composer) Key

func (b *Composer) Key() string

Key returns key for current cursor position.

func (*Composer) Len

func (b *Composer) Len() int

Len returns number of expressions that was passed.

func (*Composer) Next

func (b *Composer) Next() bool

Next move cursor to next position. Returns false if it's not possible.

func (*Composer) Oper

func (b *Composer) Oper() string

Oper returns operator for current cursor position.

func (*Composer) PlaceHolder

func (b *Composer) PlaceHolder() string

PlaceHolder returns placeholder for current cursor position.

func (*Composer) Reset

func (b *Composer) Reset()

Reset set cursor back to 0.

Jump to

Keyboard shortcuts

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