ondatra

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

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

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 13 Imported by: 0

README

ondatra

Golang SQL builder

Documentation

Index

Constants

View Source
const (
	CommandSelect = "SELECT"
	CommandInsert = "INSERT"
	CommandUpdate = "UPDATE"
	CommandDelete = "DELETE"

	JoinInner = "INNER"
	JoinLeft  = "LEFT"
	JoinRight = "RIGHT"
	JoinFull  = "FULL"

	ColumnCreatedAt = "created_at"
	ColumnUpdatedAt = "updated_at"
)

Variables

View Source
var (
	SqlDBNotSet             = errors.New("cannot run; no sql db set")
	ErrAlreadyInTransaction = errors.New("already in transaction")
	NotSetColumns           = errors.New("columns must have at least one set of values")
	NotSetValues            = errors.New("values must have at least one set of values")
)
View Source
var (
	// Dollar is a PlaceholderFormat instance that replaces placeholders with
	// dollar-prefixed positional placeholders (e.g. $1, $2, $3).
	Dollar = stringPlaceholderFormat("$")

	// Colon is a PlaceholderFormat instance that replaces placeholders with
	// colon-prefixed positional placeholders (e.g. :1, :2, :3).
	Colon = stringPlaceholderFormat(":")

	// AtP is a PlaceholderFormat instance that replaces placeholders with
	// "@p"-prefixed positional placeholders (e.g. @p1, @p2, @p3).
	AtP = stringPlaceholderFormat("@p")
)
View Source
var DebugMode = false

Functions

func BoolPtr

func BoolPtr(b sql.NullBool) *bool

func BytePtr

func BytePtr(i sql.NullByte) *byte

func DecimalPtr

func DecimalPtr(d decimal.NullDecimal) *decimal.Decimal

func Float64Ptr

func Float64Ptr(f sql.NullFloat64) *float64

func Int16Ptr

func Int16Ptr(i sql.NullInt16) *int16

func Int32Ptr

func Int32Ptr(i sql.NullInt32) *int32

func Int64Ptr

func Int64Ptr(i sql.NullInt64) *int64

func NullBool

func NullBool(b bool) sql.NullBool

func NullBoolPtr

func NullBoolPtr(b *bool) sql.NullBool

func NullBoolValid

func NullBoolValid(b bool) sql.NullBool

func NullByte

func NullByte(b byte) sql.NullByte

func NullBytePtr

func NullBytePtr(b *byte) sql.NullByte

func NullByteValid

func NullByteValid(b byte) sql.NullByte

func NullDecimal

func NullDecimal(d decimal.Decimal) decimal.NullDecimal

func NullDecimalPtr

func NullDecimalPtr(d *decimal.Decimal) decimal.NullDecimal

func NullDecimalValid

func NullDecimalValid(d decimal.Decimal) decimal.NullDecimal

func NullFloat64

func NullFloat64(f float64) sql.NullFloat64

func NullFloat64Ptr

func NullFloat64Ptr(f *float64) sql.NullFloat64

func NullFloat64Valid

func NullFloat64Valid(f float64) sql.NullFloat64

func NullInt16

func NullInt16(i int16) sql.NullInt16

func NullInt16Ptr

func NullInt16Ptr(i *int16) sql.NullInt16

func NullInt16Valid

func NullInt16Valid(i int16) sql.NullInt16

func NullInt32

func NullInt32(i int32) sql.NullInt32

func NullInt32Ptr

func NullInt32Ptr(i *int32) sql.NullInt32

func NullInt32Valid

func NullInt32Valid(i int32) sql.NullInt32

func NullInt64

func NullInt64(i int64) sql.NullInt64

func NullInt64Ptr

func NullInt64Ptr(i *int64) sql.NullInt64

func NullInt64Valid

func NullInt64Valid(i int64) sql.NullInt64

func NullString

func NullString(s string) sql.NullString

func NullStringPtr

func NullStringPtr(s *string) sql.NullString

func NullStringValid

func NullStringValid(s string) sql.NullString

func NullTime

func NullTime(t time.Time) sql.NullTime

func NullTimePtr

func NullTimePtr(t *time.Time) sql.NullTime

func NullTimeValid

func NullTimeValid(t time.Time) sql.NullTime

func StringPtr

func StringPtr(s sql.NullString) *string

func TimePtr

func TimePtr(t sql.NullTime) *time.Time

Types

type Builder

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

func NewBuilder

func NewBuilder(db *sqlx.DB) Builder

func NewBuilderTx

func NewBuilderTx(tx *sqlx.Tx) Builder

func NewBuilderWriterReader

func NewBuilderWriterReader(writerDB *sqlx.DB, readerDB *sqlx.DB) Builder

func NewEmptyBuilder

func NewEmptyBuilder() Builder

NewEmptyBuilder create empty builder only for ToSQL()

func (Builder) Clauses

