qry

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

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 6 Imported by: 0

README

qry

Go Report Card PkgGoDev Test codecov GitHub License GitHub tag (latest by date)

SQL builder and repository for SQL query generation and execution.

Scan and pass arguments manually or use generics to automatically map data into your models.

Please note that qry is in active development and API changes are possible, although effort will be put into keeping the API stable.

Table of contents

Quickstart

Installation with go modules is simple.

go get github.com/TomWright/qry

Stargazers over time

Stargazers over time

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Condition

type Condition interface {
	// Build returns an SQL statement and the related args.
	Build() (string, []any)
}

Condition is a condition that can be used in a where clause.

func And

func And(conditions ...Condition) Condition

And returns a ConditionGroup made up of many Conditions separated an AND.

func Equal

func Equal(field Field, value any) Condition

Equal returns a SimpleCondition that will check that the given field has the given value.

func JsonArrayContains

func JsonArrayContains(field Field, value any) Condition

JsonArrayContains returns a Condition that will check if the given value exists in a JSON array stored under the given field.

func NotEqual

func NotEqual(field Field, value any) Condition

NotEqual returns a SimpleCondition that will check that the given field does not have the given value.

func NotJsonArrayContains

func NotJsonArrayContains(field Field, value any) Condition

NotJsonArrayContains returns a Condition that will check that the given value does not exist in a JSON array stored under the given field.

func Or

func Or(conditions ...Condition) Condition

Or returns a ConditionGroup made up of many Conditions separated an OR.

type ConditionGroup

type ConditionGroup struct {
	Conditions []Condition
	Or         bool
}

ConditionGroup is a Condition made up of many Conditions separated by an AND or an OR.

func (*ConditionGroup) Build

func (group *ConditionGroup) Build() (string, []any)

Build returns an SQL statement and the related args. The statement is already wrapped in brackets.

type DeleteQuery

type DeleteQuery struct {
	Table     string
	Condition Condition
	Limit     int64
	Offset    int64
}

func Delete

func Delete() DeleteQuery

func (DeleteQuery) Build

func (query DeleteQuery) Build() (string, []any)

type Direction

type Direction string
const Ascending Direction = "ASC"
const Descending Direction = "DESC"

func (Direction) String

func (f Direction) String() string

type Field

type Field string

func (Field) String

func (f Field) String() string

type InsertQuery

type InsertQuery struct {
	Fields []Field
	Values [][]any
	Table  string
	Limit  int64
	Offset int64
}

func Insert

func Insert() InsertQuery

func (InsertQuery) Build

func (query InsertQuery) Build() (string, []any)

type Join

type Join struct {
	Table string
	On    Condition
	Type  string
}

func (Join) Build

func (j Join) Build() (string, []any)

type OrderBy

type OrderBy struct {
	Field     Field
	Direction Direction
}

func (OrderBy) String

func (ob OrderBy) String() string

type Query

type Query interface {
	Build() (string, []any)
}

Query is an SQL query.

type RawCondition

type RawCondition struct {
	SQL  string
	Args []any
}

RawCondition is a Condition that can be used to make more complex comparisons.

func (*RawCondition) Build

func (query *RawCondition) Build() (string, []any)

Build returns an SQL statement and the related args.

type Repository

type Repository struct {
	DB                   *sql.DB
	Table                string
	LogFn                func(string, []any)
	StandardSelectFields []Field

	PreSelectFn func(ctx context.Context, query Query) error
	PreInsertFn func(ctx context.Context, query Query) error
	PreUpdateFn func(ctx context.Context, query Query) error
	PreDeleteFn func(ctx context.Context, query Query) error

	Tracer trace.Tracer
}

func (Repository) Delete

func (repo Repository) Delete(ctx context.Context, query DeleteQuery) (sql.Result, error)

func (Repository) DeleteFn

func (repo Repository) DeleteFn(ctx context.Context, queryFn func(*DeleteQuery)) (sql.Result, error)

func (Repository) Exec

func (repo Repository) Exec(ctx context.Context, query Query) (sql.Result, error)

func (Repository) Insert

func (repo Repository) Insert(ctx context.Context, query InsertQuery) (sql.Result, error)

func (Repository) InsertFn

func (repo Repository) InsertFn(ctx context.Context, queryFn func(*InsertQuery)) (sql.Result, error)

func (Repository) Query

func (repo Repository) Query(ctx context.Context, query SelectQuery) (*sql.Rows, error)

func (Repository) QueryFn

func (repo Repository) QueryFn(ctx context.Context, queryFn func(*SelectQuery)) (*sql.Rows, error)

func (Repository) QueryRow

func (repo Repository) QueryRow(ctx context.Context, query SelectQuery) (*sql.Row, error)

func (Repository) QueryRowFn

func (repo Repository) QueryRowFn(ctx context.Context, queryFn func(*SelectQuery)) (*sql.Row, error)

func (Repository) Update

func (repo Repository) Update(ctx context.Context, query UpdateQuery) (sql.Result, error)

func (Repository) UpdateFn

func (repo Repository) UpdateFn(ctx context.Context, queryFn func(*UpdateQuery)) (sql.Result, error)

type Scanner

type Scanner interface {
	Scan(dest ...any) error
}

type SelectQuery

type SelectQuery struct {
	Fields    []Field
	Table     string
	Condition Condition
	Join      []Join
	OrderBy   []OrderBy
	Limit     int64
	Offset    int64
}

SelectQuery is a Query.

func Select

func Select() SelectQuery

func (SelectQuery) Build

func (query SelectQuery) Build() (string, []any)

