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

query

package
v1.8.87 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 4 Imported by: 0

README

Query

Performance

# https://graphviz.org/download/#mac
brew install graphviz

go test -bench=. ./core/lib/utils/query/testgen/. -cpuprofile cpu.prof
go tool pprof -svg cpu.prof > cpu.svg

go test -bench=. -trace trace.out ./core/lib/utils/query/testgen/.
go tool trace trace.out

# Data Race: Two goroutines access the same variable concurrently 
# and at least one of the accesses is a write 
# https://golang.org/doc/articles/race_detector
go test -race

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapeString added in v1.8.54

func EscapeString(value string) string

func Union

func Union(queries ...*Q) (string, error)

Types

type Column added in v1.8.23

type Column string

type Field added in v1.8.76

type Field struct {
	FieldType FieldType
	Name      Column
	As        string
	Raw       string
}

func NewField added in v1.8.76

func NewField(fieldType FieldType, column Column, opts ...string) *Field

NewField creates a new field.

NewField(FieldTypeBasic, "Foo")
NewField(FieldTypeBasic, "Foo", "Bar") <-- `Foo` AS `Bar`

func NewRawField added in v1.8.76

func NewRawField(raw string) *Field

NewRawField creates a new field.

NewRawField("`t`.`Foo` AS `Bar`)

type FieldType added in v1.8.76

type FieldType int
const (
	FieldTypeBasic FieldType = iota
	FieldTypeRaw
	FieldTypeCount
	FieldTypeSum
	FieldTypeMin
	FieldTypeMax
)

type IModel

type IModel interface {
	Table_Name() TableName
	Table_Columns() []Column
	Table_PrimaryKey() Column
	Table_PrimaryKey_Value() int64
	Table_InsertColumns() []Column
	Table_UpdateColumns() []Column
	Table_Column_Types() map[Column]string
	String() string
	Update(db db.IDB) error
	Create(db db.IDB) error
	Delete(db db.IDB) error
	FromID(db db.IDB, id int64) (IModel, error)
}

type OrderDir added in v1.8.32

type OrderDir int
const (
	OrderDirASC OrderDir = iota
	OrderDirDESC
)

func OrderDirFromString added in v1.8.33

func OrderDirFromString(s string) OrderDir

func (OrderDir) String added in v1.8.32

func (q OrderDir) String() string

type Q

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

func Delete

func Delete(model IModel) *Q

func Insert

func Insert(model IModel) *Q

func Query

func Query(model IModel) *Q

func Raw added in v1.8.31

func Raw(model IModel, query string) *Q

func Select

func Select(model IModel) *Q

func Update

func Update(model IModel) *Q

func (*Q) Alias

func (q *Q) Alias(alias string) *Q

func (*Q) Count

func (q *Q) Count(name Column, as string) *Q

Count creates a count statement

q.Count(query.Column("Foo"), "FooCounted")
COALESCE(COUNT(`t`.`Foo`), 0) AS `FooCounted`

func (*Q) Field

func (q *Q) Field(name Column) *Q

Field includes a specific field in the columns to be returned by a result set

func (*Q) FieldAs

func (q *Q) FieldAs(name Column, as string) *Q

FieldAs includes a specific field in the columns to be returned by a set aliased by `as`

func (*Q) FieldRaw

func (q *Q) FieldRaw(fieldStr, as string) *Q

FieldRaw allows for an arbitrary string (e.g. "NOW()") to be included in the select columns

func (*Q) Fields

func (q *Q) Fields(fields ...*Field) *Q

Fields injects fields as raw strings into the field clause of the query

sql, e := query.Select(&testassets.Job{}).
	Fields(
		NewField(FieldTypeBasic, "JobID"),
		NewField(FieldTypeBasic, "Name", "Foo"),
	)

func (*Q) FromFieldToString added in v1.8.76

func (q *Q) FromFieldToString(field *Field) string

func (*Q) Limit

func (q *Q) Limit(limit, offset int64) *Q

func (*Q) LimitPage added in v1.8.81

func (q *Q) LimitPage(limit, page int64) *Q

func (*Q) Max added in v1.8.57

func (q *Q) Max(name Column, as string) *Q

Max creates a max statement

q.Max(query.Column("Foo"), "MaxFoo")
COALESCE(MAX(`t`.`Foo`), 0) AS `MaxFoo`

func (*Q) Min added in v1.8.57

func (q *Q) Min(name Column, as string) *Q

Min creates a min statement

q.Min(query.Column("Foo"), "MinFoo")
COALESCE(MIN(`t`.`Foo`), 0) AS `MinFoo`

func (*Q) OrderBy

func (q *Q) OrderBy(col Column, dir OrderDir) *Q

func (*Q) Raw added in v1.8.31

func (q *Q) Raw(query string) *Q

func (*Q) Set

func (q *Q) Set(fieldName Column, value interface{}) *Q

func (*Q) String

func (q *Q) String() (string, error)

func (*Q) Sum

func (q *Q) Sum(name Column, as string) *Q

Sum creates a sum statement

q.Sum(query.Column("Foo"), "FooSummed")
COALESCE(SUM(`t`.`Foo`), 0) AS `FooSummed`

func (*Q) Where

func (q *Q) Where(args ...*WherePart) *Q

Where creates or adds to an existing where clause

  • Simple q.Where(query.EQ(query.Column("A"), "B")) WHERE `t`.`B` = `t`.`B`

  • Multiple Arguments q.Where(query.EQ(query.Column("A"), "B"), query.And(), query.EQ(query.Column("C"), "D")) WHERE `t`.`A` = 'B' AND `t`.`C` = 'D'

  • Daisy Chain q.Where(query.EQ(query.Column("A"), "B")).Where(query.And()).Where(query.EQ(query.Column("C"), "D")) WHERE `t`.`A` = 'B' AND `t`.`C` = 'D'

  • Separate lines q.Where(query.EQ(query.Column("A"), "B")) q.Where(query.And()) q.Where(query.EQ(query.Column("C"), "D")) WHERE `t`.`A` = 'B' AND `t`.`C` = 'D'

type QueryErrorType added in v1.8.51

type QueryErrorType string
const (
	QUERY_ERROR_INVALID_VALUE      QueryErrorType = "Invalid value"
	QUERY_ERROR_INVALID_COLUMN     QueryErrorType = "Invalid Column Name"
	QUERY_ERROR_EMPTY_WHERE_CLAUSE QueryErrorType = "Empty where clause"
)

type QueryType

type QueryType int
const (
	QueryTypeNotSet QueryType = iota
	QueryTypeSelect
	QueryTypeRaw
	QueryTypeUpdate
	QueryTypeDelete
	QueryTypeInsert
)

type TableName added in v1.8.23

type TableName string

type WherePart

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

WherePart is a part of a where clause. This object is an exposed part of the api to make conditional queries easier EXAMPLE:

wheres := []query.WherePart{
	query.EQ(models.ObjectRelationship_Column_IsDeleted, 0),
}
if objectTypeFrom != constants.ObjectTypeUnknown {
	wheres = append(wheres, query.And(), query.EQ(models.ObjectRelationship_Column_ObjectTypeFrom, objectTypeFrom))
}
if objectIDFrom > 0 {
	wheres = append(wheres, query.And(), query.EQ(models.ObjectRelationship_Column_ObjectIDFrom, objectIDFrom))
}

func And

func And(args ...*WherePart) *WherePart

And is an and statement with optional args that, if provided, are wrapped in parentheses Example: And() will result in the word `AND` being added to the where clause Example: And(EQ(1, 1), And(), And(2, 2)) will result in `AND ( 1 = 1 AND 2 = 2 )`

func Ands added in v1.8.45

func Ands(args ...*WherePart) *WherePart

Ands takes a list of args and separes them all by `AND` Example: Ands(query.EQ(1,1), query.EQ(2,2), query.EQ(3,3)) == 1 = 1 AND 2 = 2 AND 3 = 3

func Between

func Between(fieldName Column, from, to interface{}) *WherePart

Between is a BETWEEN statement Example: Between("")

func BitAnd added in v1.8.31

func BitAnd(fieldName Column, a, b int64) *WherePart

BitAnd `t`.`Field` & a = b Example: query.BitAnd("foo", 2, 1) -> `t`.`Foo` & 2 = 1

func EQ

func EQ(fieldName Column, value interface{}) *WherePart

EQ is an equals statement between a table column and a value

func EQF

func EQF(fieldName1, fieldName2 string) *WherePart

EQF allows for one column to be equal to another Example for a subselect

query.Select(&models.UserGroupUser{}).Alias("ugu").FieldRaw("1", "n").Where(

query.EQF("UserID", "`u`.`UserID`"),
query.And(),
query.EQ("UserGroupID", groupID),
query.And(),
query.EQ("IsDeleted", 0),

),

func Exists

func Exists(clause *Q) *WherePart

Exists is a where clause for the SQL EXISTS statement

func GT

func GT(fieldName Column, value interface{}) *WherePart

GT is a greater than statement between a table column and a value

func GTOE

func GTOE(fieldName Column, value interface{}) *WherePart

LT is a greater than or equals statement (>=) between a table column and a value

func IN

func IN(fieldName Column, values ...interface{}) *WherePart

IN is an IN clause Example: query.IN("col1", "foo", "bar", "baz")

func INInt added in v1.8.85

func INInt(fieldName Column, values []int) *WherePart

INInt is a helper function for converting a slice of string arguments into a slice of interface arguments, passed into an IN clause and returned

func INInt64 added in v1.8.15

func INInt64(fieldName Column, values []int64) *WherePart

INInt64 is a helper function for converting a slice of string arguments into a slice of interface arguments, passed into an IN clause and returned

func INString added in v1.8.15

func INString(fieldName Column, values []string) *WherePart

INString is a helper function for converting a slice of string arguments into a slice of interface arguments, passed into an IN clause and returned

func LT

func LT(fieldName Column, value interface{}) *WherePart

LT is a less than statement between a table column and a value

func LTOE

func LTOE(fieldName Column, value interface{}) *WherePart

LTE is a less than or equals (<=) statement between a table column and a value

func Like added in v1.8.16

func Like(fieldName Column, value string) *WherePart

func Mod added in v1.8.31

func Mod(fieldName Column, value, remainder int64) *WherePart

Mod is applies modulo operation on column and value testing if it equals remainder

MOD(`t`.`Field`, value) = remainder

func Modf added in v1.8.31

func Modf(value int64, fieldName Column, remainder int64) *WherePart

Modf MOD(value, `t`.`Field`) = remainder Example: query.Mod("foo", 2, 1) -> `t`.`Foo` % 2 = 1

func NE

func NE(fieldName Column, value interface{}) *WherePart

NE is a not equals statement between a table column and a value

func NOTIN added in v1.8.33

func NOTIN(fieldName Column, values ...interface{}) *WherePart

NOTIN is an NOT IN clause Example: query.NOTIN("col1", "foo", "bar", "baz") Example: queyr.NOTIN("col2", 1, 2, 3)

func NotExists added in v1.8.79

func NotExists(clause *Q) *WherePart

Exists is a where clause for the SQL EXISTS statement

func NotLike added in v1.8.16

func NotLike(fieldName Column, value string) *WherePart

func Or

func Or(args ...*WherePart) *WherePart

func Ors added in v1.8.45

func Ors(args ...*WherePart) *WherePart

Ors takes a list of args and separes them all by `OR` Example: Ors(query.EQ(1,1), query.EQ(2,2), query.EQ(3,3)) == 1 = 1 OR 2 = 2 OR 3 = 3

func PE

func PE() *WherePart

Parenthesis End

func PS

func PS() *WherePart

Parenthesis Start

func Paren added in v1.8.45

func Paren(args ...*WherePart) *WherePart

func Rawf added in v1.8.31

func Rawf(str string, args ...interface{}) *WherePart

Rawf is a raw SQL statement Example: query.Rawf("`t`.`LastRunDate` + 60000 < %d", seconds)),

