query

package module
v0.0.0-...-db17bf9 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: MIT Imports: 7 Imported by: 0

README

query

Go SQL query builder

test Go Reference

Questions or Feedback?

You can use Github Issues for feedback or questions.

License

This library is distributed under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Now

func Now() driver.Valuer

Types

type Builder

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

func NewBuilder

func NewBuilder(dialect Dialect) *Builder

func (*Builder) BulkInsert

func (b *Builder) BulkInsert(table string, columns []string) *BulkInsert

func (*Builder) BulkUpsert

func (b *Builder) BulkUpsert(table string, columns []string, conflictColumn []string) *BulkInsert

func (*Builder) Delete

func (b *Builder) Delete(table string, where Where) *Delete

Explicitly takes a WHERE, since you'll almost always do this.

You can always pass All() in case you want to truncate the table, but at least that way it's obvious.

func (*Builder) Insert

func (b *Builder) Insert(table string) *InsertUpdate

func (*Builder) Select

func (b *Builder) Select(fields, table string, args ...any) *Select

func (*Builder) Update

func (b *Builder) Update(table string, where Where) *InsertUpdate

func (*Builder) Upsert

func (b *Builder) Upsert(table string, conflictColumn ...string) *InsertUpdate

type BulkInsert

type BulkInsert struct {
	Table   string
	Columns []string
	Dialect Dialect
	Values  [][]any
	// contains filtered or unexported fields
}

func (*BulkInsert) Add

func (i *BulkInsert) Add(values ...any) error

func (*BulkInsert) Count

func (i *BulkInsert) Count() int

func (*BulkInsert) ToSQL

func (i *BulkInsert) ToSQL() (string, []any)

type Delete

type Delete struct {
	Table   string
	Dialect Dialect
	// contains filtered or unexported fields
}

func (*Delete) SetDialect

func (d *Delete) SetDialect(dialect Dialect) *Delete

func (*Delete) ToSQL

func (d *Delete) ToSQL() (string, []any)

func (*Delete) Where

func (d *Delete) Where(where Where) *Delete

type Dialect

type Dialect interface {
	Placeholder(idx int) string
	UseLastInsertId() bool
	MakeUpsert(table string, conflictColumn []string, fields []fieldValue, rows int) string
}

func DialectFromString

func DialectFromString(dialect string) (Dialect, error)

type InsertUpdate

type InsertUpdate struct {
	Table string
	// contains filtered or unexported fields
}

func (*InsertUpdate) Add

func (i *InsertUpdate) Add(key string, value any) *InsertUpdate

func (*InsertUpdate) AddSelect

func (i *InsertUpdate) AddSelect(key string, s *Select) *InsertUpdate

func (*InsertUpdate) Clauses

func (i *InsertUpdate) Clauses() map[string]any

func (*InsertUpdate) HasClauses

func (i *InsertUpdate) HasClauses() bool

func (*InsertUpdate) Returning

func (i *InsertUpdate) Returning(field string) *InsertUpdate

func (*InsertUpdate) Select

func (i *InsertUpdate) Select(s *Select) *InsertUpdate

func (*InsertUpdate) ToSQL

func (i *InsertUpdate) ToSQL() (string, []any)

func (*InsertUpdate) With

func (i *InsertUpdate) With(obj any, opts ...WithOpt) *InsertUpdate

type InsertUpdateOptions

type InsertUpdateOptions struct {
	CopyAutoIncrement bool
	CopyReadOnly      bool
}

type Join

type Join struct {
	Join  string
	Table string
	On    Where
}

type MySQLDialect

type MySQLDialect struct {
}

Generates queries using question marks

func (MySQLDialect) MakeUpsert

func (d MySQLDialect) MakeUpsert(table string, conflictColumn []string, fields []fieldValue, rows int) string

func (MySQLDialect) Placeholder

func (d MySQLDialect) Placeholder(idx int) string

func (MySQLDialect) UseLastInsertId

func (d MySQLDialect) UseLastInsertId() bool

type Options

type Options struct {
	Where   Where
	Limit   int64
	Offset  int64
	GroupBy string
	OrderBy []string
	Having  Where

	Args []any
}

func WhereID

func WhereID(v any) *Options

Helper that only selects a single ID

func (*Options) Merge

func (o *Options) Merge(opts *Options) *Options

type PostgreSQLDialect

type PostgreSQLDialect struct {
}

