orm

package
v0.42.5 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// A schema to use when non was specified during generation
	CtxUseSchema ctxKey = iota
)

Variables

View Source
var (
	ErrNothingToUpdate   = errors.New("nothing to update")
	ErrCannotRetrieveRow = errors.New("cannot retrieve inserted row")
	ErrCannotPrepare     = errors.New("supplied executor does not implement bob.Preparer")
)

Functions

func ArgsToExpression

func ArgsToExpression(querySQL string, from, to int, argIter iter.Seq[ArgWithPosition]) bob.Expression

Types

type AfterPreloader

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

AfterPreloader is embedded in a Preloader to chain loading whenever a preloaded object is scanned, it should be collected with the Collect method The loading functions should be added with AppendLoader later, when this object is called like any other bob.Loader, it calls the appended loaders with the collected objects

func NewAfterPreloader

func NewAfterPreloader[T any, Ts ~[]T]() *AfterPreloader

NewAfterPreloader returns a new AfterPreloader based on the given types

func (*AfterPreloader) AppendLoader

func (a *AfterPreloader) AppendLoader(fs ...bob.Loader)

func (*AfterPreloader) Collect

func (a *AfterPreloader) Collect(v any) error

func (*AfterPreloader) Load

func (a *AfterPreloader) Load(ctx context.Context, exec bob.Executor, _ any) error

type ArgWithPosition

type ArgWithPosition struct {
	Name       string
	Start      int
	Stop       int
	Expression bob.Expression
}

type ExecQuery

type ExecQuery[Q bob.Expression] struct {
	bob.BaseQuery[Q]
	Hooks *bob.Hooks[Q, bob.SkipQueryHooksKey]
}

func (ExecQuery[Q]) Clone

func (q ExecQuery[Q]) Clone() ExecQuery[Q]

func (ExecQuery[Q]) Exec

func (q ExecQuery[Q]) Exec(ctx context.Context, exec bob.Executor) (int64, error)

Execute the query

func (ExecQuery[Q]) RunHooks

func (q ExecQuery[Q]) RunHooks(ctx context.Context, exec bob.Executor) (context.Context, error)

type Loadable

type Loadable interface {
	AppendLoader(f ...bob.Loader)
	AppendMapperMod(f scan.MapperMod)
}

Loadable is a constraint for types that can be loaded

type Loader

type Loader[Q Loadable] func(ctx context.Context, exec bob.Executor, retrieved any) error

Loader builds a query mod that makes an extra query after the object is retrieved it can be used to prevent N+1 queries by loading relationships in batches

func (Loader[Q]) Apply

func (l Loader[Q]) Apply(q Q)

Apply satisfies the bob.Mod[Q] interface

func (Loader[Q]) Load

func (l Loader[Q]) Load(ctx context.Context, exec bob.Executor, retrieved any) error

Load is called after the original object is retrieved

func (Loader[Q]) ModifyPreloadSettings

func (l Loader[Q]) ModifyPreloadSettings(s *PreloadSettings[Q])

modifyPreloader makes a Loader also work as a mod for a Preloader

type ModExecQuery

type ModExecQuery[Q any, E bob.Expression] struct {
	ExecQuery[E]
	Mod bob.Mod[Q]
}

func (ModExecQuery[Q, E]) Apply

func (q ModExecQuery[Q, E]) Apply(e Q)

type ModQuery

type ModQuery[Q any, E bob.Expression, T, Ts any, Tr bob.Transformer[T, Ts]] struct {
	Query[E, T, Ts, Tr]
	Mod bob.Mod[Q]
}

func (ModQuery[Q, E, T, Ts, Tr]) Apply

func (q ModQuery[Q, E, T, Ts, Tr]) Apply(e Q)

type NullTypeConverter

type NullTypeConverter struct{}

func (NullTypeConverter) TypeToDestination

func (NullTypeConverter) TypeToDestination(typ reflect.Type) reflect.Value

TypeToDestination implements the TypeConverter interface and returns a reflect.Value that wraps the destination type in a wrapper struct able to handle NULL values.

func (NullTypeConverter) ValueFromDestination

func (NullTypeConverter) ValueFromDestination(val reflect.Value) reflect.Value

ValueFromDestination implements the TypeConverter interface and extracts the actual value from the wrapper struct.

type PreloadAs

type PreloadAs[Q Loadable] string

func (PreloadAs[Q]) ModifyPreloadSettings