func WhereAll added in v1.8.12

func WhereAll() *WherePart

WhereAll adds a WHERE clause of `1=1` used for convenience when conditionally adding WHERE clauses starting with a conjunction (AND/OR,etc) separating them. e.g. SELECT * FROM `Foo` WHERE 1=1

SELECT * FROM `Foo` WHERE 1=1 AND FooID = 123;

type WhereType

type WhereType int
const (
	WhereTypeEquals WhereType = iota
	WhereTypeEqualsField
	WhereTypeNotEquals
	WhereTypeGreaterThan
	WhereTypeLessThan
	WhereTypeGreaterThanOrEqualTo
	WhereTypeLessThanOrEqualTo
	WhereTypeBetween
	WhereTypeLike
	WhereTypeNotLike
	WhereTypeIN
	WhereTypeNotIN
	WhereTypeExists
	WhereTypeNotExists
	WhereTypeAnd
	WhereTypeOr
	WhereTypeParenthesisEnd
	WhereTypeParenthesisStart
	// WhereTypeNone indicates that the wherePart is a noop for the query,
	// If, however, it contains any child clauses, they will be parsed as individual wherePart objects
	WhereTypeNone
	// WhereTypeAll is a WHERE clause of `1=1` used for convenience
	// when conditionally adding WHERE clauses starting with a conjunction (AND/OR,etc)
	// separating them.
	// e.g. SELECT * FROM `Foo` WHERE 1=1
	//      SELECT * FROM `Foo` WHERE 1=1 AND FooID = 123;
	WhereTypeAll
	WhereTypeMod
	WhereTypeModF
	WhereTypeBitAnd
	WhereTypeRaw
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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