func (b Builder) Clauses(clauses ...Clause) Builder

func (Builder) Columns

func (b Builder) Columns(columns ...string) Builder

Columns use for insert columns

func (Builder) Command

func (b Builder) Command(command string) Builder

func (Builder) Delete

func (b Builder) Delete() Builder

func (Builder) Distinct

func (b Builder) Distinct() Builder

func (Builder) Exec

func (b Builder) Exec() (sql.Result, error)

func (Builder) ExecContext

func (b Builder) ExecContext(ctx context.Context) (sql.Result, error)

func (Builder) ExecRaw

func (b Builder) ExecRaw(query string, args ...any) (sql.Result, error)

func (Builder) From

func (b Builder) From(table string) Builder

func (Builder) FromSelect

func (b Builder) FromSelect(from Expr, alias string) Builder

func (Builder) Get

func (b Builder) Get(dest any) error

func (Builder) GetAll

func (b Builder) GetAll(dest any) error

func (Builder) GetAllContext

func (b Builder) GetAllContext(ctx context.Context, dest any) error

func (Builder) GetContext

func (b Builder) GetContext(ctx context.Context, dest any) error

func (Builder) GroupBy

func (b Builder) GroupBy(groupBys ...string) Builder

func (Builder) Having

func (b Builder) Having(rawSQL string, args ...any) Builder

func (Builder) Insert

func (b Builder) Insert() Builder

func (Builder) InsertReturning

func (b Builder) InsertReturning() error

func (Builder) InsertReturningContext

func (b Builder) InsertReturningContext(ctx context.Context) error

func (Builder) Into

func (b Builder) Into(table string) Builder

func (Builder) Join

func (b Builder) Join(joinType, join string, args ...any) Builder

func (Builder) JoinExpr

func (b Builder) JoinExpr(expr ...JoinExpr) Builder

func (Builder) JoinRaw

func (b Builder) JoinRaw(rawSQL string, args ...any) Builder

func (Builder) Limit

func (b Builder) Limit(limit int64) Builder

func (Builder) LimitOffset

func (b Builder) LimitOffset(limit, offset int64) Builder

func (Builder) New

func (b Builder) New() Builder

func (Builder) Offset

func (b Builder) Offset(offset int64) Builder

func (Builder) Options

func (b Builder) Options(options ...string) Builder

func (Builder) OrderBy

func (b Builder) OrderBy(orderBys ...string) Builder

func (Builder) OrderByArgs

func (b Builder) OrderByArgs(rawSQL string, args ...any) Builder

func (Builder) PlaceholderFormat

func (b Builder) PlaceholderFormat(placeholderFormat PlaceholderFormat) Builder

func (Builder) Prefix

func (b Builder) Prefix(rawSQL string, args ...any) Builder

func (Builder) Query

func (b Builder) Query() (*sql.Rows, error)

func (Builder) QueryContext

func (b Builder) QueryContext(ctx context.Context) (*sql.Rows, error)

func (Builder) QueryRow

func (b Builder) QueryRow() (*sql.Row, error)

func (Builder) QueryRowContext

func (b Builder) QueryRowContext(ctx context.Context) (*sql.Row, error)

func (Builder) Raw

func (b Builder) Raw(ctx context.Context, dest any, query string, args ...any) error

func (Builder) RunInTransaction

func (b Builder) RunInTransaction(ctx context.Context, exec func(Builder) error) error

func (Builder) Select

func (b Builder) Select(columns ...string) Builder

func (Builder) SelectColumn

func (b Builder) SelectColumn(rawSQL string, args ...any) Builder

func (Builder) SelectColumns

func (b Builder) SelectColumns(columns ...string) Builder

func (Builder) Set

func (b Builder) Set(column string, value any) Builder

Set use for update

func (Builder) SetExpr

func (b Builder) SetExpr(expr ...Expr) Builder

func (Builder) SetMap

func (b Builder) SetMap(clauses map[string]any) Builder

SetMap use for update

func (Builder) StructColumns

func (b Builder) StructColumns(object any, columns ...string) Builder

StructColumns set columns for update or insert from struct with db tags

func (Builder) Suffix

func (b Builder) Suffix(rawSQL string, args ...any) Builder

func (Builder) Table

func (b Builder) Table(table string) Builder

func (Builder) ToQueryWithArgs

func (b Builder) ToQueryWithArgs() (string, []any, error)

func (Builder) ToSQL

func (b Builder) ToSQL() (string, []any, error)

func (Builder) Update

func (b Builder) Update() Builder

func (Builder) Values

func (b Builder) Values(values ...any) Builder

Values use for insert many values array

func (Builder) Where

func (b Builder) Where(rawSQL string, args ...any) Builder

func (Builder) WhereExpr

func (b Builder) WhereExpr(expr ...Expr) Builder

type Clause