Generates queries using numbered placeholders

func (PostgreSQLDialect) MakeUpsert

func (d PostgreSQLDialect) MakeUpsert(table string, conflictColumn []string, fields []fieldValue, rows int) string

func (PostgreSQLDialect) Placeholder

func (d PostgreSQLDialect) Placeholder(idx int) string

func (PostgreSQLDialect) UseLastInsertId

func (d PostgreSQLDialect) UseLastInsertId() bool

type Select

type Select struct {
	Dialect Dialect
	Fields  string
	Table   string
	Options Options
	Joins   []Join
	Unions  []*Select
	CTEs    []With
	Args    []any
}

func (*Select) GroupBy

func (s *Select) GroupBy(field string) *Select

func (*Select) Having

func (s *Select) Having(where Where) *Select

func (*Select) Join

func (s *Select) Join(table string, on Where) *Select

func (*Select) LeftJoin

func (s *Select) LeftJoin(table string, on Where) *Select

func (*Select) Limit

func (s *Select) Limit(limit int64) *Select

func (*Select) Offset

func (s *Select) Offset(offset int64) *Select

func (*Select) OrderBy

func (s *Select) OrderBy(fields ...string) *Select

func (*Select) OrderByDesc

func (s *Select) OrderByDesc(field string) *Select

func (*Select) OrderByDir

func (s *Select) OrderByDir(field string, desc bool) *Select

func (*Select) ToSQL

func (s *Select) ToSQL() (string, []any)

func (*Select) Union

func (s *Select) Union(o *Select) *Select

func (*Select) Where

func (s *Select) Where(where Where) *Select

func (*Select) With

func (s *Select) With(o With) *Select

type SqliteDialect

type SqliteDialect struct {
}

Generates queries using numbered placeholders

func (SqliteDialect) MakeUpsert

func (d SqliteDialect) MakeUpsert(table string, conflictColumn []string, fields []fieldValue, rows int) string

func (SqliteDialect) Placeholder

func (d SqliteDialect) Placeholder(idx int) string

func (SqliteDialect) UseLastInsertId

func (d SqliteDialect) UseLastInsertId() bool

type Where

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

func All

func All() Where

func And

func And(w ...Where) Where

func Any

func Any(subQuery *Select) Where

func ArrayOverlaps

func ArrayOverlaps(field string, subQuery *Select) Where

func Exists

func Exists(subQuery *Select) Where

func Expr

func Expr(expr string, args ...any) Where

func FieldEquals

func FieldEquals(field string, value any) Where

func FieldGreaterOrEqualThan

func FieldGreaterOrEqualThan(field string, value any) Where

func FieldGreaterThan

func FieldGreaterThan(field string, value any) Where

func FieldILike

func FieldILike(field string, value any) Where

func FieldIn

func FieldIn(field string, values []any) Where

func FieldLessOrEqualThan

func FieldLessOrEqualThan(field string, value any) Where

func FieldLessThan

func FieldLessThan(field string, value any) Where

func FieldLike

func FieldLike(field string, value any) Where

func FieldNotEquals

func FieldNotEquals(field string, value any) Where

func FieldNotIn

func FieldNotIn(field string, values []any) Where

func FieldOp

func FieldOp(field, op string, value any) Where

func IDEquals

func IDEquals(v any) Where

Helper that filters only on ID

func IDIn

func IDIn(values []any) Where

func In

func In(field string, subQuery *Select) Where

func IntFieldIn

func IntFieldIn[T ~int64 | ~int32 | ~int](field string, values []T) Where

func IntFieldNotIn

func IntFieldNotIn(field string, values []int64) Where

func IsNotNull

func IsNotNull(field string) Where

func IsNull

func IsNull(field string) Where

func Or

func Or(w ...Where) Where

func StringFieldIn

func StringFieldIn(field string, values []string) Where

func StringFieldNotIn

func StringFieldNotIn(field string, values []string) Where

func (Where) Generate

func (w Where) Generate(offset int, dialect Dialect) (string, []any)

func (Where) IsEmpty

func (w Where) IsEmpty() bool

type With

type With struct {
	Name      string
	SubSelect *Select
}

type WithOpt

type WithOpt func(o *InsertUpdateOptions)

func WithAutoIncrement

func WithAutoIncrement() WithOpt

func WithReadOnly

func WithReadOnly() WithOpt

Jump to

Keyboard shortcuts

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