stmt

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 8 Imported by: 0

README

Usage Prepare Query

no case sensitive

Query

WHERE condition

operator keyword
AND and
OR or
NOT not
= eq, equal
> gt
< lt
ge, gte
le, lte
LIKE like
IS NULL isnull
IN in
BETWEEN between
Query Example
  • WHERE foo = 123

    {"EQ" : {"foo" : 123}}
    
    (EQ foo 123)
    
  • WHERE foo = "bar"

    {"EQ" : {"foo" : "bar"}}
    
    (EQ foo "bar")
    or
    (EQ foo bar)
    
  • WHERE foobar = "foo bar"

    {"EQ" : {"foo" : "foo bar"}}
    
    (EQ foobar "foo bar")
    
  • WHERE (foo = 123)

    {"AND" : {"EQ" : {"foo" : 123}}}
    
    (AND (EQ foo 123))
    
  • WHERE NOT foo = 123

    {"NOT" : {"EQ" : {"foo" : 123}}}
    
    (NOT (EQ foo 123))
    
  • WHERE (foo = 123 AND bar = 456)

    {"AND" : [{"EQ" : {"foo" : 123}}, {"EQ" : {"bar" : 456}}]}
    
    (AND (EQ foo 123) (EQ bar 456))
    
  • WHERE (foo = 123 OR bar = 456)

    {"OR" : [{"EQ" : {"foo" : 123}}, {"EQ" : {"bar" : 456}}]}
    
    (OR (EQ foo 123) (EQ bar 456))
    
  • WHERE (foo = 123 AND bar = 456 AND (baz = 789 OR foobar = 123456))

    {"AND" : [{"EQ" : {"foo" : 123}}, {"EQ" : {"bar" : 456}}, {"OR" : [{"EQ" : {"baz" : 789}}, {"EQ" : {"foobar" : 123456}}]}]}
    
    (AND (EQ foo 123) (EQ bar 456) (OR (EQ baz 789) (EQ foobar 123456)))
    
  • WHERE foo > 123

    {"GT" : {"foo" : 123}}
    
    (GT foo 123)
    
  • WHERE foo < 123

    {"LT" : {"foo" : 123}}
    
    (LT foo 123)
    
  • WHERE foo ≥ 123

    {"GE" : {"foo" : 123}}
    
    (GE foo 123) 
    
  • WHERE foo ≤ 123

    {"LE" : {"foo" : 123}}
    
    (LE foo 123) 
    
  • WHERE foo LIKE 'string%'

    {"LIKE" : {"foo" : "string%"}}
    
    (LIKE foo "string%") 
    or 
    (LIKE foo string%) 
    
  • WHERE foo IS NULL

    {"ISNULL" : "foo"}
    
    (ISNULL foo) 
    
  • WHERE foo IN (123)

    {"IN" : {"foo" : 123}}
    
    {"IN" : {"foo" : [123]}}
    
    (IN foo 123) 
    or
    (IN foo `(123)) 
    
  • WHERE foo IN (123, 456, 789)

    {"IN" : {"foo" : [123, 456, 789]}}
    
    (IN foo 123 456 789) 
    or
    (IN foo `(123 456 789)) 
    
  • WHERE foo BETWEEN 123 AND 456

    {"BETWEEN" : {"foo" : [123, 456]}}
    
    (BETWEEN foo 123 456) 
    or
    (BETWEEN foo `(123 456)) 
    

Pagination

LIMIT [OFFSET, ] ROW_COUNT

operator keyword
ROW_COUNT LIMIT
PAGE PAGE
OFFSET (PAGE - 1) * LIMIT
Pagination Example
  • LIMIT 0, 255

    {}
    
  • LIMIT 10

    {"limit":10}
    
  • LIMIT 0, 10

    {"limit" : 10, "page" : 1}
    

Order

ORDER BY column_1, column_2 [ASC|DESC]

operator keyword
ASC ASC
DESC DESC
Order Example
  • ORDER BY column_name_1 ASC

    {"ASC" : "column_name_1"}
    
  • ORDER BY column_name_1, column_name_2 DESC

    {"DESC" : ["column_name_1", "column_name_2"]}
    
  • ORDER BY column_name_1, column_name_2 ASC

    {"ASC" : ["column_name_1", "column_name_2"]}
    
  • ORDER BY column_name_1 ASC, column_name_2 DESC

    [{"ASC" : "column_name_1"}, {"DESC" : "column_name_2"}]
    
  • ORDER BY column_name_1, column_name_2 ASC, column_name_3 DESC

    [{"ASC" : ["column_name_1", "column_name_2"]}, {"DESC" : "column_name_2"}]
    

Documentation

Index

Constants

View Source
const (
	DEFAULT_PAGINATION_LIMIT = math.MaxInt8
	DEFAULT_PAGINATION_PAGE  = 1
)

Variables

View Source
var (
	ErrorInvalidArgumentEmptyString = fmt.Errorf("empty string")
	ErrorInvalidArgumentEmptyObject = fmt.Errorf("empty object")
	ErrorNotFoundHandler            = fmt.Errorf("not found handler")
	ErrorUnsupportedType            = fmt.Errorf("unsupported type")
	ErrorUnsupportedPaginationKeys  = fmt.Errorf("unsupported pagination keys")
	ErrorUnsupportedOrderKeys       = fmt.Errorf("unsupported order keys")
)
View Source
var (

	// export for resolver's SetConditionStmtBuilder
	SetConditionStmtBuilder = collection.SetConditionStmtBuilder
	// export for resolver's SetOrderStmtBuilder
	SetOrderStmtBuilder = collection.SetOrderStmtBuilder
	// export for resolver's SetPaginationStmtBuilder
	SetPaginationStmtBuilder = collection.SetPaginationStmtBuilder

	// get condition resolver
	GetConditionStmtBuilder = collection.ConditionStmtBuilder
	// get order resolver
	GetOrderStmtBuilder = collection.OrderStmtBuilder
	// get pagination resolver
	GetPaginationStmtBuilder = collection.PaginationStmtBuilder
)
View Source
var ConditionLexer = new(conditionLexer)
View Source
var OrderLexer = new(orderLexer)
View Source
var PaginationLexer = new(paginationLexer)

Functions

func BackQuote

func BackQuote(exp string) string

func CauseIter

func CauseIter(err error, iter func(er error)) error

func ErrorCompose

func ErrorCompose(a, b error) error

func MapKeys

func MapKeys(m map[string]interface{}) []string

func MapQuote

func MapQuote(mapper func(string) string) func(src ...string) []string

func Quote

func Quote(exp string) string

func Repeat

func Repeat(n int, s string) []string

Types

type ConditionBuildEngine

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

ConditionBuildEngine

implement of ConditionStmtResolver

func NewConditionBuildEngine

func NewConditionBuildEngine(dialect string, builtinSet map[string]ConditionFunctor) *ConditionBuildEngine

func (ConditionBuildEngine) Build

func (engine ConditionBuildEngine) Build(v interface{}) (ConditionResult, error)

func (ConditionBuildEngine) Dialect

func (engine ConditionBuildEngine) Dialect() string

type ConditionFunctor

type ConditionFunctor = func(v interface{}) (ConditionResult, error)

type ConditionResult

type ConditionResult interface {
	Query() string
	Args() []interface{}
}

type ConditionStmt

type ConditionStmt map[string]interface{}

func And

func And(e ...map[string]interface{}) ConditionStmt

func Equal

func Equal(a string, b interface{}) ConditionStmt

func GT

func GT(a string, b interface{}) ConditionStmt

func GTE

func GTE(a string, b interface{}) ConditionStmt

func In

func In(a string, b ...interface{}) ConditionStmt

func IsNull

func IsNull(a string) ConditionStmt

func LT

func LT(a string, b interface{}) ConditionStmt

func LTE

func LTE(a string, b interface{}) ConditionStmt

func Like

func Like(a string, b interface{}) ConditionStmt

func Not

func Not(b interface{}) ConditionStmt

func Or

func Or(e ...map[string]interface{}) ConditionStmt

func (ConditionStmt) Build

func (condition ConditionStmt) Build(engine ConditionBuildEngine) (ConditionResult, error)

func (ConditionStmt) Keys

func (condition ConditionStmt) Keys() []string

type ConditionStmtBuilder

type ConditionStmtBuilder interface {
	Build(v interface{}) (ConditionResult, error)
	Dialect() string
}

type ConditionStmtResolver

type ConditionStmtResolver = interface {
	ConditionStmtBuilder
	ConditoinContent
}

type ConditionStmtResolverSet

type ConditionStmtResolverSet = map[string]ConditionStmtResolver

type ConditoinContent

type ConditoinContent interface {
	And(v interface{}) (ConditionResult, error)
	Or(v interface{}) (ConditionResult, error)
	Not(v interface{}) (ConditionResult, error)
	Equal(v interface{}) (ConditionResult, error)
	GreaterThan(v interface{}) (ConditionResult, error)
	LessThan(v interface{}) (ConditionResult, error)
	GreaterThanOrEqual(v interface{}) (ConditionResult, error)
	LessThanOrEqual(v interface{}) (ConditionResult, error)
	Like(v interface{}) (ConditionResult, error)
	IsNull(v interface{}) (ConditionResult, error)
	In(v interface{}) (ConditionResult, error)
	Between(v interface{}) (ConditionResult, error)
}

type FakeConditionStmtBuildEngine

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

func (FakeConditionStmtBuildEngine) Build

func (engine FakeConditionStmtBuildEngine) Build(v interface{}) (ConditionResult, error)

func (FakeConditionStmtBuildEngine) Dialect

func (engine FakeConditionStmtBuildEngine) Dialect() string

type FakeOrderStmtBuildEngine

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

func (FakeOrderStmtBuildEngine) Build

func (engine FakeOrderStmtBuildEngine) Build(v interface{}) (OrderResult, error)

func (FakeOrderStmtBuildEngine) Dialect

func (engine FakeOrderStmtBuildEngine) Dialect() string

type FakePaginationStmtBuildEngine

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

func (FakePaginationStmtBuildEngine) Build

func (engine FakePaginationStmtBuildEngine) Build(v interface{}) (PaginationResult, error)

func (FakePaginationStmtBuildEngine) Dialect

func (engine FakePaginationStmtBuildEngine) Dialect() string

type OrderBuildEngine

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

OrderBuildEngine

implement of OrderStmtResolver

func NewOrderBuildEngine

func NewOrderBuildEngine(dialect string, builtinSet map[string]OrderFunctor) *OrderBuildEngine

func (OrderBuildEngine) Build

func (engine OrderBuildEngine) Build(v interface{}) (OrderResult, error)

func (OrderBuildEngine) Dialect

func (engine OrderBuildEngine) Dialect() string

type OrderContent

type OrderContent interface {
	Asc(v interface{}) (OrderResult, error)
	Desc(v interface{}) (OrderResult, error)
}

type OrderFunctor

type OrderFunctor = func(v interface{}) (OrderResult, error)

type OrderResult

type OrderResult interface {
	Order() string
	// Order() string
	Combine(OrderResult) OrderResult
}

type OrderStmt

type OrderStmt []TypeOrderStmt

OrderStmt

map[string][]string{"ASC": []string{column1, column2}, "DESC": []string{column3, column4} }

func Asc

func Asc(columns ...string) OrderStmt

func Desc

func Desc(columns ...string) OrderStmt

func (OrderStmt) Asc

func (order OrderStmt) Asc(columns ...string) OrderStmt

func (OrderStmt) Build

func (order OrderStmt) Build(engine OrderBuildEngine) (OrderResult, error)

func (OrderStmt) Desc

func (order OrderStmt) Desc(columns ...string) OrderStmt

func (OrderStmt) Keys

func (order OrderStmt) Keys() []string

type OrderStmtBuilder

type OrderStmtBuilder interface {
	Build(v interface{}) (OrderResult, error)
	Dialect() string
}

type OrderStmtResolver

type OrderStmtResolver = interface {
	OrderStmtBuilder
	OrderContent
}

type OrderStmtResolverSet

type OrderStmtResolverSet = map[string]OrderStmtResolver

type PaginationBuildEngine

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

PaginationBuildEngine

implement of PaginationStmtResolver

func NewPaginationBuildEngine

func NewPaginationBuildEngine(dialect string, builtinSet map[string]PaginationFunctor) *PaginationBuildEngine

func (PaginationBuildEngine) Build

func (engine PaginationBuildEngine) Build(v interface{}) (PaginationResult, error)

func (PaginationBuildEngine) Dialect

func (engine PaginationBuildEngine) Dialect() string

type PaginationContent

type PaginationContent interface {
	Limit(v interface{}) (PaginationResult, error)
	Page(v interface{}) (PaginationResult, error)
}

type PaginationFunctor

type PaginationFunctor = func(v interface{}) (PaginationResult, error)

type PaginationResult

type PaginationResult interface {
	String() string
	Limit() (int, bool)
	Page() (int, bool)
	Offset() int
	SetLimit(int)
	SetPage(int)
}

type PaginationStmt

type PaginationStmt map[string]int

func Limit

func Limit(limit int, page ...int) PaginationStmt

func (PaginationStmt) Build

func (PaginationStmt) Limit

func (page PaginationStmt) Limit() (int, bool)

func (PaginationStmt) Page

func (page PaginationStmt) Page() (int, bool)

func (PaginationStmt) SetLimit

func (page PaginationStmt) SetLimit(l int) (PaginationStmt, error)

func (PaginationStmt) SetPage

func (page PaginationStmt) SetPage(p int) (PaginationStmt, error)

type PaginationStmtBuilder

type PaginationStmtBuilder interface {
	Build(v interface{}) (PaginationResult, error)
	Dialect() string
}

type PaginationStmtResolver

type PaginationStmtResolver = interface {
	PaginationStmtBuilder
	PaginationContent
}

type PaginationStmtResolverSet

type PaginationStmtResolverSet = map[string]PaginationStmtResolver

type ResolverSelector

type ResolverSelector interface {
	Dialect() string
}

type TypeMap

type TypeMap = map[string]interface{}

func Map

func Map(a string, b interface{}) TypeMap

Map

type TypeOrderStmt

type TypeOrderStmt = map[string][]string

type TypeSlice

type TypeSlice = []interface{}

func Slice

func Slice(e interface{}, es ...interface{}) TypeSlice

Slice

Directories

Path Synopsis
dialects
internal

Jump to

Keyboard shortcuts

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