type Clause interface {
	Apply(b Builder) Builder
}

func Columns

func Columns(columns ...string) Clause

func Delete

func Delete() Clause

func Distinct

func Distinct() Clause

func From

func From(table string) Clause

func FromSelect

func FromSelect(from Expr, alias string) Clause

func GroupBy

func GroupBy(groupBys ...string) Clause

func Having

func Having(rawSQL string, args ...any) Clause

func Insert

func Insert() Clause

func Join

func Join(joinType, join string, args ...any) Clause

func JoinBuilder

func JoinBuilder(joinExpr ...JoinExpr) Clause

func JoinRaw

func JoinRaw(rawSQL string, args ...any) Clause

func Limit

func Limit(limit int64) Clause

func LimitOffset

func LimitOffset(limit, offset int64) Clause

func Offset

func Offset(offset int64) Clause

func Options

func Options(options ...string) Clause

func OrderBy

func OrderBy(orderBy ...string) Clause

func OrderByArgs

func OrderByArgs(rawSQL string, args ...any) Clause

func Prefix

func Prefix(rawSQL string, args ...any) Clause

func Select

func Select(columns ...string) Clause

func SelectColumn

func SelectColumn(rawSQL string, args ...any) Clause

func SelectColumns

func SelectColumns(columns ...string) Clause

func Set

func Set(column string, value any) Clause

func SetExpr

func SetExpr(expr ...Expr) Clause

func Suffix

func Suffix(rawSQL string, args ...any) Clause

func Update

func Update() Clause

func Where

func Where(rawSQL string, args ...any) Clause

func WhereExpr

func WhereExpr(expr ...Expr) Clause

type Column

type Column[T comparable] struct {
	Name    string
	Set     Value[T]
	EQ      Value[T]
	NEQ     Value[T]
	LT      Value[T]
	LTE     Value[T]
	GT      Value[T]
	GTE     Value[T]
	Like    Value[T]
	NotLike Value[T]
}

func NewColumn

func NewColumn[T comparable](name string) Column[T]

func (Column[T]) IN

func (c Column[T]) IN(value ...T) Expr

func (Column[T]) IsNotNull

func (c Column[T]) IsNotNull() Expr

func (Column[T]) IsNotNullValue

func (c Column[T]) IsNotNullValue(value bool) Expr

func (Column[T]) IsNull

func (c Column[T]) IsNull() Expr

func (Column[T]) IsNullPtr

func (c Column[T]) IsNullPtr(value *bool) Expr

func (Column[T]) IsNullValue

func (c Column[T]) IsNullValue(value bool) Expr

func (Column[T]) NIN

func (c Column[T]) NIN(value ...T) Expr

type Connection

type Connection interface {
	Rebind(sql string) string
	Get(dest any, query string, args ...any) error
	GetContext(ctx context.Context, dest any, query string, args ...any) error
	Select(dest any, query string, args ...any) error
	SelectContext(ctx context.Context, dest any, query string, args ...any) error
	Exec(query string, args ...any) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
	BeginTx(ctx context.Context) (*sqlx.Tx, error)
}

func NewDB

func NewDB(db *sqlx.DB) Connection

func NewTx

func NewTx(tx *sqlx.Tx) Connection

type DB

type DB struct {
	*sqlx.DB
}

func (*DB) BeginTx

func (c *DB) BeginTx(ctx context.Context) (*sqlx.Tx, error)

type Expr

type Expr interface {
	ToSQL() (string, []any, error)
}

func AND

func AND(conditions ...Expr) Expr

func NewExpr

func NewExpr(rawSQL string, args ...any) Expr

func OR

func OR(conditions ...Expr) Expr

type JoinExpr

type JoinExpr interface {
	Expr
	SelectColumns() []string
	NewJoin(joinType string, table Table, alias, field, relatedField string) JoinExpr
	Alias() string
	RelatedTable(relatedTable string) JoinExpr
}

func NewJoinBuilder

func NewJoinBuilder(table string) JoinExpr

type PlaceholderFormat

type PlaceholderFormat interface {
	ReplacePlaceholders(sql string) string
}

type Table

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

func NewTable

func NewTable(name string, columns []string) Table

func (Table) Columns

func (t Table) Columns() []string

func (Table) ColumnsAlias

func (t Table) ColumnsAlias(alias string) []string

func (Table) ColumnsTable

func (t Table) ColumnsTable() []string

func (Table) Name

func (t Table) Name() string

type Tx

type Tx struct {
	*sqlx.Tx
}

func (*Tx) BeginTx

func (c *Tx) BeginTx(_ context.Context) (*sqlx.Tx, error)

type Value

type Value[T comparable] string

func (Value[T]) Ptr

func (v Value[T]) Ptr(ptr *T) Expr

func (Value[T]) Value

func (v Value[T]) Value(value T) Expr

Jump to

Keyboard shortcuts

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