func (o PreloadAs[Q]) ModifyPreloadSettings(el *PreloadSettings[Q])

type PreloadExcept

type PreloadExcept[Q Loadable] []string

func (PreloadExcept[Q]) ModifyPreloadSettings

func (e PreloadExcept[Q]) ModifyPreloadSettings(el *PreloadSettings[Q])

type PreloadOnly

type PreloadOnly[Q Loadable] []string

func (PreloadOnly[Q]) ModifyPreloadSettings

func (o PreloadOnly[Q]) ModifyPreloadSettings(el *PreloadSettings[Q])

type PreloadOption

type PreloadOption[Q Loadable] interface {
	ModifyPreloadSettings(*PreloadSettings[Q])
}

type PreloadRel

type PreloadRel[E bob.Expression] struct {
	Name  string
	Sides []PreloadSide[E]
}

type PreloadSettings

type PreloadSettings[Q Loadable] struct {
	Columns     []string
	SubLoaders  []Preloader[Q]
	ExtraLoader *AfterPreloader
	Mods        [][]preloadfilter
	Alias       string
}

func NewPreloadSettings

func NewPreloadSettings[T any, Ts ~[]T, Q Loadable](cols []string) PreloadSettings[Q]

type PreloadSide

type PreloadSide[E bob.Expression] struct {
	From        nameable[E]
	To          nameable[E]
	FromColumns []string `yaml:"-"`
	ToColumns   []string `yaml:"-"`

	FromWhere []RelWhere `yaml:"from_where"`
	ToWhere   []RelWhere `yaml:"to_where"`
}

type PreloadWhere

type PreloadWhere[Q Loadable] []preloadfilter

func (PreloadWhere[Q]) ModifyPreloadSettings

func (filters PreloadWhere[Q]) ModifyPreloadSettings(el *PreloadSettings[Q])

type Preloadable

type Preloadable interface {
	Preload(name string, rel any) error
}

type PreloadableQuery

type PreloadableQuery interface {
	Loadable
	AppendJoin(clause.Join)
	AppendPreloadSelect(columns ...any)
}

type Preloader

type Preloader[Q Loadable] func(parent string) (bob.Mod[Q], scan.MapperMod, []bob.Loader)

Preloader builds a query mod that modifies the original query to retrieve related fields while it can be used as a queryMod, it does not have any direct effect. if using manually, the ApplyPreload method should be called with the query's context AFTER other mods have been applied

func Preload

func Preload[T Preloadable, Ts ~[]T, E bob.Expression, Q PreloadableQuery](rel PreloadRel[E], cols []string, opts ...PreloadOption[Q]) Preloader[Q]

func (Preloader[Q]) Apply

func (l Preloader[Q]) Apply(q Q)

Apply satisfies bob.Mod[*dialect.SelectQuery]. 1. It modifies the query to join the preloading table and the extra columns to retrieve 2. It modifies the mapper to scan the new columns. 3. It calls the original object's Preload method with the loaded object

func (Preloader[Q]) ModifyPreloadSettings

func (l Preloader[Q]) ModifyPreloadSettings(s *PreloadSettings[Q])

modifyPreloader makes a Loader also work as a mod for a Preloader

type Query

type Query[Q bob.Expression, T, Ts any, Tr bob.Transformer[T, Ts]] struct {
	ExecQuery[Q]
	Scanner scan.Mapper[T]
}

func (Query[Q, T, Ts, Tr]) All

func (q Query[Q, T, Ts, Tr]) All(ctx context.Context, exec bob.Executor) (Ts, error)

All matching rows

func (Query[Q, T, Ts, Tr]) Clone

func (q Query[Q, T, Ts, Tr]) Clone() Query[Q, T, Ts, Tr]

func (Query[Q, T, Ts, Tr]) Cursor

func (q Query[Q, T, Ts, Tr]) Cursor(ctx context.Context, exec bob.Executor) (scan.ICursor[T], error)

Cursor to scan through the results

func (Query[Q, T, Ts, Tr]) Each

func (q Query[Q, T, Ts, Tr]) Each(ctx context.Context, exec bob.Executor) (func(func(T, error) bool), error)

Each to scan through the results

func (Query[Q, T, Ts, Tr]) One

func (q Query[Q, T, Ts, Tr]) One(ctx context.Context, exec bob.Executor) (T, error)

First matching row

type RelSetDetails

type RelSetDetails struct {
	TableName string
	Mapped    []RelSetMapping
	Position  int
	Start     bool
	End       bool
}

