Documentation ¶
Index ¶
- Constants
- type CRUD
- type ChangeEventType
- type CrudBase
- func (c *CrudBase[T]) Count(ctx context.Context, filter ffapi.Filter) (count int64, err error)
- func (c *CrudBase[T]) Delete(ctx context.Context, id string, hooks ...PostCompletionHook) (err error)
- func (c *CrudBase[T]) DeleteMany(ctx context.Context, filter ffapi.Filter, hooks ...PostCompletionHook) (err error)
- func (c *CrudBase[T]) GetByID(ctx context.Context, id string, getOpts ...GetOption) (inst T, err error)
- func (c *CrudBase[T]) GetMany(ctx context.Context, filter ffapi.Filter) (instances []T, fr *ffapi.FilterResult, err error)
- func (c *CrudBase[T]) GetSequenceForID(ctx context.Context, id string) (seq int64, err error)
- func (c *CrudBase[T]) Insert(ctx context.Context, inst T, hooks ...PostCompletionHook) (err error)
- func (c *CrudBase[T]) InsertMany(ctx context.Context, instances []T, allowPartialSuccess bool, ...) (err error)
- func (c *CrudBase[T]) Replace(ctx context.Context, inst T, hooks ...PostCompletionHook) (err error)
- func (c *CrudBase[T]) Update(ctx context.Context, id string, update ffapi.Update, ...) (err error)
- func (c *CrudBase[T]) UpdateMany(ctx context.Context, filter ffapi.Filter, update ffapi.Update, ...) (err error)
- func (c *CrudBase[T]) UpdateSparse(ctx context.Context, sparseUpdate T, hooks ...PostCompletionHook) (err error)
- func (c *CrudBase[T]) Upsert(ctx context.Context, inst T, optimization UpsertOptimization, ...) (created bool, err error)
- func (c *CrudBase[T]) Validate()
- type Database
- func (s *Database) AcquireLockTx(ctx context.Context, lockName string, tx *TXWrapper) error
- func (s *Database) BeginOrUseTx(ctx context.Context) (ctx1 context.Context, tx *TXWrapper, autoCommit bool, err error)
- func (s *Database) BuildUpdate(sel sq.UpdateBuilder, update ffapi.Update, typeMap map[string]string) (sq.UpdateBuilder, error)
- func (s *Database) Close()
- func (s *Database) CommitTx(ctx context.Context, tx *TXWrapper, autoCommit bool) error
- func (s *Database) ConnLimit() int
- func (s *Database) CountQuery(ctx context.Context, table string, tx *TXWrapper, fop sq.Sqlizer, ...) (count int64, err error)
- func (s *Database) DB() *sql.DB
- func (s *Database) DeleteTx(ctx context.Context, table string, tx *TXWrapper, q sq.DeleteBuilder, ...) error
- func (s *Database) Features() SQLFeatures
- func (s *Database) FilterSelect(ctx context.Context, tableName string, sel sq.SelectBuilder, ...) (sq.SelectBuilder, sq.Sqlizer, *ffapi.FilterInfo, error)
- func (s *Database) FilterUpdate(ctx context.Context, update sq.UpdateBuilder, filter ffapi.Filter, ...) (sq.UpdateBuilder, error)
- func (s *Database) Init(ctx context.Context, provider Provider, config config.Section) (err error)
- func (s *Database) InitConfig(provider Provider, config config.Section)
- func (s *Database) InsertTx(ctx context.Context, table string, tx *TXWrapper, q sq.InsertBuilder, ...) (int64, error)
- func (s *Database) InsertTxExt(ctx context.Context, table string, tx *TXWrapper, q sq.InsertBuilder, ...) (int64, error)
- func (s *Database) InsertTxRows(ctx context.Context, table string, tx *TXWrapper, q sq.InsertBuilder, ...) error
- func (s *Database) Query(ctx context.Context, table string, q sq.SelectBuilder) (*sql.Rows, *TXWrapper, error)
- func (s *Database) QueryRes(ctx context.Context, table string, tx *TXWrapper, fop sq.Sqlizer, ...) *ffapi.FilterResult
- func (s *Database) QueryTx(ctx context.Context, table string, tx *TXWrapper, q sq.SelectBuilder) (*sql.Rows, *TXWrapper, error)
- func (s *Database) RollbackTx(ctx context.Context, tx *TXWrapper, autoCommit bool)
- func (s *Database) RunAsGroup(ctx context.Context, fn func(ctx context.Context) error) error
- func (s *Database) SequenceColumn() string
- func (s *Database) UpdateTx(ctx context.Context, table string, tx *TXWrapper, q sq.UpdateBuilder, ...) (int64, error)
- type GetOption
- type ILikeEscape
- type LikeEscape
- type MockProvider
- func (mp *MockProvider) ApplyInsertQueryCustomizations(insert sq.InsertBuilder, _ bool) (sq.InsertBuilder, bool)
- func (mp *MockProvider) Features() SQLFeatures
- func (mp *MockProvider) GetMigrationDriver(_ *sql.DB) (migratedb.Driver, error)
- func (mp *MockProvider) MigrationsDir() string
- func (mp *MockProvider) Name() string
- func (mp *MockProvider) Open(_ string) (*sql.DB, error)
- func (mp *MockProvider) SequenceColumn() string
- func (mp *MockProvider) UTInit() (*MockProvider, sqlmock.Sqlmock)
- type MockProviderConfig
- type NotILikeEscape
- type NotLikeEscape
- type PostCompletionHook
- type PreCommitAccumulator
- type Provider
- type Resource
- type ResourceBase
- type ResourceSequence
- type SQLFeatures
- type TXWrapper
- type UpsertOptimization
Constants ¶
const ( // SQLConfMigrationsAuto enables automatic migrations SQLConfMigrationsAuto = "migrations.auto" // SQLConfMigrationsDirectory is the directory containing the numerically ordered migration DDL files to apply to the database SQLConfMigrationsDirectory = "migrations.directory" // SQLConfDatasourceURL is the datasource connection URL string SQLConfDatasourceURL = "url" // SQLConfMaxConnections maximum connections to the database SQLConfMaxConnections = "maxConns" // SQLConfMaxConnIdleTime maximum connections to the database SQLConfMaxConnIdleTime = "maxConnIdleTime" // SQLConfMaxIdleConns maximum connections to the database SQLConfMaxIdleConns = "maxIdleConns" // SQLConfMaxConnLifetime maximum connections to the database SQLConfMaxConnLifetime = "maxConnLifetime" )
const ( ColumnID = "id" ColumnCreated = "created" ColumnUpdated = "updated" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CRUD ¶ added in v1.2.12
type CRUD[T Resource] interface { Validate() Upsert(ctx context.Context, inst T, optimization UpsertOptimization, hooks ...PostCompletionHook) (created bool, err error) InsertMany(ctx context.Context, instances []T, allowPartialSuccess bool, hooks ...PostCompletionHook) (err error) Insert(ctx context.Context, inst T, hooks ...PostCompletionHook) (err error) Replace(ctx context.Context, inst T, hooks ...PostCompletionHook) (err error) GetByID(ctx context.Context, id string, getOpts ...GetOption) (inst T, err error) GetSequenceForID(ctx context.Context, id string) (seq int64, err error) GetMany(ctx context.Context, filter ffapi.Filter) (instances []T, fr *ffapi.FilterResult, err error) Count(ctx context.Context, filter ffapi.Filter) (count int64, err error) Update(ctx context.Context, id string, update ffapi.Update, hooks ...PostCompletionHook) (err error) UpdateSparse(ctx context.Context, sparseUpdate T, hooks ...PostCompletionHook) (err error) UpdateMany(ctx context.Context, filter ffapi.Filter, update ffapi.Update, hooks ...PostCompletionHook) (err error) Delete(ctx context.Context, id string, hooks ...PostCompletionHook) (err error) DeleteMany(ctx context.Context, filter ffapi.Filter, hooks ...PostCompletionHook) (err error) // no events }
type ChangeEventType ¶ added in v1.2.8
type ChangeEventType int
const ( Created ChangeEventType = iota Updated Deleted )
type CrudBase ¶ added in v1.2.8
type CrudBase[T Resource] struct { DB *Database Table string Columns []string FilterFieldMap map[string]string TimesDisabled bool // no management of the time columns PatchDisabled bool // allows non-pointer fields, but prevents UpdateSparse function ImmutableColumns []string NilValue func() T // nil value typed to T NewInstance func() T ScopedFilter func() sq.Eq EventHandler func(id string, eventType ChangeEventType) GetFieldPtr func(inst T, col string) interface{} // Optional extensions ReadTableAlias string ReadOnlyColumns []string ReadQueryModifier func(sq.SelectBuilder) sq.SelectBuilder }
func (*CrudBase[T]) DeleteMany ¶ added in v1.2.18
func (*CrudBase[T]) GetSequenceForID ¶ added in v1.2.18
func (*CrudBase[T]) Insert ¶ added in v1.2.8
func (c *CrudBase[T]) Insert(ctx context.Context, inst T, hooks ...PostCompletionHook) (err error)
func (*CrudBase[T]) InsertMany ¶ added in v1.2.8
func (*CrudBase[T]) Replace ¶ added in v1.2.8
func (c *CrudBase[T]) Replace(ctx context.Context, inst T, hooks ...PostCompletionHook) (err error)
func (*CrudBase[T]) UpdateMany ¶ added in v1.2.8
func (*CrudBase[T]) UpdateSparse ¶ added in v1.2.12
func (c *CrudBase[T]) UpdateSparse(ctx context.Context, sparseUpdate T, hooks ...PostCompletionHook) (err error)
func (*CrudBase[T]) Upsert ¶ added in v1.2.8
func (c *CrudBase[T]) Upsert(ctx context.Context, inst T, optimization UpsertOptimization, hooks ...PostCompletionHook) (created bool, err error)
func (*CrudBase[T]) Validate ¶ added in v1.2.12
func (c *CrudBase[T]) Validate()
Validate checks things that must be true about a CRUD collection using this framework. Intended for use in the unit tests of microservices (will exercise all the functions of the CrudBase): - the mandatory columns exist - id/created/updated - no column has the same name as the sequence column for the DB - a unique pointer is returned for each field column - the immutable columns exist - the other functions return valid data
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
func (*Database) AcquireLockTx ¶
func (*Database) BeginOrUseTx ¶
func (*Database) BuildUpdate ¶
func (s *Database) BuildUpdate(sel sq.UpdateBuilder, update ffapi.Update, typeMap map[string]string) (sq.UpdateBuilder, error)
func (*Database) CountQuery ¶
func (*Database) Features ¶ added in v1.2.3
func (s *Database) Features() SQLFeatures
func (*Database) FilterSelect ¶
func (*Database) FilterUpdate ¶
func (*Database) InitConfig ¶
func (*Database) InsertTxExt ¶
func (*Database) InsertTxRows ¶
func (*Database) RollbackTx ¶
RollbackTx be safely called as a defer, as it is a cheap no-op if the transaction is complete
func (*Database) RunAsGroup ¶
func (*Database) SequenceColumn ¶
type ILikeEscape ¶ added in v1.2.19
func (ILikeEscape) ToSql ¶ added in v1.2.19
func (lk ILikeEscape) ToSql() (sql string, args []interface{}, err error)
type LikeEscape ¶ added in v1.2.19
func (LikeEscape) ToSql ¶ added in v1.2.19
func (lk LikeEscape) ToSql() (sql string, args []interface{}, err error)
type MockProvider ¶ added in v1.2.12
type MockProvider struct { MockProviderConfig Database // contains filtered or unexported fields }
MockProvider uses the datadog mocking framework
func NewMockProvider ¶ added in v1.2.12
func NewMockProvider() *MockProvider
func (*MockProvider) ApplyInsertQueryCustomizations ¶ added in v1.2.12
func (mp *MockProvider) ApplyInsertQueryCustomizations(insert sq.InsertBuilder, _ bool) (sq.InsertBuilder, bool)
func (*MockProvider) Features ¶ added in v1.2.12
func (mp *MockProvider) Features() SQLFeatures
func (*MockProvider) GetMigrationDriver ¶ added in v1.2.12
func (*MockProvider) MigrationsDir ¶ added in v1.2.12
func (mp *MockProvider) MigrationsDir() string
func (*MockProvider) Name ¶ added in v1.2.12
func (mp *MockProvider) Name() string
func (*MockProvider) Open ¶ added in v1.2.12
func (mp *MockProvider) Open(_ string) (*sql.DB, error)
func (*MockProvider) SequenceColumn ¶ added in v1.2.12
func (mp *MockProvider) SequenceColumn() string
func (*MockProvider) UTInit ¶ added in v1.2.12
func (mp *MockProvider) UTInit() (*MockProvider, sqlmock.Sqlmock)
init is a convenience to init for tests that aren't testing init itself
type MockProviderConfig ¶ added in v1.2.12
type NotILikeEscape ¶ added in v1.2.19
func (NotILikeEscape) ToSql ¶ added in v1.2.19
func (lk NotILikeEscape) ToSql() (sql string, args []interface{}, err error)
type NotLikeEscape ¶ added in v1.2.19
func (NotLikeEscape) ToSql ¶ added in v1.2.19
func (lk NotLikeEscape) ToSql() (sql string, args []interface{}, err error)
type PostCompletionHook ¶ added in v1.2.8
type PostCompletionHook func()
type PreCommitAccumulator ¶
PreCommitAccumulator is a structure that can accumulate state during the transaction, then has a function that is called just before commit.
type Provider ¶
type Provider interface { // Name is the name of the database driver Name() string // MigrationDir is the subdirectory for migrations MigrationsDir() string // SequenceColumn is the name of the sequence column to use SequenceColumn() string // Open creates the DB instances Open(url string) (*sql.DB, error) // GetDriver returns the driver implementation GetMigrationDriver(*sql.DB) (migratedb.Driver, error) // Features returns database specific configuration switches Features() SQLFeatures // ApplyInsertQueryCustomizations updates the INSERT query for returning the Sequence, and returns whether it needs to be run as a query to return the Sequence field ApplyInsertQueryCustomizations(insert sq.InsertBuilder, requestConflictEmptyResult bool) (updatedInsert sq.InsertBuilder, runAsQuery bool) }
Provider defines the interface an individual provider muse implement to customize the Database implementation
type ResourceBase ¶ added in v1.2.12
type ResourceBase struct { ID *fftypes.UUID `ffstruct:"ResourceBase" json:"id"` Created *fftypes.FFTime `ffstruct:"ResourceBase" json:"created"` Updated *fftypes.FFTime `ffstruct:"ResourceBase" json:"updated"` }
Resource is the default implementation of the Resource interface, but consumers of this package can implement the interface directly if they want to use different field names or field types for the fields.
func (*ResourceBase) GetID ¶ added in v1.2.12
func (r *ResourceBase) GetID() string
func (*ResourceBase) SetCreated ¶ added in v1.2.12
func (r *ResourceBase) SetCreated(t *fftypes.FFTime)
func (*ResourceBase) SetUpdated ¶ added in v1.2.12
func (r *ResourceBase) SetUpdated(t *fftypes.FFTime)
type ResourceSequence ¶ added in v1.2.18
type ResourceSequence interface {
SetSequence(int64)
}
type SQLFeatures ¶
type SQLFeatures struct { UseILIKE bool MultiRowInsert bool PlaceholderFormat sq.PlaceholderFormat AcquireLock func(lockName string) string }
func DefaultSQLProviderFeatures ¶
func DefaultSQLProviderFeatures() SQLFeatures
type TXWrapper ¶
type TXWrapper struct {
// contains filtered or unexported fields
}
func (*TXWrapper) AddPostCommitHook ¶
func (tx *TXWrapper) AddPostCommitHook(fn func())
func (*TXWrapper) PreCommitAccumulator ¶
func (tx *TXWrapper) PreCommitAccumulator() PreCommitAccumulator
func (*TXWrapper) SetPreCommitAccumulator ¶
func (tx *TXWrapper) SetPreCommitAccumulator(pca PreCommitAccumulator)
type UpsertOptimization ¶ added in v1.2.8
type UpsertOptimization int
const ( UpsertOptimizationSkip UpsertOptimization = iota UpsertOptimizationNew UpsertOptimizationExisting )