type SimpleCondition

type SimpleCondition struct {
	Field      Field
	Value      any
	Comparison string
}

SimpleCondition is a Condition that can be used to make a basic comparison. E.g. user_id = "123"

func (*SimpleCondition) Build

func (query *SimpleCondition) Build() (string, []any)

Build returns an SQL statement and the related args.

type TypedDeleteQuery

type TypedDeleteQuery[T any] struct {
	DeleteQuery

	Condition func(target *T) Condition
	Target    *T
}

func (TypedDeleteQuery[T]) Build

func (query TypedDeleteQuery[T]) Build() (string, []any)

func (TypedDeleteQuery[T]) Prepare

func (query TypedDeleteQuery[T]) Prepare() DeleteQuery

type TypedInsertQuery

type TypedInsertQuery[T any] struct {
	InsertQuery

	Values  func(target *T) map[Field]any
	Targets []*T
}

func (TypedInsertQuery[T]) Build

func (query TypedInsertQuery[T]) Build() (string, []any)

func (TypedInsertQuery[T]) Prepare

func (query TypedInsertQuery[T]) Prepare() InsertQuery

type TypedRepository

type TypedRepository[T any] struct {
	Repository

	StandardSelectFieldReferences func(target *T) []any
	StandardUpdateValues          func(target *T) map[Field]any
	StandardUpdateCondition       func(target *T) Condition
	StandardInsertValues          func(target *T) map[Field]any
	StandardDeleteCondition       func(target *T) Condition

	PreScanFn   func(ctx context.Context, query Query, target *T) error
	PostScanFn  func(ctx context.Context, query Query, target *T) error
	PreSelectFn func(ctx context.Context, query Query) error
	PreInsertFn func(ctx context.Context, query Query) error
	PreUpdateFn func(ctx context.Context, query Query) error
	PreDeleteFn func(ctx context.Context, query Query) error
}

func (TypedRepository[T]) Delete

func (repo TypedRepository[T]) Delete(ctx context.Context, query TypedDeleteQuery[T]) (sql.Result, error)

func (TypedRepository[T]) DeleteFn

func (repo TypedRepository[T]) DeleteFn(ctx context.Context, queryFn func(*TypedDeleteQuery[T])) (sql.Result, error)

func (TypedRepository[T]) DeleteQuery

func (repo TypedRepository[T]) DeleteQuery() TypedDeleteQuery[T]

func (TypedRepository[T]) Insert

func (repo TypedRepository[T]) Insert(ctx context.Context, query TypedInsertQuery[T]) (sql.Result, error)

func (TypedRepository[T]) InsertFn

func (repo TypedRepository[T]) InsertFn(ctx context.Context, queryFn func(*TypedInsertQuery[T])) (sql.Result, error)

func (TypedRepository[T]) InsertQuery

func (repo TypedRepository[T]) InsertQuery() TypedInsertQuery[T]

func (TypedRepository[T]) Query

func (repo TypedRepository[T]) Query(ctx context.Context, query TypedSelectQuery[T]) ([]*T, error)

func (TypedRepository[T]) QueryFn

func (repo TypedRepository[T]) QueryFn(ctx context.Context, queryFn func(*TypedSelectQuery[T])) ([]*T, error)

func (TypedRepository[T]) QueryRow

func (repo TypedRepository[T]) QueryRow(ctx context.Context, query TypedSelectQuery[T]) (*T, error)

func (TypedRepository[T]) QueryRowFn

func (repo TypedRepository[T]) QueryRowFn(ctx context.Context, queryFn func(*TypedSelectQuery[T])) (*T, error)

func (TypedRepository[T]) ScanRow

func (repo TypedRepository[T]) ScanRow(ctx context.Context, query Query, scanner Scanner, destFn func(*T) []interface{}) (*T, error)

func (TypedRepository[T]) SelectQuery

func (repo TypedRepository[T]) SelectQuery() TypedSelectQuery[T]

func (TypedRepository[T]) Update

func (repo TypedRepository[T]) Update(ctx context.Context, query TypedUpdateQuery[T]) (sql.Result, error)

func (TypedRepository[T]) UpdateFn

func (repo TypedRepository[T]) UpdateFn(ctx context.Context, queryFn func(*TypedUpdateQuery[T])) (sql.Result, error)

func (TypedRepository[T]) UpdateQuery

func (repo TypedRepository[T]) UpdateQuery() TypedUpdateQuery[T]

type TypedSelectQuery

type TypedSelectQuery[T any] struct {
	SelectQuery
	FieldReferences func(target *T) []any
}

func (TypedSelectQuery[T]) Build

func (query TypedSelectQuery[T]) Build() (string, []any)

func (TypedSelectQuery[T]) Prepare

func (query TypedSelectQuery[T]) Prepare() SelectQuery

type TypedUpdateQuery

type TypedUpdateQuery[T any] struct {
	UpdateQuery

	Values    func(target *T) map[Field]any
	Condition func(target *T) Condition
	Target    *T
}

func (TypedUpdateQuery[T]) Build

func (query TypedUpdateQuery[T]) Build() (string, []any)

func (TypedUpdateQuery[T]) Prepare

func (query TypedUpdateQuery[T]) Prepare() UpdateQuery

type UpdateQuery

type UpdateQuery struct {
	Values    map[Field]any
	Table     string
	Limit     int64
	Offset    int64
	Condition Condition
}

func Update

func Update() UpdateQuery

func (UpdateQuery) Build

func (query UpdateQuery) Build() (string, []any)

Jump to

Keyboard shortcuts

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