func (RelSetDetails) Columns

func (r RelSetDetails) Columns() []string

func (RelSetDetails) UniqueExternals

func (r RelSetDetails) UniqueExternals() []RelSetMapping

type RelSetMapping

type RelSetMapping struct {
	Column         string
	Value          [2]string // [0] is the SQL value, [1] is the Go value
	ExternalTable  string
	ExternalColumn string
	ExtPosition    int
	ExternalStart  bool
	ExternalEnd    bool
}

func (RelSetMapping) HasValue

func (r RelSetMapping) HasValue() bool

type RelSide

type RelSide struct {
	From string `yaml:"from"`
	To   string `yaml:"to"`

	// To make sure the column lengths match and are in the right order,
	// a slice of tupules is expected.
	// bobgen-helpers.GenConfig has a function to spread that into From/ToColumns
	Columns     [][2]string `yaml:"columns"`
	FromColumns []string    `yaml:"-"`
	ToColumns   []string    `yaml:"-"`

	FromWhere []RelWhere `yaml:"from_where"`
	ToWhere   []RelWhere `yaml:"to_where"`

	// These are columns that exist in the database, but should not be
	// considered by the relationship when determining if it is a join table
	// the columns are never set or read, so make sure they have a default value
	// or operations will fail
	// the first slice is for the from table, the second is for the to table
	IgnoredColumns [2][]string `yaml:"-"`

	// if the origin is unique
	FromUnique bool `yaml:"-"`
	// if the destination is unique
	ToUnique bool `yaml:"-"`

	// Which side to modify, "from" or "to"
	// If not set, it will try to "guess" which side to modify
	// - if only one of the sides contains a primary key,
	//   it will choose to modify the other side
	// - If (both or none) of them contains a primary key,
	//   it will try with "Unique" columns
	// - If it still cannot choose, it defaults to "to"
	Modify string `yaml:"modify"`

	// If the key is nullable. We need this to know if we can remove the
	// relationship without deleting it
	// this is set in Relationships.init()
	KeyNullable bool `yaml:"-"`
}

type RelWhere

type RelWhere struct {
	Column   string `yaml:"column"`
	SQLValue string `yaml:"sql_value"`
	GoValue  string `yaml:"go_value"`
}

type Relationship

type Relationship struct {
	Name  string    `yaml:"name"`
	Sides []RelSide `yaml:"sides"`

	// These can be set through user configuration
	Ignored bool
	// Do not create the inverse of a user configured relationship
	NoReverse bool `yaml:"no_reverse"`
	// Makes sure the factories does not require the relationship to be set.
	// Useful if you're not using foreign keys
	NeverRequired bool `yaml:"never_required"`
}

func (Relationship) Foreign

func (r Relationship) Foreign() string

func (Relationship) ForeignPosition

func (r Relationship) ForeignPosition() int

func (Relationship) InsertEarly

func (r Relationship) InsertEarly() bool

func (Relationship) IsRemovable

func (r Relationship) IsRemovable() bool

func (Relationship) IsToMany

func (r Relationship) IsToMany() bool

func (Relationship) Local

func (r Relationship) Local() string

func (Relationship) LocalPosition

func (r Relationship) LocalPosition() int

func (Relationship) NeedsMany

func (r Relationship) NeedsMany(position int) bool

NeedsMany returns true if the table on this side needs to be many

func (Relationship) Validate

func (r Relationship) Validate() error

func (Relationship) ValuedSides

func (r Relationship) ValuedSides() []RelSetDetails

type RelationshipChainError

type RelationshipChainError struct {
	Table1  string
	Column1 string
	Value   string
	Table2  string
	Column2 string
}

RelationshipChainError is the error returned when a wrong value is encountered in a relationship chain

func (*RelationshipChainError) Error

func (e *RelationshipChainError) Error() string

type SchemaTable

type SchemaTable string

func (SchemaTable) WriteSQL

func (s SchemaTable) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error)

type Setter

type Setter[T, InsertQ, UpdateQ any] interface {
	// SetColumns should return the column names that are set
	SetColumns() []string
	// Act as a mod for the insert query
	// the BeforeInsertHooks MUST be run here
	bob.Mod[InsertQ]
	// Return a mod for the update query
	// this should add "SET col1 = val1, col2 = val2, ..."
	UpdateMod() bob.Mod[UpdateQ]
}

Jump to

Keyboard shortcuts

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